From 9e7e694dbc0ea378116332f8525e8da59177f74e Mon Sep 17 00:00:00 2001 From: SmallY Date: Tue, 16 Apr 2024 21:45:37 +0800 Subject: [PATCH 1/3] Fix that kinetic stress output will never not stop --- .../multiblock/part/KineticPartMachine.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/main/java/com/gregtechceu/gtceu/common/machine/multiblock/part/KineticPartMachine.java b/src/main/java/com/gregtechceu/gtceu/common/machine/multiblock/part/KineticPartMachine.java index 98720ac072..8ed7981c9f 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/machine/multiblock/part/KineticPartMachine.java +++ b/src/main/java/com/gregtechceu/gtceu/common/machine/multiblock/part/KineticPartMachine.java @@ -2,6 +2,9 @@ import com.gregtechceu.gtceu.api.capability.recipe.IO; import com.gregtechceu.gtceu.api.machine.IMachineBlockEntity; +import com.gregtechceu.gtceu.api.machine.TickableSubscription; +import com.gregtechceu.gtceu.api.machine.feature.multiblock.IMultiController; +import com.gregtechceu.gtceu.api.machine.feature.multiblock.IWorkableMultiController; import com.gregtechceu.gtceu.api.machine.multiblock.part.TieredIOPartMachine; import com.gregtechceu.gtceu.common.machine.kinetic.IKineticMachine; import com.gregtechceu.gtceu.common.machine.trait.NotifiableStressTrait; @@ -14,6 +17,7 @@ import net.minecraft.world.entity.player.Player; import net.minecraft.world.phys.BlockHitResult; +import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; /** @@ -30,9 +34,13 @@ public class KineticPartMachine extends TieredIOPartMachine implements IKineticM @Persisted protected final NotifiableStressTrait stressTrait; + @Nullable + private TickableSubscription stopWorkingSub; + public KineticPartMachine(IMachineBlockEntity holder, int tier, IO io, Object... args) { super(holder, tier, io); this.stressTrait = createStressTrait(args); + stopWorkingSub = subscribeServerTick(stopWorkingSub, this::stopWhenControllerNotWorking); } ////////////////////////////////////// @@ -62,6 +70,47 @@ public void onRotated(Direction oldFacing, Direction newFacing) { } } + @Override + public boolean onWorking(IWorkableMultiController controller) { + if (stopWorkingSub == null) { + stopWorkingSub = subscribeServerTick(this::stopWhenControllerNotWorking); + } + return super.onWorking(controller); + } + + private void stopWhenControllerNotWorking() { + for (IMultiController controller : getControllers()) { + if (controller instanceof IWorkableMultiController workableMultiController) { + if (!workableMultiController.getRecipeLogic().isWorking()) { + getKineticHolder().stopWorking(); + assert stopWorkingSub != null; + stopWorkingSub.unsubscribe(); + stopWorkingSub = null; + } + } + } + } + + @Override + public boolean onWaiting(IWorkableMultiController controller) { + getKineticHolder().stopWorking(); + return super.onWaiting(controller); + } + + @Override + public void removedFromController(IMultiController controller) { + super.removedFromController(controller); + getKineticHolder().stopWorking(); + } + + @Override + public void setWorkingEnabled(boolean workingEnabled) { + if (!workingEnabled) { + getKineticHolder().stopWorking(); + } + super.setWorkingEnabled(workingEnabled); + } + ////////////////////////////////////// //********* GUI *********// ////////////////////////////////////// From 53cc694d990e8ef220bdd9d8e316768d567b30cf Mon Sep 17 00:00:00 2001 From: SmallY Date: Thu, 18 Apr 2024 15:19:55 +0800 Subject: [PATCH 2/3] Add onPaused callback which is called when the WorkableMultiblockMachine set workingEnabled to false --- .../feature/multiblock/IMultiPart.java | 6 ++++ .../multiblock/WorkableMultiblockMachine.java | 10 ++++++ .../multiblock/part/KineticPartMachine.java | 31 +++---------------- 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/gregtechceu/gtceu/api/machine/feature/multiblock/IMultiPart.java b/src/main/java/com/gregtechceu/gtceu/api/machine/feature/multiblock/IMultiPart.java index 99f2f620bd..088184aa16 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/machine/feature/multiblock/IMultiPart.java +++ b/src/main/java/com/gregtechceu/gtceu/api/machine/feature/multiblock/IMultiPart.java @@ -3,6 +3,7 @@ import com.gregtechceu.gtceu.api.gui.fancy.TooltipsPanel; import com.gregtechceu.gtceu.api.machine.feature.IFancyUIMachine; import com.gregtechceu.gtceu.api.machine.feature.IMachineFeature; +import com.gregtechceu.gtceu.api.machine.multiblock.WorkableMultiblockMachine; import com.gregtechceu.gtceu.api.machine.trait.IRecipeHandlerTrait; import com.gregtechceu.gtceu.api.machine.trait.RecipeLogic; import com.gregtechceu.gtceu.api.recipe.GTRecipe; @@ -91,6 +92,11 @@ default boolean onWaiting(IWorkableMultiController controller) { return true; } + /** + * Called in {@link WorkableMultiblockMachine#setWorkingEnabled(boolean)} + */ + default boolean onPaused(IWorkableMultiController controller) { return true; } + /** * Called in {@link RecipeLogic#onRecipeFinish()} before outputs are produced */ diff --git a/src/main/java/com/gregtechceu/gtceu/api/machine/multiblock/WorkableMultiblockMachine.java b/src/main/java/com/gregtechceu/gtceu/api/machine/multiblock/WorkableMultiblockMachine.java index aaf8978a11..d96751bb7f 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/machine/multiblock/WorkableMultiblockMachine.java +++ b/src/main/java/com/gregtechceu/gtceu/api/machine/multiblock/WorkableMultiblockMachine.java @@ -255,6 +255,16 @@ public void onWaiting() { IWorkableMultiController.super.onWaiting(); } + @Override + public void setWorkingEnabled(boolean isWorkingAllowed) { + if (!isWorkingAllowed) { + for (IMultiPart part : getParts()) { + part.onPaused(this); + } + } + IWorkableMultiController.super.setWorkingEnabled(isWorkingAllowed); + } + @NotNull public GTRecipeType getRecipeType() { return recipeTypes[activeRecipeType]; diff --git a/src/main/java/com/gregtechceu/gtceu/common/machine/multiblock/part/KineticPartMachine.java b/src/main/java/com/gregtechceu/gtceu/common/machine/multiblock/part/KineticPartMachine.java index 8ed7981c9f..a177f68116 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/machine/multiblock/part/KineticPartMachine.java +++ b/src/main/java/com/gregtechceu/gtceu/common/machine/multiblock/part/KineticPartMachine.java @@ -2,7 +2,6 @@ import com.gregtechceu.gtceu.api.capability.recipe.IO; import com.gregtechceu.gtceu.api.machine.IMachineBlockEntity; -import com.gregtechceu.gtceu.api.machine.TickableSubscription; import com.gregtechceu.gtceu.api.machine.feature.multiblock.IMultiController; import com.gregtechceu.gtceu.api.machine.feature.multiblock.IWorkableMultiController; import com.gregtechceu.gtceu.api.machine.multiblock.part.TieredIOPartMachine; @@ -17,7 +16,6 @@ import net.minecraft.world.entity.player.Player; import net.minecraft.world.phys.BlockHitResult; -import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; /** @@ -34,13 +32,9 @@ public class KineticPartMachine extends TieredIOPartMachine implements IKineticM @Persisted protected final NotifiableStressTrait stressTrait; - @Nullable - private TickableSubscription stopWorkingSub; - public KineticPartMachine(IMachineBlockEntity holder, int tier, IO io, Object... args) { super(holder, tier, io); this.stressTrait = createStressTrait(args); - stopWorkingSub = subscribeServerTick(stopWorkingSub, this::stopWhenControllerNotWorking); } ////////////////////////////////////// @@ -71,30 +65,15 @@ public void onRotated(Direction oldFacing, Direction newFacing) { } @Override - public boolean onWorking(IWorkableMultiController controller) { - if (stopWorkingSub == null) { - stopWorkingSub = subscribeServerTick(this::stopWhenControllerNotWorking); - } - return super.onWorking(controller); - } - - private void stopWhenControllerNotWorking() { - for (IMultiController controller : getControllers()) { - if (controller instanceof IWorkableMultiController workableMultiController) { - if (!workableMultiController.getRecipeLogic().isWorking()) { - getKineticHolder().stopWorking(); - assert stopWorkingSub != null; - stopWorkingSub.unsubscribe(); - stopWorkingSub = null; - } - } - } + public boolean onWaiting(IWorkableMultiController controller) { + getKineticHolder().stopWorking(); + return super.onWaiting(controller); } @Override - public boolean onWaiting(IWorkableMultiController controller) { + public boolean onPaused(IWorkableMultiController controller) { getKineticHolder().stopWorking(); - return super.onWaiting(controller); + return super.onPaused(controller); } @Override From 49311fc47e4cb06b85ec6f5e0842b14edd9ec34f Mon Sep 17 00:00:00 2001 From: SmallY Date: Fri, 19 Apr 2024 18:31:57 +0800 Subject: [PATCH 3/3] Format code --- .../gtceu/api/machine/feature/multiblock/IMultiPart.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/gregtechceu/gtceu/api/machine/feature/multiblock/IMultiPart.java b/src/main/java/com/gregtechceu/gtceu/api/machine/feature/multiblock/IMultiPart.java index 088184aa16..87a3c95b8b 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/machine/feature/multiblock/IMultiPart.java +++ b/src/main/java/com/gregtechceu/gtceu/api/machine/feature/multiblock/IMultiPart.java @@ -95,7 +95,9 @@ default boolean onWaiting(IWorkableMultiController controller) { /** * Called in {@link WorkableMultiblockMachine#setWorkingEnabled(boolean)} */ - default boolean onPaused(IWorkableMultiController controller) { return true; } + default boolean onPaused(IWorkableMultiController controller) { + return true; + } /** * Called in {@link RecipeLogic#onRecipeFinish()} before outputs are produced