Skip to content

Commit

Permalink
Improve state transitions for Machine Controller Cover (#3939)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Robertz <dream-master@gmx.net>
Co-authored-by: Maya <10861407+serenibyss@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 13, 2025
1 parent b51e853 commit 99db712
Showing 1 changed file with 54 additions and 27 deletions.
81 changes: 54 additions & 27 deletions src/main/java/gregtech/common/covers/CoverControlsWork.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@

public class CoverControlsWork extends CoverBehavior {

private enum State {
ENABLE_WITH_SIGNAL,
DISABLE_WITH_SIGNAL,
DISABLED,
ENABLE_WITH_SIGNAL_SAFE,
DISABLE_WITH_SIGNAL_SAFE;
}

private boolean handledShutdown = false;

public CoverControlsWork(ITexture coverTexture) {
super(coverTexture);
}
Expand All @@ -29,35 +39,52 @@ public CoverControlsWork(ITexture coverTexture) {
public int doCoverThings(ForgeDirection side, byte aInputRedstone, int aCoverID, int aCoverVariable,
ICoverable aTileEntity, long aTimer) {
if (aTileEntity instanceof IMachineProgress machine) {
if (aCoverVariable < 2) {
if ((aInputRedstone > 0) == (aCoverVariable == 0)) {
if (!machine.isAllowedToWork()) machine.enableWorking();
} else if (machine.isAllowedToWork()) machine.disableWorking();
} else if (aCoverVariable == 2) {
machine.disableWorking();
} else {
if (machine.wasShutdown() && machine.getLastShutDownReason()
.wasCritical()) {
machine.disableWorking();
if (!mPlayerNotified) {
EntityPlayer player = lastPlayer == null ? null : lastPlayer.get();
if (player != null) {
lastPlayer = null;
mPlayerNotified = true;
GTUtility.sendChatToPlayer(
player,
aTileEntity.getInventoryName() + "at "
+ String.format(
"(%d,%d,%d)",
aTileEntity.getXCoord(),
aTileEntity.getYCoord(),
aTileEntity.getZCoord())
+ " shut down.");
State state = aCoverVariable < State.values().length ? State.values()[aCoverVariable] : State.DISABLED;
switch (state) {
case ENABLE_WITH_SIGNAL, DISABLE_WITH_SIGNAL -> {
if ((aInputRedstone > 0) == (state == State.ENABLE_WITH_SIGNAL)) {
if (!machine.isAllowedToWork()) {
machine.enableWorking();
handledShutdown = false;
}
} else {
if (machine.isAllowedToWork()) machine.disableWorking();
}
}
case DISABLED -> {
if (machine.isAllowedToWork()) machine.disableWorking();
}
case ENABLE_WITH_SIGNAL_SAFE, DISABLE_WITH_SIGNAL_SAFE -> {
if (machine.wasShutdown() && machine.getLastShutDownReason()
.wasCritical() && !handledShutdown) {
if (!mPlayerNotified) {
EntityPlayer player = lastPlayer == null ? null : lastPlayer.get();
if (player != null) {
lastPlayer = null;
mPlayerNotified = true;
GTUtility.sendChatToPlayer(
player,
aTileEntity.getInventoryName() + "at "
+ String.format(
"(%d,%d,%d)",
aTileEntity.getXCoord(),
aTileEntity.getYCoord(),
aTileEntity.getZCoord())
+ " shut down.");
}
}
handledShutdown = true;
return State.DISABLED.ordinal();
} else {
if ((aInputRedstone > 0) == (state == State.ENABLE_WITH_SIGNAL_SAFE)) {
if (!machine.isAllowedToWork()) {
machine.enableWorking();
handledShutdown = false;
}
} else {
if (machine.isAllowedToWork()) machine.disableWorking();
}
}
return 2;
} else {
return 3 + doCoverThings(side, aInputRedstone, aCoverID, aCoverVariable - 3, aTileEntity, aTimer);
}
}
}
Expand Down

0 comments on commit 99db712

Please sign in to comment.