aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/interfaces
diff options
context:
space:
mode:
authorConnor-Colenso <52056774+Connor-Colenso@users.noreply.github.com>2022-09-11 01:59:07 +0100
committerGitHub <noreply@github.com>2022-09-11 01:59:07 +0100
commitfe595d51866c8b43fb07b2d8aa3d76ffcbfbc48e (patch)
treed2ad01823fff33408beb772f75ae24a8ad71d6ac /src/main/java/gregtech/api/interfaces
parent930d4392b82129a413fd23e700a94052ad3e4632 (diff)
downloadGT5-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/api/interfaces')
-rw-r--r--src/main/java/gregtech/api/interfaces/IGlobalWirelessEnergy.java220
1 files changed, 19 insertions, 201 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 = "";
-}