Search in sources :

Example 51 with TagList

use of de.keyle.knbt.TagList 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()));
    if (is == null) {
        is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(Material.STONE));
    }
    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();
        }
    }
    // add enchantment glowing
    if (icon.isGlowing()) {
        TagCompound enchTag = new TagCompound();
        enchTag.put("id", new TagString("minecraft:feather_falling"));
        enchTag.put("lvl", new TagShort(1));
        TagList enchList = new TagList();
        enchList.addTag(enchTag);
        is.getTag().set("Enchantments", ItemStackNBTConverter.compoundToVanillaCompound(enchList));
    } else {
        is.getTag().remove("Enchantments");
    }
    // hide item attributes like attack damage
    is.getTag().setInt("HideFlags", 63);
    // 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", "{\"text\":\"" + icon.getTitle() + "\"}");
    }
    if (icon.getLore().size() > 0) {
        // set Lore
        NBTTagList loreTag = new NBTTagList();
        display.set("Lore", loreTag);
        for (String loreLine : icon.getLore()) {
            IChatBaseComponent cm = CraftChatMessage.fromStringOrNull(loreLine);
            loreTag.add(NBTTagString.a(IChatBaseComponent.ChatSerializer.a(cm)));
        }
    }
    if (icon.hasMeta()) {
        TagCompound tag = new TagCompound();
        icon.getMeta().applyTo(tag);
        NBTTagCompound vanillaTag = (NBTTagCompound) ItemStackNBTConverter.compoundToVanillaCompound(tag);
        for (String key : vanillaTag.getKeys()) {
            is.getTag().set(key, vanillaTag.get(key));
        }
    }
    if (icon.getTags() != null) {
        NBTTagCompound vanillaTag = (NBTTagCompound) ItemStackNBTConverter.compoundToVanillaCompound(icon.getTags());
        for (String key : vanillaTag.getKeys()) {
            is.getTag().set(key, vanillaTag.get(key));
        }
    }
    return is;
}
Also used : TagShort(de.keyle.knbt.TagShort) TagString(de.keyle.knbt.TagString) TagString(de.keyle.knbt.TagString) TagCompound(de.keyle.knbt.TagCompound) InvocationTargetException(java.lang.reflect.InvocationTargetException) TagList(de.keyle.knbt.TagList) CraftItemStack(org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack)

Example 52 with TagList

use of de.keyle.knbt.TagList in project MyPet by xXKeyleXx.

the class CustomInventory method save.

public TagCompound save(TagCompound compound) {
    List<TagCompound> itemList = new ArrayList<>();
    for (int i = 0; i < this.items.size(); i++) {
        ItemStack itemStack = this.items.get(i);
        if (itemStack != null) {
            TagCompound item = ItemStackNBTConverter.itemStackToCompound(itemStack);
            item.getCompoundData().put("Slot", new TagByte((byte) i));
            itemList.add(item);
        }
    }
    compound.getCompoundData().put("Items", new TagList(itemList));
    return compound;
}
Also used : ArrayList(java.util.ArrayList) TagList(de.keyle.knbt.TagList) CraftItemStack(org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack) TagCompound(de.keyle.knbt.TagCompound) TagByte(de.keyle.knbt.TagByte)

Example 53 with TagList

use of de.keyle.knbt.TagList in project MyPet by xXKeyleXx.

the class MyWitherSkeleton method readExtendedInfo.

@SuppressWarnings("unchecked")
@Override
public void readExtendedInfo(TagCompound info) {
    if (info.containsKey("Equipment")) {
        TagList equipment = info.getAs("Equipment", TagList.class);
        List<TagBase> equipmentList = (List<TagBase>) equipment.getData();
        for (TagBase tag : equipmentList) {
            if (tag instanceof TagCompound) {
                TagCompound item = (TagCompound) tag;
                try {
                    ItemStack itemStack = MyPetApi.getPlatformHelper().compundToItemStack(item);
                    setEquipment(EquipmentSlot.getSlotById(item.getAs("Slot", TagInt.class).getIntData()), itemStack);
                } catch (Exception e) {
                    MyPetApi.getLogger().warning("Could not load Equipment item from pet data!");
                }
            }
        }
    }
}
Also used : TagBase(de.keyle.knbt.TagBase) TagInt(de.keyle.knbt.TagInt) TagList(de.keyle.knbt.TagList) ArrayList(java.util.ArrayList) List(java.util.List) TagList(de.keyle.knbt.TagList) ItemStack(org.bukkit.inventory.ItemStack) TagCompound(de.keyle.knbt.TagCompound)

Example 54 with TagList

use of de.keyle.knbt.TagList in project MyPet by xXKeyleXx.

the class MyStray method writeExtendedInfo.

@Override
public TagCompound writeExtendedInfo() {
    TagCompound info = super.writeExtendedInfo();
    List<TagCompound> itemList = new ArrayList<>();
    for (EquipmentSlot slot : EquipmentSlot.values()) {
        if (getEquipment(slot) != null) {
            TagCompound item = MyPetApi.getPlatformHelper().itemStackToCompund(getEquipment(slot));
            item.getCompoundData().put("Slot", new TagInt(slot.getSlotId()));
            itemList.add(item);
        }
    }
    info.getCompoundData().put("Equipment", new TagList(itemList));
    return info;
}
Also used : ArrayList(java.util.ArrayList) EquipmentSlot(de.Keyle.MyPet.api.entity.EquipmentSlot) TagInt(de.keyle.knbt.TagInt) TagList(de.keyle.knbt.TagList) TagCompound(de.keyle.knbt.TagCompound)

Example 55 with TagList

use of de.keyle.knbt.TagList in project MyPet by xXKeyleXx.

the class MyGiant method readExtendedInfo.

@SuppressWarnings("unchecked")
@Override
public void readExtendedInfo(TagCompound info) {
    if (info.containsKey("Equipment")) {
        TagList equipment = info.get("Equipment");
        List<TagBase> equipmentList = (List<TagBase>) equipment.getData();
        for (TagBase tag : equipmentList) {
            if (tag instanceof TagCompound) {
                TagCompound item = (TagCompound) tag;
                try {
                    ItemStack itemStack = MyPetApi.getPlatformHelper().compundToItemStack(item);
                    setEquipment(EquipmentSlot.getSlotById(item.getAs("Slot", TagInt.class).getIntData()), itemStack);
                } catch (Exception e) {
                    MyPetApi.getLogger().warning("Could not load Equipment item from pet data!");
                }
            }
        }
    }
}
Also used : TagBase(de.keyle.knbt.TagBase) TagInt(de.keyle.knbt.TagInt) TagList(de.keyle.knbt.TagList) ArrayList(java.util.ArrayList) List(java.util.List) TagList(de.keyle.knbt.TagList) ItemStack(org.bukkit.inventory.ItemStack) TagCompound(de.keyle.knbt.TagCompound)

Aggregations

TagCompound (de.keyle.knbt.TagCompound)57 TagList (de.keyle.knbt.TagList)57 ArrayList (java.util.ArrayList)26 TagInt (de.keyle.knbt.TagInt)19 ItemStack (org.bukkit.inventory.ItemStack)16 TagByte (de.keyle.knbt.TagByte)15 TagShort (de.keyle.knbt.TagShort)9 TagString (de.keyle.knbt.TagString)9 CompoundTag (net.minecraft.nbt.CompoundTag)8 ListTag (net.minecraft.nbt.ListTag)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 TagBase (de.keyle.knbt.TagBase)5 List (java.util.List)5 CraftItemStack (org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack)5 ItemStack (net.minecraft.world.item.ItemStack)4 CraftItemStack (org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack)3 CraftItemStack (org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack)3 Dynamic (com.mojang.serialization.Dynamic)2 EquipmentSlot (de.Keyle.MyPet.api.entity.EquipmentSlot)2 MyPetBaby (de.Keyle.MyPet.api.entity.MyPetBaby)2