diff options
author | Glease <4586901+Glease@users.noreply.github.com> | 2022-08-13 15:34:51 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-13 09:34:51 +0200 |
commit | 554d3f6a75433a90f07c751297c404adaaf12cdf (patch) | |
tree | 4bd601ab0528d1818ae94e1a63589a54c8cb6f68 /src/main/java/gregtech/loaders/misc | |
parent | bbd40c34e104dcead78290af1394e6f5867df565 (diff) | |
download | GT5-Unofficial-554d3f6a75433a90f07c751297c404adaaf12cdf.tar.gz GT5-Unofficial-554d3f6a75433a90f07c751297c404adaaf12cdf.tar.bz2 GT5-Unofficial-554d3f6a75433a90f07c751297c404adaaf12cdf.zip |
switch to gson (#1247)
* switch to gson
* json loader code cleanup
Diffstat (limited to 'src/main/java/gregtech/loaders/misc')
-rw-r--r-- | src/main/java/gregtech/loaders/misc/GT_JsonLoader.java | 96 |
1 files changed, 52 insertions, 44 deletions
diff --git a/src/main/java/gregtech/loaders/misc/GT_JsonLoader.java b/src/main/java/gregtech/loaders/misc/GT_JsonLoader.java index d46c7eae0b..aaa36389ad 100644 --- a/src/main/java/gregtech/loaders/misc/GT_JsonLoader.java +++ b/src/main/java/gregtech/loaders/misc/GT_JsonLoader.java @@ -1,10 +1,17 @@ package gregtech.loaders.misc; +import com.google.gson.Gson; +import com.google.gson.JsonObject; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; -import java.io.BufferedInputStream; -import java.io.ByteArrayOutputStream; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; import net.minecraft.client.Minecraft; import net.minecraft.client.resources.IReloadableResourceManager; @@ -12,53 +19,54 @@ import net.minecraft.client.resources.IResourceManager; import net.minecraft.client.resources.IResourceManagerReloadListener; import net.minecraft.util.ResourceLocation; -import org.json.JSONObject; - -import gregtech.api.util.GT_Log; +import static gregtech.GT_Mod.GT_FML_LOGGER; @SideOnly(Side.CLIENT) public class GT_JsonLoader implements IResourceManagerReloadListener { - private JSONObject json; - private ResourceLocation jsonLocation; - private IResourceManager resourceManager; + private static final Gson gson = new Gson(); + private JsonObject json; + private final ResourceLocation jsonLocation; + private final List<Consumer<GT_JsonLoader>> reloadListeners; - public String getString(String key) { - String s = ""; - try { - s = this.json.getString(key); - } - catch (Exception e) { - GT_Log.err.println("GT_JsonLoader" + e); - } - return s; - } + public String getString(String key) { + String s = ""; + try { + s = this.json.get(key).getAsString(); + } catch (Exception e) { + // stupid log4j + GT_FML_LOGGER.error("GT_JsonLoader " + jsonLocation + " read error", e); + } + return s; + } - public void loadJson() { - this.json = new JSONObject("{}"); - try { - BufferedInputStream bis = new BufferedInputStream(this.resourceManager.getResource(this.jsonLocation).getInputStream()); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - - for (int result = bis.read(); result != -1; result = bis.read()) { - bos.write((byte)result); - } + private void loadJson(IResourceManager rm) { + this.json = new JsonObject(); + try (Reader in = new BufferedReader(new InputStreamReader(rm.getResource(this.jsonLocation).getInputStream(), StandardCharsets.UTF_8))) { + json = gson.fromJson(in, JsonObject.class); + } catch (Exception e) { + // stupid log4j + GT_FML_LOGGER.error("GT_JsonLoader " + jsonLocation + " load error", e); + } + } - this.json = new JSONObject(bos.toString("UTF-8")); - } - catch (Exception e) { - GT_Log.err.println("GT_JsonLoader: " + e); - } - } + @Override + public void onResourceManagerReload(IResourceManager rm) { + loadJson(rm); + for (Consumer<GT_JsonLoader> listener : reloadListeners) { + listener.accept(this); + } + } - public void onResourceManagerReload(IResourceManager rm) { - loadJson(); - } + public void registerReloadListener(Consumer<GT_JsonLoader> listener) { + reloadListeners.add(listener); + } - public GT_JsonLoader (String resourcePath) { - GT_Log.err.println("GT_JsonLoader: Init"); - this.jsonLocation = new ResourceLocation("gregtech", resourcePath); - this.resourceManager = Minecraft.getMinecraft().getResourceManager(); - ((IReloadableResourceManager) Minecraft.getMinecraft().getResourceManager()).registerReloadListener(this); - loadJson(); - } -}
\ No newline at end of file + public GT_JsonLoader(String resourcePath) { + GT_FML_LOGGER.info("GT_JsonLoader: Init"); + this.jsonLocation = new ResourceLocation("gregtech", resourcePath); + reloadListeners = new ArrayList<>(); + IReloadableResourceManager rm = (IReloadableResourceManager) Minecraft.getMinecraft().getResourceManager(); + rm.registerReloadListener(this); + loadJson(rm); + } +} |