Search in sources :

Example 6 with ListTag

use of net.minecraft.nbt.ListTag in project Denizen-For-Bukkit by DenizenScript.

the class ImprovedOfflinePlayerImpl method setInventory.

@Override
public void setInventory(org.bukkit.inventory.PlayerInventory inventory) {
    CraftInventoryPlayer inv = (CraftInventoryPlayer) inventory;
    net.minecraft.nbt.CompoundTag nbtTagCompound = ((CompoundTagImpl) compound).toNMSTag();
    nbtTagCompound.put("Inventory", inv.getInventory().save(new ListTag()));
    this.compound = CompoundTagImpl.fromNMSTag(nbtTagCompound);
    if (this.autosave) {
        savePlayerData();
    }
}
Also used : CraftInventoryPlayer(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftInventoryPlayer) ListTag(net.minecraft.nbt.ListTag) CompoundTagImpl(com.denizenscript.denizen.nms.v1_18.impl.jnbt.CompoundTagImpl)

Example 7 with ListTag

use of net.minecraft.nbt.ListTag in project MyPet by xXKeyleXx.

the class ItemStackNBTConverter method compoundToVanillaCompound.

public static Tag compoundToVanillaCompound(TagBase tag) {
    switch(TagType.getTypeById(tag.getTagTypeId())) {
        case Int:
            return IntTag.valueOf(((TagInt) tag).getIntData());
        case Short:
            return ShortTag.valueOf(((TagShort) tag).getShortData());
        case String:
            return StringTag.valueOf(((TagString) tag).getStringData());
        case Byte:
            return ByteTag.valueOf(((TagByte) tag).getByteData());
        case Byte_Array:
            return new ByteArrayTag(((TagByteArray) tag).getByteArrayData());
        case Double:
            return DoubleTag.valueOf(((TagDouble) tag).getDoubleData());
        case Float:
            return FloatTag.valueOf(((TagFloat) tag).getFloatData());
        case Int_Array:
            return new IntArrayTag(((TagIntArray) tag).getIntArrayData());
        case Long:
            return LongTag.valueOf(((TagLong) tag).getLongData());
        case List:
            TagList TagList = (TagList) tag;
            ListTag tagList = new ListTag();
            for (TagBase tagInList : TagList.getReadOnlyList()) {
                tagList.add(compoundToVanillaCompound(tagInList));
            }
            return tagList;
        case Compound:
            TagCompound TagCompound = (TagCompound) tag;
            CompoundTag tagCompound = new CompoundTag();
            for (String name : TagCompound.getCompoundData().keySet()) {
                tagCompound.put(name, compoundToVanillaCompound(TagCompound.getCompoundData().get(name)));
            }
            return tagCompound;
        case End:
            return null;
    }
    throw new IllegalArgumentException("Not a valid tag type");
}
Also used : IntArrayTag(net.minecraft.nbt.IntArrayTag) TagBase(de.keyle.knbt.TagBase) TagList(de.keyle.knbt.TagList) TagString(de.keyle.knbt.TagString) ListTag(net.minecraft.nbt.ListTag) TagCompound(de.keyle.knbt.TagCompound) CompoundTag(net.minecraft.nbt.CompoundTag) ByteArrayTag(net.minecraft.nbt.ByteArrayTag)

Example 8 with ListTag

use of net.minecraft.nbt.ListTag in project MyPet by xXKeyleXx.

the class ItemStackNBTConverter method vanillaCompoundToCompound.

public static TagBase vanillaCompoundToCompound(Tag vanillaTag) {
    switch(vanillaTag.getId()) {
        case 1:
            return new TagByte(((ByteTag) vanillaTag).getAsByte());
        case 2:
            return new TagShort(((ShortTag) vanillaTag).getAsShort());
        case 3:
            return new TagInt(((IntTag) vanillaTag).getAsInt());
        case 4:
            return new TagLong(((LongTag) vanillaTag).getAsLong());
        case 5:
            return new TagFloat(((FloatTag) vanillaTag).getAsFloat());
        case 6:
            return new TagDouble(((DoubleTag) vanillaTag).getAsDouble());
        case 7:
            return new TagByteArray(((ByteArrayTag) vanillaTag).getAsByteArray());
        case 8:
            return new TagString(vanillaTag.getAsString());
        case 9:
            ListTag tagList = (ListTag) vanillaTag;
            List compoundList = new ArrayList();
            try {
                ArrayList list = (ArrayList) TAG_LIST_LIST.get(tagList);
                for (Object aList : list) {
                    compoundList.add(vanillaCompoundToCompound((Tag) aList));
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return new TagList(compoundList);
        case 10:
            TagCompound compound = new TagCompound();
            CompoundTag tagCompound = ((CompoundTag) vanillaTag);
            Set<String> keys = tagCompound.getAllKeys();
            for (String tagName : keys) {
                compound.getCompoundData().put(tagName, vanillaCompoundToCompound(tagCompound.get(tagName)));
            }
            return compound;
        case 11:
            return new TagIntArray(((IntArrayTag) vanillaTag).getAsIntArray());
    }
    return null;
}
Also used : TagIntArray(de.keyle.knbt.TagIntArray) TagShort(de.keyle.knbt.TagShort) TagInt(de.keyle.knbt.TagInt) ArrayList(java.util.ArrayList) TagString(de.keyle.knbt.TagString) TagString(de.keyle.knbt.TagString) TagFloat(de.keyle.knbt.TagFloat) ListTag(net.minecraft.nbt.ListTag) TagCompound(de.keyle.knbt.TagCompound) TagDouble(de.keyle.knbt.TagDouble) TagByteArray(de.keyle.knbt.TagByteArray) TagLong(de.keyle.knbt.TagLong) TagList(de.keyle.knbt.TagList) ArrayList(java.util.ArrayList) TagList(de.keyle.knbt.TagList) List(java.util.List) DoubleTag(net.minecraft.nbt.DoubleTag) Tag(net.minecraft.nbt.Tag) StringTag(net.minecraft.nbt.StringTag) ByteTag(net.minecraft.nbt.ByteTag) IntTag(net.minecraft.nbt.IntTag) IntArrayTag(net.minecraft.nbt.IntArrayTag) FloatTag(net.minecraft.nbt.FloatTag) ByteArrayTag(net.minecraft.nbt.ByteArrayTag) LongTag(net.minecraft.nbt.LongTag) ShortTag(net.minecraft.nbt.ShortTag) CompoundTag(net.minecraft.nbt.CompoundTag) ListTag(net.minecraft.nbt.ListTag) TagByte(de.keyle.knbt.TagByte) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 9 with ListTag

use of net.minecraft.nbt.ListTag in project MyPet by xXKeyleXx.

the class IconMenuInventory method createItemStack.

protected ItemStack createItemStack(IconMenuItem icon) {
    // TODO Check if this works properly
    ItemStack is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(icon.getMaterial(), icon.getAmount()));
    if (is == null) {
        is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(Material.STONE));
    }
    if (is.getTag() == null) {
        is.setTag(new CompoundTag());
    }
    if (icon.getBukkitMeta() != null) {
        try {
            applyToItemMethod.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().put("Enchantments", ItemStackNBTConverter.compoundToVanillaCompound(enchList));
    } else {
        is.getTag().remove("Enchantments");
    }
    // hide item attributes like attack damage
    is.getTag().putInt("HideFlags", 63);
    // Prepare display tag
    CompoundTag display;
    if (is.getTag().contains("display")) {
        display = is.getTag().getCompound("display");
    } else {
        display = new CompoundTag();
        is.getTag().put("display", display);
    }
    // set Title
    if (!icon.getTitle().equals("")) {
        display.putString("Name", "{\"text\":\"" + icon.getTitle() + "\"}");
    }
    if (icon.getLore().size() > 0) {
        // set Lore
        ListTag loreTag = new ListTag();
        display.put("Lore", loreTag);
        for (String loreLine : icon.getLore()) {
            Component cm = CraftChatMessage.fromStringOrNull(loreLine);
            loreTag.add(StringTag.valueOf(Component.Serializer.toJson(cm)));
        }
    }
    if (icon.hasMeta()) {
        TagCompound tag = new TagCompound();
        icon.getMeta().applyTo(tag);
        CompoundTag vanillaTag = (CompoundTag) ItemStackNBTConverter.compoundToVanillaCompound(tag);
        for (String key : vanillaTag.getAllKeys()) {
            is.getTag().put(key, vanillaTag.get(key));
        }
    }
    if (icon.getTags() != null) {
        CompoundTag vanillaTag = (CompoundTag) ItemStackNBTConverter.compoundToVanillaCompound(icon.getTags());
        for (String key : vanillaTag.getAllKeys()) {
            is.getTag().put(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) ListTag(net.minecraft.nbt.ListTag) InvocationTargetException(java.lang.reflect.InvocationTargetException) TagList(de.keyle.knbt.TagList) ItemStack(net.minecraft.world.item.ItemStack) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack) Component(net.minecraft.network.chat.Component) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 10 with ListTag

use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.

the class BookItemStackData method set.

private static <V> boolean set(final ItemStack holder, final List<V> value, final Function<V, String> predicate) {
    final ListTag list = value.stream().map(predicate).collect(NBTCollectors.toStringTagList());
    holder.addTagElement(Constants.Item.Book.ITEM_BOOK_PAGES, list);
    final CompoundTag compound = holder.getOrCreateTag();
    if (!compound.contains(Constants.Item.Book.ITEM_BOOK_TITLE)) {
        compound.putString(Constants.Item.Book.ITEM_BOOK_TITLE, Constants.Item.Book.INVALID_TITLE);
    }
    if (!compound.contains(Constants.Item.Book.ITEM_BOOK_AUTHOR)) {
        compound.putString(Constants.Item.Book.ITEM_BOOK_AUTHOR, Constants.Item.Book.INVALID_TITLE);
    }
    compound.putBoolean(Constants.Item.Book.ITEM_BOOK_RESOLVED, true);
    return false;
}
Also used : ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

ListTag (net.minecraft.nbt.ListTag)52 CompoundTag (net.minecraft.nbt.CompoundTag)38 ItemStack (net.minecraft.world.item.ItemStack)10 TagCompound (de.keyle.knbt.TagCompound)8 TagList (de.keyle.knbt.TagList)8 List (java.util.List)7 IntArrayTag (net.minecraft.nbt.IntArrayTag)7 ResourceLocation (net.minecraft.resources.ResourceLocation)7 TagString (de.keyle.knbt.TagString)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)5 StringTag (net.minecraft.nbt.StringTag)5 ByteArrayTag (net.minecraft.nbt.ByteArrayTag)4 IntTag (net.minecraft.nbt.IntTag)4 Tag (net.minecraft.nbt.Tag)4 ByteTag (net.minecraft.nbt.ByteTag)3 DoubleTag (net.minecraft.nbt.DoubleTag)3 FloatTag (net.minecraft.nbt.FloatTag)3 LongTag (net.minecraft.nbt.LongTag)3 ShortTag (net.minecraft.nbt.ShortTag)3