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 *********// //////////////////////////////////////