Beispiel #1
0
  @Override
  public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (args.length > 0) {
      if (args[0].equalsIgnoreCase("pay")) {
        if (args.length >= 3) {
          if (plugin.getServer().getPlayer(args[1]) != null) {
            Player player = plugin.getServer().getPlayer(args[1]);
            if (((Player) sender).getWorld().equals(player.getWorld())) {
              if (((Player) sender).getLocation().distance(player.getLocation()) <= 8) {
                Currency currency = plugin.getPrimaryCurrency();
                if (args.length >= 4) {
                  if (plugin.getCurrency(args[3]) != null) {
                    currency = plugin.getCurrency(args[3]);
                  }
                }
                if (Integer.parseInt(args[2]) > 0) {
                  try {
                    plugin.transferMoney(
                        (Player) sender, player, currency, Integer.parseInt(args[2]));
                    sender.sendMessage(
                        plugin.getPrefix()
                            + ChatColor.GREEN
                            + ((Player) sender).getDisplayName()
                            + ChatColor.GREEN
                            + " gave "
                            + args[2]
                            + " "
                            + (Integer.parseInt(args[2]) == 1
                                ? currency.getNameSingular()
                                : currency.getNamePlural())
                            + " to "
                            + player.getDisplayName());
                    player.sendMessage(
                        plugin.getPrefix()
                            + ChatColor.GREEN
                            + ((Player) sender).getDisplayName()
                            + ChatColor.GREEN
                            + " gave "
                            + args[2]
                            + " "
                            + (Integer.parseInt(args[2]) == 1
                                ? currency.getNameSingular()
                                : currency.getNamePlural())
                            + " to "
                            + player.getDisplayName());
                  } catch (NumberFormatException exception) {
                    sender.sendMessage(
                        plugin.getPrefix()
                            + ChatColor.RED
                            + "You must specify a number for the amount of money to send.");
                  }
                } else {
                  sender.sendMessage(
                      plugin.getPrefix()
                          + ChatColor.RED
                          + "You must pay at least 1 "
                          + currency.getNameSingular()
                          + "!");
                }
              } else {
                sender.sendMessage(
                    plugin.getPrefix()
                        + ChatColor.RED
                        + ((Player) sender).getDisplayName()
                        + ChatColor.RED
                        + " must be standing a little closer to "
                        + player.getDisplayName()
                        + ChatColor.RED
                        + " in order to give them any money.");
              }
            }

          } else {
            sender.sendMessage(
                plugin.getPrefix()
                    + ChatColor.RED
                    + "Usage: /money pay [player] [amount] (currency)");
          }
        }
      } else if (args[0].equalsIgnoreCase("set")) {
        if (sender.hasPermission("wayward.economy.command.money.set")) {
          if (args.length >= 3) {
            if (plugin.getServer().getPlayer(args[1]) != null) {
              Player player = plugin.getServer().getPlayer(args[1]);
              Currency currency = plugin.getPrimaryCurrency();
              if (args.length >= 4) {
                if (plugin.getCurrency(args[3]) != null) {
                  currency = plugin.getCurrency(args[3]);
                }
              }
              try {
                plugin.setMoney(player, currency, Integer.parseInt(args[2]));
                sender.sendMessage(
                    plugin.getPrefix()
                        + ChatColor.GREEN
                        + "Set "
                        + player.getDisplayName()
                        + ChatColor.GREEN
                        + "'s "
                        + currency.getNameSingular()
                        + " balance to "
                        + args[2]);
                player.sendMessage(
                    plugin.getPrefix()
                        + ChatColor.GREEN
                        + "Your "
                        + currency.getNameSingular()
                        + " balance was set to "
                        + args[2]);
              } catch (NumberFormatException exception) {
                sender.sendMessage(
                    plugin.getPrefix()
                        + ChatColor.RED
                        + "You must specify a number for the amount of money to set.");
              }
            } else {
              sender.sendMessage(plugin.getPrefix() + ChatColor.RED + "That player is not online.");
            }
          } else {
            sender.sendMessage(
                plugin.getPrefix()
                    + ChatColor.RED
                    + "Usage: /money set [player] [amount] (currency)");
          }
        } else {
          sender.sendMessage(plugin.getPrefix() + ChatColor.RED + "You do not have permission.");
        }
      } else if (args[0].equalsIgnoreCase("add")) {
        if (sender.hasPermission("wayward.economy.command.money.set")) {
          if (args.length >= 3) {
            if (plugin.getServer().getPlayer(args[1]) != null) {
              Player player = plugin.getServer().getPlayer(args[1]);
              Currency currency = plugin.getPrimaryCurrency();
              if (args.length >= 4) {
                if (plugin.getCurrency(args[3]) != null) {
                  currency = plugin.getCurrency(args[3]);
                }
              }
              try {
                plugin.addMoney(player, currency, Integer.parseInt(args[2]));
                sender.sendMessage(
                    plugin.getPrefix()
                        + ChatColor.GREEN
                        + "Set "
                        + player.getDisplayName()
                        + ChatColor.GREEN
                        + "'s "
                        + currency.getNameSingular()
                        + " balance to "
                        + plugin.getMoney(player));
                player.sendMessage(
                    plugin.getPrefix()
                        + ChatColor.GREEN
                        + "Your "
                        + currency.getNameSingular()
                        + " balance was set to "
                        + plugin.getMoney(player));
              } catch (NumberFormatException exception) {
                sender.sendMessage(
                    plugin.getPrefix()
                        + ChatColor.RED
                        + "You must specify a number for the amount of money to add.");
              }
            } else {
              sender.sendMessage(plugin.getPrefix() + ChatColor.RED + "That player is not online.");
            }
          } else {
            sender.sendMessage(
                plugin.getPrefix()
                    + ChatColor.RED
                    + "Usage: /money add [player] [amount] (currency)");
          }
        } else {
          sender.sendMessage(plugin.getPrefix() + ChatColor.RED + "You do not have permission.");
        }
      } else if (args[0].equalsIgnoreCase("top")) {
        sender.sendMessage(plugin.getPrefix() + ChatColor.GREEN + "Top balances: ");
        int i = 1;
        for (Character character : plugin.getRichestCharacters()) {
          if (character != null) {
            sender.sendMessage(
                ChatColor.GRAY
                    + ""
                    + i
                    + ". "
                    + (character.isDead() ? ChatColor.RED : ChatColor.GREEN)
                    + (character.isNameHidden()
                        ? ChatColor.MAGIC + character.getName() + ChatColor.RESET
                        : character.getName())
                    + ChatColor.GRAY
                    + " ("
                    + (character.isDead() ? ChatColor.RED + "Dead" : ChatColor.GREEN + "Alive")
                    + ChatColor.GRAY
                    + "): "
                    + plugin.getMoney(character)
                    + " "
                    + (plugin.getMoney(character) == 1
                        ? plugin.getPrimaryCurrency().getNameSingular()
                        : plugin.getPrimaryCurrency().getNamePlural()));
          } else {
            break;
          }
          i++;
        }
      } else if (plugin.getCurrency(args[0]) != null) {
        Currency currency = plugin.getCurrency(args[0]);
        sender.sendMessage(
            plugin.getPrefix()
                + ChatColor.GREEN
                + currency.getNameSingular()
                + " balance: "
                + plugin.getMoney((Player) sender, currency));
      } else if (plugin.getServer().getPlayer(args[0]) != null) {
        Player player = plugin.getServer().getPlayer(args[0]);
        sender.sendMessage(plugin.getPrefix() + ChatColor.GREEN + "Balance: ");
        for (Currency currency : plugin.getCurrencies()) {
          sender.sendMessage(
              ChatColor.GREEN
                  + ""
                  + plugin.getMoney(player, currency)
                  + " "
                  + (plugin.getMoney(player, currency) == 1
                      ? currency.getNameSingular()
                      : currency.getNamePlural()));
        }
      } else {
        sender.sendMessage(plugin.getPrefix() + ChatColor.RED + "Unknown command!");
      }
    } else {
      sender.sendMessage(plugin.getPrefix() + ChatColor.GREEN + "Balance: ");
      for (Currency currency : plugin.getCurrencies()) {
        sender.sendMessage(
            ChatColor.GREEN
                + ""
                + plugin.getMoney((Player) sender, currency)
                + " "
                + (plugin.getMoney((Player) sender, currency) == 1
                    ? currency.getNameSingular()
                    : currency.getNamePlural()));
      }
    }
    return true;
  }
 @EventHandler
 public void onPlayerInteract(PlayerInteractEvent event) {
   if (event.hasBlock()) {
     if (event.getClickedBlock().getState() instanceof Sign) {
       Sign sign = (Sign) event.getClickedBlock().getState();
       // Banks
       if (sign.getLine(0).equalsIgnoreCase(ChatColor.GOLD + "[bank]")) {
         event.setCancelled(true);
         RegisteredServiceProvider<CharacterPlugin> characterPluginProvider =
             plugin.getServer().getServicesManager().getRegistration(CharacterPlugin.class);
         if (characterPluginProvider != null) {
           CharacterPlugin characterPlugin = characterPluginProvider.getProvider();
           Character character = characterPlugin.getActiveCharacter(event.getPlayer());
           Currency currency = plugin.getCurrency(sign.getLine(3));
           if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
             switch (sign.getLine(2)) {
               case "1":
                 sign.setLine(2, "10");
                 sign.update();
                 break;
               case "10":
                 sign.setLine(2, "100");
                 sign.update();
                 break;
               case "100":
                 sign.setLine(2, "1000");
                 sign.update();
                 break;
               case "1000":
                 sign.setLine(2, "1");
                 sign.update();
                 break;
               default:
                 break;
             }
           } else if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
             if (sign.getLine(1).equalsIgnoreCase("withdraw")) {
               if (plugin.getMoney(character, currency) + Integer.parseInt(sign.getLine(2))
                   > plugin.getMaximumMoney()) {
                 event
                     .getPlayer()
                     .sendMessage(
                         plugin.getPrefix()
                             + ChatColor.RED
                             + "You cannot withdraw that amount, it would not fit in your wallet.");
               } else if (Integer.parseInt(sign.getLine(2))
                   > plugin.getBankBalance(character, currency)) {
                 event
                     .getPlayer()
                     .sendMessage(
                         plugin.getPrefix()
                             + ChatColor.RED
                             + "You cannot withdraw that amount, your bank balance is not high enough.");
               } else {
                 plugin.setBankBalance(
                     character,
                     currency,
                     plugin.getBankBalance(character, currency)
                         - Integer.parseInt(sign.getLine(2)));
                 plugin.addMoney(character, currency, Integer.parseInt(sign.getLine(2)));
                 event
                     .getPlayer()
                     .sendMessage(
                         new String[] {
                           plugin.getPrefix()
                               + ChatColor.GREEN
                               + "Withdrew "
                               + sign.getLine(2)
                               + " "
                               + (Integer.parseInt(sign.getLine(2)) == 1
                                   ? currency.getNameSingular()
                                   : currency.getNamePlural()),
                           ChatColor.GRAY
                               + "Wallet balance: "
                               + plugin.getMoney(character, currency),
                           ChatColor.GRAY
                               + "Bank balance: "
                               + plugin.getBankBalance(character, currency)
                         });
               }
             } else if (sign.getLine(1).equalsIgnoreCase("deposit")) {
               if (Integer.parseInt(sign.getLine(2)) > plugin.getMoney(character, currency)) {
                 event
                     .getPlayer()
                     .sendMessage(
                         plugin.getPrefix()
                             + ChatColor.RED
                             + "You cannot deposit that amount, your wallet balance is not high enough.");
               } else {
                 plugin.setBankBalance(
                     character,
                     currency,
                     plugin.getBankBalance(character, currency)
                         + Integer.parseInt(sign.getLine(2)));
                 plugin.addMoney(character, currency, -Integer.parseInt(sign.getLine(2)));
                 event
                     .getPlayer()
                     .sendMessage(
                         new String[] {
                           plugin.getPrefix()
                               + ChatColor.GREEN
                               + "Deposited "
                               + sign.getLine(2)
                               + " "
                               + (Integer.parseInt(sign.getLine(2)) == 1
                                   ? currency.getNameSingular()
                                   : currency.getNamePlural()),
                           ChatColor.GRAY
                               + "Wallet balance: "
                               + plugin.getMoney(character, currency),
                           ChatColor.GRAY
                               + "Bank balance: "
                               + plugin.getBankBalance(character, currency)
                         });
               }
             } else if (sign.getLine(1).equalsIgnoreCase("balance")) {
               event
                   .getPlayer()
                   .sendMessage(
                       plugin.getPrefix()
                           + ChatColor.GREEN
                           + "Balance: "
                           + plugin.getBankBalance(character, currency));
             }
           }
         }
       }
       // Shops
       if (event.getClickedBlock().getRelative(BlockFace.DOWN).getType() == Material.CHEST) {
         if (event.getClickedBlock().getRelative(BlockFace.DOWN).getState() instanceof Chest) {
           Chest chest = (Chest) event.getClickedBlock().getRelative(BlockFace.DOWN).getState();
           if (sign.getLine(0).equalsIgnoreCase(ChatColor.DARK_PURPLE + "[shop]")) {
             event.setCancelled(true);
             if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
               RegisteredServiceProvider<CharacterPlugin> characterPluginProvider =
                   plugin.getServer().getServicesManager().getRegistration(CharacterPlugin.class);
               if (characterPluginProvider != null) {
                 CharacterPlugin characterPlugin = characterPluginProvider.getProvider();
                 if ((sign.getLine(3).equalsIgnoreCase("admin")
                         && event.getPlayer().hasPermission("wayward.economy.shop.admin"))
                     || characterPlugin.getActiveCharacter(event.getPlayer()).getId()
                         == Integer.parseInt(sign.getLine(3))) {
                   event.getClickedBlock().setType(Material.AIR);
                   plugin.addMoney(
                       characterPlugin.getActiveCharacter(event.getPlayer()),
                       plugin.getConfig().getInt("shop.sell", 50));
                   event
                       .getPlayer()
                       .sendMessage(
                           plugin.getPrefix()
                               + ChatColor.GREEN
                               + "Sold shop for "
                               + plugin.getConfig().getInt("shop.sell", 50)
                               + " "
                               + plugin.getPrimaryCurrency().getNamePlural());
                   return;
                 }
               }
             }
             if (validateShopSign(sign, event.getPlayer())) {
               if (sign.getLine(1).toLowerCase().contains("buy")) {
                 event.getPlayer().openInventory(chest.getInventory());
               } else if (sign.getLine(1).toLowerCase().contains("sell")) {
                 if (event.getPlayer().getItemInHand().getType()
                     == Material.matchMaterial(sign.getLine(1).split(" ")[1].replace(' ', '_'))) {
                   if (event.getPlayer().getItemInHand().getAmount()
                       >= Integer.parseInt(sign.getLine(1).split(" ")[2])) {
                     RegisteredServiceProvider<CharacterPlugin> characterPluginProvider =
                         Bukkit.getServer()
                             .getServicesManager()
                             .getRegistration(CharacterPlugin.class);
                     if (characterPluginProvider != null) {
                       CharacterPlugin characterPlugin = characterPluginProvider.getProvider();
                       try {
                         event
                             .getPlayer()
                             .sendMessage(
                                 plugin.getPrefix()
                                     + ChatColor.GREEN
                                     + "Sold "
                                     + Integer.parseInt(sign.getLine(1).split(" ")[2])
                                     + " x "
                                     + event
                                         .getPlayer()
                                         .getItemInHand()
                                         .getType()
                                         .toString()
                                         .toLowerCase()
                                         .replace('_', ' ')
                                     + " for "
                                     + Integer.parseInt(sign.getLine(2).replace("for ", ""))
                                     + " "
                                     + (Integer.parseInt(sign.getLine(2).replace("for ", "")) == 1
                                         ? plugin.getPrimaryCurrency().getNameSingular()
                                         : plugin.getPrimaryCurrency().getNamePlural()));
                         if (characterPlugin
                             .getCharacter(Integer.parseInt(sign.getLine(3)))
                             .getPlayer()
                             .isOnline()) {
                           characterPlugin
                               .getCharacter(Integer.parseInt(sign.getLine(3)))
                               .getPlayer()
                               .getPlayer()
                               .sendMessage(
                                   plugin.getPrefix()
                                       + ChatColor.GREEN
                                       + "Your shop bought "
                                       + Integer.parseInt(sign.getLine(1).split(" ")[2])
                                       + " x "
                                       + event
                                           .getPlayer()
                                           .getItemInHand()
                                           .getType()
                                           .toString()
                                           .toLowerCase()
                                           .replace('_', ' ')
                                       + " for "
                                       + Integer.parseInt(sign.getLine(2).replace("for ", ""))
                                       + (Integer.parseInt(sign.getLine(2).replace("for ", ""))
                                               == 1
                                           ? plugin.getPrimaryCurrency().getNameSingular()
                                           : plugin.getPrimaryCurrency().getNamePlural())
                                       + " from "
                                       + event.getPlayer().getDisplayName());
                         }
                         if (sign.getLine(3).equalsIgnoreCase("admin")) {
                           plugin.addMoney(
                               event.getPlayer(),
                               -Integer.parseInt(sign.getLine(2).split(" ")[1]));
                         } else {
                           plugin.transferMoney(
                               characterPlugin.getCharacter(Integer.parseInt(sign.getLine(3))),
                               characterPlugin.getActiveCharacter(event.getPlayer()),
                               Integer.parseInt(sign.getLine(2).replace("for ", "")));
                         }
                         ItemStack item = new ItemStack(event.getPlayer().getItemInHand());
                         item.setAmount(Integer.parseInt(sign.getLine(1).split(" ")[2]));
                         chest.getInventory().addItem(item);
                         if (event.getPlayer().getItemInHand().getAmount()
                             > Integer.parseInt(sign.getLine(1).split(" ")[2])) {
                           event
                               .getPlayer()
                               .getItemInHand()
                               .setAmount(
                                   event.getPlayer().getItemInHand().getAmount()
                                       - Integer.parseInt(sign.getLine(1).split(" ")[2]));
                         } else {
                           event.getPlayer().setItemInHand(null);
                         }
                       } catch (NumberFormatException ignored) {
                       }
                     }
                   } else {
                     event
                         .getPlayer()
                         .sendMessage(
                             plugin.getPrefix()
                                 + ChatColor.RED
                                 + "You do not have enough "
                                 + event
                                     .getPlayer()
                                     .getItemInHand()
                                     .getType()
                                     .toString()
                                     .toLowerCase()
                                     .replace('_', ' ')
                                 + "s to sell");
                   }
                 } else {
                   event
                       .getPlayer()
                       .sendMessage(
                           plugin.getPrefix()
                               + ChatColor.RED
                               + "You must be holding at least "
                               + Integer.parseInt(sign.getLine(1).split(" ")[2])
                               + " "
                               + Material.matchMaterial(
                                       sign.getLine(1).split(" ")[1].replace(' ', '_'))
                                   .toString()
                                   .toLowerCase()
                                   .replace('_', ' ')
                               + "s");
                 }
               }
             }
           }
         }
       }
     }
   }
 }