Search in sources :

Example 1 with NBTPrimitive

use of net.minecraft.nbt.NBTBase.NBTPrimitive in project PneumaticCraft by MineMaarten.

the class NBTToJsonConverter method getObject.

private JsonObject getObject(NBTTagCompound tag) {
    Set<String> keys = tag.func_150296_c();
    JsonObject jsonRoot = new JsonObject();
    for (String key : keys) {
        JsonObject keyObject = new JsonObject();
        jsonRoot.add(key, keyObject);
        NBTBase nbt = tag.getTag(key);
        keyObject.addProperty("type", nbt.getId());
        if (nbt instanceof NBTTagCompound) {
            keyObject.add("value", getObject((NBTTagCompound) nbt));
        } else if (nbt instanceof NBTPrimitive) {
            keyObject.addProperty("value", ((NBTPrimitive) nbt).func_150286_g());
        } else if (nbt instanceof NBTTagString) {
            keyObject.addProperty("value", ((NBTTagString) nbt).func_150285_a_());
        } else if (nbt instanceof NBTTagList) {
            JsonArray array = new JsonArray();
            NBTTagList tagList = (NBTTagList) nbt;
            for (int i = 0; i < tagList.tagCount(); i++) {
                array.add(getObject(tagList.getCompoundTagAt(i)));
            }
            keyObject.add("value", array);
        } else if (nbt instanceof NBTTagIntArray) {
            JsonArray array = new JsonArray();
            NBTTagIntArray intArray = (NBTTagIntArray) nbt;
            for (int i : intArray.func_150302_c()) {
                array.add(new JsonPrimitive(i));
            }
            keyObject.add("value", array);
        } else {
            throw new IllegalArgumentException("NBT to JSON converter doesn't support the nbt tag: " + NBTBase.NBTTypes[nbt.getId()] + ", tag: " + nbt);
        }
    }
    return jsonRoot;
}
Also used : NBTPrimitive(net.minecraft.nbt.NBTBase.NBTPrimitive) JsonPrimitive(com.google.gson.JsonPrimitive) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) JsonObject(com.google.gson.JsonObject) NBTTagString(net.minecraft.nbt.NBTTagString) NBTTagIntArray(net.minecraft.nbt.NBTTagIntArray) NBTTagList(net.minecraft.nbt.NBTTagList) JsonArray(com.google.gson.JsonArray) NBTBase(net.minecraft.nbt.NBTBase) NBTTagString(net.minecraft.nbt.NBTTagString)

Aggregations

JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 NBTBase (net.minecraft.nbt.NBTBase)1 NBTPrimitive (net.minecraft.nbt.NBTBase.NBTPrimitive)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 NBTTagIntArray (net.minecraft.nbt.NBTTagIntArray)1 NBTTagList (net.minecraft.nbt.NBTTagList)1 NBTTagString (net.minecraft.nbt.NBTTagString)1