/** * Adds the specified enchantments to this item stack in an unsafe manner. * <p> * This method is the same as calling {@link * #addUnsafeEnchantment(org.bukkit.enchantments.Enchantment, int)} for * each element of the map. * * @param enchantments Enchantments to add */ @Utility public void addUnsafeEnchantments(Map<Enchantment, Integer> enchantments) { for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) { addUnsafeEnchantment(entry.getKey(), entry.getValue()); } }
result.addUnsafeEnchantment(enchantment, (Integer) entry.getValue());
/** * Adds the specified {@link Enchantment} to this item stack. * <p> * If this item stack already contained the given enchantment (at any * level), it will be replaced. * * @param ench Enchantment to add * @param level Level of the enchantment * @throws IllegalArgumentException if enchantment null, or enchantment is * not applicable */ @Utility public void addEnchantment(Enchantment ench, int level) { Validate.notNull(ench, "Enchantment cannot be null"); if ((level < ench.getStartLevel()) || (level > ench.getMaxLevel())) { throw new IllegalArgumentException("Enchantment level is either too low or too high (given " + level + ", bounds are " + ench.getStartLevel() + " to " + ench.getMaxLevel() + ")"); } else if (!ench.canEnchantItem(this)) { throw new IllegalArgumentException("Specified enchantment cannot be applied to this itemstack"); } addUnsafeEnchantment(ench, level); }
item.addUnsafeEnchantment(enchantment, level);
.forEach(player -> { ItemStack itemInHand = player.getItemInHand(); itemInHand.addUnsafeEnchantment(enchantment, level); player.setItemInHand(itemInHand); successMessage.send(sender);
item.setItemMeta(meta); } else { item.addUnsafeEnchantment(enchantment.getKey(), enchantment.getValue());
/** * Returns the item that will result when this anvil is applied to the currently loaded items, * combining them, provided that two items which can be combined are loaded. A return of null * doesn't imply that the anvil cannot be activated, since it may still be able to repair/name a * single item. * * @return the resulting item, or null if two items that can be combined are not loaded */ public ItemStack getForged() { if (InventoryUtil.isEmpty(getFirstItem()) || InventoryUtil.isEmpty(getSecondItem())) { return null; } if (getSecondItem().getType() == Material.ENCHANTED_BOOK) { EnchantmentStorageMeta book = (EnchantmentStorageMeta) getSecondItem().getItemMeta(); ItemStack result; if (InventoryUtil.isEmpty(getResultItem())) { result = getFirstItem().clone(); } else { result = getResultItem(); } book.getStoredEnchants().forEach((enchantment, level) -> { if (enchantment.canEnchantItem(result) || result.getType() == Material.ENCHANTED_BOOK) { result.addUnsafeEnchantment(enchantment, level); } }); return result; } return null; }
@Override public void addUnsafeEnchantment(MCEnchantment e, int level) { if(is == null) { return; } is.addUnsafeEnchantment(((BukkitMCEnchantment) e).__Enchantment(), level); }
public ItemStackBuilder enchant(Enchantment enchantment) { return transform(itemStack -> itemStack.addUnsafeEnchantment(enchantment, 1)); }
public ItemStackBuilder enchant(Enchantment enchantment, int level) { return transform(itemStack -> itemStack.addUnsafeEnchantment(enchantment, level)); }
public ItemStackBuilder enchant(Enchantment enchantment) { return transform(itemStack -> itemStack.addUnsafeEnchantment(enchantment, 1)); }
public ItemStackBuilder enchant(Enchantment enchantment, int level) { return transform(itemStack -> itemStack.addUnsafeEnchantment(enchantment, level)); }
/** * Adds the specified enchantments to this item stack in an unsafe manner. * <p> * This method is the same as calling {@link * #addUnsafeEnchantment(org.bukkit.enchantments.Enchantment, int)} for * each element of the map. * * @param enchantments Enchantments to add */ @Utility public void addUnsafeEnchantments(Map<Enchantment, Integer> enchantments) { for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) { addUnsafeEnchantment(entry.getKey(), entry.getValue()); } }
/** * Returns a real itemstack * * @return */ public ItemStack toItemStack() { ItemStack is = new ItemStack(getTypeId(), getAmount(), getDurability(), getData()); for (Entry<Enchantment, Integer> ench : enchantments.entrySet()) { is.addUnsafeEnchantment(ench.getKey(), Math.min(ench.getValue(), ench.getKey().getMaxLevel())); } return is; }
public static void applyEnchantments(ItemStack item, ConfigurationSection enchantConfig) { if (item == null || enchantConfig == null) return; Collection<String> enchantKeys = enchantConfig.getKeys(false); for (String enchantKey : enchantKeys) { try { Enchantment enchantment = Enchantment.getByName(enchantKey.toUpperCase()); item.addUnsafeEnchantment(enchantment, enchantConfig.getInt(enchantKey)); } catch (Exception ex) { Bukkit.getLogger().warning("Invalid enchantment: " + enchantKey); } } }
private static void addEnchantment(ItemStack stack, String ench) { String[] parts = ench.split(":"); if (parts.length != 2) { return; } Enchantment enchantment = getEnchantment(parts[0]); if (enchantment == null) { return; } int lvl = Integer.parseInt(parts[1]); if (stack.getType() == Material.ENCHANTED_BOOK) { EnchantmentStorageMeta esm = (EnchantmentStorageMeta) stack.getItemMeta(); esm.addStoredEnchant(enchantment, lvl, true); stack.setItemMeta(esm); } else { stack.addUnsafeEnchantment(enchantment, lvl); } }
@SuppressWarnings("deprecation") private void parseEnchants() { if (this.isMetarizable()) { Enchantment en = null; int level = 0; ConfigurationSection newSection = (ConfigurationSection) (this.configSection); ConfigurationSection enchantSection = (ConfigurationSection) newSection.get("enchants"); for (String key : enchantSection.getKeys(false)) { if (Utils.isNumber(key)) { en = Enchantment.getById(Integer.parseInt(key)); level = Integer.parseInt(enchantSection.get(key).toString()); } else { en = Enchantment.getByName(key.toUpperCase()); level = Integer.parseInt(enchantSection.get(key).toString()); } if (en == null) { continue; } this.finalStack.addUnsafeEnchantment(en, level); } } }
@SuppressWarnings("deprecation") private void parseEnchants() { if (this.isMetarizable()) { Enchantment en = null; int level = 0; ConfigurationSection newSection = (ConfigurationSection) (this.configSection); ConfigurationSection enchantSection = (ConfigurationSection) newSection.get("enchants"); for (String key : enchantSection.getKeys(false)) { if (Utils.isNumber(key)) { en = Enchantment.getById(Integer.parseInt(key)); level = Integer.parseInt(enchantSection.get(key).toString()); } else { en = Enchantment.getByName(key.toUpperCase()); level = Integer.parseInt(enchantSection.get(key).toString()); } if (en == null) { continue; } this.finalStack.addUnsafeEnchantment(en, level); } } }
/** * Adds the specified {@link Enchantment} to this item stack. * <p> * If this item stack already contained the given enchantment (at any * level), it will be replaced. * * @param ench Enchantment to add * @param level Level of the enchantment * @throws IllegalArgumentException if enchantment null, or enchantment is * not applicable */ @Utility public void addEnchantment(Enchantment ench, int level) { Validate.notNull(ench, "Enchantment cannot be null"); if ((level < ench.getStartLevel()) || (level > ench.getMaxLevel())) { throw new IllegalArgumentException("Enchantment level is either too low or too high (given " + level + ", bounds are " + ench.getStartLevel() + " to " + ench.getMaxLevel() + ")"); } else if (!ench.canEnchantItem(this)) { throw new IllegalArgumentException("Specified enchantment cannot be applied to this itemstack"); } addUnsafeEnchantment(ench, level); }
@EventHandler public void onEnchant(EnchantItemEvent e) { if (Talisman.checkFor(e, SlimefunItem.getByID("MAGICIAN_TALISMAN"))) { List<String> enchantments = new ArrayList<String>(); for (Enchantment en: Enchantment.values()) { for (int i = 1; i <= en.getMaxLevel(); i++) { if ((Boolean) Slimefun.getItemValue("MAGICIAN_TALISMAN", "allow-enchantments." + en.getName() + ".level." + i) && en.canEnchantItem(e.getItem())) enchantments.add(en.getName() + "-" + i); } } String enchant = enchantments.get(SlimefunStartup.randomize(enchantments.size())); e.getEnchantsToAdd().put(Enchantment.getByName(enchant.split("-")[0]), Integer.parseInt(enchant.split("-")[1])); } if (!e.getEnchantsToAdd().containsKey(Enchantment.SILK_TOUCH) && Enchantment.LOOT_BONUS_BLOCKS.canEnchantItem(e.getItem())) { if (Talisman.checkFor(e, SlimefunItem.getByID("WIZARD_TALISMAN"))) { if (e.getEnchantsToAdd().containsKey(Enchantment.LOOT_BONUS_BLOCKS)) e.getEnchantsToAdd().remove(Enchantment.LOOT_BONUS_BLOCKS); Set<Enchantment> enchantments = e.getEnchantsToAdd().keySet(); for (Enchantment en: enchantments) { if (SlimefunStartup.chance(100, 40)) e.getEnchantsToAdd().put(en, SlimefunStartup.randomize(3) + 1); } e.getItem().addUnsafeEnchantment(Enchantment.LOOT_BONUS_BLOCKS, SlimefunStartup.randomize(3) + 3); } } }