Search in sources :

Example 1 with ByteArrayTag

use of net.minecraft.nbt.ByteArrayTag 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 2 with ByteArrayTag

use of net.minecraft.nbt.ByteArrayTag 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 3 with ByteArrayTag

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

the class NBTTranslator method setInternal.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static void setInternal(Tag base, byte type, DataView view, String key) {
    checkNotNull(base);
    checkNotNull(view);
    checkNotNull(key);
    checkArgument(!key.isEmpty());
    checkArgument(type > Constants.NBT.TAG_END && type <= Constants.NBT.TAG_INT_ARRAY);
    switch(type) {
        case Constants.NBT.TAG_BYTE:
            if (key.contains(NBTTranslator.BOOLEAN_IDENTIFIER)) {
                view.set(of(key.replace(NBTTranslator.BOOLEAN_IDENTIFIER, "")), (((ByteTag) base).getAsByte() != 0));
            } else {
                view.set(of(key), ((ByteTag) base).getAsByte());
            }
            break;
        case Constants.NBT.TAG_SHORT:
            view.set(of(key), ((ShortTag) base).getAsShort());
            break;
        case Constants.NBT.TAG_INT:
            view.set(of(key), ((IntTag) base).getAsInt());
            break;
        case Constants.NBT.TAG_LONG:
            view.set(of(key), ((LongTag) base).getAsLong());
            break;
        case Constants.NBT.TAG_FLOAT:
            view.set(of(key), ((FloatTag) base).getAsFloat());
            break;
        case Constants.NBT.TAG_DOUBLE:
            view.set(of(key), ((DoubleTag) base).getAsDouble());
            break;
        case Constants.NBT.TAG_BYTE_ARRAY:
            view.set(of(key), ((ByteArrayTag) base).getAsByteArray());
            break;
        case Constants.NBT.TAG_STRING:
            view.set(of(key), base.getAsString());
            break;
        case Constants.NBT.TAG_LIST:
            ListTag list = (ListTag) base;
            byte listType = list.getElementType();
            int count = list.size();
            List objectList = Lists.newArrayListWithCapacity(count);
            for (final Tag inbt : list) {
                objectList.add(NBTTranslator.fromTagBase(inbt, listType));
            }
            view.set(of(key), objectList);
            break;
        case Constants.NBT.TAG_COMPOUND:
            DataView internalView = view.createView(of(key));
            CompoundTag compound = (CompoundTag) base;
            for (String internalKey : compound.getAllKeys()) {
                Tag internalBase = compound.get(internalKey);
                byte internalType = internalBase.getId();
                // Basically.... more recursion.
                // Reasoning: This avoids creating a new DataContainer which would
                // then be copied in to the owning DataView anyways. We can internally
                // set the actual data directly to the child view instead.
                NBTTranslator.setInternal(internalBase, internalType, internalView, internalKey);
            }
            break;
        case Constants.NBT.TAG_INT_ARRAY:
            view.set(of(key), ((IntArrayTag) base).getAsIntArray());
            break;
        case Constants.NBT.TAG_LONG_ARRAY:
            view.set(of(key), ((LongArrayTag) base).getAsLongArray());
            break;
        default:
            throw new IllegalArgumentException("Unknown NBT type " + type);
    }
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) List(java.util.List) LongArrayTag(net.minecraft.nbt.LongArrayTag) 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) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 4 with ByteArrayTag

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

the class NBTTranslator method fromTagBase.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static Object fromTagBase(Tag base, byte type) {
    switch(type) {
        case Constants.NBT.TAG_BYTE:
            return ((ByteTag) base).getAsByte();
        case Constants.NBT.TAG_SHORT:
            return (((ShortTag) base)).getAsShort();
        case Constants.NBT.TAG_INT:
            return ((IntTag) base).getAsInt();
        case Constants.NBT.TAG_LONG:
            return ((LongTag) base).getAsLong();
        case Constants.NBT.TAG_FLOAT:
            return ((FloatTag) base).getAsFloat();
        case Constants.NBT.TAG_DOUBLE:
            return ((DoubleTag) base).getAsDouble();
        case Constants.NBT.TAG_BYTE_ARRAY:
            return ((ByteArrayTag) base).getAsByteArray();
        case Constants.NBT.TAG_STRING:
            return base.getAsString();
        case Constants.NBT.TAG_LIST:
            ListTag list = (ListTag) base;
            byte listType = list.getElementType();
            int count = list.size();
            List objectList = Lists.newArrayListWithCapacity(count);
            for (Tag inbt : list) {
                objectList.add(NBTTranslator.fromTagBase(inbt, listType));
            }
            return objectList;
        case Constants.NBT.TAG_COMPOUND:
            return NBTTranslator.getViewFromCompound((CompoundTag) base);
        case Constants.NBT.TAG_INT_ARRAY:
            return ((IntArrayTag) base).getAsIntArray();
        case Constants.NBT.TAG_LONG_ARRAY:
            return ((LongArrayTag) base).getAsLongArray();
        default:
            return null;
    }
}
Also used : IntArrayTag(net.minecraft.nbt.IntArrayTag) DoubleTag(net.minecraft.nbt.DoubleTag) ListTag(net.minecraft.nbt.ListTag) FloatTag(net.minecraft.nbt.FloatTag) ByteTag(net.minecraft.nbt.ByteTag) LongArrayTag(net.minecraft.nbt.LongArrayTag) List(java.util.List) LongArrayTag(net.minecraft.nbt.LongArrayTag) 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) IntTag(net.minecraft.nbt.IntTag) LongTag(net.minecraft.nbt.LongTag) ByteArrayTag(net.minecraft.nbt.ByteArrayTag)

Example 5 with ByteArrayTag

use of net.minecraft.nbt.ByteArrayTag 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)

Aggregations

ByteArrayTag (net.minecraft.nbt.ByteArrayTag)6 CompoundTag (net.minecraft.nbt.CompoundTag)6 IntArrayTag (net.minecraft.nbt.IntArrayTag)6 ListTag (net.minecraft.nbt.ListTag)6 TagCompound (de.keyle.knbt.TagCompound)4 TagList (de.keyle.knbt.TagList)4 TagString (de.keyle.knbt.TagString)4 List (java.util.List)4 ByteTag (net.minecraft.nbt.ByteTag)4 DoubleTag (net.minecraft.nbt.DoubleTag)4 FloatTag (net.minecraft.nbt.FloatTag)4 IntTag (net.minecraft.nbt.IntTag)4 LongTag (net.minecraft.nbt.LongTag)4 ShortTag (net.minecraft.nbt.ShortTag)4 StringTag (net.minecraft.nbt.StringTag)4 Tag (net.minecraft.nbt.Tag)4 TagBase (de.keyle.knbt.TagBase)2 TagByte (de.keyle.knbt.TagByte)2 TagByteArray (de.keyle.knbt.TagByteArray)2 TagDouble (de.keyle.knbt.TagDouble)2