Fix memory leak with CapManipulationBehaviourBase

Fixes #7023
This commit is contained in:
IThundxr 2024-10-06 13:31:18 -04:00
parent e3331d363d
commit b78688e944
Failed to generate hash of commit
5 changed files with 35 additions and 4 deletions

View file

@ -120,6 +120,12 @@ public class MechanicalCrafterBlockEntity extends KineticBlockEntity {
registerAwardables(behaviours, AllAdvancements.CRAFTER, AllAdvancements.CRAFTER_LAZY);
}
@Override
public void invalidateCaps() {
inserting.removeListener();
super.invalidateCaps();
}
@Override
public void onSpeedChanged(float previousSpeed) {
super.onSpeedChanged(previousSpeed);
@ -132,6 +138,7 @@ public class MechanicalCrafterBlockEntity extends KineticBlockEntity {
public void blockChanged() {
removeBehaviour(InvManipulationBehaviour.TYPE);
inserting.removeListener();
inserting = new InvManipulationBehaviour(this, this::getTargetFace);
attachBehaviourLate(inserting);
}

View file

@ -273,6 +273,12 @@ public class FunnelBlockEntity extends SmartBlockEntity implements IHaveHovering
registerAwardables(behaviours, AllAdvancements.FUNNEL);
}
@Override
public void invalidateCaps() {
invManipulation.removeListener();
super.invalidateCaps();
}
private boolean supportsAmountOnFilter() {
BlockState blockState = getBlockState();
boolean beltFunnelsupportsAmount = false;

View file

@ -32,10 +32,10 @@ public class SmartObserverBlockEntity extends SmartBlockEntity {
private FilteringBehaviour filtering;
private InvManipulationBehaviour observedInventory;
private TankManipulationBehaviour observedTank;
private VersionedInventoryTrackerBehaviour invVersionTracker;
private boolean sustainSignal;
public int turnOffTicks = 0;
public SmartObserverBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
@ -56,13 +56,20 @@ public class SmartObserverBlockEntity extends SmartBlockEntity {
behaviours.add(observedTank = new TankManipulationBehaviour(this, towardBlockFacing).bypassSidedness());
}
@Override
public void invalidateCaps() {
observedInventory.removeListener();
observedTank.removeListener();
super.invalidateCaps();
}
@Override
public void tick() {
super.tick();
if (level.isClientSide())
return;
BlockState state = getBlockState();
if (turnOffTicks > 0) {
turnOffTicks--;

View file

@ -233,6 +233,13 @@ public class ThresholdSwitchBlockEntity extends SmartBlockEntity {
behaviours.add(observedTank = new TankManipulationBehaviour(this, towardBlockFacing).bypassSidedness());
}
@Override
public void invalidateCaps() {
observedInventory.removeListener();
observedTank.removeListener();
super.invalidateCaps();
}
public float getLevelForDisplay() {
return currentLevel == -1 ? 0 : currentLevel;
}

View file

@ -80,6 +80,10 @@ public abstract class CapManipulationBehaviourBase<T, S extends CapManipulationB
targetCapability = LazyOptional.empty();
}
public void removeListener() {
targetCapability.removeListener(new HashableNonNullConsumer<>(this::onHandlerInvalidated, this));
}
@Override
public void lazyTick() {
super.lazyTick();