public static void enchantValidate(Player p, String e, int rlvl) { // shut up for now :P HashMap<Enchantment, Integer> fen = new HashMap<Enchantment, Integer>(); ItemStack i = null; int lvl = 0; Enchantment en = Enchantment.getById(getEnchantId(e)); if (p.getItemInHand().getTypeId() == ((ItemStack) item.get(p.getName())).getTypeId()) { i = p.getItemInHand(); if (i.getAmount() == 1) { if (i.containsEnchantment(en)) { int plvl = i.getEnchantmentLevel(en); lvl = plvl + rlvl; } else { lvl = rlvl; } fen.put(en, lvl); enchants.put(p.getName(), fen); ChatListener.add(p, 2); p.sendMessage( "The enchant will cost you: " + getEnchantCost(p, e.toLowerCase(), rlvl) + "! Should I continue? Please type \"yes\" or \"no\"!"); } else { p.sendMessage( ChatColor.RED + "You have more then one item in your slot, please try again!"); } } }
public boolean addEnchantmentsToPlayerInventorySlot( String playerName, int slot, List<Object> enchantments) { try { Player p = getPlayerExact(playerName); PlayerInventory inv = p.getInventory(); ItemStack it; if (slot == inv.getHeldItemSlot()) it = inv.getHelmet(); else if (slot == 102) it = inv.getChestplate(); else if (slot == 101) it = inv.getLeggings(); else if (slot == 100) it = inv.getBoots(); else it = inv.getItem(slot); for (int i = 0; i < enchantments.size(); i++) { JSONObject o = (JSONObject) enchantments.get(i); it.addEnchantment( Enchantment.getById(Integer.valueOf(o.get("enchantment").toString())), Integer.valueOf(o.get("level").toString())); } p.saveData(); return true; } catch (NullPointerException e) { return false; } }
public static Map<Enchantment, Integer> convertEnchantLevelMap(JsonElement jsonElement) { JsonObject json = jsonElement.getAsJsonObject(); Map<Enchantment, Integer> ret = new HashMap<Enchantment, Integer>(); for (Entry<String, JsonElement> entry : json.entrySet()) { int id = Integer.valueOf(entry.getKey()); Enchantment ench = Enchantment.getById(id); int lvl = entry.getValue().getAsInt(); ret.put(ench, lvl); } return ret; }
public static SwagStack stringToSwagStack(String swagString) { String[] swagPieces = swagString.split(","); SwagStack swagStack = new SwagStack(Integer.valueOf(swagPieces[0])); if (!swagPieces[1].equals("0")) { if (swagPieces[1].contains("-")) { String[] dmg = swagPieces[1].split("-"); swagStack.setMinDmg(Short.valueOf(dmg[0])); swagStack.setMaxDmg(Short.valueOf(dmg[1])); } else { swagStack.setMinDmg(Short.valueOf(swagPieces[1])); swagStack.setMaxDmg(Short.valueOf(swagPieces[1])); } } if (swagPieces[2].contains("-")) { String[] amnt = swagPieces[2].split("-"); swagStack.setMinAmnt(Integer.valueOf(amnt[0])); swagStack.setMaxAmnt(Integer.valueOf(amnt[1])); } else { swagStack.setMinAmnt(Integer.valueOf(swagPieces[2])); swagStack.setMaxAmnt(Integer.valueOf(swagPieces[2])); } if (!swagPieces[3].equals("0")) { Map<Enchantment, Integer> enchantMap = new HashMap<Enchantment, Integer>(); String[] enchants = swagPieces[3].split("&"); for (String enchant : enchants) { String[] fullEnchant = enchant.split("#"); enchantMap.put( Enchantment.getById(Integer.valueOf(fullEnchant[0])), Integer.valueOf(fullEnchant[1])); } swagStack.setEnchants(enchantMap); } return swagStack; }
public boolean addEnchantmentToPlayerInventorySlot( String playerName, int slot, int enchantmentID, int level) { try { Player p = getPlayerExact(playerName); PlayerInventory inv = p.getInventory(); ItemStack it; if (slot == 103) it = inv.getHelmet(); else if (slot == 102) it = inv.getChestplate(); else if (slot == 101) it = inv.getLeggings(); else if (slot == 100) it = inv.getBoots(); else it = inv.getItem(slot); it.addEnchantment(Enchantment.getById(enchantmentID), level); p.saveData(); return true; } catch (NullPointerException e) { return false; } }
public void spawnChest(Location l) { l.getBlock().setType(Material.CHEST); Chest c = (Chest) l.getBlock().getState(); List<String> items = BGFiles.chestconf.getStringList("ITEMS"); for (String item : items) { Random r = new Random(); String[] oneitem = item.split(","); if (Boolean.parseBoolean(oneitem[3]) || r.nextBoolean()) { ItemStack i; Integer id = Integer.valueOf(Integer.parseInt(oneitem[0])); Integer minAmount = Integer.valueOf(Integer.parseInt(oneitem[1])); Integer maxAmount = Integer.valueOf(Integer.parseInt(oneitem[2])); Integer amount = r.nextInt((maxAmount + 1) - minAmount) + minAmount; i = new ItemStack(id.intValue(), amount.intValue()); if (oneitem.length == 6) { i.addUnsafeEnchantment( Enchantment.getById(Integer.parseInt(oneitem[4])), Integer.parseInt(oneitem[5])); } c.getInventory().addItem(new ItemStack[] {i}); } } c.update(true); DecimalFormat df = new DecimalFormat("##.#"); BGChat.printInfoChat( "Chest spawned at X: " + df.format(l.getX()) + " | Y: " + df.format(l.getY()) + " | Z: " + df.format(l.getZ())); }
public boolean setPlayerInventorySlotWithDataDamageAndEnchantments( String playerName, int slot, int blockID, final int data, int damage, int quantity, Object[] enchantments) { try { if (blockID == 0) { return clearPlayerInventorySlot(playerName, slot); } Player p = getPlayerExact(playerName); PlayerInventory inv = p.getInventory(); ItemStack it = (new MaterialData(blockID, (byte) data)).toItemStack(quantity); it.setDurability((short) data); for (int i = 0; i < enchantments.length; i++) { JSONObject o = (JSONObject) enchantments[i]; it.addEnchantment( Enchantment.getById(Integer.valueOf(o.get("enchantment").toString())), Integer.valueOf(o.get("level").toString())); } 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 Inventory StringToInventory(String invString) { String[] serializedBlocks = invString.split(";"); String invInfo = serializedBlocks[0]; Inventory deserializedInventory = Bukkit.getServer().createInventory(null, Integer.valueOf(invInfo)); for (int i = 1; i < serializedBlocks.length; i++) { String[] serializedBlock = serializedBlocks[i].split("#"); int stackPosition = Integer.valueOf(serializedBlock[0]); if (stackPosition >= deserializedInventory.getSize()) { continue; } ItemStack is = null; Boolean createdItemStack = false; String[] serializedItemStack = serializedBlock[1].split(":"); for (String itemInfo : serializedItemStack) { String[] itemAttribute = itemInfo.split("@"); if (itemAttribute[0].equals("t")) { is = new ItemStack(Material.getMaterial(Integer.valueOf(itemAttribute[1]))); createdItemStack = true; } else if (itemAttribute[0].equals("d") && createdItemStack) { is.setDurability(Short.valueOf(itemAttribute[1])); } else if (itemAttribute[0].equals("a") && createdItemStack) { is.setAmount(Integer.valueOf(itemAttribute[1])); } else if (itemAttribute[0].equals("e") && createdItemStack) { is.addEnchantment( Enchantment.getById(Integer.valueOf(itemAttribute[1])), Integer.valueOf(itemAttribute[2])); } } deserializedInventory.setItem(stackPosition, is); } return deserializedInventory; }
public boolean removeEnchantmentsFromPlayerInventorySlot( String playerName, int slot, List<Object> enchantments) { try { Player p = getPlayerExact(playerName); PlayerInventory inv = p.getInventory(); ItemStack it; if (slot == inv.getHeldItemSlot()) it = inv.getHelmet(); else if (slot == 102) it = inv.getChestplate(); else if (slot == 101) it = inv.getLeggings(); else if (slot == 100) it = inv.getBoots(); else it = inv.getItem(slot); for (Object o : enchantments) { it.removeEnchantment(Enchantment.getById(Integer.valueOf(o.toString()))); } p.saveData(); return true; } catch (NullPointerException e) { return false; } }
/** * Restores items from inventory string to specified player. Inventory string will be deleted. * * @param player player that will receive items */ public void restoreInventory(Player player) { if (inventory == null) return; String[] inv = inventory.split(";"); for (String i : inv) { if (i == null || i.trim().equals("")) return; String[] items = i.split(","); ItemStack item = new ItemStack(Integer.parseInt(items[0]), Integer.parseInt(items[1])); item.setDurability(Short.parseShort(items[2])); if (items.length > 3 && items[3].contains(":")) { String[] enchantments = items[3].split("\\*"); for (String e : enchantments) { item.addUnsafeEnchantment( Enchantment.getById(Integer.parseInt(e.split(":")[0])), Integer.parseInt(e.split(":")[1])); } } if (player.getInventory().firstEmpty() == -1) player.getWorld().dropItem(player.getLocation(), item); else player.getInventory().addItem(item); } inventory = ""; InputOutput.UpdatePrisoner(this); }
@SuppressWarnings({"deprecation"}) @Override public MaterialAndData getBlock(Vector v) { int x = v.getBlockX() + center.getBlockX(); int y = v.getBlockY() + center.getBlockY(); int z = v.getBlockZ() + center.getBlockZ(); try { if (x < 0 || x >= size.getBlockZ() || y < 0 || y >= size.getBlockY() || z < 0 || z >= size.getBlockZ()) { return null; } com.sk89q.worldedit.Vector vector = new com.sk89q.worldedit.Vector(x, y, z); BaseBlock baseBlock = weSchematic.getBlock(vector); Material material = Material.getMaterial(baseBlock.getId()); int materialData = baseBlock.getData(); MaterialAndData blockData = new MaterialAndData(material, (byte) materialData); // Note.. we don't actually get a SignBlock here, for some reason. // May have something to do with loading schematics not actually supporting sign // text, it doesn't work with //schematic and //paste, either. // It looks like //paste works in a dev build of WE, but it still doesn't give me the blocks // Looking at WE's code, it seems like the part that's needed is commented out... ?? if (material == Material.SIGN_POST || material == Material.WALL_SIGN) { try { if (baseBlock.hasNbtData()) { SignBlock signBlock = new SignBlock(material.getId(), materialData); CompoundTag nbtData = baseBlock.getNbtData(); signBlock.setNbtData(nbtData); blockData.setSignLines(signBlock.getText()); } } catch (Throwable ex) { ex.printStackTrace(); } } else if (material == Material.COMMAND) { try { if (baseBlock.hasNbtData()) { CompoundTag nbtRoot = baseBlock.getNbtData(); Map<String, Tag> rootValues = nbtRoot.getValue(); if (rootValues.containsKey("Command")) { Object commandValue = rootValues.get("Command").getValue(); blockData.setCommandLine((String) commandValue); } if (rootValues.containsKey("CustomName")) { Object nameValue = rootValues.get("CustomName").getValue(); blockData.setCustomName((String) nameValue); } } } catch (Throwable ex) { ex.printStackTrace(); } } else if (material == Material.CHEST) { try { if (baseBlock.hasNbtData()) { ChestBlock chestBlock = new ChestBlock(materialData); CompoundTag nbtRoot = baseBlock.getNbtData(); chestBlock.setNbtData(nbtRoot); BaseItemStack[] items = chestBlock.getItems(); if (items != null && items.length > 0) { ItemStack[] contents = new ItemStack[items.length]; for (int i = 0; i < items.length; i++) { if (items[i] != null) { Material itemMaterial = Material.getMaterial(items[i].getType()); // Bukkit.getLogger().info("Item from chest: " + itemMaterial + " at " + i + " / " // + contents.length); short itemData = items[i].getData(); int itemAmount = items[i].getAmount(); ItemStack newStack = new ItemStack(itemMaterial, itemAmount, itemData); Map<Integer, Integer> enchantments = items[i].getEnchantments(); if (enchantments != null && enchantments.size() > 0) { for (Entry<Integer, Integer> enchantment : enchantments.entrySet()) { try { Enchantment enchantmentType = Enchantment.getById(enchantment.getKey()); newStack.addEnchantment(enchantmentType, enchantment.getValue()); } catch (Exception ex) { // This seems to happen a lot .. like on potions especially. ex.printStackTrace(); } } } contents[i] = newStack; } } blockData.setInventoryContents(contents); } } } catch (Throwable ex) { ex.printStackTrace(); } } return blockData; } catch (ArrayIndexOutOfBoundsException ex) { // TODO: Figure out why this still happens, even with the size check } catch (Throwable ex) { ex.printStackTrace(); } return null; }
/** * Loads an item from a configuration map. * * @param map The map, containing information on the item. * @return An itemstack created from the config */ @SuppressWarnings("unchecked") public static ItemStack loadItem(Map<?, ?> map) { Material itemMaterial = Material.AIR; int itemCount = 1; short itemDamage = 0; String name = ""; if (map.containsKey("name")) { try { name = (String) map.get("name"); } catch (NullPointerException err) { System.out.println(ChatColor.RED + "Error: Bad name in outfit item definition."); } } // Load material if (map.containsKey("material")) { try { itemMaterial = Material.valueOf(((String) map.get("material")).trim().toUpperCase()); } catch (IllegalArgumentException err) { System.out.println(ChatColor.RED + "Error: Bad material in outfit item definition."); } } // Load itemcount if (map.containsKey("count")) { try { itemCount = (Integer) map.get("count"); } catch (NumberFormatException err) { System.out.println(ChatColor.RED + "Error: Bad item count in outfit item definition."); } } if (map.containsKey("damage")) { try { itemDamage = ((Integer) map.get("damage")).shortValue(); } catch (NumberFormatException err) { System.out.println(ChatColor.RED + "Error: Bad item damage in outfit item definition."); } } if (itemMaterial != Material.AIR) { ItemStack itemStack = new ItemStack(itemMaterial, itemCount, itemDamage); if (!name.equals("")) { ItemMeta meta = itemStack.getItemMeta(); meta.setDisplayName(name); itemStack.setItemMeta(meta); } if (map.containsKey("color")) { int[] rgb = ConfigLoader.getCoords((String) map.get("color")); if (Outfit.isItemLeatherArmor(itemStack)) { try { LeatherArmorMeta meta = (LeatherArmorMeta) itemStack.getItemMeta(); meta.setColor(Color.fromRGB(rgb[0], rgb[1], rgb[2])); itemStack.setItemMeta(meta); } catch (IllegalArgumentException err) { System.out.println(ChatColor.RED + "Error: Bad item color in outfit item definition."); } } } // Enchant item if (map.containsKey("enchants")) { try { List<String> enchants = (List<String>) map.get("enchants"); for (String enchant : enchants) { String[] enchantSplit = enchant.split(", "); int enchantId = Integer.parseInt(enchantSplit[0]); int enchantLevel = 1; if (enchantSplit.length >= 2) { Integer.parseInt(enchantSplit[1]); } itemStack.addEnchantment(Enchantment.getById(enchantId), enchantLevel); } } catch (Exception err) { } } return itemStack; } return null; }
public static void giveKit(Player p) { p.getInventory().clear(); p.getInventory().setHelmet(null); p.getInventory().setChestplate(null); p.getInventory().setLeggings(null); p.getInventory().setBoots(null); if (!KIT.containsKey(p)) { if (plugin.COMPASS.booleanValue()) { p.getInventory().addItem(new ItemStack[] {new ItemStack(Material.COMPASS, 1)}); } if (plugin.DEFAULT_KIT) { ConfigurationSection def = BGFiles.kitconf.getConfigurationSection("default"); setKitDisplayName(p, "Default"); List<String> kititems = def.getStringList("ITEMS"); for (String item : kititems) { String[] oneitem = item.split(","); ItemStack i = null; Integer id = null; Integer amount = null; Short durability = null; if (oneitem[0].contains(":")) { String[] ITEM_ID = oneitem[0].split(":"); id = Integer.valueOf(Integer.parseInt(ITEM_ID[0])); amount = Integer.valueOf(Integer.parseInt(oneitem[1])); durability = Short.valueOf(Short.parseShort(ITEM_ID[1])); i = new ItemStack(id.intValue(), amount.intValue(), durability.shortValue()); } else { id = Integer.valueOf(Integer.parseInt(oneitem[0])); amount = Integer.valueOf(Integer.parseInt(oneitem[1])); i = new ItemStack(id.intValue(), amount.intValue()); } if (oneitem.length == 4) { i.addUnsafeEnchantment( Enchantment.getById(Integer.parseInt(oneitem[2])), Integer.parseInt(oneitem[3])); } if ((id.intValue() < 298) || (317 < id.intValue())) { p.getInventory().addItem(new ItemStack[] {i}); } else if ((id.intValue() == 298) || (id.intValue() == 302) || (id.intValue() == 306) || (id.intValue() == 310) || (id.intValue() == 314)) { i.setAmount(1); p.getInventory().setHelmet(i); } else if ((id.intValue() == 299) || (id.intValue() == 303) || (id.intValue() == 307) || (id.intValue() == 311) || (id.intValue() == 315)) { i.setAmount(1); p.getInventory().setChestplate(i); } else if ((id.intValue() == 300) || (id.intValue() == 304) || (id.intValue() == 308) || (id.intValue() == 312) || (id.intValue() == 316)) { i.setAmount(1); p.getInventory().setLeggings(i); } else if ((id.intValue() == 301) || (id.intValue() == 305) || (id.intValue() == 309) || (id.intValue() == 313) || (id.intValue() == 317)) { i.setAmount(1); p.getInventory().setBoots(i); } } List<String> pots = def.getStringList("POTION"); for (String pot : pots) { if (pot != null & pot != "") { if (!pot.equals(0)) { String[] potion = pot.split(","); if (Integer.parseInt(potion[0]) != 0) { if (Integer.parseInt(potion[1]) == 0) { p.addPotionEffect( new PotionEffect( PotionEffectType.getById(Integer.parseInt(potion[0])), plugin.MAX_GAME_RUNNING_TIME * 1200, Integer.parseInt(potion[2]))); } else { p.addPotionEffect( new PotionEffect( PotionEffectType.getById(Integer.parseInt(potion[0])), Integer.parseInt(potion[1]) * 20, Integer.parseInt(potion[2]))); } } } } } } return; } String kitname = (String) KIT.get(p); ConfigurationSection kit = BGFiles.kitconf.getConfigurationSection(kitname.toLowerCase()); List<String> kititems = kit.getStringList("ITEMS"); for (String item : kititems) { String[] oneitem = item.split(","); ItemStack i = null; Integer id = null; Integer amount = null; Short durability = null; if (oneitem[0].contains(":")) { String[] ITEM_ID = oneitem[0].split(":"); id = Integer.valueOf(Integer.parseInt(ITEM_ID[0])); amount = Integer.valueOf(Integer.parseInt(oneitem[1])); durability = Short.valueOf(Short.parseShort(ITEM_ID[1])); i = new ItemStack(id.intValue(), amount.intValue(), durability.shortValue()); } else { id = Integer.valueOf(Integer.parseInt(oneitem[0])); amount = Integer.valueOf(Integer.parseInt(oneitem[1])); i = new ItemStack(id.intValue(), amount.intValue()); } if (oneitem.length == 4) { i.addUnsafeEnchantment( Enchantment.getById(Integer.parseInt(oneitem[2])), Integer.parseInt(oneitem[3])); } if ((id.intValue() < 298) || (317 < id.intValue())) { p.getInventory().addItem(new ItemStack[] {i}); } else if ((id.intValue() == 298) || (id.intValue() == 302) || (id.intValue() == 306) || (id.intValue() == 310) || (id.intValue() == 314)) { i.setAmount(1); p.getInventory().setHelmet(i); } else if ((id.intValue() == 299) || (id.intValue() == 303) || (id.intValue() == 307) || (id.intValue() == 311) || (id.intValue() == 315)) { i.setAmount(1); p.getInventory().setChestplate(i); } else if ((id.intValue() == 300) || (id.intValue() == 304) || (id.intValue() == 308) || (id.intValue() == 312) || (id.intValue() == 316)) { i.setAmount(1); p.getInventory().setLeggings(i); } else if ((id.intValue() == 301) || (id.intValue() == 305) || (id.intValue() == 309) || (id.intValue() == 313) || (id.intValue() == 317)) { i.setAmount(1); p.getInventory().setBoots(i); } } List<String> pots = kit.getStringList("POTION"); for (String pot : pots) { if (pot != null & pot != "") { if (!pot.equals(0)) { String[] potion = pot.split(","); if (Integer.parseInt(potion[0]) != 0) { if (Integer.parseInt(potion[1]) == 0) { p.addPotionEffect( new PotionEffect( PotionEffectType.getById(Integer.parseInt(potion[0])), plugin.MAX_GAME_RUNNING_TIME * 1200, Integer.parseInt(potion[2]))); } else { p.addPotionEffect( new PotionEffect( PotionEffectType.getById(Integer.parseInt(potion[0])), Integer.parseInt(potion[1]) * 20, Integer.parseInt(potion[2]))); } } } } } if (plugin.COMPASS.booleanValue()) p.getInventory().addItem(new ItemStack[] {new ItemStack(Material.COMPASS, 1)}); }