From 56cb041c27dcd7291e6c476c4d6a79c3edc7a124 Mon Sep 17 00:00:00 2001 From: BlueWeabo <76872108+BlueWeabo@users.noreply.github.com> Date: Sat, 23 Sep 2023 00:02:17 +0300 Subject: [PATCH] make a wireless network manager class instead of using an interface --- .../api/interfaces/IGlobalWirelessEnergy.java | 92 ++-------- .../java/gregtech/api/logic/PowerLogic.java | 4 +- .../common/misc/WirelessNetworkManager.java | 164 ++++++++++++++++++ 3 files changed, 183 insertions(+), 77 deletions(-) create mode 100644 src/main/java/gregtech/common/misc/WirelessNetworkManager.java diff --git a/src/main/java/gregtech/api/interfaces/IGlobalWirelessEnergy.java b/src/main/java/gregtech/api/interfaces/IGlobalWirelessEnergy.java index be4db5412a7..30dc071115f 100644 --- a/src/main/java/gregtech/api/interfaces/IGlobalWirelessEnergy.java +++ b/src/main/java/gregtech/api/interfaces/IGlobalWirelessEnergy.java @@ -1,25 +1,25 @@ package gregtech.api.interfaces; -import static gregtech.common.misc.GlobalVariableStorage.GlobalEnergy; -import static gregtech.common.misc.GlobalVariableStorage.GlobalEnergyName; -import static gregtech.common.misc.GlobalVariableStorage.GlobalEnergyTeam; - import java.math.BigInteger; import java.util.UUID; import net.minecraft.entity.player.EntityPlayer; import gregtech.api.interfaces.tileentity.IGregTechTileEntity; -import gregtech.common.misc.GlobalEnergyWorldSavedData; +import gregtech.common.misc.WirelessNetworkManager; // 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. +/** + * Use WirelessNetworkManager instead + */ +@Deprecated public interface IGlobalWirelessEnergy { // 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); + WirelessNetworkManager.joinUserNetwork(user_uuid_0, user_uuid_1); } // Adds a user to the energy map if they do not already exist. Otherwise, do nothing. Will also check if the user @@ -27,35 +27,18 @@ default void joinUserNetwork(String user_uuid_0, String user_uuid_1) { // tick of a machine being placed only. default void strongCheckOrAddUser(EntityPlayer user) { - strongCheckOrAddUser( + WirelessNetworkManager.strongCheckOrAddUser( user.getUniqueID() .toString(), user.getDisplayName()); } default void strongCheckOrAddUser(UUID user_uuid, String user_name) { - strongCheckOrAddUser(user_uuid.toString(), user_name); + WirelessNetworkManager.strongCheckOrAddUser(user_uuid.toString(), user_name); } default void strongCheckOrAddUser(String user_uuid, String user_name) { - - // Check if the user has a team. Add them if not. - GlobalEnergyTeam.putIfAbsent(user_uuid, user_uuid); - - // Check if the user is in the global energy map. - GlobalEnergy.putIfAbsent(user_uuid, BigInteger.ZERO); - - // If the username linked to the users fixed uuid is not equal to their current name then remove it. - // This indicates that their username has changed. - if (!(GlobalEnergyName.getOrDefault(user_uuid, "") - .equals(user_name))) { - String old_name = GlobalEnergyName.get(user_uuid); - GlobalEnergyName.remove(old_name); - } - - // Add UUID -> Name, Name -> UUID. - GlobalEnergyName.put(user_name, user_uuid); - GlobalEnergyName.put(user_uuid, user_name); + WirelessNetworkManager.strongCheckOrAddUser(user_uuid, user_name); } // ------------------------------------------------------------------------------------ @@ -64,30 +47,7 @@ default void strongCheckOrAddUser(String user_uuid, String user_name) { // BigIntegers have much slower operations than longs/ints. You should call these methods // as infrequently as possible and bulk store values to add to the global map. default boolean addEUToGlobalEnergyMap(String userUUID, 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 ADD EU"); - exception.printStackTrace(); - } - - // Get the team UUID. Users are by default in a team with a UUID equal to their player UUID. - String teamUUID = GlobalEnergyTeam.getOrDefault(userUUID, userUUID); - - // Get the teams total energy stored. If they are not in the map, return 0 EU. - BigInteger totalEU = GlobalEnergy.getOrDefault(teamUUID, BigInteger.ZERO); - totalEU = totalEU.add(EU); - - // If there is sufficient EU then complete the operation and return true. - if (totalEU.signum() >= 0) { - GlobalEnergy.put(teamUUID, totalEU); - return true; - } - - // There is insufficient EU so cancel the operation and return false. - return false; + return WirelessNetworkManager.addEUToGlobalEnergyMap(userUUID, EU); } default boolean addEUToGlobalEnergyMap(UUID user_uuid, BigInteger EU) { @@ -113,28 +73,20 @@ default boolean addEUToGlobalEnergyMap(String user_uuid, int EU) { // ------------------------------------------------------------------------------------ default BigInteger getUserEU(String user_uuid) { - return GlobalEnergy.getOrDefault(GlobalEnergyTeam.getOrDefault(user_uuid, user_uuid), BigInteger.ZERO); + return WirelessNetworkManager.getUserEU(user_uuid); } // 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); + WirelessNetworkManager.setUserEU(user_uuid, EU); } default String GetUsernameFromUUID(String uuid) { - return GlobalEnergyName.getOrDefault(GlobalEnergyTeam.getOrDefault(uuid, ""), ""); + return WirelessNetworkManager.GetUsernameFromUUID(uuid); } default String getUUIDFromUsername(String username) { - return GlobalEnergyTeam.getOrDefault(GlobalEnergyName.getOrDefault(username, ""), ""); + return WirelessNetworkManager.getUUIDFromUsername(username); } /** @@ -143,24 +95,14 @@ default String getUUIDFromUsername(String username) { * @return */ default String getRawUUIDFromUsername(String username) { - return GlobalEnergyName.getOrDefault(username, ""); + return WirelessNetworkManager.getRawUUIDFromUsername(username); } static void clearGlobalEnergyInformationMaps() { - // Do not use this unless you are 100% certain you know what you are doing. - GlobalEnergy.clear(); - GlobalEnergyName.clear(); - GlobalEnergyTeam.clear(); + WirelessNetworkManager.clearGlobalEnergyInformationMaps(); } default String processInitialSettings(final IGregTechTileEntity machine) { - - // UUID and username of the owner. - final String UUID = machine.getOwnerUuid() - .toString(); - final String name = machine.getOwnerName(); - - strongCheckOrAddUser(UUID, name); - return UUID; + return WirelessNetworkManager.processInitialSettings(machine); } } diff --git a/src/main/java/gregtech/api/logic/PowerLogic.java b/src/main/java/gregtech/api/logic/PowerLogic.java index 793dc553346..572d6bb02bd 100644 --- a/src/main/java/gregtech/api/logic/PowerLogic.java +++ b/src/main/java/gregtech/api/logic/PowerLogic.java @@ -1,5 +1,7 @@ package gregtech.api.logic; +import static gregtech.common.misc.WirelessNetworkManager.addEUToGlobalEnergyMap; + import java.util.UUID; import javax.annotation.Nonnull; @@ -7,9 +9,7 @@ import net.minecraft.nbt.NBTTagCompound; import gregtech.api.enums.GT_Values.NBT; -import gregtech.api.interfaces.IGlobalWirelessEnergy; -public class PowerLogic implements IGlobalWirelessEnergy { /** * Power logic for machines. This is used to store all the important variables for a machine to have energy and use it * in any way. diff --git a/src/main/java/gregtech/common/misc/WirelessNetworkManager.java b/src/main/java/gregtech/common/misc/WirelessNetworkManager.java new file mode 100644 index 00000000000..26f84b23e51 --- /dev/null +++ b/src/main/java/gregtech/common/misc/WirelessNetworkManager.java @@ -0,0 +1,164 @@ +package gregtech.common.misc; + +import static gregtech.common.misc.GlobalVariableStorage.GlobalEnergy; +import static gregtech.common.misc.GlobalVariableStorage.GlobalEnergyName; +import static gregtech.common.misc.GlobalVariableStorage.GlobalEnergyTeam; + +import java.math.BigInteger; +import java.util.UUID; + +import net.minecraft.entity.player.EntityPlayer; + +import gregtech.api.interfaces.tileentity.IGregTechTileEntity; + +public class WirelessNetworkManager { + + private WirelessNetworkManager() {} + + // User 0 will join user 1 by calling this function. They will share the same energy network. + public static void joinUserNetwork(String user_uuid_0, String user_uuid_1) { + GlobalEnergyTeam.put(user_uuid_0, user_uuid_1); + } + + // 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. + + public static void strongCheckOrAddUser(EntityPlayer user) { + strongCheckOrAddUser( + user.getUniqueID() + .toString(), + user.getDisplayName()); + } + + public static void strongCheckOrAddUser(UUID user_uuid, String user_name) { + strongCheckOrAddUser(user_uuid.toString(), user_name); + } + + public static void strongCheckOrAddUser(String user_uuid, String user_name) { + + // Check if the user has a team. Add them if not. + GlobalEnergyTeam.putIfAbsent(user_uuid, user_uuid); + + // Check if the user is in the global energy map. + GlobalEnergy.putIfAbsent(user_uuid, BigInteger.ZERO); + + // If the username linked to the users fixed uuid is not equal to their current name then remove it. + // This indicates that their username has changed. + if (!(GlobalEnergyName.getOrDefault(user_uuid, "") + .equals(user_name))) { + String old_name = GlobalEnergyName.get(user_uuid); + GlobalEnergyName.remove(old_name); + } + + // Add UUID -> Name, Name -> UUID. + GlobalEnergyName.put(user_name, user_uuid); + GlobalEnergyName.put(user_uuid, user_name); + } + + // ------------------------------------------------------------------------------------ + // Add EU to the users global energy. You can enter a negative number to subtract it. + // If the value goes below 0 it will return false and not perform the operation. + // BigIntegers have much slower operations than longs/ints. You should call these methods + // as infrequently as possible and bulk store values to add to the global map. + public static boolean addEUToGlobalEnergyMap(String userUUID, 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 ADD EU"); + exception.printStackTrace(); + } + + // Get the team UUID. Users are by public static in a team with a UUID equal to their player UUID. + String teamUUID = GlobalEnergyTeam.getOrDefault(userUUID, userUUID); + + // Get the teams total energy stored. If they are not in the map, return 0 EU. + BigInteger totalEU = GlobalEnergy.getOrDefault(teamUUID, BigInteger.ZERO); + totalEU = totalEU.add(EU); + + // If there is sufficient EU then complete the operation and return true. + if (totalEU.signum() >= 0) { + GlobalEnergy.put(teamUUID, totalEU); + return true; + } + + // There is insufficient EU so cancel the operation and return false. + return false; + } + + public static boolean addEUToGlobalEnergyMap(UUID user_uuid, BigInteger EU) { + return addEUToGlobalEnergyMap(user_uuid.toString(), EU); + } + + public static boolean addEUToGlobalEnergyMap(UUID user_uuid, long EU) { + return addEUToGlobalEnergyMap(user_uuid.toString(), BigInteger.valueOf(EU)); + } + + public static boolean addEUToGlobalEnergyMap(UUID user_uuid, int EU) { + return addEUToGlobalEnergyMap(user_uuid.toString(), BigInteger.valueOf(EU)); + } + + public static boolean addEUToGlobalEnergyMap(String user_uuid, long EU) { + return addEUToGlobalEnergyMap(user_uuid, BigInteger.valueOf(EU)); + } + + public static boolean addEUToGlobalEnergyMap(String user_uuid, int EU) { + return addEUToGlobalEnergyMap(user_uuid, BigInteger.valueOf(EU)); + } + + // ------------------------------------------------------------------------------------ + + public static BigInteger getUserEU(String user_uuid) { + return GlobalEnergy.getOrDefault(GlobalEnergyTeam.getOrDefault(user_uuid, user_uuid), BigInteger.ZERO); + } + + // This overwrites the EU in the network. Only use this if you are absolutely sure you know what you are doing. + public static 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); + } + + public static String GetUsernameFromUUID(String uuid) { + return GlobalEnergyName.getOrDefault(GlobalEnergyTeam.getOrDefault(uuid, ""), ""); + } + + public static String getUUIDFromUsername(String username) { + return GlobalEnergyTeam.getOrDefault(GlobalEnergyName.getOrDefault(username, ""), ""); + } + + /** + * + * @param username + * @return + */ + public static String getRawUUIDFromUsername(String username) { + return GlobalEnergyName.getOrDefault(username, ""); + } + + public static void clearGlobalEnergyInformationMaps() { + // Do not use this unless you are 100% certain you know what you are doing. + GlobalEnergy.clear(); + GlobalEnergyName.clear(); + GlobalEnergyTeam.clear(); + } + + public static String processInitialSettings(final IGregTechTileEntity machine) { + + // UUID and username of the owner. + final String UUID = machine.getOwnerUuid() + .toString(); + final String name = machine.getOwnerName(); + + strongCheckOrAddUser(UUID, name); + return UUID; + } +}