Beispiel #1
0
  @Override
  public boolean onCommand(
      CommandSender sender, Command command, String commandLabel, String[] args) {
    String commandName = command.getName().toLowerCase();
    try {

      lastCommand =
          (sender instanceof Player ? "player:" : "console:")
              + commandName
              + " "
              + Str.argStr(args);

      // i don't like seeing these messages all the time..
      // Log(((Player) sender).getName() + " used command " + command.getName());

      if (BetterShop.iConomy == null && BetterShop.economy == null) {
        BSutils.sendMessage(
            sender, "\u00A74 BetterShop is missing a dependency. Check the console.");
        Log(Level.SEVERE, "Missing: iConomy or BOSEconomy", false);
        return true;
      }

      if (stock != null && config.useItemStock) {
        stock.checkStockRestock();
      }

      if (commandName.equals("shop")) {
        if (args.length > 0) {
          if (args[0].equalsIgnoreCase("list")) {
            commandName = "shoplist";
          } else if (args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("?")) {
            commandName = "shophelp";
          } else if (args[0].equalsIgnoreCase("buy")) {
            commandName = "shopbuy";
          } else if (args[0].equalsIgnoreCase("sell")) {
            commandName = "shopsell";
          } else if (args[0].equalsIgnoreCase("add")) {
            commandName = "shopadd";
          } else if (args[0].equalsIgnoreCase("remove")) {
            commandName = "shopremove";
          } else if (args[0].equalsIgnoreCase("load") || args[0].equalsIgnoreCase("reload")) {
            commandName = "shopload";
          } else if (args[0].equalsIgnoreCase("check")) {
            commandName = "shopcheck";
          } else if (args[0].equalsIgnoreCase("sellall")) {
            commandName = "shopsellall";
          } else if (args[0].equalsIgnoreCase("sellstack")) {
            commandName = "shopsellstack";
          } else if (args[0].equalsIgnoreCase("buystack")) {
            commandName = "shopbuystack";
          } else if (args[0].equalsIgnoreCase("buyall")) {
            commandName = "shopbuyall";
          } else if (args[0].equalsIgnoreCase("sellagain")) {
            commandName = "shopsellagain";
          } else if (args[0].equalsIgnoreCase("buyagain")) {
            commandName = "shopbuyagain";
          } else if (args[0].equalsIgnoreCase("listkits")) {
            commandName = "shoplistkits";
          } else if (args[0].equalsIgnoreCase("restock")) {
            if (BSutils.hasPermission(sender, BSutils.BetterShopPermission.ADMIN_RESTOCK, true)) {
              stock.Restock(true);
              sender.sendMessage("Stock set to initial values");
            }
          } else if (args[0].equalsIgnoreCase("backup")) {
            if (BSutils.hasPermission(sender, BSutils.BetterShopPermission.ADMIN_BACKUP, true)) {
              SimpleDateFormat formatter = new SimpleDateFormat("_yyyy_MM_dd_HH-mm-ss");
              String backFname =
                  BSConfig.pluginFolder.getPath()
                      + File.separatorChar
                      + config.tableName
                      + formatter.format(new java.util.Date())
                      + ".csv";
              try {
                if (pricelist.saveFile(new File(backFname))) {
                  sender.sendMessage("Backup saved as " + backFname);
                }
              } catch (IOException ex) {
                Log(Level.SEVERE, "Failed to save backup file " + backFname, ex);
                sender.sendMessage("\u00A74Failed to save backup file " + backFname);
              }
            }
            return true;
          } else if (args[0].equalsIgnoreCase("import")) {
            return bscommand.importDB(sender, args);
          } else if (args[0].equalsIgnoreCase("restore")) {
            return bscommand.restoreDB(sender, args);
          } else if (args[0].equalsIgnoreCase("update")) {
            if (sender.isOp()) {
              Log("Downloading & Installing Update");
              BSutils.sendMessage(sender, "Downloading & Installing Update");
              ServerReload sreload = new ServerReload(getServer());
              if (Updater.downloadUpdate()) {
                Log("Update Downloaded: Restarting Server..");
                BSutils.sendMessage(sender, "Download Successful.. reloading server");
                // this.setEnabled(false);
                // this.getServer().dispatchCommand((CommandSender) new CommanderSenderImpl(this),
                // "stop");
                // this.getServer().dispatchCommand(new AdminCommandSender(this), "stop");

                // this.getServer().reload();
                sreload.start(500);
              }
            } else {
              BSutils.sendMessage(sender, "Only an OP can update the shop plugin");
            }
            return true;
          } else if (args[0].equalsIgnoreCase("ver") || args[0].equalsIgnoreCase("version")) {
            // allow admin.info or developers access to plugin status (so if i find a bug i can see
            // if it's current)
            if (BSutils.hasPermission(sender, BSutils.BetterShopPermission.ADMIN_INFO, false)
                || (sender instanceof Player
                    && (((Player) sender).getDisplayName().equals("jascotty2")
                        || ((Player) sender).getDisplayName().equals("jjfs85")))) {
              BSutils.sendMessage(sender, "version " + pdfFile.getVersion());
              if (Updater.isUpToDate()) {
                BSutils.sendMessage(sender, "Version is up-to-date");
              } else {
                BSutils.sendMessage(sender, "Newer Version Avaliable");
              }
              return true;
            }
          } else {
            return false;
          }
          // now remove [0]
          if (args.length > 1) {
            String newArgs[] = new String[args.length - 1];
            for (int i = 1; i < args.length; ++i) {
              newArgs[i - 1] = args[i];
            }
            args = newArgs;
          } else {
            args = new String[0];
          }

        } else {
          return false;
        }
      }

      // check if using history
      if (commandName.equals("shopbuyagain") || commandName.equals("shopsellagain")) {
        if (args.length > 0 || BSutils.anonymousCheck(sender)) {
          return false;
        }
        if (commandName.equals("shopbuyagain")) {
          String action = bscommand.userbuyHistory.get(((Player) sender).getDisplayName());
          if (action == null) {
            BSutils.sendMessage(sender, "You have no recent buying history");
            return true;
          } else {
            // trim command & put into args
            String cm[] = action.split(" ");
            commandName = cm[0];
            args = new String[cm.length - 1];
            for (int i = 1; i < cm.length; ++i) {
              args[i - 1] = cm[i];
            }
          }
        } else { // if (commandName.equals("shopsellagain")) {
          String action = bscommand.usersellHistory.get(((Player) sender).getDisplayName());
          if (action == null) {
            BSutils.sendMessage(sender, "You have no recent sell history");
            return true;
          } else {
            // trim command & put into args
            String cm[] = action.split(" ");
            commandName = cm[0];
            args = new String[cm.length - 1];
            for (int i = 1; i < cm.length; ++i) {
              args[i - 1] = cm[i];
            }
          }
        }
        // System.out.println("new command: " + commandName);
        // System.out.println(BSCommand.argStr(args));
      }
      if (!config.useGlobalCommandShop()
          && Str.isIn(
              commandName,
              new String[] {
                "shopbuy", "shopbuyall", "shopbuystack",
                "shopsell", "shopsellall",
                    "shopsellstack", /*"shoplist", "shopitems", "shopcheck", "shoplistkits",
                                     "shopadd", "shopremove"*/
              })) {

        BSutils.sendMessage(sender, "Shop is disabled from here");
        return true;
      }

      if (commandName.equals("shoplist")) {
        return bscommand.list(sender, args);
      } else if (commandName.equals("shopitems")) {
        return bscommand.listitems(sender, args);
      } else if (commandName.equals("shophelp")) {
        return bscommand.help(sender, args);
      } else if (commandName.equals("shopbuy")) {
        return bscommand.buy(sender, args);
      } else if (commandName.equals("shopbuyall")) {
        ArrayList<String> arg = new ArrayList<String>();
        arg.addAll(Arrays.asList(args));
        arg.add("all");
        return bscommand.buy(sender, arg.toArray(new String[0]));
      } else if (commandName.equals("shopbuystack")) {
        return bscommand.buystack(sender, args);
      } else if (commandName.equals("shopsell")) {
        return bscommand.sell(sender, args);
      } else if (commandName.equals("shopsellall")) {
        return bscommand.sellall(sender, args);
      } else if (commandName.equals("shopsellstack")) {
        return bscommand.sellstack(sender, args);
      } else if (commandName.equals("shopadd")) {
        return bscommand.add(sender, args);
      } else if (commandName.equals("shopremove")) {
        return bscommand.remove(sender, args);
      } else if (commandName.equals("shopload")) {
        return bscommand.load(sender);
      } else if (commandName.equals("shopcheck")) {
        return bscommand.check(sender, args);
      } else if (commandName.equals("shoplistkits")) {
        return bscommand.listkits(sender, args);
      }

      return false;
    } catch (Exception e) {
      BSutils.sendMessage(sender, "Unexpected Error!");
      Log(Level.SEVERE, e);
    }
    return true;
  }
Beispiel #2
0
  public void onEnable() {
    pdfFile = this.getDescription();
    logger.log(
        Level.INFO,
        String.format("Loading %s version %s ...", pdfFile.getName(), pdfFile.getVersion()));

    // ready items db (needed for pricelist, sorting in config, item lookup, ...)
    try {
      ItemDB.load(BSConfig.pluginFolder);
      // Log("Itemsdb loaded");
    } catch (Exception e) {
      Log(Level.SEVERE, "cannot load items db: closing plugin", e, false);
      this.setEnabled(false);
      return;
    }

    if (config == null) {
      config = new BSConfig();
      // Log("config loaded");
      if (config.checkUpdates) {
        if (config.autoUpdate) {
          if (!Updater.isUpToDate(true)) {
            Log("Downloading & Installing Update");
            ServerReload sreload = new ServerReload(getServer());
            if (Updater.downloadUpdate()) {
              Log("Update Downloaded: Restarting Server..");
              // this.setEnabled(false);
              // this.getServer().dispatchCommand((CommandSender) new CommanderSenderImpl(this),
              // "stop");
              // this.getServer().dispatchCommand(new AdminCommandSender(this), "stop");

              try {
                // (new ServerReload(getServer())).start(500);
                sreload.start(500);
              } catch (Exception e) { // just in case...
                this.getServer().reload();
              }
              return;
            }
          }
        } else {
          Updater.check();
        }
      }
    } else {
      config.load();
    }
    pricelist = new BSPriceList();
    transactions = new BSTransactionLog();
    stock = new BSItemStock();
    signShop = new BSSignShop(this);

    if (!pricelist.load()) {
      Log(Level.SEVERE, "cannot load pricelist: " + pricelist.pricelistName(), false);
      // todo: add handlers for if not loaded?
      this.setEnabled(false);
      return;
    } else if (!transactions.load()) {
      Log(Level.SEVERE, "cannot load transaction log", false);
      // this.setEnabled(false);
      // return;
    }
    if (config.useItemStock && !stock.load()) {
      Log(Level.SEVERE, "cannot load stock database", false);
      stock = null;
    }
    if (config.signShopEnabled && !signShop.load()) {
      Log(Level.SEVERE, "cannot load sign shop database", false);
    }
    if (config.signShopEnabled && config.tntSignDestroyProtection) {
      signShop.startProtecting();
    }

    pListener = new PluginListener(this);

    hookDepends();
    registerHelp();
    // isLoaded = true;

    // for monster purchasing
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvent(Event.Type.ENTITY_DAMAGE, entityListener, Priority.Normal, this);
    pm.registerEvent(Event.Type.ENTITY_TARGET, entityListener, Priority.Normal, this);
    // for sign events
    pm.registerEvent(Event.Type.PLAYER_INTERACT, signShop, Priority.Normal, this);
    pm.registerEvent(Event.Type.BLOCK_BREAK, signShop.signDestroy, Priority.Normal, this);
    // pm.registerEvent(Event.Type.BLOCK_DAMAGE, signShop.signDestroy, Priority.Normal, this);
    // pm.registerEvent(Event.Type.BLOCK_CANBUILD, signShop.buildStopper, Priority.Normal, this);
    pm.registerEvent(Event.Type.BLOCK_PLACE, signShop.signDestroy, Priority.Normal, this);

    // monitor plugins - if any are enabled/disabled by a plugin manager
    pm.registerEvent(Event.Type.PLUGIN_ENABLE, pListener, Priority.Monitor, this);

    // Just output some info so we can check all is well
    logger.log(
        Level.INFO,
        pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!",
        new Object[] {pdfFile.getName(), pdfFile.getVersion()});
  }