From 554d3f6a75433a90f07c751297c404adaaf12cdf Mon Sep 17 00:00:00 2001 From: Glease <4586901+Glease@users.noreply.github.com> Date: Sat, 13 Aug 2022 15:34:51 +0800 Subject: switch to gson (#1247) * switch to gson * json loader code cleanup --- dependencies.gradle | 3 - src/main/java/gregtech/GT_Mod.java | 7 +- src/main/java/gregtech/api/enums/GuiColors.java | 200 ++++++++++----------- src/main/java/gregtech/common/GT_Client.java | 15 +- .../java/gregtech/loaders/misc/GT_JsonLoader.java | 96 +++++----- 5 files changed, 159 insertions(+), 162 deletions(-) diff --git a/dependencies.gradle b/dependencies.gradle index 14631756e0..a51aa1a909 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -70,9 +70,6 @@ dependencies { compileOnly("com.google.auto.value:auto-value-annotations:1.8.2") { transitive = false } - compileOnly("org.json:json:20220320") { - transitive = false - } annotationProcessor("com.google.auto.value:auto-value:1.8.2") testImplementation(platform('org.junit:junit-bom:5.8.2')) diff --git a/src/main/java/gregtech/GT_Mod.java b/src/main/java/gregtech/GT_Mod.java index 72deff5953..f6120f4697 100644 --- a/src/main/java/gregtech/GT_Mod.java +++ b/src/main/java/gregtech/GT_Mod.java @@ -203,11 +203,6 @@ public class GT_Mod implements IGT_Mod { } } - if (FMLCommonHandler.instance().getSide() == Side.CLIENT) { - MinecraftForge.EVENT_BUS.register(new ExtraIcons()); - jsonGuiColors = new GT_JsonLoader("textures/guiColors.json"); - } - Configuration tMainConfig = GT_PreLoad.getConfiguration(aEvent.getModConfigurationDirectory()); GT_PreLoad.initCompat(); GT_PreLoad.createLogFiles(aEvent.getModConfigurationDirectory().getParentFile(), tMainConfig); @@ -465,7 +460,7 @@ public class GT_Mod implements IGT_Mod { GT_PostLoad.identifyAnySteam(); achievements = new GT_Achievements(); - + ReverseShapedRecipe.runReverseRecipes(); ReverseShapelessRecipe.runReverseRecipes(); diff --git a/src/main/java/gregtech/api/enums/GuiColors.java b/src/main/java/gregtech/api/enums/GuiColors.java index 3e7582bb7b..a78dee5cf3 100644 --- a/src/main/java/gregtech/api/enums/GuiColors.java +++ b/src/main/java/gregtech/api/enums/GuiColors.java @@ -1,110 +1,100 @@ package gregtech.api.enums; -import cpw.mods.fml.common.FMLCommonHandler; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; - import gregtech.api.util.GT_Log; -import gregtech.GT_Mod; - -public enum GuiColors { - //RGB Colors: Name and default value - oneByOne (0x404040), - twoByTwo (0x404040), - twoByTwoFluid (0x404040), - twoByTwoFluidInventory (0x404040), - threeByThree (0x404040), - fourByFour (0x404040), - basicMachine (0x404040), - basicTankTitle (0x404040), - basicTankInventory (0x404040), - basicTankLiquidAmount (0xFAFAFF), - basicTankLiquidValue (0xFAFAFF), - maintenanceHatch (0x404040), - maintenanceHatchRepair (0x404040), - - multiMachineTitle (0xFAFAFF), - multiMachineDrillBaseText (0xFAFAFF), - multiMachineIncompleteStructure (0xFAFAFF), - multiMachineLargeTurbineText (0xFAFAFF), - multiMachineMaintenanceText (0xFAFAFF), - multiMachineMalletRestart (0xFAFAFF), - multiMachineRunningPerfectly (0xFAFAFF), - - outputHatchTitle (0x404040), - outputHatchInventory (0x404040), - outputHatchAmount (0xFAFAFF), - outputHatchValue (0xFAFAFF), - outputHatchFluidName (0xFAFAFF), - outputHatchLockedFluid (0xFAFAFF), - - boiler (0x404040), - bronzeBlastFurnace (0x404040), - fusionReactor (0xFF0000), - industrialApiary (0x404040), - microwaveEnergyTransmitter (0xFAFAFF), - primitiveBlastFurnace (0x404040), - quantumChestTitle (0x404040), - quantumChestAmount (0xFAFAFF), - regulator (0xFAFAFF), - teleporter (0xFAFAFF), - - fluidDisplayStackRenderer (0xFFFFFF), - - //ARGB Colors: Name and default value - dialogSelectItem (0xFF555555), - pollutionRenderer (0xFFFFFFFF), - screenText (0xFF222222), - - coverArm (0xFF555555), - coverControlsWork (0xFF555555), - coverConveyor (0xFF555555), - coverDoesWork (0xFF555555), - coverEUMeter (0xFF555555), - coverFluidFilterName (0xFF222222), - coverFluidFilter (0xFF555555), - coverFluidRegulatorWarn (0xFFFF0000), - coverFluidRegulator (0xFF555555), - coverItemFilter (0xFF555555), - coverItemMeter (0xFF555555), - coverLiquidMeter (0xFF555555), - coverMaintenance (0xFF555555), - coverPlayerDetector (0xFF555555), - coverPump (0xFF555555), - coverRedstoneWirelessBase (0xFF555555), - coverShutter (0xFF555555), - - NEIText (0xFF000000) - ; - - private final String root; - private final int color; - - GuiColors() { - this.root = "GT5U.gui.color"; - this.color = 0x000000; - } - - GuiColors(final int hex) { - this.root = "GT5U.gui.color"; - this.color = hex; - } - - public int getColor() { - int color = this.color; - String hex = ""; - - if (FMLCommonHandler.instance().getSide() == Side.CLIENT && GT_Mod.jsonGuiColors != null) { - hex = GT_Mod.jsonGuiColors.getString(this.root + "." + this.toString()); - - try { - if (!hex.isEmpty()) {color = Integer.parseUnsignedInt(hex, 16);} - } - catch (final NumberFormatException e) { - GT_Log.err.println("Couldn't format color correctly for: " + this.root + " -> " + hex); - } + +import java.util.function.Function; + +public enum GuiColors { + //RGB Colors: Name and default value + oneByOne(0x404040), + twoByTwo(0x404040), + twoByTwoFluid(0x404040), + twoByTwoFluidInventory(0x404040), + threeByThree(0x404040), + fourByFour(0x404040), + basicMachine(0x404040), + basicTankTitle(0x404040), + basicTankInventory(0x404040), + basicTankLiquidAmount(0xFAFAFF), + basicTankLiquidValue(0xFAFAFF), + maintenanceHatch(0x404040), + maintenanceHatchRepair(0x404040), + + multiMachineTitle(0xFAFAFF), + multiMachineDrillBaseText(0xFAFAFF), + multiMachineIncompleteStructure(0xFAFAFF), + multiMachineLargeTurbineText(0xFAFAFF), + multiMachineMaintenanceText(0xFAFAFF), + multiMachineMalletRestart(0xFAFAFF), + multiMachineRunningPerfectly(0xFAFAFF), + + outputHatchTitle(0x404040), + outputHatchInventory(0x404040), + outputHatchAmount(0xFAFAFF), + outputHatchValue(0xFAFAFF), + outputHatchFluidName(0xFAFAFF), + outputHatchLockedFluid(0xFAFAFF), + + boiler(0x404040), + bronzeBlastFurnace(0x404040), + fusionReactor(0xFF0000), + industrialApiary(0x404040), + microwaveEnergyTransmitter(0xFAFAFF), + primitiveBlastFurnace(0x404040), + quantumChestTitle(0x404040), + quantumChestAmount(0xFAFAFF), + regulator(0xFAFAFF), + teleporter(0xFAFAFF), + + fluidDisplayStackRenderer(0xFFFFFF), + + //ARGB Colors: Name and default value + dialogSelectItem(0xFF555555), + pollutionRenderer(0xFFFFFFFF), + screenText(0xFF222222), + + coverArm(0xFF555555), + coverControlsWork(0xFF555555), + coverConveyor(0xFF555555), + coverDoesWork(0xFF555555), + coverEUMeter(0xFF555555), + coverFluidFilterName(0xFF222222), + coverFluidFilter(0xFF555555), + coverFluidRegulatorWarn(0xFFFF0000), + coverFluidRegulator(0xFF555555), + coverItemFilter(0xFF555555), + coverItemMeter(0xFF555555), + coverLiquidMeter(0xFF555555), + coverMaintenance(0xFF555555), + coverPlayerDetector(0xFF555555), + coverPump(0xFF555555), + coverRedstoneWirelessBase(0xFF555555), + coverShutter(0xFF555555), + + NEIText(0xFF000000); + + private final int defaultColor; + private int color; + + GuiColors(final int hex) { + this.defaultColor = hex; + } + + public void reload(Function l) { + color = this.defaultColor; + + String hex = l.apply("GT5U.gui.color." + this); + + try { + if (!hex.isEmpty()) { + color = Integer.parseUnsignedInt(hex, 16); + } + } catch (final NumberFormatException e) { + GT_Log.err.println("Couldn't format color correctly for: GT5U.gui.color." + this + " -> " + hex); } + } - return color; - } -} \ No newline at end of file + public int getColor() { + return color; + } +} diff --git a/src/main/java/gregtech/common/GT_Client.java b/src/main/java/gregtech/common/GT_Client.java index 2a37cbd142..0d2a76731b 100644 --- a/src/main/java/gregtech/common/GT_Client.java +++ b/src/main/java/gregtech/common/GT_Client.java @@ -19,10 +19,7 @@ import cpw.mods.fml.common.gameevent.TickEvent; import cpw.mods.fml.common.network.FMLNetworkEvent; import gregtech.GT_Mod; import gregtech.api.GregTech_API; -import gregtech.api.enums.GT_Values; -import gregtech.api.enums.ItemList; -import gregtech.api.enums.Materials; -import gregtech.api.enums.SoundResource; +import gregtech.api.enums.*; import gregtech.api.interfaces.IHasFluidDisplayItem; import gregtech.api.interfaces.tileentity.ICoverable; import gregtech.api.interfaces.tileentity.IGregTechTileEntity; @@ -41,6 +38,8 @@ import gregtech.common.entities.GT_Entity_Arrow; import gregtech.common.entities.GT_Entity_Arrow_Potion; import gregtech.common.net.MessageUpdateFluidDisplayItem; import gregtech.common.render.*; +import gregtech.loaders.ExtraIcons; +import gregtech.loaders.misc.GT_JsonLoader; import gregtech.loaders.preload.GT_PreLoad; import ic2.api.tile.IWrenchable; import net.minecraft.block.Block; @@ -57,6 +56,7 @@ import net.minecraft.util.MovingObjectPosition; import net.minecraft.world.ChunkCoordIntPair; import net.minecraft.world.World; import net.minecraftforge.client.event.DrawBlockHighlightEvent; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.oredict.OreDictionary; import org.lwjgl.opengl.GL11; @@ -399,6 +399,13 @@ public class GT_Client extends GT_Proxy @Override public void onPreLoad() { super.onPreLoad(); + + MinecraftForge.EVENT_BUS.register(new ExtraIcons()); + GT_Mod.jsonGuiColors = new GT_JsonLoader("textures/guiColors.json"); + for (GuiColors value : GuiColors.values()) { + GT_Mod.jsonGuiColors.registerReloadListener(l -> value.reload(l::getString)); + } + final String[] arr = { "renadi", "hanakocz", "MysteryDump", "Flaver4", "x_Fame", "Peluche321", "Goshen_Ithilien", "manf", "Bimgo", "leagris", "IAmMinecrafter02", "Cerous", "Devilin_Pixy", "Bkarlsson87", "BadAlchemy", "CaballoCraft", "melanclock", "Resursator", "demanzke", "AndrewAmmerlaan", 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> 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 listener : reloadListeners) { + listener.accept(this); + } + } - public void onResourceManagerReload(IResourceManager rm) { - loadJson(); - } + public void registerReloadListener(Consumer 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); + } +} -- cgit