From 65e5ee4b431cc7513e53d61827249148b06de79a Mon Sep 17 00:00:00 2001 From: Cow Date: Tue, 6 Jul 2021 20:24:21 +0200 Subject: Added 'lowest BINs' support to Chest Tracker & Analyzer --- .../de/cowtipper/cowlection/util/GsonUtils.java | 31 ++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'src/main/java/de/cowtipper/cowlection/util/GsonUtils.java') 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 { + @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 entry : lowestBins.entrySet()) { + try { + lowestBinsCache.put(entry.getKey(), entry.getValue().getAsInt()); + } catch (ClassCastException | NumberFormatException ignored) { + // somehow not an integer + } + } + return lowestBinsCache; + } + } } -- cgit