Search in sources :

Example 1 with EnchantmentStorageMeta

use of org.bukkit.inventory.meta.EnchantmentStorageMeta in project Prism-Bukkit by prism.

the class ItemStackAction method setItemStackFromNewDataFormat.

/**
	 * 
	 */
protected void setItemStackFromNewDataFormat() {
    if (data == null || !data.startsWith("{"))
        return;
    actionData = gson.fromJson(data, ItemStackActionData.class);
    item = new ItemStack(this.block_id, actionData.amt, (short) this.block_subid);
    // Restore enchantment
    if (actionData.enchs != null && actionData.enchs.length > 0) {
        for (final String ench : actionData.enchs) {
            final String[] enchArgs = ench.split(":");
            final Enchantment enchantment = Enchantment.getById(Integer.parseInt(enchArgs[0]));
            // Restore book enchantment
            if (item.getType().equals(Material.ENCHANTED_BOOK)) {
                final EnchantmentStorageMeta bookEnchantments = (EnchantmentStorageMeta) item.getItemMeta();
                bookEnchantments.addStoredEnchant(enchantment, Integer.parseInt(enchArgs[1]), false);
                item.setItemMeta(bookEnchantments);
            } else // Restore item enchantment
            {
                item.addUnsafeEnchantment(enchantment, Integer.parseInt(enchArgs[1]));
            }
        }
    }
    // Leather color
    if (item.getType().name().contains("LEATHER_") && actionData.color > 0) {
        final LeatherArmorMeta lam = (LeatherArmorMeta) item.getItemMeta();
        lam.setColor(Color.fromRGB(actionData.color));
        item.setItemMeta(lam);
    } else // Skulls
    if (item.getType().equals(Material.SKULL_ITEM) && actionData.owner != null) {
        final SkullMeta meta = (SkullMeta) item.getItemMeta();
        meta.setOwner(actionData.owner);
        item.setItemMeta(meta);
    } else // Written books
    if (item.getItemMeta() instanceof BookMeta) {
        final BookMeta bookMeta = (BookMeta) item.getItemMeta();
        bookMeta.setAuthor(actionData.by);
        bookMeta.setTitle(actionData.title);
        bookMeta.setPages(actionData.content);
        item.setItemMeta(bookMeta);
    }
    // Fireworks
    if (block_id == 402 && actionData.effectColors != null && actionData.effectColors.length > 0) {
        final FireworkEffectMeta fireworkMeta = (FireworkEffectMeta) item.getItemMeta();
        final Builder effect = FireworkEffect.builder();
        if (actionData.effectColors != null) {
            for (int i = 0; i < actionData.effectColors.length; i++) {
                effect.withColor(Color.fromRGB(actionData.effectColors[i]));
            }
            fireworkMeta.setEffect(effect.build());
        }
        if (actionData.fadeColors != null) {
            for (int i = 0; i < actionData.fadeColors.length; i++) {
                effect.withFade(Color.fromRGB(actionData.fadeColors[i]));
            }
            fireworkMeta.setEffect(effect.build());
        }
        if (actionData.hasFlicker) {
            effect.flicker(true);
        }
        if (actionData.hasTrail) {
            effect.trail(true);
        }
        fireworkMeta.setEffect(effect.build());
        item.setItemMeta(fireworkMeta);
    }
    // Item display names
    final ItemMeta meta = item.getItemMeta();
    if (actionData.name != null) {
        meta.setDisplayName(actionData.name);
    }
    if (actionData.lore != null) {
        meta.setLore(Arrays.asList(actionData.lore));
    }
    item.setItemMeta(meta);
}
Also used : Builder(org.bukkit.FireworkEffect.Builder) SkullMeta(org.bukkit.inventory.meta.SkullMeta) EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) LeatherArmorMeta(org.bukkit.inventory.meta.LeatherArmorMeta) FireworkEffectMeta(org.bukkit.inventory.meta.FireworkEffectMeta) ItemStack(org.bukkit.inventory.ItemStack) Enchantment(org.bukkit.enchantments.Enchantment) BookMeta(org.bukkit.inventory.meta.BookMeta) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 2 with EnchantmentStorageMeta

use of org.bukkit.inventory.meta.EnchantmentStorageMeta in project Prism-Bukkit by prism.

the class ItemStackAction method setItem.

/**
     * 
     * @param action_type
     * @param block
     * @param player
     */
public void setItem(ItemStack item, int quantity, int slot, Map<Enchantment, Integer> enchantments) {
    actionData = new ItemStackActionData();
    if (enchantments != null) {
        this.enchantments = enchantments;
    }
    if (item == null || item.getAmount() <= 0) {
        this.setCanceled(true);
        return;
    }
    this.item = item;
    if (enchantments == null) {
        this.enchantments = item.getEnchantments();
    }
    // Set basics
    this.block_id = item.getTypeId();
    this.block_subid = item.getDurability();
    actionData.amt = quantity;
    if (slot >= 0) {
        actionData.slot = slot;
    }
    // Set additional data all items may have
    final ItemMeta meta = item.getItemMeta();
    if (meta != null && meta.getDisplayName() != null) {
        actionData.name = meta.getDisplayName();
    }
    // Leather Coloring
    if (meta != null && item.getType().name().contains("LEATHER_")) {
        final LeatherArmorMeta lam = (LeatherArmorMeta) meta;
        if (lam.getColor() != null) {
            actionData.color = lam.getColor().asRGB();
        }
    } else // Skull Owner
    if (meta != null && item.getType().equals(Material.SKULL_ITEM)) {
        final SkullMeta skull = (SkullMeta) meta;
        if (skull.hasOwner()) {
            actionData.owner = skull.getOwner();
        }
    }
    // Written books
    if (meta != null && meta instanceof BookMeta) {
        final BookMeta bookMeta = (BookMeta) meta;
        actionData.by = bookMeta.getAuthor();
        actionData.title = bookMeta.getTitle();
        actionData.content = bookMeta.getPages().toArray(new String[0]);
    }
    // Lore
    if (meta != null && meta.getLore() != null) {
        actionData.lore = meta.getLore().toArray(new String[0]);
    }
    // Enchantments
    if (!this.enchantments.isEmpty()) {
        final String[] enchs = new String[this.enchantments.size()];
        int i = 0;
        for (final Entry<Enchantment, Integer> ench : this.enchantments.entrySet()) {
            enchs[i] = ench.getKey().getId() + ":" + ench.getValue();
            i++;
        }
        actionData.enchs = enchs;
    } else // Book enchantments
    if (meta != null && item.getType().equals(Material.ENCHANTED_BOOK)) {
        final EnchantmentStorageMeta bookEnchantments = (EnchantmentStorageMeta) meta;
        if (bookEnchantments.hasStoredEnchants()) {
            if (bookEnchantments.getStoredEnchants().size() > 0) {
                final String[] enchs = new String[bookEnchantments.getStoredEnchants().size()];
                int i = 0;
                for (final Entry<Enchantment, Integer> ench : bookEnchantments.getStoredEnchants().entrySet()) {
                    enchs[i] = ench.getKey().getId() + ":" + ench.getValue();
                    i++;
                }
                actionData.enchs = enchs;
            }
        }
    }
    // Fireworks
    if (meta != null && block_id == 402) {
        final FireworkEffectMeta fireworkMeta = (FireworkEffectMeta) meta;
        if (fireworkMeta.hasEffect()) {
            final FireworkEffect effect = fireworkMeta.getEffect();
            if (!effect.getColors().isEmpty()) {
                final int[] effectColors = new int[effect.getColors().size()];
                int i = 0;
                for (final Color effectColor : effect.getColors()) {
                    effectColors[i] = effectColor.asRGB();
                    i++;
                }
                actionData.effectColors = effectColors;
            }
            if (!effect.getFadeColors().isEmpty()) {
                final int[] fadeColors = new int[effect.getColors().size()];
                final int i = 0;
                for (final Color fadeColor : effect.getFadeColors()) {
                    fadeColors[i] = fadeColor.asRGB();
                }
                actionData.fadeColors = fadeColors;
            }
            if (effect.hasFlicker()) {
                actionData.hasFlicker = true;
            }
            if (effect.hasTrail()) {
                actionData.hasTrail = true;
            }
        }
    }
}
Also used : Color(org.bukkit.Color) SkullMeta(org.bukkit.inventory.meta.SkullMeta) FireworkEffect(org.bukkit.FireworkEffect) Entry(java.util.Map.Entry) EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) LeatherArmorMeta(org.bukkit.inventory.meta.LeatherArmorMeta) FireworkEffectMeta(org.bukkit.inventory.meta.FireworkEffectMeta) Enchantment(org.bukkit.enchantments.Enchantment) BookMeta(org.bukkit.inventory.meta.BookMeta) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 3 with EnchantmentStorageMeta

use of org.bukkit.inventory.meta.EnchantmentStorageMeta in project Denizen-For-Bukkit by DenizenScript.

the class ItemEnchantments method adjust.

@Override
public void adjust(Mechanism mechanism) {
    if (mechanism.matches("enchantments")) {
        for (String enchant : mechanism.getValue().asType(dList.class)) {
            if (!enchant.contains(",")) {
                dB.echoError("Invalid enchantment format, use name,level|...");
            } else {
                String[] data = enchant.split(",", 2);
                if (Integer.valueOf(data[1]) == null) {
                    dB.echoError("Cannot apply enchantment '" + data[0] + "': '" + data[1] + "' is not a valid integer!");
                } else {
                    try {
                        Enchantment ench = Enchantment.getByName(data[0].toUpperCase());
                        if (ench != null) {
                            if (item.getItemStack().getType() == Material.ENCHANTED_BOOK) {
                                EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemStack().getItemMeta();
                                meta.addStoredEnchant(ench, Integer.valueOf(data[1]), true);
                                item.getItemStack().setItemMeta(meta);
                            } else {
                                item.getItemStack().addUnsafeEnchantment(ench, Integer.valueOf(data[1]));
                            }
                        } else {
                            dB.echoError("Unknown enchantment '" + data[0] + "'");
                        }
                    } catch (NullPointerException e) {
                        dB.echoError("Unknown enchantment '" + data[0] + "'");
                    }
                }
            }
        }
    }
}
Also used : EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) Enchantment(org.bukkit.enchantments.Enchantment)

Example 4 with EnchantmentStorageMeta

use of org.bukkit.inventory.meta.EnchantmentStorageMeta 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 5 with EnchantmentStorageMeta

use of org.bukkit.inventory.meta.EnchantmentStorageMeta in project Glowstone by GlowstoneMC.

the class GlowAnvilInventory method getForged.

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;
}
Also used : EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

EnchantmentStorageMeta (org.bukkit.inventory.meta.EnchantmentStorageMeta)5 Enchantment (org.bukkit.enchantments.Enchantment)4 ItemStack (org.bukkit.inventory.ItemStack)3 BookMeta (org.bukkit.inventory.meta.BookMeta)2 FireworkEffectMeta (org.bukkit.inventory.meta.FireworkEffectMeta)2 ItemMeta (org.bukkit.inventory.meta.ItemMeta)2 LeatherArmorMeta (org.bukkit.inventory.meta.LeatherArmorMeta)2 SkullMeta (org.bukkit.inventory.meta.SkullMeta)2 Entry (java.util.Map.Entry)1 GlowEnchantment (net.glowstone.constants.GlowEnchantment)1 Color (org.bukkit.Color)1 FireworkEffect (org.bukkit.FireworkEffect)1 Builder (org.bukkit.FireworkEffect.Builder)1 EnchantItemEvent (org.bukkit.event.enchantment.EnchantItemEvent)1