@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH) public void onPlayerInteract(PlayerInteractEvent event) { if (event.getAction() == Action.LEFT_CLICK_BLOCK && event.hasItem()) { // The player didn't click an enchantment table, Creative players will instantly destroy. Player player = event.getPlayer(); if (event.getClickedBlock().getType() == Material.ENCHANTMENT_TABLE && player.getGameMode() != GameMode.CREATIVE) { // The player didn't click with an enchanted book. ItemStack stack = event.getItem(); if (stack != null && stack.getType() == Material.ENCHANTED_BOOK) { ItemMeta meta = stack.getItemMeta(); if (meta instanceof EnchantmentStorageMeta) { EnchantmentStorageMeta enchantmentStorageMeta = (EnchantmentStorageMeta) meta; for (Enchantment enchantment : enchantmentStorageMeta.getStoredEnchants().keySet()) { enchantmentStorageMeta.removeStoredEnchant(enchantment); } event.setCancelled(true); player.setItemInHand(EMPTY_BOOK); player.sendMessage(ChatColor.GREEN + "You reverted this item to its original form."); } } } } }
public static ItemStack getRandomEnchant(List<Enchantment>enchants, int amount){ ItemStack result = new ItemStack(Material.ENCHANTED_BOOK); List<Enchantment>left = new ArrayList<Enchantment>(enchants); EnchantmentStorageMeta meta = (EnchantmentStorageMeta) result.getItemMeta(); if(amount > left.size()) amount = left.size(); for(int i = 0 ; i < amount && !left.isEmpty() ; i++){ Enchantment e = VanillaPlusCore.getRandom(left); left.remove(e); int level = VanillaPlusCore.getRandom().nextInt(100); level = level % (e.getMaxLevel()+1-e.getStartLevel())+e.getStartLevel(); meta.addStoredEnchant(e, level, false); } result.setItemMeta(meta); return result; }
@Override @Nullable protected ItemStack[] get(Event event) { ItemStack input = book.getSingle(event); EnchantmentStorageMeta meta = (EnchantmentStorageMeta) input.getItemMeta(); final EnchantmentType[] enchs = enchants.getArray(event); for (final EnchantmentType ench : enchs) { if (meta.hasStoredEnchant(ench.getType())) { meta.removeEnchant(ench.getType()); } meta.addStoredEnchant(ench.getType(), ench.getLevel(), true); } ItemStack result = book.getSingle(event); result.setItemMeta(meta); return new ItemStack[]{result}; }
public static ItemMeta parse(Element xml, ItemMeta source) { if (source instanceof BannerMeta) { return parseBanner(xml, (BannerMeta) source); } else if (source instanceof BookMeta) { return parseBook(xml, (BookMeta) source); } else if (source instanceof EnchantmentStorageMeta) { return parseEnchantmentStorage(xml, (EnchantmentStorageMeta) source); } else if (source instanceof FireworkMeta) { return parseFirework(xml, (FireworkMeta) source); } else if (source instanceof FireworkEffectMeta) { return parseFireworkEffect(xml, (FireworkEffectMeta) source); } else if (source instanceof LeatherArmorMeta) { return parseLeatherArmor(xml, (LeatherArmorMeta) source); } else if (source instanceof MapMeta) { return parseMap(xml, (MapMeta) source); } else if (source instanceof PotionMeta) { return parsePotion(xml, (PotionMeta) source); } else if (source instanceof SkullMeta) { return parseSkull(xml, (SkullMeta) source); } else if (source instanceof SpawnEggMeta) { return parseSpawnEgg(xml, (SpawnEggMeta) source); } return source; }
public static EnchantmentStorageMeta parseEnchantmentStorage(Element xml, EnchantmentStorageMeta source) { Element book = xml.getChild("enchanted-book"); if (book != null) { for (Element enchantment : book.getChildren("enchantment")) { Enchantment type = Enchantment.getByName(XMLParser.parseEnumValue(enchantment.getTextNormalize())); int level = 1; try { Attribute levelAttribute = enchantment.getAttribute("level"); if (levelAttribute != null) { level = levelAttribute.getIntValue(); } } catch (DataConversionException ignored) { } source.addStoredEnchant(type, level, false); } } return source; }
private void addMcEnchant(final EnchantmentStorageMeta meta, final by enchNbt) { try { // Here we are not using (dh.e(String)) because it does not fail correctly: // it returns 0 instead of throwing an exception. // Instead, we use (dw.e()). This way either the cast will fail or a NPE will be thrown. final short enchId = ((cj) enchNbt.a("r1_6_4/net/minecraft/server/id")).a; final short enchLvl = ((cj) enchNbt.a("lvl")).a; @SuppressWarnings("deprecation") final Enchantment ench = Enchantment.getById(enchId); if (ench == null) { Log.warn("Unknown Enchantment ID (" + enchId + "), ignored."); } else { meta.addStoredEnchant(ench, enchLvl, false); } } catch (final RuntimeException e) { if (e instanceof ClassCastException || e instanceof NullPointerException) { Log.error("Failed to add Enchantment to Enchanted Book, ignored.\nThe NBT was: " + enchNbt, e); } else { throw e; } } }
/** * Add an enchantment to an {@link ItemStack}. * * @param stack The item stack. * @param enchant The enchantment to add. * @param level The enchantment level. */ public static void addEnchantment(ItemStack stack, Enchantment enchant, int level) { PreCon.notNull(stack); PreCon.notNull(enchant); PreCon.positiveNumber(level); ItemMeta meta = stack.getItemMeta(); // check for enchantment storage items such as enchanted books if (meta instanceof EnchantmentStorageMeta) { EnchantmentStorageMeta storage = (EnchantmentStorageMeta)meta; storage.addStoredEnchant(enchant, level, true); stack.setItemMeta(storage); } else { stack.addUnsafeEnchantment(enchant, level); } }
/** * Gets a String representing all special meta of this ItemStack, if any. * * @param is the ItemStack * @param separators the separators * * @return a String representing this ItemStack's special meta or an empty String * * @throws InventoryUtilException if something goes wrong */ public static String getSpecialMetaString(final ItemStack is, final String[] separators) throws InventoryUtilException { final ItemMeta meta = is.getItemMeta(); if (meta instanceof BookMeta) { return getBookMetaString((BookMeta)meta); } else if (meta instanceof EnchantmentStorageMeta) { return getEnchantmentStorageMetaString((EnchantmentStorageMeta)meta, separators); } else if (meta instanceof FireworkEffectMeta) { return getFireworkEffectMetaString((FireworkEffectMeta)meta); } else if (meta instanceof FireworkMeta) { return getFireworkMetaString((FireworkMeta)meta, separators); } else if (meta instanceof LeatherArmorMeta) { return getLeatherArmorMetaString((LeatherArmorMeta)meta); } else if (meta instanceof MapMeta) { return getMapMetaString((MapMeta)meta); } else if (meta instanceof PotionMeta) { return getPotionMetaString((PotionMeta)meta, separators); } else if (meta instanceof SkullMeta) { return getSkullMetaString((SkullMeta)meta); } else { throw new InventoryUtilException("Unknown Meta type '" + meta.getClass().getName() + "', please report this to the author (Ribesg)!"); } }
private static String getEnchantmentStorageMetaString(final EnchantmentStorageMeta meta, final String[] separators) { if (meta.getStoredEnchants().isEmpty()) { return ""; } else { final StringBuilder enchantmentsStringBuilder = new StringBuilder(); final Map<Enchantment, Integer> sortedEnchantmentMap = new TreeMap<>(EnchantmentUtil.ENCHANTMENT_COMPARATOR); sortedEnchantmentMap.putAll(meta.getStoredEnchants()); for (final Map.Entry<Enchantment, Integer> e : sortedEnchantmentMap.entrySet()) { enchantmentsStringBuilder.append(e.getKey().getName()); enchantmentsStringBuilder.append(separators[2]); enchantmentsStringBuilder.append(e.getValue()); enchantmentsStringBuilder.append(separators[1]); } return enchantmentsStringBuilder.substring(0, enchantmentsStringBuilder.length() - separators[1].length()); } }
@Override public void processMeta(Player player, ItemMeta m) { super.processMeta(player, m); EnchantmentStorageMeta meta = (EnchantmentStorageMeta) m; for(Map.Entry<Enchantment, PlaceholderValue<Integer>> ench : storedEnchantments.entrySet()) meta.addStoredEnchant(ench.getKey(), ench.getValue().resolve(player), true); }
public EnchantedBookMaker setLores(String... lores) { EnchantmentStorageMeta itemMeta = (EnchantmentStorageMeta)this.itemStack.getItemMeta(); List<String> loresList = new ArrayList<>(); for (String lore : lores) { loresList.add(lore); } itemMeta.setLore(loresList); this.itemStack.setItemMeta(itemMeta); return this; }
private void addEnchantments(String enchantments, String level, ItemStack is) { try { if (enchantments == null || level == null) return; ArrayList<String> enchants = new ArrayList<String>(Arrays.asList(enchantments.split("!"))); ArrayList<String> levels = new ArrayList<String>(Arrays.asList(level.split("!"))); if (enchantments.isEmpty()) return; for (int i = 0; i < enchants.size(); i++) { if (!enchants.get(i).equalsIgnoreCase(" ") && !enchants.get(i).equalsIgnoreCase("")) { if (is.getType() == Material.ENCHANTED_BOOK) { EnchantmentStorageMeta meta = (EnchantmentStorageMeta) is.getItemMeta(); meta.addStoredEnchant(Enchantment.getByName(enchants.get(i)), Integer.parseInt(levels.get(i)), true); is.setItemMeta(meta); } else is.addEnchantment(Enchantment.getByName(enchants.get(i)), Integer.parseInt(levels.get(i))); } } enchants.clear(); levels.clear(); } catch(Exception e) { if(e instanceof java.lang.IllegalArgumentException) { Logger.ERROR.log("couldn't add enchantment " + enchantments + " level " + level + " to " + is.getType().toString()); Logger.ERROR.log("Cause: java.lang.IllegalArgumentException"); } else { e.printStackTrace(); } } }
@Override public void setTo(ItemMeta meta) { if(meta instanceof EnchantmentStorageMeta){ EnchantmentStorageMeta enc = (EnchantmentStorageMeta) meta; for(Enchants a : array) { Enchantment ment = Enchantment.getById(a.id); if(ment != null) enc.addStoredEnchant(ment,a.level,true); } } }
@Override public SubMeta setFrom(ItemMeta meta) { if(meta instanceof EnchantmentStorageMeta){ EnchantmentStorageMeta enc = (EnchantmentStorageMeta) meta; for(Enchantment a : enc.getStoredEnchants().keySet()) array.add(new Enchants(a,enc.getStoredEnchants().get(a))); } return this; }
@Override @Nullable protected Integer[] get(Event event) { ItemStack input = book.getSingle(event); EnchantmentStorageMeta meta = (EnchantmentStorageMeta) input.getItemMeta(); return new Integer[]{meta.getStoredEnchantLevel(enchant.getSingle(event).getType())}; }
public void change(Event event, Object[] delta, Changer.ChangeMode mode){ EnchantmentStorageMeta meta = (EnchantmentStorageMeta) book.getSingle(event).getItemMeta(); Enchantment ench = enchant.getSingle(event).getType(); Integer level = meta.getStoredEnchantLevel(ench); Logging.info("Initial level: " + level); if (meta.hasStoredEnchant(ench)) meta.removeStoredEnchant(ench); if (mode == ChangeMode.SET) level = ((Number) delta[0]).intValue(); if (mode == ChangeMode.ADD) level += ((Number) delta[0]).intValue(); if (mode == ChangeMode.REMOVE) level -= ((Number) delta[0]).intValue(); Logging.info("New level: " + level); if (level > 0) meta.addStoredEnchant(ench, level, true); book.getSingle(event).setItemMeta(meta); }
@Override @Nullable protected EnchantmentType[] get(Event event) { ItemStack input = book.getSingle(event); EnchantmentStorageMeta meta = (EnchantmentStorageMeta) input.getItemMeta(); Map<Enchantment, Integer> map = meta.getStoredEnchants(); final List<EnchantmentType> l = new ArrayList<EnchantmentType>(); for (Map.Entry<Enchantment, Integer> ansh : map.entrySet()) { l.add(new EnchantmentType(ansh.getKey(), ansh.getValue())); } return l.toArray((EnchantmentType[])new EnchantmentType[l.size()]); }
public Iterator<EnchantmentType> iterator(Event event) { ItemStack input = book.getSingle(event); EnchantmentStorageMeta meta = (EnchantmentStorageMeta) input.getItemMeta(); Map<Enchantment, Integer> map = meta.getStoredEnchants(); final List<EnchantmentType> l = new ArrayList<EnchantmentType>(); for (Map.Entry<Enchantment, Integer> ansh : map.entrySet()) { l.add(new EnchantmentType(ansh.getKey(), ansh.getValue())); } return l.iterator(); }
@Override public void a(final int index, final ye yeArg) { /* * (ye.d) is the obfuscated field name of ItemStack.id * (ye.b) is the obfuscated field name of ItemStack.stackSize * (ye.k()) is the obfuscated method name of ItemStack.getItemDamage() */ @SuppressWarnings("deprecation") final ItemStack item = new ItemStack( yeArg.d, yeArg.b, (short) yeArg.k() ); /* * (by) is the obfuscated class name of NBTTagCompound * (ye.d) is the obfuscated field name of ItemStack.stackTagCompound * (cg) is the obfuscated class name of NBTTagList * (by.a(String)) is the obfuscated method name of NBTTagCompound.getTag(String) * (cg.c()) is the obfuscated method name of NBTTagList.tagCount() * (cg.b(int)) is the obfuscated method name of NBTTagList.getCompoundTagAt(int) */ // Enchanted Books can be generated, let's handle that. if (item.getType() == Material.ENCHANTED_BOOK) { final by itemNbt = yeArg.e; if (itemNbt != null) { final cg storedEnchNbt = (cg) itemNbt.a("StoredEnchantments"); if (storedEnchNbt != null && storedEnchNbt.c() > 0) { final EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta(); for (int i = 0; i < storedEnchNbt.c(); i++) { this.addMcEnchant(meta, (by) storedEnchNbt.b(i)); } item.setItemMeta(meta); } } } this.inv.setItem(index, item); }
@Test public void testConflictingStoredEnchantment() { EnchantmentStorageMeta itemMeta = (EnchantmentStorageMeta) Bukkit.getItemFactory().getItemMeta(Material.ENCHANTED_BOOK); assertThat(itemMeta.hasConflictingStoredEnchant(Enchantment.DURABILITY), is(false)); itemMeta.addStoredEnchant(Enchantment.SILK_TOUCH, 1, false); assertThat(itemMeta.hasConflictingStoredEnchant(Enchantment.DURABILITY), is(false)); assertThat(itemMeta.hasConflictingStoredEnchant(Enchantment.LOOT_BONUS_BLOCKS), is(true)); assertThat(itemMeta.hasConflictingStoredEnchant(null), is(false)); }
@Test public void testConflictingStoredEnchantments() { EnchantmentStorageMeta itemMeta = (EnchantmentStorageMeta) Bukkit.getItemFactory().getItemMeta(Material.ENCHANTED_BOOK); itemMeta.addStoredEnchant(Enchantment.DURABILITY, 6, true); itemMeta.addStoredEnchant(Enchantment.DIG_SPEED, 6, true); assertThat(itemMeta.hasConflictingStoredEnchant(Enchantment.LOOT_BONUS_BLOCKS), is(false)); itemMeta.addStoredEnchant(Enchantment.SILK_TOUCH, 1, false); assertThat(itemMeta.hasConflictingStoredEnchant(Enchantment.LOOT_BONUS_BLOCKS), is(true)); assertThat(itemMeta.hasConflictingStoredEnchant(null), is(false)); }
/** * Get EnchantmentStorageMeta from a JSON string * * @param json The string to use * @return The EnchantmentStorageMeta constructed, null if an error occurred */ public static EnchantmentStorageMeta getEnchantedBookMeta(String json) { try { return getEnchantedBookMeta(new JSONObject(json)); } catch (JSONException e) { e.printStackTrace(); return null; } }
/** * Serialize EnchantmentStorageMeta into JSONObject form. * * @param meta The EnchantmentStorageMeta to serialize * @return The JSONObject form of the given EnchantmentStorageMeta */ public static JSONObject serializeEnchantedBookMeta(EnchantmentStorageMeta meta) { try { JSONObject root = new JSONObject(); String enchants = EnchantmentSerialization.serializeEnchantments(meta.getStoredEnchants()); root.put("enchantments", enchants); return root; } catch (JSONException e) { e.printStackTrace(); return null; } }
/** * Serialize EnchantmentStorageMeta into string form. * * @param meta The EnchantmentStorageMet to use * @param pretty Whether the resulting string should be 'pretty' or not * @param indentFactor The amount of spaces to use for a tab * @return The serialized string */ public static String serializeEnchantedBookMetaAsString(EnchantmentStorageMeta meta, boolean pretty, int indentFactor) { try { if (pretty) { return serializeEnchantedBookMeta(meta).toString(indentFactor); } else { return serializeEnchantedBookMeta(meta).toString(); } } catch (JSONException e) { e.printStackTrace(); return null; } }
public static Map<Enchantment, Integer> getEnchantments(ItemStack itemStack) { if (itemStack.getItemMeta() instanceof EnchantmentStorageMeta) { EnchantmentStorageMeta esm = (EnchantmentStorageMeta) itemStack.getItemMeta(); return esm.getStoredEnchants(); } else { return itemStack.getEnchantments(); } }
@SuppressWarnings("deprecation") private void setTag(ItemStack is, ItemTag itemtag) { List<Ench> enchants = itemtag.getEnchants(); //Integer repaircost = itemtag.getRepairCost(); List<String> pages = itemtag.getPages(); String author = itemtag.getAuthor(); String title = itemtag.getTitle(); Display display = itemtag.getDisplay(); ItemMeta itemmeta = is.getItemMeta(); if (display != null) { List<String> lores = display.getLore(); String name = display.getName(); itemmeta.setLore(lores); itemmeta.setDisplayName(name); } if (itemmeta instanceof BookMeta) { BookMeta bookmeta = (BookMeta) itemmeta; bookmeta.setAuthor(author); bookmeta.setTitle(title); bookmeta.setPages(pages); } if (itemmeta instanceof EnchantmentStorageMeta) { EnchantmentStorageMeta enchantmentstoragemeta = (EnchantmentStorageMeta) itemmeta; for (Ench enchant : enchants) { enchantmentstoragemeta.addEnchant(Enchantment.getById(enchant.getId()), enchant.getLvl(), true); } } is.setItemMeta(itemmeta); }
@SuppressWarnings("deprecation") public CardboardMetaEnchantment(ItemStack item) { this.id = item.getTypeId(); EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta(); for (Enchantment e : meta.getStoredEnchants().keySet()) { this.enchantments.put(new CardboardEnchantment(e), meta.getStoredEnchants().get(e)); } }
@SuppressWarnings("deprecation") public ItemMeta unbox() { ItemFactory factory = Bukkit.getServer().getItemFactory(); EnchantmentStorageMeta meta = (EnchantmentStorageMeta) factory.getItemMeta(Material.getMaterial(this.id)); for (CardboardEnchantment e : this.enchantments.keySet()) { meta.addStoredEnchant(e.unbox(), this.enchantments.get(e), true); } return meta; }
public CardboardMetaEnchantment(ItemStack item) { this.id = item.getTypeId(); EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta(); for (Enchantment e : meta.getStoredEnchants().keySet()) { this.enchantments.put(new CardboardEnchantment(e), meta.getStoredEnchants().get(e)); } }