diff options
author | Connor-Colenso <52056774+Connor-Colenso@users.noreply.github.com> | 2022-09-11 01:59:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-11 01:59:07 +0100 |
commit | fe595d51866c8b43fb07b2d8aa3d76ffcbfbc48e (patch) | |
tree | d2ad01823fff33408beb772f75ae24a8ad71d6ac /src/main/java/gregtech/common/misc/GlobalEnergyWorldSavedData.java | |
parent | 930d4392b82129a413fd23e700a94052ad3e4632 (diff) | |
download | GT5-Unofficial-fe595d51866c8b43fb07b2d8aa3d76ffcbfbc48e.tar.gz GT5-Unofficial-fe595d51866c8b43fb07b2d8aa3d76ffcbfbc48e.tar.bz2 GT5-Unofficial-fe595d51866c8b43fb07b2d8aa3d76ffcbfbc48e.zip |
Fix global wireless EU bugging by transferring to world NBT system (#1371)
* Transfer to NBT
* Half working, write only.
* Fix load data
* Works, thanks Kuba.
* spotlessApply (#1372)
Co-authored-by: Connor-Colenso <52056774+Connor-Colenso@users.noreply.github.com>
Co-authored-by: GitHub GTNH Actions <>
* Fix NPE in unit tests
* Spotless
* Move clear map
* Spotless and import cleanup
* Suppressions and warning corrections
* Warnings, warnings, warnings smh.
Co-authored-by: GTNH-Colen <54497873+GTNH-Colen@users.noreply.github.com>
Co-authored-by: kuba6000 <kuba.123123.6000@gmail.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Diffstat (limited to 'src/main/java/gregtech/common/misc/GlobalEnergyWorldSavedData.java')
-rw-r--r-- | src/main/java/gregtech/common/misc/GlobalEnergyWorldSavedData.java | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/src/main/java/gregtech/common/misc/GlobalEnergyWorldSavedData.java b/src/main/java/gregtech/common/misc/GlobalEnergyWorldSavedData.java new file mode 100644 index 0000000000..621fb3beae --- /dev/null +++ b/src/main/java/gregtech/common/misc/GlobalEnergyWorldSavedData.java @@ -0,0 +1,134 @@ +package gregtech.common.misc; + +import static gregtech.common.misc.GlobalVariableStorage.*; + +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import java.io.*; +import java.math.BigInteger; +import java.util.HashMap; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.world.World; +import net.minecraft.world.WorldSavedData; +import net.minecraft.world.storage.MapStorage; +import net.minecraftforge.event.world.WorldEvent; + +public class GlobalEnergyWorldSavedData extends WorldSavedData { + + public static GlobalEnergyWorldSavedData INSTANCE; + + private static final String DATA_NAME = "GregTech_WirelessEUWorldSavedData"; + + private static final String GlobalEnergyNBTTag = "GregTech_GlobalEnergy_MapNBTTag"; + private static final String GlobalEnergyNameNBTTag = "GregTech_GlobalEnergyName_MapNBTTag"; + private static final String GlobalEnergyTeamNBTTag = "GregTech_GlobalEnergyTeam_MapNBTTag"; + + private static void loadInstance(World world) { + + GlobalEnergy.clear(); + GlobalEnergyTeam.clear(); + GlobalEnergyName.clear(); + + MapStorage storage = world.mapStorage; + INSTANCE = (GlobalEnergyWorldSavedData) storage.loadData(GlobalEnergyWorldSavedData.class, DATA_NAME); + if (INSTANCE == null) { + INSTANCE = new GlobalEnergyWorldSavedData(); + storage.setData(DATA_NAME, INSTANCE); + } + INSTANCE.markDirty(); + } + + @SuppressWarnings("unused") + @SubscribeEvent + public void onWorldLoad(WorldEvent.Load event) { + if (!event.world.isRemote && event.world.provider.dimensionId == 0) { + loadInstance(event.world); + } + } + + public GlobalEnergyWorldSavedData() { + super(DATA_NAME); + } + + @SuppressWarnings("unused") + public GlobalEnergyWorldSavedData(String name) { + super(name); + } + + @Override + @SuppressWarnings("unchecked") + public void readFromNBT(NBTTagCompound nbtTagCompound) { + + try { + byte[] ba = nbtTagCompound.getByteArray(GlobalEnergyNBTTag); + InputStream byteArrayInputStream = new ByteArrayInputStream(ba); + ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); + Object data = objectInputStream.readObject(); + GlobalEnergy = (HashMap<String, BigInteger>) data; + } catch (IOException | ClassNotFoundException exception) { + System.out.println(GlobalEnergyNBTTag + " FAILED"); + exception.printStackTrace(); + } + + try { + byte[] ba = nbtTagCompound.getByteArray(GlobalEnergyNameNBTTag); + InputStream byteArrayInputStream = new ByteArrayInputStream(ba); + ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); + Object data = objectInputStream.readObject(); + GlobalEnergyName = (HashMap<String, String>) data; + } catch (IOException | ClassNotFoundException exception) { + System.out.println(GlobalEnergyNameNBTTag + " FAILED"); + exception.printStackTrace(); + } + + try { + byte[] ba = nbtTagCompound.getByteArray(GlobalEnergyTeamNBTTag); + InputStream byteArrayInputStream = new ByteArrayInputStream(ba); + ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); + Object data = objectInputStream.readObject(); + GlobalEnergyTeam = (HashMap<String, String>) data; + } catch (IOException | ClassNotFoundException exception) { + System.out.println(GlobalEnergyTeamNBTTag + " FAILED"); + exception.printStackTrace(); + } + } + + @Override + public void writeToNBT(NBTTagCompound nbtTagCompound) { + + try { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); + objectOutputStream.writeObject(GlobalEnergy); + objectOutputStream.flush(); + byte[] data = byteArrayOutputStream.toByteArray(); + nbtTagCompound.setByteArray(GlobalEnergyNBTTag, data); + } catch (IOException exception) { + System.out.println(GlobalEnergyNBTTag + " SAVE FAILED"); + exception.printStackTrace(); + } + + try { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); + objectOutputStream.writeObject(GlobalEnergyName); + objectOutputStream.flush(); + byte[] data = byteArrayOutputStream.toByteArray(); + nbtTagCompound.setByteArray(GlobalEnergyNameNBTTag, data); + } catch (Exception exception) { + System.out.println(GlobalEnergyNameNBTTag + " SAVE FAILED"); + exception.printStackTrace(); + } + + try { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); + objectOutputStream.writeObject(GlobalEnergyTeam); + objectOutputStream.flush(); + byte[] data = byteArrayOutputStream.toByteArray(); + nbtTagCompound.setByteArray(GlobalEnergyTeamNBTTag, data); + } catch (IOException exception) { + System.out.println(GlobalEnergyTeamNBTTag + " SAVE FAILED"); + exception.printStackTrace(); + } + } +} |