Skip to content

Commit

Permalink
clean up and add documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueWeabo committed Sep 23, 2023
1 parent 56cb041 commit 04602a0
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static long emitEnergyToNetwork(long voltage, long amperage, IEnergyConne
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 Down Expand Up @@ -107,19 +107,21 @@ public static long emitEnergyToNetwork(long voltage, long amperage, IEnergyConne
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) {
if (outputSide == ForgeDirection.UNKNOWN) return;
final PowerLogic emitterLogic = emitter.getPowerLogic();
if (emitterLogic == null) return;
long usedAmperes = 0;
long voltage = emitterLogic.getVoltage();
long amperage = emitterLogic.getAmperage();
if (!(emitter instanceof final IHasWorldObjectAndCoords emitterTile)) {
return;
}
if (usedAmperes > emitterLogic.getAmperage()) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

Expand All @@ -26,7 +27,7 @@ public interface FluidInventoryLogicHost extends IFluidHandler {
*
* @param side The side from where fluids are being inputted or extracted from
* @param type The type of inventory being accessed. For inputting its Input, For outputting its Output.
* @return The Fluid Logic responsible for said type.
* @return The Fluid Logic responsible for said type. Can return null if the side is invalid
*/
@Nullable
FluidInventoryLogic getFluidLogic(@Nonnull ForgeDirection side, @Nonnull InventoryType type);
Expand All @@ -38,11 +39,14 @@ public interface FluidInventoryLogicHost extends IFluidHandler {
* @param id ID of the locked inventory. A null id is all inventories of said controller of said type
* @return The Fluid Logic responsible for everything that should be done with said inventory
*/
@Nullable
@Nonnull
default FluidInventoryLogic getFluidLogic(@Nonnull InventoryType type, @Nullable UUID id) {
return getFluidLogic(ForgeDirection.UNKNOWN, type);
return Objects.requireNonNull(getFluidLogic(ForgeDirection.UNKNOWN, type));
}

/**
* Returns an empty set if the type is {@link InventoryType#Both} or when the machine isn't a controller.
*/
@Nonnull
default Set<Entry<UUID, FluidInventoryLogic>> getAllFluidInventoryLogics(@Nonnull InventoryType type) {
return new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

Expand All @@ -23,7 +24,7 @@ public interface ItemInventoryLogicHost extends ISidedInventory {
*
* @param side The side from where items are being inputted or extracted from
* @param type The type of inventory being accessed. For inputting its Input, For outputting its Output.
* @return The Item Logic responsible for said type.
* @return The Item Logic responsible for said type. Will return null if the side is not valid
*/
@Nullable
ItemInventoryLogic getItemLogic(@Nonnull ForgeDirection side, @Nonnull InventoryType type);
Expand All @@ -35,9 +36,9 @@ public interface ItemInventoryLogicHost extends ISidedInventory {
* @param id ID of the locked inventory. A null id is all inventories of said controller of said type
* @return The Item Logic responsible for everything that should be done with said inventory
*/
@Nullable
@Nonnull
default ItemInventoryLogic getItemLogic(@Nonnull InventoryType type, @Nullable UUID id) {
return getItemLogic(ForgeDirection.UNKNOWN, type);
return Objects.requireNonNull(getItemLogic(ForgeDirection.UNKNOWN, type));
}

/**
Expand All @@ -50,6 +51,10 @@ default InventoryType getItemInventoryType() {
return null;
}

/**
* Returns an empty set if the type is {@link InventoryType#Both} or this is used when the machine isn't a
* controller
*/
@Nonnull
default Set<Entry<UUID, ItemInventoryLogic>> getAllItemInventoryLogics(@Nonnull InventoryType type) {
return new HashSet<>();
Expand Down
34 changes: 30 additions & 4 deletions src/main/java/gregtech/api/logic/interfaces/PowerLogicHost.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gregtech.api.logic.interfaces;

import java.util.Objects;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand All @@ -8,28 +10,52 @@
import gregtech.api.interfaces.tileentity.IEnergyConnected;
import gregtech.api.logic.PowerLogic;

/**
* Power logic class for one to use to enable a machine to use energy
*/
public interface PowerLogicHost {

/**
*
* @param side Side being access to try and get the power logic from
* @return Can return null if the side doesn't allow the return of the logic.
*/
@Nullable
PowerLogic getPowerLogic(@Nonnull ForgeDirection side);

@Nullable
/**
* Gives the power logic ignoring the side.
*/
@Nonnull
default PowerLogic getPowerLogic() {
return getPowerLogic(ForgeDirection.UNKNOWN);
return Objects.requireNonNull(getPowerLogic(ForgeDirection.UNKNOWN));
}

/**
* Shortcut to the method of {@link PowerLogic#isEnergyReceiver()}
*/
default boolean isEnergyReceiver() {
return false;
return getPowerLogic().isEnergyReceiver();
}

/**
* Shortcut to the method of {@link PowerLogic#isEnergyEmitter()}
*/
default boolean isEnergyEmitter() {
return false;
return getPowerLogic().isEnergyEmitter();
}

/**
* Method for emitting energy to other blocks and machines. Override when it needs to be changed.
*/
default void emitEnergyFromLogic() {
IEnergyConnected.Util.emitEnergyToNetwork(this, getPowerOutputSide());
}

/**
* From where does the machine output energy from?
* When the output side is {@link ForgeDirection#UNKNOWN} then it won't output energy
*/
@Nonnull
ForgeDirection getPowerOutputSide();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gregtech.api.logic.interfaces;

import javax.annotation.Nonnull;

import gregtech.api.enums.VoidingMode;
import gregtech.api.interfaces.tileentity.IMachineProgress;
import gregtech.api.interfaces.tileentity.IVoidable;
Expand All @@ -8,21 +10,29 @@
public interface ProcessingLogicHost<P extends MuTEProcessingLogic<P>>
extends IVoidable, ItemInventoryLogicHost, FluidInventoryLogicHost, IMachineProgress {

/**
* Get the processing logic for the current machine
*/
@Nonnull
P getProcessingLogic();

boolean isInputSeparated();

/**
* Get what the machine can void or not
*/
@Nonnull
VoidingMode getVoidMode();

/**
* Called when the processing logic should be updated by {@link #needsUpdate()}
*/
default void updateProcessingLogic(P processingLogic) {}
default void updateProcessingLogic(@Nonnull P processingLogic) {}

/**
* called before the recipe check, but after any other updates
* Called before the recipe check, but after any other updates
*/
default void setProcessingLogicPower(P processingLogic) {}
default void setProcessingLogicPower(@Nonnull P processingLogic) {}

/**
* DO NOT CALL YOURSELF!!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ public abstract class MultiTileBasicMachine<P extends MuTEProcessingLogic<P>> ex
protected boolean isElectric = true;
protected boolean isSteam = false;
protected boolean acceptsFuel = false;
protected boolean canUseWireless = false;
protected boolean canUseLaser = false;

protected byte soundEvent = 0;
protected int soundEventValue = 0;
Expand Down Expand Up @@ -527,22 +525,6 @@ protected void setFuel(boolean acceptsFuel) {
this.acceptsFuel = acceptsFuel;
}

protected boolean canUseWireless() {
return canUseWireless;
}

protected boolean drainEut(long eut) {
return decreaseStoredEnergyUnits(eut, false);
}

protected boolean generateEut(long eut) {
return increaseStoredEnergyUnits(eut, true);
}

protected boolean isGenerator() {
return false;
}

protected boolean consumeFuel() {
if (isElectric() || isSteam()) return false;
if (isActive() && burnTime <= 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gregtech.api.multitileentity.multiblock.base;

import static gregtech.api.util.GT_Utility.moveMultipleItemStacks;
import static gregtech.common.misc.WirelessNetworkManager.strongCheckOrAddUser;
import static mcp.mobius.waila.api.SpecialChars.*;

import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -59,7 +60,6 @@
import gregtech.api.enums.VoidingMode;
import gregtech.api.gui.modularui.GT_UITextures;
import gregtech.api.interfaces.IDescribable;
import gregtech.api.interfaces.IGlobalWirelessEnergy;
import gregtech.api.interfaces.fluid.IFluidStore;
import gregtech.api.interfaces.modularui.ControllerWithOptionalFeatures;
import gregtech.api.logic.ControllerFluidLogic;
Expand Down Expand Up @@ -91,7 +91,7 @@
*/
public abstract class Controller<T extends Controller<T, P>, P extends MuTEProcessingLogic<P>>
extends MultiTileBasicMachine<P> implements IAlignment, IMultiBlockController, IDescribable, IMTE_AddToolTips,
ISurvivalConstructable, ControllerWithOptionalFeatures, IGlobalWirelessEnergy {
ISurvivalConstructable, ControllerWithOptionalFeatures {

public static final String ALL_INVENTORIES_NAME = "all";
protected static final int AUTO_OUTPUT_FREQUENCY_TICK = 20;
Expand Down

0 comments on commit 04602a0

Please sign in to comment.