Search in sources :

Example 26 with Enchantment

use of org.bukkit.enchantments.Enchantment in project Glowstone by GlowstoneMC.

the class GlowMetaItem method readNbtEnchants.

protected static Map<Enchantment, Integer> readNbtEnchants(String name, CompoundTag tag) {
    Map<Enchantment, Integer> result = null;
    if (tag.isList(name, TagType.COMPOUND)) {
        Iterable<CompoundTag> enchs = tag.getCompoundList(name);
        for (CompoundTag enchantmentTag : enchs) {
            if (enchantmentTag.isShort("id") && enchantmentTag.isShort("lvl")) {
                Enchantment enchantment = Enchantment.getById(enchantmentTag.getShort("id"));
                if (result == null)
                    result = new HashMap<>(4);
                result.put(enchantment, (int) enchantmentTag.getShort("lvl"));
            }
        }
    }
    return result;
}
Also used : Enchantment(org.bukkit.enchantments.Enchantment) CompoundTag(net.glowstone.util.nbt.CompoundTag)

Example 27 with Enchantment

use of org.bukkit.enchantments.Enchantment in project Glowstone by GlowstoneMC.

the class GlowMetaItem method writeNbtEnchants.

protected static void writeNbtEnchants(String name, CompoundTag to, Map<Enchantment, Integer> enchants) {
    List<CompoundTag> ench = new ArrayList<>();
    for (Entry<Enchantment, Integer> enchantment : enchants.entrySet()) {
        CompoundTag enchantmentTag = new CompoundTag();
        enchantmentTag.putShort("id", enchantment.getKey().getId());
        enchantmentTag.putShort("lvl", enchantment.getValue());
        ench.add(enchantmentTag);
    }
    to.putCompoundList(name, ench);
}
Also used : Enchantment(org.bukkit.enchantments.Enchantment) CompoundTag(net.glowstone.util.nbt.CompoundTag)

Example 28 with Enchantment

use of org.bukkit.enchantments.Enchantment in project Glowstone by GlowstoneMC.

the class EnchantmentManager method onPlayerEnchant.

public void onPlayerEnchant(int clicked) {
    if (enchLevelCosts[clicked] <= 0 || isMaliciousClicked(clicked))
        return;
    ItemStack item = inventory.getItem();
    List<LeveledEnchant> enchants = calculateCurrentEnchants(item, clicked, enchLevelCosts[clicked]);
    if (enchants == null)
        enchants = new ArrayList<>();
    EnchantItemEvent event = EventFactory.callEvent(new EnchantItemEvent(player, player.getOpenInventory(), inventory.getLocation().getBlock(), item.clone(), enchLevelCosts[clicked], toMap(enchants), clicked));
    if (event.isCancelled() || player.getGameMode() != GameMode.CREATIVE && event.getExpLevelCost() > player.getLevel())
        return;
    boolean isBook = item.getType() == Material.BOOK;
    if (isBook)
        item.setType(Material.ENCHANTED_BOOK);
    Map<Enchantment, Integer> toAdd = event.getEnchantsToAdd();
    if (toAdd == null || toAdd.isEmpty()) {
        return;
    }
    for (Entry<Enchantment, Integer> enchantment : toAdd.entrySet()) {
        try {
            if (isBook) {
                EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
                //TODO is true correct here?
                meta.addStoredEnchant(enchantment.getKey(), enchantment.getValue(), true);
                item.setItemMeta(meta);
            } else {
                item.addUnsafeEnchantment(enchantment.getKey(), enchantment.getValue());
            }
        } catch (IllegalArgumentException e) {
        //ignore, since plugins are allowed to add enchantments that can't be applied
        }
    }
    player.enchanted(clicked);
    if (player.getGameMode() != GameMode.CREATIVE) {
        ItemStack res = inventory.getSecondary();
        res.setAmount(res.getAmount() - clicked + 1);
        if (res.getAmount() <= 0)
            inventory.setSecondary(null);
    }
    xpSeed = player.getXpSeed();
    update();
}
Also used : EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) EnchantItemEvent(org.bukkit.event.enchantment.EnchantItemEvent) ItemStack(org.bukkit.inventory.ItemStack) Enchantment(org.bukkit.enchantments.Enchantment) GlowEnchantment(net.glowstone.constants.GlowEnchantment)

Example 29 with Enchantment

use of org.bukkit.enchantments.Enchantment in project Essentials by drtshock.

the class YamlStorageWriter method writeItemStack.

private void writeItemStack(final Object data) {
    final ItemStack itemStack = (ItemStack) data;
    writeMaterialData(itemStack.getData());
    writer.print(' ');
    writer.print(itemStack.getAmount());
    for (Entry<Enchantment, Integer> entry : itemStack.getEnchantments().entrySet()) {
        writer.print(' ');
        writeEnchantmentLevel(entry);
    }
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) Enchantment(org.bukkit.enchantments.Enchantment)

Example 30 with Enchantment

use of org.bukkit.enchantments.Enchantment in project Essentials by drtshock.

the class SignEnchant method onSignCreate.

@Override
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException {
    final ItemStack stack;
    try {
        stack = sign.getLine(1).equals("*") || sign.getLine(1).equalsIgnoreCase("any") ? null : getItemStack(sign.getLine(1), 1, ess);
    } catch (SignException e) {
        sign.setLine(1, "§c<item|any>");
        throw e;
    }
    final String[] enchantLevel = sign.getLine(2).split(":");
    if (enchantLevel.length != 2) {
        sign.setLine(2, "§c<enchant>");
        throw new SignException(tl("invalidSignLine", 3));
    }
    final Enchantment enchantment = Enchantments.getByName(enchantLevel[0]);
    if (enchantment == null) {
        sign.setLine(2, "§c<enchant>");
        throw new SignException(tl("enchantmentNotFound"));
    }
    int level;
    try {
        level = Integer.parseInt(enchantLevel[1]);
    } catch (NumberFormatException ex) {
        sign.setLine(2, "§c<enchant>");
        throw new SignException(ex.getMessage(), ex);
    }
    final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments() && player.isAuthorized("essentials.enchantments.allowunsafe") && player.isAuthorized("essentials.signs.enchant.allowunsafe");
    if (level < 0 || (!allowUnsafe && level > enchantment.getMaxLevel())) {
        level = enchantment.getMaxLevel();
        sign.setLine(2, enchantLevel[0] + ":" + level);
    }
    try {
        if (stack != null) {
            if (allowUnsafe) {
                stack.addUnsafeEnchantment(enchantment, level);
            } else {
                stack.addEnchantment(enchantment, level);
            }
        }
    } catch (Throwable ex) {
        throw new SignException(ex.getMessage(), ex);
    }
    getTrade(sign, 3, ess);
    return true;
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) Enchantment(org.bukkit.enchantments.Enchantment)

Aggregations

Enchantment (org.bukkit.enchantments.Enchantment)38 ItemStack (org.bukkit.inventory.ItemStack)16 EnchantmentStorageMeta (org.bukkit.inventory.meta.EnchantmentStorageMeta)5 ItemMeta (org.bukkit.inventory.meta.ItemMeta)5 Map (java.util.Map)4 GlowEnchantment (net.glowstone.constants.GlowEnchantment)3 Entry (java.util.Map.Entry)2 CompoundTag (net.glowstone.util.nbt.CompoundTag)2 Color (org.bukkit.Color)2 FireworkEffect (org.bukkit.FireworkEffect)2 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)2 BookMeta (org.bukkit.inventory.meta.BookMeta)2 FireworkEffectMeta (org.bukkit.inventory.meta.FireworkEffectMeta)2 LeatherArmorMeta (org.bukkit.inventory.meta.LeatherArmorMeta)2 SkullMeta (org.bukkit.inventory.meta.SkullMeta)2 PotionEffect (org.bukkit.potion.PotionEffect)2 ChargeException (com.earth2me.essentials.ChargeException)1 MetaItemStack (com.earth2me.essentials.MetaItemStack)1 Trade (com.earth2me.essentials.Trade)1 ImmutableMap (com.google.common.collect.ImmutableMap)1