diff options
author | Cow <cow@volloeko.de> | 2020-07-20 01:05:44 +0200 |
---|---|---|
committer | Cow <cow@volloeko.de> | 2020-07-20 01:05:44 +0200 |
commit | fb12a4a5cd257c78ea802107babc2a3dff9ef927 (patch) | |
tree | 371c47953b486d4a29290f4016de22318a3a0515 /src/main/java/eu/olli/cowlection/util | |
parent | 606722d21d96eaa22a55302014415f2c7c19d9c6 (diff) | |
download | Cowlection-fb12a4a5cd257c78ea802107babc2a3dff9ef927.tar.gz Cowlection-fb12a4a5cd257c78ea802107babc2a3dff9ef927.tar.bz2 Cowlection-fb12a4a5cd257c78ea802107babc2a3dff9ef927.zip |
Copy inventories as JSON
Diffstat (limited to 'src/main/java/eu/olli/cowlection/util')
-rw-r--r-- | src/main/java/eu/olli/cowlection/util/GsonUtils.java | 54 |
1 files changed, 51 insertions, 3 deletions
diff --git a/src/main/java/eu/olli/cowlection/util/GsonUtils.java b/src/main/java/eu/olli/cowlection/util/GsonUtils.java index ece3faf..e0f1a64 100644 --- a/src/main/java/eu/olli/cowlection/util/GsonUtils.java +++ b/src/main/java/eu/olli/cowlection/util/GsonUtils.java @@ -1,8 +1,12 @@ package eu.olli.cowlection.util; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; +import com.google.gson.*; import com.mojang.util.UUIDTypeAdapter; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.nbt.NBTTagString; +import net.minecraftforge.common.util.Constants; import java.io.Reader; import java.lang.reflect.Type; @@ -10,6 +14,7 @@ import java.util.UUID; public final class GsonUtils { private static final Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create(); + private static final Gson gsonPrettyPrinter = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).setPrettyPrinting().create(); private GsonUtils() { } @@ -23,6 +28,49 @@ public final class GsonUtils { } public static String toJson(Object object) { - return gson.toJson(object); + if (object instanceof NBTBase) { + return gsonPrettyPrinter.toJson(nbtToJson((NBTBase) object)); + } else { + return gson.toJson(object); + } + } + + private static JsonElement nbtToJson(NBTBase nbtElement) { + if (nbtElement instanceof NBTBase.NBTPrimitive) { + NBTBase.NBTPrimitive nbtNumber = (NBTBase.NBTPrimitive) nbtElement; + switch (nbtNumber.getId()) { + case Constants.NBT.TAG_BYTE: + return new JsonPrimitive(nbtNumber.getByte()); + case Constants.NBT.TAG_SHORT: + return new JsonPrimitive(nbtNumber.getShort()); + case Constants.NBT.TAG_INT: + return new JsonPrimitive(nbtNumber.getInt()); + case Constants.NBT.TAG_LONG: + return new JsonPrimitive(nbtNumber.getLong()); + case Constants.NBT.TAG_FLOAT: + return new JsonPrimitive(nbtNumber.getFloat()); + case Constants.NBT.TAG_DOUBLE: + return new JsonPrimitive(nbtNumber.getDouble()); + default: + return new JsonObject(); + } + } else if (nbtElement instanceof NBTTagString) { + return new JsonPrimitive(((NBTTagString) nbtElement).getString()); + } else if (nbtElement instanceof NBTTagList) { + NBTTagList nbtList = (NBTTagList) nbtElement; + JsonArray jsonArray = new JsonArray(); + for (int tagId = 0; tagId < nbtList.tagCount(); tagId++) { + jsonArray.add(nbtToJson(nbtList.get(tagId))); + } + return jsonArray; + } else if (nbtElement instanceof NBTTagCompound) { + NBTTagCompound nbtCompound = (NBTTagCompound) nbtElement; + JsonObject jsonObject = new JsonObject(); + for (String nbtEntry : nbtCompound.getKeySet()) { + jsonObject.add(nbtEntry, nbtToJson(nbtCompound.getTag(nbtEntry))); + } + return jsonObject; + } + return new JsonObject(); } } |