diff options
Diffstat (limited to 'src/main/java')
6 files changed, 178 insertions, 210 deletions
diff --git a/src/main/java/gregtech/api/interfaces/IGlobalWirelessEnergy.java b/src/main/java/gregtech/api/interfaces/IGlobalWirelessEnergy.java index cc825f9d1a..91ada956bf 100644 --- a/src/main/java/gregtech/api/interfaces/IGlobalWirelessEnergy.java +++ b/src/main/java/gregtech/api/interfaces/IGlobalWirelessEnergy.java @@ -1,201 +1,23 @@ package gregtech.api.interfaces; -import java.io.File; -import java.io.IOException; +import static gregtech.common.misc.GlobalVariableStorage.*; + +import gregtech.common.misc.GlobalEnergyWorldSavedData; import java.math.BigInteger; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.HashMap; -import java.util.List; import java.util.UUID; -import java.util.stream.Collectors; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.world.World; // If you are adding very late-game content feel free to tap into this interface. // The eventual goal is to bypass laser/dynamo stuff and have energy deposited directly from ultra-endgame // multi-blocks directly into the users network. +@SuppressWarnings("unused") public interface IGlobalWirelessEnergy { - // --------------------- NEVER access these maps! Use the methods provided! --------------------- - - // Global EU map. - HashMap<String, BigInteger> GlobalEnergy = new HashMap<>(100, 0.9f); - - // Maps user IDs to usernames and vice versa. Seems redundant but this makes accessing this - // easier in certain locations (like gt commands). - HashMap<String, String> GlobalEnergyName = new HashMap<>(100, 0.9f); - - // Maps UUIDs to other UUIDs. This allows users to join a team. - HashMap<String, String> GlobalEnergyTeam = new HashMap<>(100, 0.9f); - - // ---------------------------------------------------------------------------------------------- - - // Folder - String GlobalEnergyFolderName = "GlobalEnergyInformationStorage"; - - // 3 txt file names. Do not change. - String GlobalEnergyMapFileName = "GlobalEnergyMap"; - String GlobalEnergyNameFileName = "GlobalEnergyNameMap"; - String GlobalEnergyTeamFileName = "GlobalEnergyTeamMap"; - // User 0 will join user 1 by calling this function. They will share the same energy network. default void joinUserNetwork(String user_uuid_0, String user_uuid_1) { GlobalEnergyTeam.put(user_uuid_0, user_uuid_1); } - // --- Save data for global energy network -- - - default void saveGlobalEnergyInfo(String world_name) { - // Replace chars because of bug in forge that doesn't understand MC converts . to _ upon world creation. - world_name = world_name.replace('.', '_'); - createStorageIfNotExist(world_name); - saveGlobalEnergyMap(world_name); - saveGlobalEnergyName(world_name); - saveGlobalEnergyTeam(world_name); - } - - default void saveGlobalEnergyMap(String world_name) { - try { - List<String> lines = GlobalEnergy.entrySet().stream() - .map(entry -> entry.getKey() + ":" + entry.getValue()) - .collect(Collectors.toList()); - - Path path = Paths.get("./saves/" + world_name + "/" + GlobalEnergyFolderName + "/" + GlobalEnergyMapFileName - + ".txt") - .toAbsolutePath(); - Files.write(path, lines); - - } catch (IOException e) { - e.printStackTrace(); - } - } - - default void saveGlobalEnergyName(String world_name) { - try { - List<String> lines = GlobalEnergyName.entrySet().stream() - .map(entry -> entry.getKey() + ":" + entry.getValue()) - .collect(Collectors.toList()); - - Path path = Paths.get("./saves/" + world_name + "/" + GlobalEnergyFolderName + "/" - + GlobalEnergyNameFileName + ".txt") - .toAbsolutePath(); - Files.write(path, lines); - - } catch (IOException e) { - e.printStackTrace(); - } - } - - default void saveGlobalEnergyTeam(String world_name) { - try { - List<String> lines = GlobalEnergyTeam.entrySet().stream() - .map(entry -> entry.getKey() + ":" + entry.getValue()) - .collect(Collectors.toList()); - - Path path = Paths.get("./saves/" + world_name + "/" + GlobalEnergyFolderName + "/" - + GlobalEnergyTeamFileName + ".txt") - .toAbsolutePath(); - Files.write(path, lines); - - } catch (IOException e) { - e.printStackTrace(); - } - } - - // --- Load data for global energy network --- - - default void loadGlobalEnergyInfo(World world) { - // Replace chars because of bug in forge that doesn't understand MC converts . to _ upon world creation. - String world_name = world.getWorldInfo().getWorldName().replace('.', '_'); - PrivateGlobalEnergy.WorldName = world_name; - createStorageIfNotExist(world_name); - loadGlobalEnergyMap(world); - loadGlobalEnergyName(world); - loadGlobalEnergyTeam(world); - } - - default void loadGlobalEnergyMap(World world) { - try { - Path path = Paths.get("./saves/" + world.getWorldInfo().getWorldName() + "/" + GlobalEnergyFolderName + "/" - + GlobalEnergyMapFileName + ".txt") - .toAbsolutePath(); - - String[] data; - for (String line : Files.readAllLines(path)) { - data = line.split(":"); - - String UUID = data[0]; - BigInteger num = new BigInteger(data[1]); - - GlobalEnergy.put(UUID, num); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - - default void loadGlobalEnergyName(World world) { - try { - Path path = Paths.get("./saves/" + world.getWorldInfo().getWorldName() + "/" + GlobalEnergyFolderName + "/" - + GlobalEnergyNameFileName + ".txt") - .toAbsolutePath(); - - String[] data; - for (String line : Files.readAllLines(path)) { - data = line.split(":"); - - GlobalEnergyName.put(data[0], data[1]); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - - default void loadGlobalEnergyTeam(World world) { - try { - Path path = Paths.get("./saves/" + world.getWorldInfo().getWorldName() + "/" + GlobalEnergyFolderName + "/" - + GlobalEnergyTeamFileName + ".txt") - .toAbsolutePath(); - - String[] data; - for (String line : Files.readAllLines(path)) { - data = line.split(":"); - - GlobalEnergyName.put(data[0], data[1]); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - - // ------------------ - - default void createStorageIfNotExist(String world_name) { - Path folder_path = Paths.get("./saves/" + world_name + "/" + GlobalEnergyFolderName) - .toAbsolutePath(); - - // Create folder for storing global energy network info. - try { - Files.createDirectories(folder_path); - } catch (IOException e) { - e.printStackTrace(); - } - - // Create txt files. - try { - File file_0 = new File(folder_path + "/" + GlobalEnergyMapFileName + ".txt"); - file_0.createNewFile(); - File file_1 = new File(folder_path + "/" + GlobalEnergyNameFileName + ".txt"); - file_1.createNewFile(); - File file_2 = new File(folder_path + "/" + GlobalEnergyTeamFileName + ".txt"); - file_2.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - } - } - // Adds a user to the energy map if they do not already exist. Otherwise, do nothing. Will also check if the user // has changed their username and adjust the maps accordingly. This should be called infrequently. Ideally on first // tick of a machine being placed only. @@ -235,11 +57,12 @@ public interface IGlobalWirelessEnergy { // as infrequently as possible and bulk store values to add to the global map. default boolean addEUToGlobalEnergyMap(String user_uuid, BigInteger EU) { - PrivateGlobalEnergy.EnergyAdds += 1; - - if (PrivateGlobalEnergy.EnergyAddsBeforeSaving >= PrivateGlobalEnergy.EnergyAdds) { - saveGlobalEnergyInfo(PrivateGlobalEnergy.WorldName); - PrivateGlobalEnergy.EnergyAdds = 0; + // Mark the data as dirty and in need of saving. + try { + GlobalEnergyWorldSavedData.INSTANCE.markDirty(); + } catch (Exception exception) { + System.out.println("COULD NOT MARK GLOBAL ENERGY AS DIRTY IN ADD EU"); + exception.printStackTrace(); } // Get the team UUID. Users are by default in a team with a UUID equal to their player UUID. @@ -287,6 +110,14 @@ public interface IGlobalWirelessEnergy { // This overwrites the EU in the network. Only use this if you are absolutely sure you know what you are doing. default void setUserEU(String user_uuid, BigInteger EU) { + // Mark the data as dirty and in need of saving. + try { + GlobalEnergyWorldSavedData.INSTANCE.markDirty(); + } catch (Exception exception) { + System.out.println("COULD NOT MARK GLOBAL ENERGY AS DIRTY IN SET EU"); + exception.printStackTrace(); + } + GlobalEnergy.put(GlobalEnergyTeam.get(user_uuid), EU); } @@ -298,23 +129,10 @@ public interface IGlobalWirelessEnergy { return GlobalEnergyTeam.getOrDefault(GlobalEnergyName.getOrDefault(username, ""), ""); } - default void clearMaps() { + static void clearGlobalEnergyInformationMaps() { // Do not use this unless you are 100% certain you know what you are doing. GlobalEnergy.clear(); GlobalEnergyName.clear(); GlobalEnergyTeam.clear(); } } - -class PrivateGlobalEnergy { - // EnergyAdds Counts the number of times energy has been added to the network since the last save. - public static long EnergyAdds = 0; - - // EnergyAddsBeforeSaving stores the number of times energy must be added or removed from the network before a save - // is initiated. Do not set this number very low, or you may cause lag. If you use a large number of wireless - // machines increase this value. - public static long EnergyAddsBeforeSaving = 1000; - - // Name of the folder the world is in. - public static String WorldName = ""; -} diff --git a/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_Wireless_Dynamo.java b/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_Wireless_Dynamo.java index 3fd496e960..140a81f766 100644 --- a/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_Wireless_Dynamo.java +++ b/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_Wireless_Dynamo.java @@ -124,9 +124,6 @@ public class GT_MetaTileEntity_Wireless_Dynamo extends GT_MetaTileEntity_Hatch_D owner_uuid = aBaseMetaTileEntity.getOwnerUuid().toString(); owner_name = aBaseMetaTileEntity.getOwnerName(); - // Attempt to load in map from file. - if (GlobalEnergy.size() == 0) loadGlobalEnergyInfo(aBaseMetaTileEntity.getWorld()); - strongCheckOrAddUser(owner_uuid, owner_name); } diff --git a/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_Wireless_Hatch.java b/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_Wireless_Hatch.java index baffde6f3f..ee1af3281b 100644 --- a/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_Wireless_Hatch.java +++ b/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_Wireless_Hatch.java @@ -129,9 +129,6 @@ public class GT_MetaTileEntity_Wireless_Hatch extends GT_MetaTileEntity_Hatch_En owner_uuid = aBaseMetaTileEntity.getOwnerUuid().toString(); owner_name = aBaseMetaTileEntity.getOwnerName(); - // Attempt to load in map from file. - if (GlobalEnergy.size() == 0) loadGlobalEnergyInfo(aBaseMetaTileEntity.getWorld()); - strongCheckOrAddUser(owner_uuid, owner_name); if (addEUToGlobalEnergyMap(owner_uuid, eu_transferred_per_operation.negate())) @@ -144,7 +141,7 @@ public class GT_MetaTileEntity_Wireless_Hatch extends GT_MetaTileEntity_Hatch_En // Every ticks_between_energy_addition add eu_transferred_per_operation to internal EU storage from network. if (aTick % ticks_between_energy_addition == 0L) { - long total_eu = this.getBaseMetaTileEntity().getStoredEU(); + long total_eu = getBaseMetaTileEntity().getStoredEU(); // Can the machine store the EU being added? long new_eu_storage = total_eu + eu_transferred_per_operation_long; diff --git a/src/main/java/gregtech/common/GT_Proxy.java b/src/main/java/gregtech/common/GT_Proxy.java index ef82266ed8..9f5e6a0516 100644 --- a/src/main/java/gregtech/common/GT_Proxy.java +++ b/src/main/java/gregtech/common/GT_Proxy.java @@ -76,6 +76,7 @@ import gregtech.common.gui.GT_ContainerVolumetricFlask; import gregtech.common.gui.GT_GUIContainerVolumetricFlask; import gregtech.common.items.GT_MetaGenerated_Item_98; import gregtech.common.items.GT_MetaGenerated_Tool_01; +import gregtech.common.misc.GlobalEnergyWorldSavedData; import java.io.File; import java.lang.reflect.InvocationTargetException; import java.text.DateFormat; @@ -1038,6 +1039,8 @@ public abstract class GT_Proxy implements IGT_Mod, IGuiHandler, IFuelHandler, IG GT_OreDictUnificator.get(OrePrefixes.bucketClay, Materials.Empty, 1L))); } + MinecraftForge.EVENT_BUS.register(new GlobalEnergyWorldSavedData("")); + // IC2 Hazmat addFullHazmatToIC2Item("hazmatHelmet"); addFullHazmatToIC2Item("hazmatChestplate"); @@ -1297,8 +1300,6 @@ public abstract class GT_Proxy implements IGT_Mod, IGuiHandler, IFuelHandler, IG public void onServerStopping() { File tSaveDirectory = getSaveDirectory(); GregTech_API.sWirelessRedstone.clear(); - saveGlobalEnergyInfo(mUniverse.getWorldInfo().getWorldName()); - clearMaps(); if (tSaveDirectory != null) { try { for (int i = 1; i < GregTech_API.METATILEENTITIES.length; i++) { 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(); + } + } +} diff --git a/src/main/java/gregtech/common/misc/GlobalVariableStorage.java b/src/main/java/gregtech/common/misc/GlobalVariableStorage.java new file mode 100644 index 0000000000..5ec0ed9c82 --- /dev/null +++ b/src/main/java/gregtech/common/misc/GlobalVariableStorage.java @@ -0,0 +1,21 @@ +package gregtech.common.misc; + +import java.math.BigInteger; +import java.util.HashMap; + +public abstract class GlobalVariableStorage { + // --------------------- NEVER access these maps! Use the methods provided! --------------------- + + // Global EU map. + public static HashMap<String, BigInteger> GlobalEnergy = new HashMap<>(100, 0.9f); + + // Maps user IDs to usernames and vice versa. Seems redundant but this makes accessing this + // easier in certain locations (like gt commands). + public static HashMap<String, String> GlobalEnergyName = new HashMap<>(100, 0.9f); + + // Maps UUIDs to other UUIDs. This allows users to join a team. + public static HashMap<String, String> GlobalEnergyTeam = new HashMap<>(100, 0.9f); + + // ---------------------------------------------------------------------------------------------- + +} |