diff options
author | Cow <cow@volloeko.de> | 2021-07-06 20:24:21 +0200 |
---|---|---|
committer | Cow <cow@volloeko.de> | 2021-07-06 20:24:21 +0200 |
commit | 65e5ee4b431cc7513e53d61827249148b06de79a (patch) | |
tree | c55e9c7ebd0c7e37a7a81de0df10b79f67e39930 /src/main/java/de/cowtipper/cowlection/util/GsonUtils.java | |
parent | c0c7011643a6ffa7c549ad4eff662309644b244c (diff) | |
download | Cowlection-65e5ee4b431cc7513e53d61827249148b06de79a.tar.gz Cowlection-65e5ee4b431cc7513e53d61827249148b06de79a.tar.bz2 Cowlection-65e5ee4b431cc7513e53d61827249148b06de79a.zip |
Added 'lowest BINs' support to Chest Tracker & Analyzer
Diffstat (limited to 'src/main/java/de/cowtipper/cowlection/util/GsonUtils.java')
-rw-r--r-- | src/main/java/de/cowtipper/cowlection/util/GsonUtils.java | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/src/main/java/de/cowtipper/cowlection/util/GsonUtils.java b/src/main/java/de/cowtipper/cowlection/util/GsonUtils.java index e97a88d..d2d3e28 100644 --- a/src/main/java/de/cowtipper/cowlection/util/GsonUtils.java +++ b/src/main/java/de/cowtipper/cowlection/util/GsonUtils.java @@ -2,6 +2,7 @@ package de.cowtipper.cowlection.util; import com.google.gson.*; import com.mojang.util.UUIDTypeAdapter; +import de.cowtipper.cowlection.chesttracker.LowestBinsCache; import de.cowtipper.cowlection.data.HyPlayerData; import net.minecraft.nbt.*; import net.minecraftforge.common.util.Constants; @@ -19,8 +20,14 @@ import java.util.TreeMap; import java.util.UUID; public final class GsonUtils { - private static final Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).registerTypeAdapter(HyPlayerData.class, new HyPlayerDataDeserializer()).create(); - private static final Gson gsonPrettyPrinter = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).setPrettyPrinting().create(); + private static final Gson gson = new GsonBuilder() + .registerTypeAdapter(UUID.class, new UUIDTypeAdapter()) + .registerTypeAdapter(HyPlayerData.class, new HyPlayerDataDeserializer()) + .registerTypeAdapter(LowestBinsCache.class, new LowestBinsDeserializer()) + .create(); + private static final Gson gsonPrettyPrinter = new GsonBuilder() + .registerTypeAdapter(UUID.class, new UUIDTypeAdapter()) + .setPrettyPrinting().create(); private GsonUtils() { } @@ -168,4 +175,24 @@ public final class GsonUtils { return hyPlayerData; } } + + public static class LowestBinsDeserializer implements JsonDeserializer<LowestBinsCache> { + @Override + public LowestBinsCache deserialize(JsonElement json, Type type, JsonDeserializationContext jdc) throws JsonParseException { + LowestBinsCache lowestBinsCache = new LowestBinsCache(); + if (!json.isJsonObject()) { + // invalid JSON + return lowestBinsCache; + } + JsonObject lowestBins = json.getAsJsonObject(); + for (Map.Entry<String, JsonElement> entry : lowestBins.entrySet()) { + try { + lowestBinsCache.put(entry.getKey(), entry.getValue().getAsInt()); + } catch (ClassCastException | NumberFormatException ignored) { + // somehow not an integer + } + } + return lowestBinsCache; + } + } } |