Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a Simple PowerOutput task and cleans up some of the code. #2303

Merged
merged 10 commits into from
Sep 25, 2023
92 changes: 17 additions & 75 deletions src/main/java/gregtech/api/interfaces/IGlobalWirelessEnergy.java
Original file line number Diff line number Diff line change
@@ -1,61 +1,44 @@
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
BlueWeabo marked this conversation as resolved.
Show resolved Hide resolved
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
// has changed their username and adjust the maps accordingly. This should be called infrequently. Ideally on first
// 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);
}

// ------------------------------------------------------------------------------------
Expand All @@ -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) {
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
}
103 changes: 87 additions & 16 deletions src/main/java/gregtech/api/interfaces/tileentity/IEnergyConnected.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package gregtech.api.interfaces.tileentity;

import java.util.Objects;

import javax.annotation.Nonnull;

import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;

Expand Down Expand Up @@ -58,19 +62,19 @@ final class Util {
*
* @return the used Amperage.
*/
public static long emitEnergyToNetwork(long aVoltage, long aAmperage, IEnergyConnected aEmitter) {
long rUsedAmperes = 0;
if (!(aEmitter instanceof IHasWorldObjectAndCoords emitterTile)) {
public static long emitEnergyToNetwork(long voltage, long amperage, IEnergyConnected emitter) {
long usedAmperes = 0;
if (!(emitter instanceof IHasWorldObjectAndCoords emitterTile)) {
return 0;
}

for (final ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) {
if (rUsedAmperes > aAmperage) break;
if (!aEmitter.outputsEnergyTo(side)) {
if (usedAmperes > amperage) break;
if (!emitter.outputsEnergyTo(side)) {
continue;
}

final ForgeDirection oppositeSide = side.getOpposite();
final ForgeDirection oppositeSide = Objects.requireNonNull(side.getOpposite());
final TileEntity tTileEntity = emitterTile.getTileEntityAtSide(side);
if (tTileEntity instanceof PowerLogicHost host) {

Expand All @@ -79,28 +83,95 @@ public static long emitEnergyToNetwork(long aVoltage, long aAmperage, IEnergyCon
continue;
}

rUsedAmperes += logic.injectEnergy(aVoltage, aAmperage - rUsedAmperes);
usedAmperes += logic.injectEnergy(voltage, amperage - usedAmperes);
} else if (tTileEntity instanceof IEnergyConnected energyConnected) {
if (aEmitter.getColorization() >= 0) {
if (emitter.getColorization() >= 0) {
final byte tColor = energyConnected.getColorization();
if (tColor >= 0 && tColor != aEmitter.getColorization()) continue;
if (tColor >= 0 && tColor != emitter.getColorization()) continue;
}
rUsedAmperes += energyConnected.injectEnergyUnits(oppositeSide, aVoltage, aAmperage - rUsedAmperes);
usedAmperes += energyConnected.injectEnergyUnits(oppositeSide, voltage, amperage - usedAmperes);

} else if (tTileEntity instanceof IEnergySink sink) {
if (sink.acceptsEnergyFrom((TileEntity) aEmitter, oppositeSide)) {
while (aAmperage > rUsedAmperes && sink.getDemandedEnergy() > 0
&& sink.injectEnergy(oppositeSide, aVoltage, aVoltage) < aVoltage) rUsedAmperes++;
if (sink.acceptsEnergyFrom((TileEntity) emitter, oppositeSide)) {
while (amperage > usedAmperes && sink.getDemandedEnergy() > 0
&& sink.injectEnergy(oppositeSide, voltage, voltage) < voltage) usedAmperes++;
}
} else if (GregTech_API.mOutputRF && tTileEntity instanceof IEnergyReceiver receiver) {
final int rfOut = GT_Utility.safeInt(aVoltage * GregTech_API.mEUtoRF / 100);
final int rfOut = GT_Utility.safeInt(voltage * GregTech_API.mEUtoRF / 100);
if (receiver.receiveEnergy(oppositeSide, rfOut, true) == rfOut) {
receiver.receiveEnergy(oppositeSide, rfOut, false);
rUsedAmperes++;
usedAmperes++;
}
}
}
return usedAmperes;
}

/**
* Same as {@link #emitEnergyToNetwork(long, long, IEnergyConnected)}, but instead we remove the energy directly from the logic itself.
* @param emitter The host, which is trying to emit energy in the network
* @param outputSide side from where energy is being outputted to. If its {@link ForgeDirection#UNKNOWN} then it doesn't emit energy to the network
*/
public static void emitEnergyToNetwork(@Nonnull final PowerLogicHost emitter, @Nonnull final ForgeDirection outputSide) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method has too many duplicated code with the another one

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if I made the other basically have a wrapper class for IEnergyConnected to work for this method? The wrapper class would make its own "powerLogic" and make it just have a "infinite" stored energy, but the same amps and voltage. And this method will still return the amperage, but it won't be needed if directly using a MuTE.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it be used outside of this class? I think it's too complicated compared to what we want to achieve.

if (outputSide == ForgeDirection.UNKNOWN) return;
final PowerLogic emitterLogic = emitter.getPowerLogic();
long usedAmperes = 0;
long voltage = emitterLogic.getVoltage();
long amperage = emitterLogic.getAmperage();
if (!(emitter instanceof final IHasWorldObjectAndCoords emitterTile)) {
return;
}
// We need to make sure we can actually output energy on this side. This is more of a safety check.
if (emitter.getPowerLogic(outputSide) == null) {
return;
}

final ForgeDirection oppositeSide = Objects.requireNonNull(outputSide.getOpposite());
final TileEntity tileEntity = emitterTile.getTileEntityAtSide(outputSide);
if (tileEntity instanceof PowerLogicHost host) {

final PowerLogic logic = host.getPowerLogic(oppositeSide);
if (logic == null || logic.isEnergyReceiver()) {
return;
}

usedAmperes += logic.injectEnergy(voltage, amperage);
emitterLogic.removeEnergyUnsafe(usedAmperes * voltage);
return;
}

if (tileEntity instanceof IEnergyConnected energyConnected) {
if (emitter instanceof IColoredTileEntity coloredEmitter && coloredEmitter.getColorization() >= 0) {
final byte tColor = energyConnected.getColorization();
if (tColor >= 0 && tColor != coloredEmitter.getColorization()) {
return;
}
}
usedAmperes += energyConnected.injectEnergyUnits(oppositeSide, voltage, amperage - usedAmperes);
emitterLogic.removeEnergyUnsafe(usedAmperes * voltage);
return;
}

if (tileEntity instanceof IEnergySink sink) {
if (sink.acceptsEnergyFrom((TileEntity) emitter, oppositeSide)) {
while (amperage > usedAmperes && sink.getDemandedEnergy() > 0
&& sink.injectEnergy(oppositeSide, voltage, voltage) < voltage) {
usedAmperes++;
}
emitterLogic.removeEnergyUnsafe(usedAmperes * voltage);
return;
}
}

if (GregTech_API.mOutputRF && tileEntity instanceof IEnergyReceiver receiver) {
final int rfOut = GT_Utility.safeInt(voltage * GregTech_API.mEUtoRF / 100);
if (receiver.receiveEnergy(oppositeSide, rfOut, true) == rfOut) {
receiver.receiveEnergy(oppositeSide, rfOut, false);
usedAmperes++;
emitterLogic.removeEnergyUnsafe(usedAmperes * voltage);
return;
}
}
return rUsedAmperes;
}
}
}
Loading