Java Code Examples for org.bukkit.inventory.ItemStack#addUnsafeEnchantment()

The following examples show how to use org.bukkit.inventory.ItemStack#addUnsafeEnchantment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScenarioManager.java    From UhcCore with GNU General Public License v3.0 6 votes vote down vote up
public Inventory getScenarioEditInventory(){

        Inventory inv = Bukkit.createInventory(null,6*ROW, Lang.SCENARIO_GLOBAL_INVENTORY_EDIT);

        // add edit item
        ItemStack back = new ItemStack(Material.ARROW);
        ItemMeta itemMeta = back.getItemMeta();
        itemMeta.setDisplayName(Lang.SCENARIO_GLOBAL_ITEM_BACK);
        back.setItemMeta(itemMeta);
        inv.setItem(5*ROW+8,back);

        for (Scenario scenario : Scenario.values()){
            if (!scenario.isCompatibleWithVersion()){
                continue;
            }

            ItemStack scenarioItem = scenario.getScenarioItem();
            if (isActivated(scenario)){
                scenarioItem.addUnsafeEnchantment(Enchantment.DURABILITY, 1);
                scenarioItem.setAmount(2);
            }
            inv.addItem(scenarioItem);
        }

        return inv;
    }
 
Example 2
Source File: ScenarioManager.java    From UhcCore with GNU General Public License v3.0 6 votes vote down vote up
public Inventory getScenarioVoteInventory(UhcPlayer uhcPlayer){
    Set<Scenario> playerVotes = uhcPlayer.getScenarioVotes();
    Set<Scenario> blacklist = GameManager.getGameManager().getConfiguration().getScenarioBlackList();
    Inventory inv = Bukkit.createInventory(null,5*ROW, Lang.SCENARIO_GLOBAL_INVENTORY_VOTE);

    for (Scenario scenario : Scenario.values()){
        // Don't add to menu when blacklisted / not compatible / already enabled.
        if (blacklist.contains(scenario) || !scenario.isCompatibleWithVersion() || isActivated(scenario)){
            continue;
        }

        ItemStack item = scenario.getScenarioItem();

        if (playerVotes.contains(scenario)) {
            item.addUnsafeEnchantment(Enchantment.DURABILITY, 1);
            item.setAmount(2);
        }
        inv.addItem(item);
    }
    return inv;
}
 
Example 3
Source File: ItemBuilder.java    From Crazy-Crates with MIT License 6 votes vote down vote up
private ItemStack addGlow(ItemStack item) {
    if (glowing) {
        try {
            if (item != null) {
                if (item.hasItemMeta()) {
                    if (item.getItemMeta().hasEnchants()) {
                        return item;
                    }
                }
                item.addUnsafeEnchantment(Enchantment.LUCK, 1);
                ItemMeta meta = item.getItemMeta();
                meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
                item.setItemMeta(meta);
            }
            return item;
        } catch (NoClassDefFoundError e) {
            return item;
        }
    }
    return item;
}
 
Example 4
Source File: EnchantManager.java    From ce with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static ItemStack addEnchant(ItemStack item, CEnchantment ce, int level) {
    ItemMeta im = item.getItemMeta();
    List<String> lore = new ArrayList<String>();
    if (im.hasLore()) {
        lore = im.getLore();
        if (maxEnchants < enchantments.size()) {
            int counter = maxEnchants;
            for (String s : lore)
                if (containsEnchantment(s)) {
                    counter--;
                    if (counter <= 0) {
                        return item;
                    }
                }
        }
    }
    if (level > ce.getEnchantmentMaxLevel())
        level = ce.getEnchantmentMaxLevel();
    lore.add(ce.getDisplayName() + " " + intToLevel(level));
    im.setLore(lore);
    item.setItemMeta(im);
    item.addUnsafeEnchantment(glowEnchantment, 0);
    return item;
}
 
Example 5
Source File: AdminResCommand.java    From civcraft with GNU General Public License v2.0 6 votes vote down vote up
public void enchant_cmd() throws CivException {
	Player player = getPlayer();
	String enchant = getNamedString(1, "Enchant name");
	int level = getNamedInteger(2);
	
	
	ItemStack stack = player.getItemInHand();
	Enchantment ench = Enchantment.getByName(enchant);
	if (ench == null) {
		String out ="";
		for (Enchantment ench2 : Enchantment.values()) {
			out += ench2.getName()+",";
		}
		throw new CivException("No enchantment called "+enchant+" Options:"+out);
	}
	
	stack.addUnsafeEnchantment(ench, level);
	CivMessage.sendSuccess(sender, "Enchanted.");
}
 
Example 6
Source File: EnchantManager.java    From ce with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static ItemStack getEnchantBook(HashMap<CEnchantment, Integer> list) {
    ItemStack item = new ItemStack(Material.ENCHANTED_BOOK);
    ItemMeta im = item.getItemMeta();
    List<String> lore = new ArrayList<String>();
    for (CEnchantment ce : list.keySet()) {
        lore.add(lorePrefix + ce.getDisplayName() + " " + intToLevel(list.get(ce)));
    }
    im.setLore(lore);
    im.setDisplayName(enchantBookName);
    item.setItemMeta(im);
    item.addUnsafeEnchantment(glowEnchantment, 0);
    return item;
}
 
Example 7
Source File: EnchantManager.java    From ce with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static ItemStack getEnchantBook(CEnchantment ce, int level) {
    ItemStack item = new ItemStack(Material.ENCHANTED_BOOK);
    ItemMeta im = item.getItemMeta();
    im.setLore(Arrays.asList(new String[] { lorePrefix + ce.getDisplayName() + " " + intToLevel(level) }));
    im.setDisplayName(enchantBookName);
    item.setItemMeta(im);
    item.addUnsafeEnchantment(glowEnchantment, 0);
    return item;
}
 
Example 8
Source File: GoneFishingListener.java    From UhcCore with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onGameStarted(UhcStartedEvent e){
    ItemStack rod = new ItemStack(Material.FISHING_ROD);
    rod.addUnsafeEnchantment(Enchantment.LURE, lureLevel);
    rod.addUnsafeEnchantment(Enchantment.LUCK, luckLevel);

    ItemMeta meta = rod.getItemMeta();
    VersionUtils.getVersionUtils().setItemUnbreakable(meta, true);
    rod.setItemMeta(meta);

    ItemStack anvils = new ItemStack(Material.ANVIL, 64);

    for (UhcPlayer uhcPlayer : e.getPlayersManager().getOnlinePlayingPlayers()){
        try {
            // Give the rod
            uhcPlayer.getPlayer().getInventory().addItem(rod);

            // Give player 10000 xl levels
            uhcPlayer.getPlayer().setLevel(10000);

            // Give player 64 anvils
            uhcPlayer.getPlayer().getInventory().addItem(anvils);
        }catch (UhcPlayerNotOnlineException ex){
            // No rod for offline players
        }
    }
}
 
Example 9
Source File: StickTools.java    From StackMob-3 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void giveStackingStick(Player player){
    ItemStack stack = new ItemStack(material, 1);
    stack.addUnsafeEnchantment(Enchantment.DURABILITY,1);

    ItemMeta stackMeta = stack.getItemMeta();
    stackMeta.setDisplayName(itemName);
    stackMeta.setLore(itemLore);
    stack.setItemMeta(stackMeta);

    player.getInventory().addItem(stack);
}
 
Example 10
Source File: EnchantManager.java    From ce with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static ItemStack addEnchantments(ItemStack item, HashMap<CEnchantment, Integer> list) {
    ItemMeta im = item.getItemMeta();
    List<String> lore = new ArrayList<String>();
    if (im.hasLore()) {
        lore = im.getLore();
        if (maxEnchants < enchantments.size()) {
            int counter = maxEnchants - list.size();
            for (String s : lore)
                if (containsEnchantment(s)) {
                    counter--;
                    if (counter <= 0) {
                        return item;
                    }
                }
        }
    }
    for (CEnchantment ce : list.keySet()) {
        int level = list.get(ce);
        if (level > ce.getEnchantmentMaxLevel())
            level = ce.getEnchantmentMaxLevel();
        lore.add(ce.getDisplayName() + " " + intToLevel(level));
    }
    im.setLore(lore);
    item.setItemMeta(im);
    item.addUnsafeEnchantment(glowEnchantment, 0);
    return item;
}
 
Example 11
Source File: Key.java    From CratesPlus with GNU General Public License v3.0 5 votes vote down vote up
public ItemStack getKeyItem(Integer amount) {
    ItemStack keyItem = new ItemStack(getMaterial(), amount, getData());
    if (isEnchanted())
        keyItem.addUnsafeEnchantment(Enchantment.DURABILITY, 1);
    ItemMeta keyItemMeta = keyItem.getItemMeta();
    String title = getName().replaceAll("%type%", getCrate().getName(true));
    keyItemMeta.setDisplayName(title);
    keyItemMeta.setLore(getLore());
    ArrayList<String> flags = new ArrayList<>();
    flags.add("HIDE_ENCHANTS");
    keyItemMeta = cratesPlus.getVersion_util().handleItemFlags(keyItemMeta, flags);
    keyItem.setItemMeta(keyItemMeta);
    return keyItem;
}
 
Example 12
Source File: Library.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
public ItemStack addEnchantment(ItemStack item, LibraryEnchantment ench) {
	if (ench.enchant != null) {
		item.addUnsafeEnchantment(ench.enchant, ench.level);
	} else {
		item = LoreMaterial.addEnhancement(item, ench.enhancement);
	}
	return item;
}
 
Example 13
Source File: ExprShinyItem.java    From skRayFall with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Nullable
public ItemStack convert(ItemStack itemStack) {
    if (itemStack.getType() == Material.BOW) {
        itemStack.addUnsafeEnchantment(Enchantment.DIG_SPEED, 1);
    } else {
        itemStack.addUnsafeEnchantment(Enchantment.ARROW_DAMAGE, 1);
    }
    ItemMeta meta = itemStack.getItemMeta();
    meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
    itemStack.setItemMeta(meta);
    return itemStack;
}
 
Example 14
Source File: KitUtils.java    From AnnihilationPro with MIT License 4 votes vote down vote up
public static ItemStack addEnchant(ItemStack s, Enchantment m, int level)
{
	s.addUnsafeEnchantment(m, level);
	return s;
}
 
Example 15
Source File: AutoEnchanter.java    From Slimefun4 with GNU General Public License v3.0 4 votes vote down vote up
private MachineRecipe findRecipe(BlockMenu menu) {
    for (int slot : getInputSlots()) {
        ItemStack target = menu.getItemInSlot(slot == getInputSlots()[0] ? getInputSlots()[1] : getInputSlots()[0]);

        // Check if the item is enchantable
        if (!isEnchantable(target)) {
            return null;
        }

        ItemStack item = menu.getItemInSlot(slot);

        // Enchant
        if (item != null && item.getType() == Material.ENCHANTED_BOOK && target != null) {
            Map<Enchantment, Integer> enchantments = new HashMap<>();
            Set<ItemEnchantment> emeraldEnchantments = new HashSet<>();
            int amount = 0;
            int specialAmount = 0;
            EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();

            for (Map.Entry<Enchantment, Integer> e : meta.getStoredEnchants().entrySet()) {
                if (e.getKey().canEnchantItem(target)) {
                    amount++;
                    enchantments.put(e.getKey(), e.getValue());
                }
            }

            if (SlimefunPlugin.getThirdPartySupportService().isEmeraldEnchantsInstalled()) {
                for (ItemEnchantment enchantment : EmeraldEnchants.getInstance().getRegistry().getEnchantments(item)) {
                    if (EmeraldEnchants.getInstance().getRegistry().isApplicable(target, enchantment.getEnchantment()) && EmeraldEnchants.getInstance().getRegistry().getEnchantmentLevel(target, enchantment.getEnchantment().getName()) < enchantment.getLevel()) {
                        amount++;
                        specialAmount++;
                        emeraldEnchantments.add(enchantment);
                    }
                }
                specialAmount += EmeraldEnchants.getInstance().getRegistry().getEnchantments(target).size();
            }

            if (amount > 0 && specialAmount <= emeraldEnchantsLimit) {
                ItemStack enchantedItem = target.clone();
                enchantedItem.setAmount(1);

                for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
                    enchantedItem.addUnsafeEnchantment(entry.getKey(), entry.getValue());
                }

                for (ItemEnchantment ench : emeraldEnchantments) {
                    EmeraldEnchants.getInstance().getRegistry().applyEnchantment(enchantedItem, ench.getEnchantment(), ench.getLevel());
                }

                return new MachineRecipe(75 * amount, new ItemStack[] { target, item }, new ItemStack[] { enchantedItem, new ItemStack(Material.BOOK) });
            }

            return null;
        }
    }

    return null;
}
 
Example 16
Source File: ZEnchantment.java    From XSeries with MIT License 4 votes vote down vote up
/**
 * Adds an unsafe enchantment to the given item from a string.
 * <p>
 * <blockquote><pre>
 *    ItemStack item = ...;
 *    addEnchantFromString(item, "unbreaking, 10");
 *    addEnchantFromString(item, "mending");
 * </pre></blockquote>
 * <p>
 * Note that if you set your item's meta {@link ItemStack#setItemMeta(ItemMeta)} the enchantment
 * will be removed.
 * You need to use {@link ItemMeta#addEnchant(Enchantment, int, boolean)} instead.
 * You can use the {@link #getByName(String)} method in this case.
 *
 * @param item        the item to add the enchantment to.
 * @param enchantment the enchantment string containing the enchantment name and level (optional)
 * @return an enchanted {@link ItemStack}.
 * @since 1.0.0
 */
@Nonnull
public static ItemStack addEnchantFromString(@Nonnull ItemStack item, @Nonnull String enchantment) {
    Objects.requireNonNull(item, "Cannot add enchantment to null ItemStack");
    Validate.notEmpty(enchantment, "Cannot add null or empty enchantment to item");

    String[] split = enchantment.contains(",") ? StringUtils.deleteWhitespace(enchantment).split(",") : SPACE.matcher(enchantment).replaceAll(" ").split(" ");
    Optional<Enchantment> enchant = getByName(split[0]);
    if (!enchant.isPresent()) return item;

    int lvl = 1;
    if (split.length > 1) lvl = Integer.parseInt(split[1]);

    item.addUnsafeEnchantment(enchant.get(), lvl);
    return item;
}
 
Example 17
Source File: ItemStackImpl.java    From Civs with GNU General Public License v3.0 4 votes vote down vote up
public static ItemStack deserialize(Map<String, Object> args) {
    int version = args.containsKey("v") ? ((Number)args.get("v")).intValue() : -1;
    short damage = 0;
    int amount = 1;
    if (args.containsKey("damage")) {
        damage = ((Number)args.get("damage")).shortValue();
    }

    Material type;
    if (version < 0) {
        type = Material.getMaterial("LEGACY_" + (String)args.get("type"));
        byte dataVal = type.getMaxDurability() == 0 ? (byte)damage : 0;
        type = Bukkit.getUnsafe().fromLegacy(new MaterialData(type, dataVal), true);
        if (dataVal != 0) {
            damage = 0;
        }
    } else {
        type = Material.getMaterial((String)args.get("type"));
    }

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

    ItemStack result = new ItemStack(type, amount, damage);
    Object raw;
    if (args.containsKey("enchantments")) {
        raw = args.get("enchantments");
        if (raw instanceof Map) {
            Map<?, ?> map = (Map)raw;
            Iterator var9 = map.entrySet().iterator();

            while(var9.hasNext()) {
                Entry<?, ?> entry = (Entry)var9.next();
                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")) {
        raw = args.get("meta");
        if (raw instanceof ItemMeta) {
            result.setItemMeta((ItemMeta)raw);
        }
    }

    if (version < 0 && args.containsKey("damage")) {
        result.setDurability(damage);
    }

    return result;
}
 
Example 18
Source File: ItemManager.java    From EnchantmentsEnhance with GNU General Public License v3.0 4 votes vote down vote up
public static boolean applyEnchantmentToItem(ItemStack item, String ench, int level) {
    if (PackageManager.getDisabled().contains(ench)) {
        return false;
    }
    ItemMeta meta = item.getItemMeta();
    List<String> newlore = (meta.hasLore() ? meta.getLore() : new ArrayList<>());
    Enchantment vanilla = Enchantment.getByName(ench.toUpperCase());

    if (vanilla != null) {
        int lvl = (item.getEnchantmentLevel(vanilla)) + level;
        if (lvl > 0) {
            item.addUnsafeEnchantment(Enchantment.getByName(ench.toUpperCase()), lvl);
        } else {
            item.removeEnchantment(vanilla);
        }
        return true;
    } else {
        if (ench.equalsIgnoreCase("random")) {
            String name = PackageManager.getEnabled().get(new Random().nextInt(PackageManager.getEnabled().size())).name();
            return applyEnchantmentToItem(item, name, level);
        }
        String enchantment = SettingsManager.lang.getString("enchantments." + ench.toLowerCase());
        int keptLevel = 0;
        if (enchantment != null) {
            String currEnch = ChatColor.stripColor(Util.toColor(enchantment));
            for (int i = 0; i < newlore.size(); i++) {
                String[] curr = ChatColor.stripColor(newlore.get(i)).split(
                        " ");
                if (curr.length == 2 && curr[0].equals(currEnch)) {
                    // Adds original level.
                    keptLevel += Util.romanToInt(curr[1]);
                    newlore.remove(i);
                    i--;
                }
            }
            int max = 1;
            try {
                max = Main.getApi().getEnchantmentMaxLevel(ench);
            } catch (NullPointerException ex) {
            }
            int finalLevel = ((level + keptLevel) > max) ? max : level + keptLevel;
            if (finalLevel > 0) {
                newlore.add(Util.UNIQUEID + Util.toColor("&7" + enchantment + " " + Util.intToRoman(finalLevel)));
                meta.setLore(newlore);
                item.setItemMeta(meta);
                if (item.getEnchantments().isEmpty()) {
                    CompatibilityManager.glow
                            .addGlow(item);
                }
                return true;
            } else {
                meta.setLore(newlore);
                item.setItemMeta(meta);
                if (item.getEnchantments().isEmpty()) {
                    CompatibilityManager.glow
                            .addGlow(item);
                }
            }
        }
        return false;
    }
}
 
Example 19
Source File: ItemGetter_Late_1_8.java    From Quests with MIT License 4 votes vote down vote up
@Override
public ItemStack getItem(String path, ConfigurationSection config, Quests plugin, Filter... excludes) {
    if (path != null && !path.equals("")) {
        path = path + ".";
    }
    List<Filter> filters = Arrays.asList(excludes);


    String cName = config.getString(path + "name", path + "name");
    String cType = config.getString(path + "item", config.getString(path + "type", path + "item"));
    List<String> cLore = config.getStringList(path + "lore");
    List<String> cItemFlags = config.getStringList(path + "itemflags");

    String name;
    Material type = null;
    int data = 0;

    // material
    ItemStack is = getItemStack(cType, plugin);
    ItemMeta ism = is.getItemMeta();

    // lore
    if (!filters.contains(Filter.LORE)) {
        List<String> lore = new ArrayList<>();
        if (cLore != null) {
            for (String s : cLore) {
                lore.add(ChatColor.translateAlternateColorCodes('&', s));
            }
        }
        ism.setLore(lore);
    }

    // name
    if (!filters.contains(Filter.DISPLAY_NAME)) {
        name = ChatColor.translateAlternateColorCodes('&', cName);
        ism.setDisplayName(name);
    }


    // item flags
    if (!filters.contains(Filter.ITEM_FLAGS)) {
        if (config.isSet(path + "itemflags")) {
            for (String flag : cItemFlags) {
                for (ItemFlag iflag : ItemFlag.values()) {
                    if (iflag.toString().equals(flag)) {
                        ism.addItemFlags(iflag);
                        break;
                    }
                }
            }
        }
    }

    // enchantments
    if (!filters.contains(Filter.ENCHANTMENTS)) {
        if (config.isSet(path + "enchantments")) {
            for (String key : config.getStringList(path + "enchantments")) {
                String[] split = key.split(":");
                String ench = split[0];
                String levelName;
                if (split.length >= 2) {
                    levelName = split[1];
                } else {
                    levelName = "1";
                }

                Enchantment enchantment;
                if ((enchantment = Enchantment.getByName(ench)) == null) {
                    plugin.getQuestsLogger().debug("Unrecognised enchantment: " + ench);
                    continue;
                }

                int level;
                try {
                    level = Integer.parseInt(levelName);
                } catch (NumberFormatException e) {
                    level = 1;
                }

                is.addUnsafeEnchantment(enchantment, level);
            }
        }
    }

    is.setItemMeta(ism);
    return is;
}