Search in sources :

Example 1 with Enchantment

use of net.minecraft.server.v1_11_R1.Enchantment in project PublicCrafters by BananaPuncher714.

the class CustomContainerAnvil method e.

public void e() {
    EntityHuman player = ((CraftHumanEntity) this.player).getHandle();
    ItemStack itemstack = subcontainer.getItem(0);
    this.a = 1;
    int i = 0;
    byte b0 = 0;
    byte b1 = 0;
    if (itemstack.isEmpty()) {
        CraftEventFactory.callPrepareAnvilEvent(getBukkitView(), ItemStack.a);
        this.a = 0;
    } else {
        ItemStack itemstack1 = itemstack.cloneItemStack();
        if ((itemstack1.getCount() > 1) && (!player.abilities.canInstantlyBuild) && (!(itemstack1.getItem() instanceof ItemNameTag))) {
            itemstack1.setCount(1);
        }
        ItemStack itemstack2 = subcontainer.getItem(1);
        Map map = EnchantmentManager.a(itemstack1);
        int j = b0 + itemstack.getRepairCost() + (itemstack2.isEmpty() ? 0 : itemstack2.getRepairCost());
        xpCost = 0;
        if (!itemstack2.isEmpty()) {
            boolean flag = (itemstack2.getItem() == Items.ENCHANTED_BOOK) && (!Items.ENCHANTED_BOOK.h(itemstack2).isEmpty());
            if ((itemstack1.f()) && (itemstack1.getItem().a(itemstack, itemstack2))) {
                int k = Math.min(itemstack1.i(), itemstack1.k() / 4);
                if (k <= 0) {
                    CraftEventFactory.callPrepareAnvilEvent(getBukkitView(), ItemStack.a);
                    this.a = 0;
                    return;
                }
                for (int l = 0; (k > 0) && (l < itemstack2.getCount()); l++) {
                    int i1 = itemstack1.i() - k;
                    itemstack1.setData(i1);
                    i++;
                    k = Math.min(itemstack1.i(), itemstack1.k() / 4);
                    xpCost = l;
                }
            } else {
                if ((!flag) && ((itemstack1.getItem() != itemstack2.getItem()) || (!itemstack1.f()))) {
                    CraftEventFactory.callPrepareAnvilEvent(getBukkitView(), ItemStack.a);
                    this.a = 0;
                    return;
                }
                if ((itemstack1.f()) && (!flag)) {
                    int k = itemstack.k() - itemstack.i();
                    int l = itemstack2.k() - itemstack2.i();
                    int i1 = l + itemstack1.k() * 12 / 100;
                    int j1 = k + i1;
                    int k1 = itemstack1.k() - j1;
                    if (k1 < 0) {
                        k1 = 0;
                    }
                    if (k1 < itemstack1.getData()) {
                        itemstack1.setData(k1);
                        i += 2;
                    }
                }
                Map<Enchantment, Integer> map1 = EnchantmentManager.a(itemstack2);
                boolean flag1 = false;
                boolean flag2 = false;
                Iterator iterator = map1.keySet().iterator();
                while (iterator.hasNext()) {
                    Enchantment enchantment = (Enchantment) iterator.next();
                    if (enchantment != null) {
                        int l1 = map.containsKey(enchantment) ? ((Integer) map.get(enchantment)).intValue() : 0;
                        int i2 = ((Integer) map1.get(enchantment)).intValue();
                        i2 = l1 == i2 ? i2 + 1 : Math.max(i2, l1);
                        boolean flag3 = enchantment.canEnchant(itemstack);
                        if ((player.abilities.canInstantlyBuild) || (itemstack.getItem() == Items.ENCHANTED_BOOK)) {
                            flag3 = true;
                        }
                        Iterator iterator1 = map.keySet().iterator();
                        while (iterator1.hasNext()) {
                            Enchantment enchantment1 = (Enchantment) iterator1.next();
                            if ((enchantment1 != enchantment) && (!enchantment.c(enchantment1))) {
                                flag3 = false;
                                i++;
                            }
                        }
                        if (!flag3) {
                            flag2 = true;
                        } else {
                            flag1 = true;
                            if (i2 > enchantment.getMaxLevel()) {
                                i2 = enchantment.getMaxLevel();
                            }
                            map.put(enchantment, Integer.valueOf(i2));
                            int j2 = 0;
                            switch(enchantment.e()) {
                                case COMMON:
                                    j2 = 1;
                                    break;
                                case RARE:
                                    j2 = 2;
                                    break;
                                case UNCOMMON:
                                    j2 = 4;
                                    break;
                                case VERY_RARE:
                                    j2 = 8;
                            }
                            if (flag) {
                                j2 = Math.max(1, j2 / 2);
                            }
                            i += j2 * i2;
                        }
                    }
                }
                if ((flag2) && (!flag1)) {
                    CraftEventFactory.callPrepareAnvilEvent(getBukkitView(), ItemStack.a);
                    this.a = 0;
                    return;
                }
            }
        }
        if (StringUtils.isBlank(this.l)) {
            if (itemstack.hasName()) {
                b1 = 1;
                i += b1;
                itemstack1.s();
            }
        } else if (!this.l.equals(itemstack.getName())) {
            b1 = 1;
            i += b1;
            itemstack1.g(this.l);
        }
        this.a = (j + i);
        if (i <= 0) {
            itemstack1 = ItemStack.a;
        }
        if ((b1 == i) && (b1 > 0) && (this.a >= 40)) {
            this.a = 39;
        }
        if ((this.a >= 40) && (!player.abilities.canInstantlyBuild)) {
            itemstack1 = ItemStack.a;
        }
        if (!itemstack1.isEmpty()) {
            int k2 = itemstack1.getRepairCost();
            if ((!itemstack2.isEmpty()) && (k2 < itemstack2.getRepairCost())) {
                k2 = itemstack2.getRepairCost();
            }
            if ((b1 != i) || (b1 == 0)) {
                k2 = k2 * 2 + 1;
            }
            itemstack1.setRepairCost(k2);
            EnchantmentManager.a(map, itemstack1);
        }
        CraftEventFactory.callPrepareAnvilEvent(getBukkitView(), itemstack1);
        b();
    }
}
Also used : EntityHuman(net.minecraft.server.v1_11_R1.EntityHuman) ItemNameTag(net.minecraft.server.v1_11_R1.ItemNameTag) CraftHumanEntity(org.bukkit.craftbukkit.v1_11_R1.entity.CraftHumanEntity) Iterator(java.util.Iterator) ItemStack(net.minecraft.server.v1_11_R1.ItemStack) Enchantment(net.minecraft.server.v1_11_R1.Enchantment) Map(java.util.Map)

Example 2 with Enchantment

use of net.minecraft.server.v1_11_R1.Enchantment in project MyPet by xXKeyleXx.

the class IconMenuInventory method createItemStack.

protected ItemStack createItemStack(IconMenuItem icon) {
    ItemStack is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(icon.getMaterial(), icon.getAmount(), (short) icon.getData()));
    // TODO allow items like FIRE (51)
    if (is == null) {
        is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(Material.SAPLING));
    }
    NBTTagList emptyList = new NBTTagList();
    if (is.getTag() == null) {
        is.setTag(new NBTTagCompound());
    }
    if (icon.getBukkitMeta() != null) {
        try {
            applyToItemMethhod.invoke(icon.getBukkitMeta(), is.getTag());
        } catch (InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    // remove item attributes like attack damage
    is.getTag().set("AttributeModifiers", emptyList);
    // add enchantment glowing
    if (icon.isGlowing()) {
        TagCompound enchTag = new TagCompound();
        enchTag.put("id", new TagShort(2));
        enchTag.put("lvl", new TagShort(1));
        TagList enchList = new TagList();
        enchList.addTag(enchTag);
        is.getTag().set("ench", ItemStackNBTConverter.compoundToVanillaCompound(enchList));
        is.getTag().setInt("HideFlags", 1);
    } else {
        is.getTag().remove("ench");
    }
    // Prepare display tag
    NBTTagCompound display;
    if (is.getTag().hasKey("display")) {
        display = is.getTag().getCompound("display");
    } else {
        display = new NBTTagCompound();
        is.getTag().set("display", display);
    }
    // set Title
    if (!icon.getTitle().equals("")) {
        display.setString("Name", icon.getTitle());
    }
    if (icon.getLore().size() > 0) {
        // set Lore
        NBTTagList loreTag = new NBTTagList();
        display.set("Lore", loreTag);
        for (String loreLine : icon.getLore()) {
            loreTag.add(new NBTTagString(loreLine));
        }
    }
    if (icon.hasMeta()) {
        TagCompound tag = new TagCompound();
        icon.getMeta().applyTo(tag);
        NBTTagCompound vanillaTag = (NBTTagCompound) ItemStackNBTConverter.compoundToVanillaCompound(tag);
        for (String key : vanillaTag.c()) {
            is.getTag().set(key, vanillaTag.get(key));
        }
    }
    if (icon.getTags() != null) {
        NBTTagCompound vanillaTag = (NBTTagCompound) ItemStackNBTConverter.compoundToVanillaCompound(icon.getTags());
        for (String key : vanillaTag.c()) {
            is.getTag().set(key, vanillaTag.get(key));
        }
    }
    return is;
}
Also used : TagShort(de.keyle.knbt.TagShort) NBTTagCompound(net.minecraft.server.v1_11_R1.NBTTagCompound) NBTTagString(net.minecraft.server.v1_11_R1.NBTTagString) NBTTagCompound(net.minecraft.server.v1_11_R1.NBTTagCompound) TagCompound(de.keyle.knbt.TagCompound) InvocationTargetException(java.lang.reflect.InvocationTargetException) NBTTagList(net.minecraft.server.v1_11_R1.NBTTagList) NBTTagList(net.minecraft.server.v1_11_R1.NBTTagList) TagList(de.keyle.knbt.TagList) NBTTagString(net.minecraft.server.v1_11_R1.NBTTagString) CraftItemStack(org.bukkit.craftbukkit.v1_11_R1.inventory.CraftItemStack) ItemStack(net.minecraft.server.v1_11_R1.ItemStack)

Aggregations

ItemStack (net.minecraft.server.v1_11_R1.ItemStack)2 TagCompound (de.keyle.knbt.TagCompound)1 TagList (de.keyle.knbt.TagList)1 TagShort (de.keyle.knbt.TagShort)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Enchantment (net.minecraft.server.v1_11_R1.Enchantment)1 EntityHuman (net.minecraft.server.v1_11_R1.EntityHuman)1 ItemNameTag (net.minecraft.server.v1_11_R1.ItemNameTag)1 NBTTagCompound (net.minecraft.server.v1_11_R1.NBTTagCompound)1 NBTTagList (net.minecraft.server.v1_11_R1.NBTTagList)1 NBTTagString (net.minecraft.server.v1_11_R1.NBTTagString)1 CraftHumanEntity (org.bukkit.craftbukkit.v1_11_R1.entity.CraftHumanEntity)1 CraftItemStack (org.bukkit.craftbukkit.v1_11_R1.inventory.CraftItemStack)1