Search in sources :

Example 1 with LongArrayTag

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

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

Aggregations

List (java.util.List)2 ByteArrayTag (net.minecraft.nbt.ByteArrayTag)2 ByteTag (net.minecraft.nbt.ByteTag)2 CompoundTag (net.minecraft.nbt.CompoundTag)2 DoubleTag (net.minecraft.nbt.DoubleTag)2 FloatTag (net.minecraft.nbt.FloatTag)2 IntArrayTag (net.minecraft.nbt.IntArrayTag)2 IntTag (net.minecraft.nbt.IntTag)2 ListTag (net.minecraft.nbt.ListTag)2 LongArrayTag (net.minecraft.nbt.LongArrayTag)2 LongTag (net.minecraft.nbt.LongTag)2 ShortTag (net.minecraft.nbt.ShortTag)2 StringTag (net.minecraft.nbt.StringTag)2 Tag (net.minecraft.nbt.Tag)2 DataView (org.spongepowered.api.data.persistence.DataView)1