Search in sources :

Example 6 with Enchantment

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

the class MetaItemStack method parseEnchantmentStrings.

private void parseEnchantmentStrings(final CommandSource sender, final boolean allowUnsafe, final String[] split, final IEssentials ess) throws Exception {
    final Enchantment enchantment = Enchantments.getByName(split[0]);
    if (enchantment == null || !hasMetaPermission(sender, "enchantments." + enchantment.getName().toLowerCase(Locale.ENGLISH), false, false, ess)) {
        return;
    }
    int level = -1;
    if (split.length > 1) {
        try {
            level = Integer.parseInt(split[1]);
        } catch (NumberFormatException ex) {
            level = -1;
        }
    }
    if (level < 0 || (!allowUnsafe && level > enchantment.getMaxLevel())) {
        level = enchantment.getMaxLevel();
    }
    addEnchantment(sender, allowUnsafe, enchantment, level);
}
Also used : Enchantment(org.bukkit.enchantments.Enchantment)

Example 7 with Enchantment

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

the class EnchantmentLevel method equals.

@Override
public boolean equals(final Object obj) {
    if (obj instanceof Entry) {
        final Entry entry = (Entry) obj;
        if (entry.getKey() instanceof Enchantment && entry.getValue() instanceof Integer) {
            final Enchantment enchant = (Enchantment) entry.getKey();
            final Integer lvl = (Integer) entry.getValue();
            return this.enchantment.equals(enchant) && this.level == lvl.intValue();
        }
    }
    return false;
}
Also used : Entry(java.util.Map.Entry) Enchantment(org.bukkit.enchantments.Enchantment)

Example 8 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 9 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 10 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)27 ItemStack (org.bukkit.inventory.ItemStack)15 ItemMeta (org.bukkit.inventory.meta.ItemMeta)5 Map (java.util.Map)4 EnchantmentStorageMeta (org.bukkit.inventory.meta.EnchantmentStorageMeta)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 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 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1