Search in sources :

Example 16 with Enchantment

use of org.bukkit.enchantments.Enchantment 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 17 with Enchantment

use of org.bukkit.enchantments.Enchantment 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 18 with Enchantment

use of org.bukkit.enchantments.Enchantment 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 19 with Enchantment

use of org.bukkit.enchantments.Enchantment in project Denizen-For-Bukkit by DenizenScript.

the class dItem method comparesTo.

public int comparesTo(ItemStack item) {
    if (item == null) {
        return -1;
    }
    int determination = 0;
    ItemStack compared = getItemStack();
    ItemStack compared_to = item;
    // Material IDs
    if (compared.getTypeId() != compared_to.getTypeId()) {
        return -1;
    }
    // If compared_to has item meta, and compared does not, return -1
    if (compared_to.hasItemMeta()) {
        if (!compared.hasItemMeta()) {
            return -1;
        }
        // If compared_to has a display name, and compared does not, return -1
        if (compared_to.getItemMeta().hasDisplayName()) {
            if (!compared.getItemMeta().hasDisplayName()) {
                return -1;
            }
            // return -1.
            if (compared_to.getItemMeta().getDisplayName().toUpperCase().startsWith(compared.getItemMeta().getDisplayName().toUpperCase())) {
                // it is similar, but modified. Perhaps 'engraved' or something?
                if (compared.getItemMeta().getDisplayName().length() > compared_to.getItemMeta().getDisplayName().length()) {
                    determination++;
                }
            } else {
                return -1;
            }
        }
        // If compared_to has lore, and compared does not, return -1
        if (compared_to.getItemMeta().hasLore()) {
            if (!compared.getItemMeta().hasLore()) {
                return -1;
            }
            // If compared doesn't have a piece of lore contained in compared_to, return -1
            for (String lore : compared_to.getItemMeta().getLore()) {
                if (!compared.getItemMeta().getLore().contains(lore)) {
                    return -1;
                }
            }
            // the determination.
            if (compared.getItemMeta().getLore().size() > compared_to.getItemMeta().getLore().size()) {
                determination++;
            }
        }
        if (!compared_to.getItemMeta().getEnchants().isEmpty()) {
            if (compared.getItemMeta().getEnchants().isEmpty()) {
                return -1;
            }
            for (Map.Entry<Enchantment, Integer> enchant : compared_to.getItemMeta().getEnchants().entrySet()) {
                if (!compared.getItemMeta().getEnchants().containsKey(enchant.getKey()) || compared.getItemMeta().getEnchants().get(enchant.getKey()) < enchant.getValue()) {
                    return -1;
                }
            }
            if (compared.getItemMeta().getEnchants().size() > compared_to.getItemMeta().getEnchants().size()) {
                determination++;
            }
        }
    }
    if (isRepairable()) {
        if (compared.getDurability() < compared_to.getDurability()) {
            determination++;
        }
    } else // Check data
    if (getItemStack().getData().getData() != item.getData().getData()) {
        return -1;
    }
    return determination;
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) Enchantment(org.bukkit.enchantments.Enchantment) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with Enchantment

use of org.bukkit.enchantments.Enchantment in project Dragonet-Legacy by DragonetMC.

the class CraftingDataPacket method encode.

@Override
public void encode() {
    enchants = 0;
    try {
        setChannel(NetworkChannel.CHANNEL_WORLD_EVENTS);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        PEBinaryWriter writer = new PEBinaryWriter(bos);
        writer.writeByte((byte) (this.pid() & 0xFF));
        for (Object o : recipies) {
            if (Recipe.class.isAssignableFrom(o.getClass())) {
                Recipe r = (Recipe) o;
                if (ShapelessRecipe.class.isInstance(r)) {
                    writer.writeInt(ENTRY_SHAPELESS);
                    ByteArrayOutputStream ebos = new ByteArrayOutputStream();
                    PEBinaryWriter ewriter = new PEBinaryWriter(ebos);
                    /* Write data */
                    {
                        ewriter.writeInt(((ShapelessRecipe) r).getIngredientList().size());
                        for (ItemStack stack : ((ShapelessRecipe) r).getIngredientList()) {
                            PEInventorySlot.writeSlot(ewriter, PEInventorySlot.fromItemStack(stack));
                        }
                        ewriter.writeInt(1);
                        PEInventorySlot.writeSlot(ewriter, PEInventorySlot.fromItemStack(r.getResult()));
                    }
                    writer.writeInt(ebos.toByteArray().length);
                    writer.write(ebos.toByteArray());
                } else if (FurnaceRecipe.class.isInstance(r)) {
                    FurnaceRecipe f = (FurnaceRecipe) r;
                    if (f.getResult().getDurability() != 0) {
                        writer.writeInt(ENTRY_FURNACE_DATA);
                        ByteArrayOutputStream ebos = new ByteArrayOutputStream();
                        PEBinaryWriter ewriter = new PEBinaryWriter(ebos);
                        /* Write data */
                        {
                            ewriter.writeInt(f.getInput().getTypeId() << 16 | f.getInput().getDurability());
                            PEInventorySlot.writeSlot(ewriter, PEInventorySlot.fromItemStack(f.getResult()));
                        }
                        writer.writeInt(ebos.toByteArray().length);
                        writer.write(ebos.toByteArray());
                    } else {
                        writer.writeInt(ENTRY_FURNACE);
                        ByteArrayOutputStream ebos = new ByteArrayOutputStream();
                        PEBinaryWriter ewriter = new PEBinaryWriter(ebos);
                        /* Write data */
                        {
                            ewriter.writeInt(f.getInput().getTypeId());
                            PEInventorySlot.writeSlot(ewriter, PEInventorySlot.fromItemStack(f.getResult()));
                        }
                        writer.writeInt(ebos.toByteArray().length);
                        writer.write(ebos.toByteArray());
                    }
                } else if (ShapedRecipe.class.isInstance(r)) {
                    ShapedRecipe sr = (ShapedRecipe) r;
                    writer.writeInt(ENTRY_SHAPED);
                    ByteArrayOutputStream ebos = new ByteArrayOutputStream();
                    PEBinaryWriter ewriter = new PEBinaryWriter(ebos);
                    /* Write data */
                    {
                        ewriter.writeInt(sr.getShape()[0].length());
                        ewriter.writeInt(sr.getShape().length);
                        for (int pos = 0; pos > sr.getShape()[0].length(); pos++) {
                            for (String line : sr.getShape()) {
                                ItemStack stack = sr.getIngredientMap().get(line.charAt(pos));
                                PEInventorySlot.writeSlot(ewriter, PEInventorySlot.fromItemStack(stack));
                            }
                        }
                        ewriter.writeInt(1);
                        PEInventorySlot.writeSlot(ewriter, PEInventorySlot.fromItemStack(sr.getResult()));
                        long mostUUID = sr.getResult().getTypeId() << 32 | sr.getResult().getDurability() << 16 | (sr.getResult().getAmount() & 0xFFFF);
                        UUID uid = new UUID(mostUUID, mostUUID);
                        ewriter.writeUUID(uid);
                    }
                    writer.writeInt(ebos.toByteArray().length);
                    writer.write(ebos.toByteArray());
                } else {
                    writer.writeInt(-1);
                    writer.writeInt(0);
                }
            } else if (Enchantment.class.isAssignableFrom(o.getClass())) {
                Enchantment e = (Enchantment) o;
                writer.writeInt(ENTRY_ENCHANT);
                ByteArrayOutputStream ebos = new ByteArrayOutputStream();
                PEBinaryWriter ewriter = new PEBinaryWriter(ebos);
                /* Write data */
                {
                    ewriter.writeInt(enchants);
                    enchants++;
                    ewriter.writeInt(e.getId());
                    //HACK: Always return this.
                    ewriter.writeInt(e.getMaxLevel());
                    //HACK: Always 1 for now. TO BE FIXED! 
                    ewriter.writeInt(1);
                    ewriter.writeString(e.getName());
                }
                writer.writeInt(ebos.toByteArray().length);
                writer.write(ebos.toByteArray());
            }
        }
        writer.writeInt(recipies.size());
        this.setData(bos.toByteArray());
    } catch (IOException e) {
    }
}
Also used : PEBinaryWriter(org.dragonet.utilities.io.PEBinaryWriter) ShapelessRecipe(org.bukkit.inventory.ShapelessRecipe) ShapedRecipe(org.bukkit.inventory.ShapedRecipe) Recipe(org.bukkit.inventory.Recipe) FurnaceRecipe(org.bukkit.inventory.FurnaceRecipe) ShapedRecipe(org.bukkit.inventory.ShapedRecipe) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FurnaceRecipe(org.bukkit.inventory.FurnaceRecipe) IOException(java.io.IOException) ItemStack(org.bukkit.inventory.ItemStack) UUID(java.util.UUID) 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