Example #1
0
  public boolean setPlayerInventorySlot(
      String playerName, int slot, int blockID, int damage, int quantity) {
    try {
      if (blockID == 0) {
        return clearPlayerInventorySlot(playerName, slot);
      }

      Player p = getPlayerExact(playerName);
      PlayerInventory inv = p.getInventory();
      ItemStack it =
          new ItemStack(blockID, quantity, Short.valueOf(String.valueOf(damage)).shortValue());

      if (slot == 103) inv.setHelmet(it);
      else if (slot == 102) inv.setChestplate(it);
      else if (slot == 101) inv.setLeggings(it);
      else if (slot == 100) inv.setBoots(it);
      else inv.setItem(slot, it);

      p.saveData();

      return true;
    } catch (NullPointerException e) {
      return false;
    }
  }
  public static short getAnyDataShort(String objectString, String enumValue)
      throws IllegalArgumentException {
    // Firstly, can the string be cast directly as a short?
    try {
      Short s = Short.parseShort(enumValue);
      return s;
    } catch (NumberFormatException ex) {
    }

    // If not, test the enum
    if (objectString.equalsIgnoreCase("LOG")
        || objectString.equalsIgnoreCase("LEAVES")
        || objectString.equalsIgnoreCase("SAPLING")) {
      return (short) TreeSpecies.valueOf(enumValue).getData();
    } else if (objectString.equalsIgnoreCase("WOOL")
        || objectString.equalsIgnoreCase("CREATURE_SHEEP")) {
      return (short) DyeColor.valueOf(enumValue).getData();
    } else if (objectString.equalsIgnoreCase("INK_SACK")) {
      return (short) (0xF - DyeColor.valueOf(enumValue).getData());
    } else if (objectString.equalsIgnoreCase("COAL")) {
      return (short) (CoalType.valueOf(enumValue).getData());
    } else if (objectString.equalsIgnoreCase("CROPS")) {
      return (short) (CropState.valueOf(enumValue).getData());
    } else if (objectString.equalsIgnoreCase("STEP")
        || objectString.equalsIgnoreCase("DOUBLE_STEP")) {
      if (enumValue.equalsIgnoreCase("STONE")) return 0;
      else if (enumValue.equalsIgnoreCase("SANDSTONE")) return 1;
      else if (enumValue.equalsIgnoreCase("WOOD")) return 2;
      else if (enumValue.equalsIgnoreCase("COBBLESTONE")) return 3;
      else throw new IllegalArgumentException();
    } else if (objectString.equalsIgnoreCase("CREATURE_PIG")) {
      if (enumValue.equalsIgnoreCase("UNSADDLED")) return 0;
      else if (enumValue.equalsIgnoreCase("SADDLED")) return 1;
      else throw new IllegalArgumentException();
    } else if (objectString.equalsIgnoreCase("CREATURE_CREEPER")) {
      if (enumValue.equalsIgnoreCase("UNPOWERED")) return 0;
      else if (enumValue.equalsIgnoreCase("POWERED")) return 1;
      else throw new IllegalArgumentException();
    } else if (objectString.equalsIgnoreCase("CREATURE_WOLF")) {
      if (enumValue.equalsIgnoreCase("NEUTRAL")) return 0;
      else if (enumValue.equalsIgnoreCase("TAME")) return 1;
      else if (enumValue.equalsIgnoreCase("TAMED")) return 1;
      else if (enumValue.equalsIgnoreCase("ANGRY")) return 2;
      else throw new IllegalArgumentException();
    } else if (objectString.equalsIgnoreCase("CREATURE_SLIME")) {
      if (enumValue.equalsIgnoreCase("TINY")) return 1;
      else if (enumValue.equalsIgnoreCase("SMALL")) return 2;
      else if (enumValue.equalsIgnoreCase("BIG")) return 3;
      else if (enumValue.equalsIgnoreCase("HUGE")) return 4;
      else throw new IllegalArgumentException();
    } else {
      return 0;
    }
  }
Example #3
0
  public ItemStack parseItemStack(String itemstr) {
    if (itemstr.isEmpty()) return null;

    String istr = itemstr;
    String enchant = "";
    String name = "";

    if (istr.contains("$")) {
      name = istr.substring(0, istr.indexOf("$"));
      istr = istr.substring(name.length() + 1);
    }
    if (istr.contains("@")) {
      enchant = istr.substring(istr.indexOf("@") + 1);
      istr = istr.substring(0, istr.indexOf("@"));
    }
    int id = -1;
    int amount = 1;
    short data = 0;
    String[] si = istr.split("\\*");

    if (si.length > 0) {
      if (si.length == 2) amount = Math.max(getMinMaxRandom(si[1]), 1);
      String ti[] = si[0].split(":");
      if (ti.length > 0) {
        if (ti[0].matches("[0-9]*")) id = Integer.parseInt(ti[0]);
        else {
          Material m = Material.getMaterial(ti[0].toUpperCase());
          if (m == null) {
            logOnce("wrongitem" + ti[0], "Could not parse item material name (id) " + ti[0]);
            return null;
          }
          id = m.getId();
        }
        if ((ti.length == 2) && (ti[1]).matches("[0-9]*")) data = Short.parseShort(ti[1]);
        ItemStack item = new ItemStack(id, amount, data);
        if (!enchant.isEmpty()) {
          item = setEnchantments(item, enchant);
        }
        if (!name.isEmpty()) {
          ItemMeta im = item.getItemMeta();
          im.setDisplayName(ChatColor.translateAlternateColorCodes('&', name.replace("_", " ")));
          item.setItemMeta(im);
        }
        return item;
      }
    }
    return null;
  }
  @SuppressWarnings("deprecation")
  @Override
  protected boolean onCommand(Player player, String command, String[] args) {
    if (command.equalsIgnoreCase("createkit")) {
      if (player.hasPermission(Permissions.COMMAND_KIT_CREATE)) {
        if (this.getPlugin().cmdValues.createKits) {
          if (Utilities.inPvPWorld(player)) {
            if (args.length == 0) {
              Lang.sendMessage(
                  player,
                  Lang.COMMAND_GEN_USAGE,
                  command.toLowerCase() + " [<kit>|<kit> <guiitem>]");
              Lang.sendMessage(player, Lang.COMMAND_CREATE_KIT_DESCRIPTION);
            } else if (args.length > 0 && args.length < 3) {
              String kitName = args[0];

              boolean containsKit = this.getPlugin().getKitsConfig().contains(kitName);
              if (!containsKit) {
                List<String> currentKits = this.getPlugin().getKitList();
                List<String> currentKitsLC = Utilities.toLowerCaseList(currentKits);
                if (currentKitsLC.contains(kitName.toLowerCase()))
                  kitName = currentKits.get(currentKitsLC.indexOf(kitName.toLowerCase()));
                containsKit = this.getPlugin().getKitsConfig().contains(kitName);
              }

              if (!this.containsIllegalCharacters(kitName)) {
                if (args.length == 2) {
                  if (args[1].contains(":")) {
                    String[] guiSplit = args[1].split(":");
                    if (guiSplit.length == 2) {
                      if (!this.isInteger(guiSplit[0]) || !this.isInteger(guiSplit[1])) {
                        Lang.sendMessage(
                            player,
                            Lang.COMMAND_GEN_USAGE,
                            command.toLowerCase() + " [<kit>|<kit> <guiitem>]");
                        return true;
                      }
                    } else {
                      if (!this.isInteger(args[1])) {
                        Lang.sendMessage(
                            player,
                            Lang.COMMAND_GEN_USAGE,
                            command.toLowerCase() + " [<kit>|<kit> <guiitem>]");
                        return true;
                      }
                    }
                  } else {
                    if (!this.isInteger(args[1])) {
                      Lang.sendMessage(
                          player,
                          Lang.COMMAND_GEN_USAGE,
                          command.toLowerCase() + " [<kit>|<kit> <guiitem>]");
                      return true;
                    }
                  }
                }

                Map<Integer, ItemStack> itemsInInv = new HashMap<>();
                List<ItemStack> armourInInv = new ArrayList<>();
                ItemStack[] pContents = player.getInventory().getContents();
                if (pContents == null) pContents = new ItemStack[player.getInventory().getSize()];
                for (int i = 0; i < player.getInventory().getSize(); i++) {
                  if (pContents.length > i && pContents[i] != null) itemsInInv.put(i, pContents[i]);
                  else itemsInInv.put(i, new ItemStack(Material.AIR));
                }
                for (ItemStack armour : player.getInventory().getArmorContents())
                  if (armour != null && armour.getType() != Material.AIR) armourInInv.add(armour);
                PlayerCreateKitEvent createKitEvent =
                    new PlayerCreateKitEvent(player, kitName, itemsInInv, armourInInv, false);
                player.getServer().getPluginManager().callEvent(createKitEvent);

                if (!createKitEvent.isCancelled()) {
                  itemsInInv = createKitEvent.getKitContentsWithSlots();
                  armourInInv = createKitEvent.getKitArmour();
                  if (itemsInInv.size() > 0 || armourInInv.size() > 0) {
                    if (containsKit) {
                      this.getPlugin().getKitsConfig().set(kitName, null);
                      this.getPlugin().saveKitsConfig();
                      if (this.getPlugin().kitList.containsKey(kitName))
                        this.getPlugin().kitList.remove(kitName);
                    }

                    final Kit kit =
                        new Kit(kitName, itemsInInv).setRealName(kitName).setArmour(armourInInv);
                    if (args.length == 2) {
                      ItemStack guiItem = null;
                      try {
                        guiItem = new ItemStack(Integer.parseInt(args[1]));
                      } catch (Exception ignored) {
                      }
                      try {
                        if (args[1].contains(":")) {
                          String[] guiSplit = args[1].split(":");
                          guiItem = new ItemStack(Integer.parseInt(guiSplit[0]));
                          guiItem.setDurability(Short.parseShort(guiSplit[1]));
                        }
                      } catch (Exception ignored) {
                      }
                      if (guiItem != null) {
                        if (guiItem.getType() != Material.AIR) {
                          kit.setGuiItem(guiItem);
                        }
                      }
                    }

                    List<PotionEffect> kitPotionEffects = new ArrayList<>();
                    for (PotionEffect potionEffect : player.getActivePotionEffects()) {
                      if (potionEffect != null) kitPotionEffects.add(potionEffect);
                    }
                    if (!kitPotionEffects.isEmpty()) kit.setPotionEffects(kitPotionEffects);

                    kit.setMaxHealth((int) player.getMaxHealth());

                    this.getPlugin().getKitsConfig().set(kitName, kit.serialize());
                    this.getPlugin().kitList.put(kitName, kit);
                    this.getPlugin().saveKitsConfig();

                    try {
                      player
                          .getServer()
                          .getPluginManager()
                          .addPermission(new Permission("kingkits.kits." + kitName.toLowerCase()));
                      player
                          .getServer()
                          .getPluginManager()
                          .addPermission(new Permission("kingkits.free." + kitName.toLowerCase()));
                    } catch (Exception ignored) {
                    }
                    Lang.sendMessage(
                        player,
                        containsKit ? Lang.COMMAND_CREATE_OVERWRITTEN : Lang.COMMAND_CREATE_CREATED,
                        kitName);

                    if (this.getPlugin().configValues.removeItemsOnCreateKit) {
                      player.getInventory().clear();
                      player.getInventory().setArmorContents(null);
                    }
                  } else {
                    Lang.sendMessage(player, Lang.COMMAND_CREATE_EMPTY_INV);
                  }
                } else {
                  Lang.sendMessage(player, Lang.COMMAND_CREATE_DENIED);
                }
              } else {
                Lang.sendMessage(player, Lang.COMMAND_CREATE_ILLEGAL_CHARACTERS);
              }
            } else {
              Lang.sendMessage(
                  player,
                  Lang.COMMAND_GEN_USAGE,
                  command.toLowerCase() + " [<kit>|<kit> <guiitem>]");
            }
          } else {
            Lang.sendMessage(player, Lang.COMMAND_GEN_WORLD);
          }
        } else {
          Lang.sendMessage(player, Lang.COMMAND_GEN_DISABLED);
        }
      } else {
        this.sendNoAccess(player);
      }
      return true;
    }
    return false;
  }