Search in sources :

Example 1 with NBTTagIntArray

use of net.minecraft.nbt.NBTTagIntArray in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class StorageLastRelay method readNBT.

@Override
public void readNBT(Capability<ICapabilityLastRelay> capability, ICapabilityLastRelay instance, EnumFacing side, NBTBase nbt) {
    NBTTagIntArray tag = (NBTTagIntArray) nbt;
    int[] backingArray = tag.getIntArray();
    //If all these values are 0, then assume the blockPos was just null anyways
    if (!(backingArray[0] == 0 && backingArray[1] == 0 && backingArray[2] == 0)) {
        BlockPos pos = new BlockPos(backingArray[0], backingArray[1], backingArray[2]);
        instance.setLastRelay(pos);
    }
}
Also used : BlockPos(net.minecraft.util.math.BlockPos) NBTTagIntArray(net.minecraft.nbt.NBTTagIntArray)

Example 2 with NBTTagIntArray

use of net.minecraft.nbt.NBTTagIntArray in project BetterQuesting by Funwayguy.

the class NBTConverter method JSONtoNBT_Element.

/**
 * Tries to interpret the tagID from the JsonElement's contents
 */
private static NBTBase JSONtoNBT_Element(JsonElement jObj, byte id, boolean format) {
    if (jObj == null) {
        return new NBTTagString();
    }
    byte tagID = id <= 0 ? fallbackTagID(jObj) : id;
    try {
        if (tagID >= 1 && tagID <= 6) {
            return instanceNumber(jObj.getAsNumber(), tagID);
        } else if (tagID == 8) {
            return new NBTTagString(jObj.getAsString());
        } else if (tagID == 10) {
            return JSONtoNBT_Object(jObj.getAsJsonObject(), new NBTTagCompound(), format);
        } else if (// Byte array
        tagID == 7) {
            JsonArray jAry = jObj.getAsJsonArray();
            byte[] bAry = new byte[jAry.size()];
            for (int i = 0; i < jAry.size(); i++) {
                bAry[i] = jAry.get(i).getAsByte();
            }
            return new NBTTagByteArray(bAry);
        } else if (tagID == 11) {
            JsonArray jAry = jObj.getAsJsonArray();
            int[] iAry = new int[jAry.size()];
            for (int i = 0; i < jAry.size(); i++) {
                iAry[i] = jAry.get(i).getAsInt();
            }
            return new NBTTagIntArray(iAry);
        } else if (tagID == 9) {
            NBTTagList tList = new NBTTagList();
            if (jObj.isJsonArray()) {
                JsonArray jAry = jObj.getAsJsonArray();
                for (int i = 0; i < jAry.size(); i++) {
                    JsonElement jElm = jAry.get(i);
                    tList.appendTag(JSONtoNBT_Element(jElm, (byte) 0, format));
                }
            } else if (jObj.isJsonObject()) {
                JsonObject jAry = jObj.getAsJsonObject();
                for (Entry<String, JsonElement> entry : jAry.entrySet()) {
                    try {
                        String[] s = entry.getKey().split(":");
                        byte id2 = Byte.parseByte(s[s.length - 1]);
                        // String key = entry.getKey().substring(0, entry.getKey().lastIndexOf(":" + id));
                        tList.appendTag(JSONtoNBT_Element(entry.getValue(), id2, format));
                    } catch (Exception e) {
                        tList.appendTag(JSONtoNBT_Element(entry.getValue(), (byte) 0, format));
                        continue;
                    }
                }
            }
            return tList;
        }
    } catch (Exception e) {
        QuestingAPI.getLogger().log(Level.ERROR, "An error occured while parsing JsonElement to NBTBase (" + tagID + "):", e);
    }
    QuestingAPI.getLogger().log(Level.WARN, "Unknown NBT representation for " + jObj.toString() + " (ID: " + tagID + ")");
    return new NBTTagString();
}
Also used : NBTTagCompound(net.minecraft.nbt.NBTTagCompound) JsonObject(com.google.gson.JsonObject) NBTTagString(net.minecraft.nbt.NBTTagString) NBTTagIntArray(net.minecraft.nbt.NBTTagIntArray) JsonArray(com.google.gson.JsonArray) NBTTagList(net.minecraft.nbt.NBTTagList) JsonElement(com.google.gson.JsonElement) NBTTagString(net.minecraft.nbt.NBTTagString) NBTTagByteArray(net.minecraft.nbt.NBTTagByteArray)

Example 3 with NBTTagIntArray

use of net.minecraft.nbt.NBTTagIntArray in project XNet by McJty.

the class ChunkBlob method writeToNBT.

public NBTTagCompound writeToNBT(NBTTagCompound compound) {
    compound.setInteger("lastBlob", lastBlobId);
    List<Integer> m = new ArrayList<>();
    for (Map.Entry<BlobId, Set<NetworkId>> entry : networkMappings.entrySet()) {
        m.add(entry.getKey().getId());
        for (NetworkId v : entry.getValue()) {
            m.add(v.getId());
        }
        m.add(-1);
    }
    NBTTagIntArray mappings = new NBTTagIntArray(m.stream().mapToInt(i -> i).toArray());
    compound.setTag("mappings", mappings);
    m.clear();
    for (Map.Entry<IntPos, BlobId> entry : blobAllocations.entrySet()) {
        m.add(entry.getKey().getPos());
        m.add(entry.getValue().getId());
    }
    NBTTagIntArray allocations = new NBTTagIntArray(m.stream().mapToInt(i -> i).toArray());
    compound.setTag("allocations", allocations);
    m.clear();
    for (Map.Entry<IntPos, NetworkId> entry : networkProviders.entrySet()) {
        m.add(entry.getKey().getPos());
        m.add(entry.getValue().getId());
    }
    NBTTagIntArray providers = new NBTTagIntArray(m.stream().mapToInt(i -> i).toArray());
    compound.setTag("providers", providers);
    m.clear();
    for (Map.Entry<IntPos, ConsumerId> entry : networkConsumers.entrySet()) {
        m.add(entry.getKey().getPos());
        m.add(entry.getValue().getId());
    }
    NBTTagIntArray consumers = new NBTTagIntArray(m.stream().mapToInt(i -> i).toArray());
    compound.setTag("consumers", consumers);
    m.clear();
    for (Map.Entry<BlobId, ColorId> entry : blobColors.entrySet()) {
        m.add(entry.getKey().getId());
        m.add(entry.getValue().getId());
    }
    NBTTagIntArray colors = new NBTTagIntArray(m.stream().mapToInt(i -> i).toArray());
    compound.setTag("colors", colors);
    return compound;
}
Also used : NetworkId(mcjty.xnet.api.keys.NetworkId) NBTTagIntArray(net.minecraft.nbt.NBTTagIntArray) ConsumerId(mcjty.xnet.api.keys.ConsumerId)

Example 4 with NBTTagIntArray

use of net.minecraft.nbt.NBTTagIntArray in project Random-Things by lumien231.

the class TileEntityRuneBase method writeDataToNBT.

@Override
public void writeDataToNBT(NBTTagCompound compound, boolean sync) {
    NBTTagList runeList = new NBTTagList();
    for (int i = 0; i < runeData.length; i++) {
        runeList.appendTag(new NBTTagIntArray(runeData[i]));
    }
    compound.setTag("runeData", runeList);
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) NBTTagIntArray(net.minecraft.nbt.NBTTagIntArray)

Example 5 with NBTTagIntArray

use of net.minecraft.nbt.NBTTagIntArray in project BuildCraft by BuildCraft.

the class JsonUtil method registerNbtSerializersDeserializers.

public static GsonBuilder registerNbtSerializersDeserializers(GsonBuilder gsonBuilder) {
    return gsonBuilder.registerTypeAdapterFactory(new TypeAdapterFactory() {

        @Override
        public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
            return type.getRawType() == NBTBase.class ? new TypeAdapter<T>() {

                @Override
                public void write(JsonWriter out, T value) throws IOException {
                    // noinspection unchecked, RedundantCast
                    Streams.write(((JsonSerializer<T>) (JsonSerializer<NBTBase>) (src, typeOfSrc, context) -> {
                        if (src == NBTUtilBC.NBT_NULL) {
                            return JsonNull.INSTANCE;
                        }
                        switch(src.getId()) {
                            case Constants.NBT.TAG_BYTE:
                                return context.serialize(src, NBTTagByte.class);
                            case Constants.NBT.TAG_SHORT:
                                return context.serialize(src, NBTTagShort.class);
                            case Constants.NBT.TAG_INT:
                                return context.serialize(src, NBTTagInt.class);
                            case Constants.NBT.TAG_LONG:
                                return context.serialize(src, NBTTagLong.class);
                            case Constants.NBT.TAG_FLOAT:
                                return context.serialize(src, NBTTagFloat.class);
                            case Constants.NBT.TAG_DOUBLE:
                                return context.serialize(src, NBTTagDouble.class);
                            case Constants.NBT.TAG_BYTE_ARRAY:
                                return context.serialize(src, NBTTagByteArray.class);
                            case Constants.NBT.TAG_STRING:
                                return context.serialize(src, NBTTagString.class);
                            case Constants.NBT.TAG_LIST:
                                return context.serialize(src, NBTTagList.class);
                            case Constants.NBT.TAG_COMPOUND:
                                return context.serialize(src, NBTTagCompound.class);
                            case Constants.NBT.TAG_INT_ARRAY:
                                return context.serialize(src, NBTTagIntArray.class);
                            default:
                                throw new IllegalArgumentException(src.toString());
                        }
                    }).serialize(value, type.getType(), new JsonSerializationContext() {

                        @Override
                        public JsonElement serialize(Object src) {
                            return gson.toJsonTree(src);
                        }

                        @Override
                        public JsonElement serialize(Object src, Type typeOfSrc) {
                            return gson.toJsonTree(src, typeOfSrc);
                        }
                    }), out);
                }

                @Override
                public T read(JsonReader in) throws IOException {
                    return ((JsonDeserializer<T>) (json, typeOfT, context) -> {
                        if (json.isJsonNull()) {
                            // noinspection unchecked
                            return (T) NBTUtilBC.NBT_NULL;
                        }
                        if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber()) {
                            Number number = json.getAsJsonPrimitive().getAsNumber();
                            if (number instanceof BigInteger || number instanceof Long || number instanceof Integer || number instanceof Short || number instanceof Byte) {
                                return context.deserialize(json, NBTTagLong.class);
                            } else {
                                return context.deserialize(json, NBTTagDouble.class);
                            }
                        }
                        if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isBoolean()) {
                            return context.deserialize(new JsonPrimitive(json.getAsJsonPrimitive().getAsBoolean() ? (byte) 1 : (byte) 0), NBTTagByte.class);
                        }
                        if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isString()) {
                            return context.deserialize(json, NBTTagString.class);
                        }
                        if (json.isJsonArray()) {
                            return context.deserialize(json, NBTTagList.class);
                        }
                        if (json.isJsonObject()) {
                            return context.deserialize(json, NBTTagCompound.class);
                        }
                        throw new IllegalArgumentException(json.toString());
                    }).deserialize(Streams.parse(in), type.getType(), gson::fromJson);
                }
            } : null;
        }
    }).registerTypeAdapter(NBTTagByte.class, (JsonSerializer<NBTTagByte>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getByte())).registerTypeAdapter(NBTTagByte.class, (JsonDeserializer<NBTTagByte>) (json, typeOfT, context) -> new NBTTagByte(json.getAsJsonPrimitive().getAsByte())).registerTypeAdapter(NBTTagShort.class, (JsonSerializer<NBTTagShort>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getShort())).registerTypeAdapter(NBTTagShort.class, (JsonDeserializer<NBTTagShort>) (json, typeOfT, context) -> new NBTTagShort(json.getAsJsonPrimitive().getAsShort())).registerTypeAdapter(NBTTagInt.class, (JsonSerializer<NBTTagInt>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getInt())).registerTypeAdapter(NBTTagInt.class, (JsonDeserializer<NBTTagInt>) (json, typeOfT, context) -> new NBTTagInt(json.getAsJsonPrimitive().getAsInt())).registerTypeAdapter(NBTTagLong.class, (JsonSerializer<NBTTagLong>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getLong())).registerTypeAdapter(NBTTagLong.class, (JsonDeserializer<NBTTagLong>) (json, typeOfT, context) -> new NBTTagLong(json.getAsJsonPrimitive().getAsLong())).registerTypeAdapter(NBTTagFloat.class, (JsonSerializer<NBTTagFloat>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getFloat())).registerTypeAdapter(NBTTagFloat.class, (JsonDeserializer<NBTTagFloat>) (json, typeOfT, context) -> new NBTTagFloat(json.getAsJsonPrimitive().getAsFloat())).registerTypeAdapter(NBTTagDouble.class, (JsonSerializer<NBTTagDouble>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getDouble())).registerTypeAdapter(NBTTagDouble.class, (JsonDeserializer<NBTTagDouble>) (json, typeOfT, context) -> new NBTTagDouble(json.getAsJsonPrimitive().getAsDouble())).registerTypeAdapter(NBTTagByteArray.class, (JsonSerializer<NBTTagByteArray>) (src, typeOfSrc, context) -> {
        JsonArray jsonArray = new JsonArray();
        for (byte element : src.getByteArray()) {
            jsonArray.add(new JsonPrimitive(element));
        }
        return jsonArray;
    }).registerTypeAdapter(NBTTagByteArray.class, (JsonDeserializer<NBTTagByteArray>) (json, typeOfT, context) -> new NBTTagByteArray(ArrayUtils.toPrimitive(StreamSupport.stream(json.getAsJsonArray().spliterator(), false).map(JsonElement::getAsByte).toArray(Byte[]::new)))).registerTypeAdapter(NBTTagString.class, (JsonSerializer<NBTTagString>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getString())).registerTypeAdapter(NBTTagString.class, (JsonDeserializer<NBTTagString>) (json, typeOfT, context) -> new NBTTagString(json.getAsJsonPrimitive().getAsString())).registerTypeAdapter(NBTTagList.class, (JsonSerializer<NBTTagList>) (src, typeOfSrc, context) -> {
        JsonArray jsonArray = new JsonArray();
        for (int i = 0; i < src.tagCount(); i++) {
            NBTBase element = src.get(i);
            jsonArray.add(context.serialize(element, NBTBase.class));
        }
        return jsonArray;
    }).registerTypeAdapter(NBTTagList.class, (JsonDeserializer<NBTTagList>) (json, typeOfT, context) -> {
        NBTTagList nbtTagList = new NBTTagList();
        StreamSupport.stream(json.getAsJsonArray().spliterator(), false).map(element -> context.<NBTBase>deserialize(element, NBTBase.class)).forEach(nbtTagList::appendTag);
        return nbtTagList;
    }).registerTypeAdapter(NBTTagCompound.class, (JsonSerializer<NBTTagCompound>) (src, typeOfSrc, context) -> {
        JsonObject jsonObject = new JsonObject();
        for (String key : src.getKeySet()) {
            jsonObject.add(key, context.serialize(src.getTag(key), NBTBase.class));
        }
        return jsonObject;
    }).registerTypeAdapter(NBTTagCompound.class, (JsonDeserializer<NBTTagCompound>) (json, typeOfT, context) -> {
        NBTTagCompound nbtTagCompound = new NBTTagCompound();
        for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
            nbtTagCompound.setTag(entry.getKey(), context.deserialize(entry.getValue(), NBTBase.class));
        }
        return nbtTagCompound;
    }).registerTypeAdapter(NBTTagIntArray.class, (JsonSerializer<NBTTagIntArray>) (src, typeOfSrc, context) -> {
        JsonArray jsonArray = new JsonArray();
        for (int element : src.getIntArray()) {
            jsonArray.add(new JsonPrimitive(element));
        }
        return jsonArray;
    }).registerTypeAdapter(NBTTagIntArray.class, (JsonDeserializer<NBTTagIntArray>) (json, typeOfT, context) -> new NBTTagIntArray(StreamSupport.stream(json.getAsJsonArray().spliterator(), false).mapToInt(JsonElement::getAsByte).toArray()));
}
Also used : JsonObject(com.google.gson.JsonObject) TypeToken(com.google.gson.reflect.TypeToken) NBTTagByte(net.minecraft.nbt.NBTTagByte) NBTTagByteArray(net.minecraft.nbt.NBTTagByteArray) Constants(net.minecraftforge.common.util.Constants) HashMap(java.util.HashMap) JsonSerializer(com.google.gson.JsonSerializer) ArrayUtils(org.apache.commons.lang3.ArrayUtils) NBTTagString(net.minecraft.nbt.NBTTagString) GsonBuilder(com.google.gson.GsonBuilder) TypeAdapter(com.google.gson.TypeAdapter) JsonReader(com.google.gson.stream.JsonReader) NBTTagFloat(net.minecraft.nbt.NBTTagFloat) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) NBTTagList(net.minecraft.nbt.NBTTagList) ImmutableList(com.google.common.collect.ImmutableList) Gson(com.google.gson.Gson) Map(java.util.Map) JsonSerializationContext(com.google.gson.JsonSerializationContext) BigInteger(java.math.BigInteger) StreamSupport(java.util.stream.StreamSupport) JsonPrimitive(com.google.gson.JsonPrimitive) JsonWriter(com.google.gson.stream.JsonWriter) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) NBTTagInt(net.minecraft.nbt.NBTTagInt) NBTTagDouble(net.minecraft.nbt.NBTTagDouble) NBTTagShort(net.minecraft.nbt.NBTTagShort) Streams(com.google.gson.internal.Streams) ImmutableMap(com.google.common.collect.ImmutableMap) JsonSyntaxException(com.google.gson.JsonSyntaxException) NBTBase(net.minecraft.nbt.NBTBase) NBTTagIntArray(net.minecraft.nbt.NBTTagIntArray) IOException(java.io.IOException) JsonArray(com.google.gson.JsonArray) Type(java.lang.reflect.Type) JsonNull(com.google.gson.JsonNull) Entry(java.util.Map.Entry) JsonDeserializer(com.google.gson.JsonDeserializer) NBTTagLong(net.minecraft.nbt.NBTTagLong) TypeAdapterFactory(com.google.gson.TypeAdapterFactory) JsonPrimitive(com.google.gson.JsonPrimitive) NBTTagByte(net.minecraft.nbt.NBTTagByte) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) JsonSerializer(com.google.gson.JsonSerializer) NBTTagString(net.minecraft.nbt.NBTTagString) JsonDeserializer(com.google.gson.JsonDeserializer) NBTTagIntArray(net.minecraft.nbt.NBTTagIntArray) NBTTagList(net.minecraft.nbt.NBTTagList) Entry(java.util.Map.Entry) NBTBase(net.minecraft.nbt.NBTBase) JsonReader(com.google.gson.stream.JsonReader) NBTTagString(net.minecraft.nbt.NBTTagString) NBTTagShort(net.minecraft.nbt.NBTTagShort) TypeAdapterFactory(com.google.gson.TypeAdapterFactory) NBTTagFloat(net.minecraft.nbt.NBTTagFloat) NBTTagInt(net.minecraft.nbt.NBTTagInt) JsonWriter(com.google.gson.stream.JsonWriter) BigInteger(java.math.BigInteger) JsonArray(com.google.gson.JsonArray) Type(java.lang.reflect.Type) NBTTagShort(net.minecraft.nbt.NBTTagShort) JsonElement(com.google.gson.JsonElement) JsonSerializationContext(com.google.gson.JsonSerializationContext) NBTTagByte(net.minecraft.nbt.NBTTagByte) TypeAdapter(com.google.gson.TypeAdapter) NBTTagLong(net.minecraft.nbt.NBTTagLong) BigInteger(java.math.BigInteger) JsonObject(com.google.gson.JsonObject) NBTTagByteArray(net.minecraft.nbt.NBTTagByteArray) NBTTagLong(net.minecraft.nbt.NBTTagLong) NBTTagDouble(net.minecraft.nbt.NBTTagDouble)

Aggregations

NBTTagIntArray (net.minecraft.nbt.NBTTagIntArray)20 NBTTagList (net.minecraft.nbt.NBTTagList)15 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)14 NBTTagString (net.minecraft.nbt.NBTTagString)11 NBTTagByteArray (net.minecraft.nbt.NBTTagByteArray)10 NBTTagDouble (net.minecraft.nbt.NBTTagDouble)9 NBTTagFloat (net.minecraft.nbt.NBTTagFloat)9 NBTTagByte (net.minecraft.nbt.NBTTagByte)8 NBTTagInt (net.minecraft.nbt.NBTTagInt)8 NBTTagLong (net.minecraft.nbt.NBTTagLong)8 NBTBase (net.minecraft.nbt.NBTBase)7 NBTTagShort (net.minecraft.nbt.NBTTagShort)7 JsonArray (com.google.gson.JsonArray)5 JsonObject (com.google.gson.JsonObject)5 Map (java.util.Map)5 JsonElement (com.google.gson.JsonElement)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 JsonPrimitive (com.google.gson.JsonPrimitive)2 TByteArrayList (gnu.trove.list.array.TByteArrayList)2