public ItemStack getBoots(String id) {
   FileConfiguration c = SettingsManager.getInstance().getClasses();
   if (c.contains(id + ".boots")) {
     Material m = Material.getMaterial(c.getInt(id + ".boots.id"));
     ItemStack is = new ItemStack(m, 1);
     if (m == Material.LEATHER_BOOTS && c.contains(id + ".boots.color")) {
       String color = c.getString(id + ".boots.color");
       String[] rgb = color.split(",");
       int r = Integer.parseInt(rgb[0]);
       int g = Integer.parseInt(rgb[1]);
       int b = Integer.parseInt(rgb[2]);
       is = Colorizer.setColor(is, r, g, b);
     }
     if (c.contains(id + ".boots.enchantments")) {
       for (String e : c.getConfigurationSection(id + ".boots.enchantments").getKeys(false)) {
         Enchantment enchant = Enchantment.getByName(e);
         int level = Integer.parseInt(c.getString(id + ".boots.enchantments." + e));
         is.addUnsafeEnchantment(enchant, level);
       }
     }
     return is;
   } else {
     return new ItemStack(Material.AIR);
   }
 }
Beispiel #2
0
 public ItemStack setEnchantments(ItemStack item, String enchants) {
   ItemStack i = item.clone();
   if (enchants.isEmpty()) return i;
   String[] ln = enchants.split(",");
   for (String ec : ln) {
     if (ec.isEmpty()) continue;
     Color clr = colorByName(ec);
     if (clr != null) {
       if (isIdInList(item.getTypeId(), "298,299,300,301")) {
         LeatherArmorMeta meta = (LeatherArmorMeta) i.getItemMeta();
         meta.setColor(clr);
         i.setItemMeta(meta);
       }
     } else {
       String ench = ec;
       int level = 1;
       if (ec.contains(":")) {
         ench = ec.substring(0, ec.indexOf(":"));
         level = Math.max(1, getMinMaxRandom(ec.substring(ench.length() + 1)));
       }
       Enchantment e = Enchantment.getByName(ench.toUpperCase());
       if (e == null) continue;
       i.addUnsafeEnchantment(e, level);
     }
   }
   return i;
 }
 @SuppressWarnings("deprecation")
 public ArrayList<ItemStack> getInventory(String id) {
   FileConfiguration c = SettingsManager.getInstance().getClasses();
   ArrayList<ItemStack> inv = new ArrayList<ItemStack>();
   for (String item : c.getConfigurationSection(id + ".items").getKeys(false)) {
     try {
       Material m = Material.getMaterial(c.getInt(id + ".items." + item + ".id"));
       int amount = c.getInt(id + ".items." + item + ".amount");
       ItemStack is = new ItemStack(m, amount);
       if (c.contains(id + ".items." + item + ".id-modifier")) {
         int idm = c.getInt(id + ".items." + item + ".id-modifier");
         is.setDurability((short) idm);
       }
       if (c.contains(id + ".items." + item + ".enchantments")) {
         for (String e :
             c.getConfigurationSection(id + ".items." + item + ".enchantments").getKeys(false)) {
           Enchantment enchant = Enchantment.getByName(e);
           int level = Integer.parseInt(c.getString(id + ".items." + item + ".enchantments." + e));
           is.addUnsafeEnchantment(enchant, level);
         }
       }
       inv.add(is);
     } catch (Exception e) {
       System.out.println(
           "Error adding item " + item + " for class " + id + ", please check the yml file.");
     }
   }
   return inv;
 }
Beispiel #4
0
  /**
   * parse through dat to create a new item with enchantments, quantity, and damage value
   *
   * @param cfg to parse through
   * @return the new ItemStack
   */
  private ItemStack parseItem(ConfigurationSection cfg) {
    Map<String, Object> vals = cfg.getValues(true);
    ItemStack newItem;
    // check material
    newItem =
        new ItemStack(Material.getMaterial((cfg.getString("material", "stone")).toUpperCase()));

    // check damage value

    newItem.setDurability((short) (cfg.getInt("damage", 0)));

    // check quantity

    newItem.setAmount(cfg.getInt("amount", 1));

    // enchantment parsing
    for (String s : cfg.getStringList("enchantments")) {
      String[] parts = s.split(":");
      String enchant = parts[0];
      int level = 1;

      if (parts.length > 1) {
        level = Integer.parseInt(parts[1]);
      }
      newItem.addUnsafeEnchantment(Enchantment.getByName(enchant.toUpperCase()), level);
    }

    // System.out.println("Item: "+ newItem.toString());
    return newItem;

    // ItemStack is = new ItemStack(Material.);
  }
  /**
   * Add an enchantment.
   *
   * @param level The enchantment level.
   * @param enchantmentName The enchantment name.
   * @return Self for chaining.
   * @throws IllegalArgumentException if the enchantment name is not recognized.
   */
  public ItemStackBuilder enchant(int level, String enchantmentName) {
    PreCon.notNull(enchantmentName);

    Enchantment enchantment = Enchantment.getByName(enchantmentName);
    if (enchantment == null)
      throw new IllegalArgumentException("Cannot find an enchantment named " + enchantmentName);

    return enchant(level, enchantment);
  }
  public static CustomEnchantment valueOf(String s) {
    String[] name = s.split("-");
    int lvl = 1;
    if (name.length > 1 && !name[1].isEmpty()) {
      lvl = Integer.valueOf(name[1]);
    }

    Enchantment en = Enchantment.getByName(name[0]);
    CustomEnchantment ce = enchants.get(name[0]);

    if (en != null) {
      return new CustomEnchantment(en, lvl);
    } else if (ce != null) {
      return new CustomEnchantment(ce, lvl);
    } else {
      return null;
    }
  }
Beispiel #7
0
 public static ArrayList<ItemStack> getFinalItems(String reward, Player player) {
   ArrayList<ItemStack> items = new ArrayList<ItemStack>();
   for (String l : Main.settings.getFile(GUI.Crate.get(player)).getStringList(reward + ".Items")) {
     ArrayList<String> lore = new ArrayList<String>();
     HashMap<Enchantment, Integer> enchs = new HashMap<Enchantment, Integer>();
     String name = "";
     int amount = 1;
     String m = "Stone";
     for (String i : l.split(", ")) {
       if (i.contains("Item:")) {
         i = i.replaceAll("Item:", "");
         m = i;
       }
       if (i.contains("Name:")) {
         i = i.replaceAll("Name:", "");
         i = i.replaceAll("_", " ");
         name = color(i);
       }
       if (i.contains("Amount:")) {
         i = i.replaceAll("Amount:", "");
         amount = Integer.parseInt(i);
       }
       if (i.contains("Lore:")) {
         i = i.replaceAll("Lore:", "");
         for (String L : i.split(",")) {
           L = color(L);
           lore.add(L);
         }
       }
       for (String enc : getEnchants()) {
         if (i.contains(enc + ":")) {
           String[] breakdown = i.split(":");
           int lvl = Integer.parseInt(breakdown[1]);
           enchs.put(Enchantment.getByName(enc), lvl);
         }
       }
     }
     items.add(makeItem(m, amount, name, lore, enchs));
   }
   return items;
 }
Beispiel #8
0
 public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
   if (sender.hasPermission("bukkit.command.enchant")) {
     Player p = Bukkit.getPlayerExact(sender.getName());
     if (args.length == 0) {
       List<String> names = new ArrayList<String>();
       for (Enchantment enchant : Enchantment.values()) names.add(enchant.getName().toUpperCase());
       sender.sendMessage(
           ChatColor.GREEN
               + "Enchants: |"
               + ChatColor.RED
               + StringUtils.join(names, ChatColor.GREEN + "|  |" + ChatColor.RED)
               + ChatColor.GREEN
               + "|");
     } else {
       Enchantment enchant = Enchantment.getByName(args[0].toUpperCase());
       if (enchant == null) {
         for (Enchantment enchantment : Enchantment.values())
           if (enchantment.getName().equalsIgnoreCase(args[0])) enchant = enchantment;
         if (enchant == null) {
           sender.sendMessage(ChatColor.GREEN + "Enchant: " + args[0] + " - Not found");
           return true;
         }
       }
       int level = enchant.getMaxLevel();
       if (args.length > 1) {
         if (lib.isNumeric(args[1])) level = Integer.parseInt(args[1]);
       }
       if (p.getItemInHand() != null && p.getItemInHand().getType() != Material.AIR) {
         p.getItemInHand().addUnsafeEnchantment(enchant, level);
         p.sendMessage(
             ChatColor.GREEN
                 + "Item enchanted with "
                 + enchant.getName()
                 + " with level "
                 + level);
         updateEnchants(p.getItemInHand());
       } else p.sendMessage(ChatColor.GREEN + "Unable to enchant item");
     }
   } else sender.sendMessage(ChatColor.RED + "You do not have permission to use this command");
   return true;
 }
  /**
   * Required method for configuration serialization
   *
   * @param args map to deserialize
   * @return deserialized item stack
   * @see ConfigurationSerializable
   */
  public static ItemStack deserialize(Map<String, Object> args) {
    Material type = Material.getMaterial((String) args.get("type"));
    short damage = 0;
    int amount = 1;

    if (args.containsKey("damage")) {
      damage = ((Number) args.get("damage")).shortValue();
    }

    if (args.containsKey("amount")) {
      amount = ((Number) args.get("amount")).intValue();
    }

    ItemStack result = new ItemStack(type, amount, damage);

    if (args.containsKey("enchantments")) { // Backward compatiblity, @deprecated
      Object raw = args.get("enchantments");

      if (raw instanceof Map) {
        Map<?, ?> map = (Map<?, ?>) raw;

        for (Map.Entry<?, ?> entry : map.entrySet()) {
          Enchantment enchantment = Enchantment.getByName(entry.getKey().toString());

          if ((enchantment != null) && (entry.getValue() instanceof Integer)) {
            result.addUnsafeEnchantment(enchantment, (Integer) entry.getValue());
          }
        }
      }
    } else if (args.containsKey(
        "meta")) { // We cannot and will not have meta when enchantments (pre-ItemMeta) exist
      Object raw = args.get("meta");
      if (raw instanceof ItemMeta) {
        result.setItemMeta((ItemMeta) raw);
      }
    }

    return result;
  }
 public ItemStack getHelmet(String id) {
   FileConfiguration c = SettingsManager.getInstance().getClasses();
   if (c.contains(id + ".helmet")) {
     Material m = Material.getMaterial(c.getInt(id + ".helmet.id"));
     ItemStack is = new ItemStack(m, 1);
     if (m == Material.LEATHER_HELMET && c.contains(id + ".helmet.color")) {
       String color = c.getString(id + ".helmet.color");
       String[] rgb = color.split(",");
       int r = Integer.parseInt(rgb[0]);
       int g = Integer.parseInt(rgb[1]);
       int b = Integer.parseInt(rgb[2]);
       is = Colorizer.setColor(is, r, g, b);
     }
     if (c.contains(id + ".helmet.id-modifier")) {
       int idm = c.getInt(id + ".helmet.id-modifier");
       is.setDurability((short) idm);
     }
     if (c.getInt(id + ".helmet.id") == 397
         && c.getInt(id + ".helmet.id-modifier") == 3
         && c.contains(id + ".helmet.player")) {
       String player = c.getString(id + ".helmet.player");
       SkullMeta meta = (SkullMeta) is.getItemMeta();
       meta.setOwner(player);
       is.setItemMeta(meta);
     }
     if (c.contains(id + ".helmet.enchantments")) {
       for (String e : c.getConfigurationSection(id + ".helmet.enchantments").getKeys(false)) {
         Enchantment enchant = Enchantment.getByName(e);
         int level = Integer.parseInt(c.getString(id + ".helmet.enchantments." + e));
         is.addUnsafeEnchantment(enchant, level);
       }
     }
     return is;
   } else {
     return new ItemStack(Material.AIR);
   }
 }
 @Override
 public ItemStack getItemStack(final String path) {
   final ItemStack stack =
       new ItemStack(
           Material.valueOf(getString(path + ".type", "AIR")),
           getInt(path + ".amount", 1),
           (short) getInt(path + ".damage", 0));
   final ConfigurationSection enchants = getConfigurationSection(path + ".enchant");
   if (enchants != null) {
     for (String enchant : enchants.getKeys(false)) {
       final Enchantment enchantment = Enchantment.getByName(enchant.toUpperCase(Locale.ENGLISH));
       if (enchantment == null) {
         continue;
       }
       final int level = getInt(path + ".enchant." + enchant, enchantment.getStartLevel());
       stack.addUnsafeEnchantment(enchantment, level);
     }
   }
   return stack;
   /*
    * ,
    * (byte)getInt(path + ".data", 0)
    */
 }
Beispiel #12
0
  public DataFileCache readCache(String playername) {
    final File file = new File("cache/" + playername + ".cache");

    ItemStack[] stacki = new ItemStack[36];
    ItemStack[] stacka = new ItemStack[4];
    String group = null;
    boolean op = false;

    if (!file.exists()) {
      return new DataFileCache(stacki, stacka);
    }

    Scanner reader = null;
    try {
      reader = new Scanner(file);

      int i = 0;
      int a = 0;
      while (reader.hasNextLine()) {
        final String line = reader.nextLine();

        if (!line.contains(":")) {
          // the fist line rapresent the player group and operator status
          final String[] playerInfo = line.split(";");
          group = playerInfo[0];

          if (Integer.parseInt(playerInfo[1]) == 1) {
            op = true;
          } else op = false;

          continue;
        }

        final String[] in = line.split(":");

        /*if (in.length != 4) {
        	continue;
        } */

        if (!in[0].equals("i") && !in[0].equals("w")) {
          continue;
        }
        // can enchant item? size ofstring in file - 4  all / 2 = number of enchant
        if (in[0].equals("i")) {
          stacki[i] =
              new ItemStack(
                  Integer.parseInt(in[1]), Integer.parseInt(in[2]), Short.parseShort((in[3])));
          // qui c'e' un problema serio!
          if (in.length > 4 && !in[4].isEmpty()) {
            for (int k = 4; k < in.length - 1; k++) {
              // System.out.println("enchant "+in[k]);
              stacki[i].addUnsafeEnchantment(
                  Enchantment.getByName(in[k]), Integer.parseInt(in[k + 1]));
              k++;
            }
          }
          i++;
        } else {
          stacka[a] =
              new ItemStack(
                  Integer.parseInt(in[1]), Integer.parseInt(in[2]), Short.parseShort((in[3])));
          if (in.length > 4 && !in[4].isEmpty()) {
            for (int k = 4; k < in.length - 1; k++) {
              // System.out.println("enchant "+in[k]);
              stacka[a].addUnsafeEnchantment(
                  Enchantment.getByName(in[k]), Integer.parseInt(in[k + 1]));
              k++;
            }
          }
          a++;
        }
      }
    } catch (final Exception e) {
      e.printStackTrace();
    } finally {
      if (reader != null) {
        reader.close();
      }
    }
    return new DataFileCache(stacki, stacka, group, op);
  }
  // ENCHANTMENT
  public static Enchantment getEnchantment(String enc) {
    enc = enc.toUpperCase();
    Enchantment en = Enchantment.getByName(enc);

    if (en == null) {
      switch (enc) {
        case "PROTECTION":
          en = Enchantment.PROTECTION_ENVIRONMENTAL;
          break;
        case "FIRE_PROTECTION":
          en = Enchantment.PROTECTION_FIRE;
          break;
        case "FEATHER_FALLING":
          en = Enchantment.PROTECTION_FALL;
          break;
        case "BLAST_PROTECTION":
          en = Enchantment.PROTECTION_EXPLOSIONS;
          break;
        case "PROJECTILE_PROTCETION":
          en = Enchantment.PROTECTION_PROJECTILE;
          break;
        case "RESPIRATION":
          en = Enchantment.OXYGEN;
          break;
        case "AQUA_AFFINITY":
          en = Enchantment.WATER_WORKER;
          break;
        case "SHARPNESS":
          en = Enchantment.DAMAGE_ALL;
          break;
        case "SMITE":
          en = Enchantment.DAMAGE_UNDEAD;
          break;
        case "BANE_OF_ARTHROPODS":
          en = Enchantment.DAMAGE_ARTHROPODS;
          break;
        case "LOOTING":
          en = Enchantment.LOOT_BONUS_MOBS;
          break;
        case "EFFICIENCY":
          en = Enchantment.DIG_SPEED;
          break;
        case "UNBREAKING":
          en = Enchantment.DURABILITY;
          break;
        case "FORTUNE":
          en = Enchantment.LOOT_BONUS_BLOCKS;
          break;
        case "POWER":
          en = Enchantment.ARROW_DAMAGE;
          break;
        case "PUNCH":
          en = Enchantment.ARROW_KNOCKBACK;
          break;
        case "FLAME":
          en = Enchantment.ARROW_FIRE;
          break;
        case "INFINITY":
          en = Enchantment.ARROW_INFINITE;
          break;
        case "LUCK_OF_THE_SEA":
          en = Enchantment.LUCK;
          break;
      }
    }

    return en;
  }
Beispiel #14
0
 // Enchantments
 public static Map<Enchantment, Integer> loadEnchantments(final ConfigurationSection config) {
   final Map<Enchantment, Integer> map = new LinkedHashMap<Enchantment, Integer>();
   for (final String name : config.getKeys(false))
     map.put(Enchantment.getByName(name), config.getInt(name));
   return map;
 }