diff --git a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/energy/EnergyStorageImpl.java b/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/energy/EnergyStorageImpl.java index 829e6eae2..b98e91f2f 100644 --- a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/energy/EnergyStorageImpl.java +++ b/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/energy/EnergyStorageImpl.java @@ -32,7 +32,6 @@ public long receive(final long amount, final Action action) { final long maxReceive = Math.min(amount, spaceRemaining); if (maxReceive > 0 && action == Action.EXECUTE) { stored += maxReceive; - changed(); } return maxReceive; } @@ -42,12 +41,7 @@ public long extract(final long amount, final Action action) { final long maxExtract = Math.min(stored, amount); if (maxExtract > 0 && action == Action.EXECUTE) { stored -= maxExtract; - changed(); } return maxExtract; } - - protected void changed() { - // no op - } } diff --git a/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/energy/EnergyStorageImplTest.java b/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/energy/EnergyStorageImplTest.java index fa043eb9b..558600c51 100644 --- a/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/energy/EnergyStorageImplTest.java +++ b/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/energy/EnergyStorageImplTest.java @@ -14,18 +14,10 @@ class EnergyStorageImplTest { EnergyStorage sut; - int changeCount; @BeforeEach void setUp() { - changeCount = 0; - sut = new EnergyStorageImpl(100) { - @Override - protected void changed() { - super.changed(); - changeCount++; - } - }; + sut = new EnergyStorageImpl(100); } @Test @@ -41,20 +33,13 @@ void testInvalidCapacity() { @EnumSource(Action.class) void shouldNotReceiveEnergyOnZeroCapacityStorage(final Action action) { // Arrange - final EnergyStorage zeroCapacitySut = new EnergyStorageImpl(0) { - @Override - protected void changed() { - super.changed(); - changeCount++; - } - }; + final EnergyStorage zeroCapacitySut = new EnergyStorageImpl(0); // Act final long inserted = zeroCapacitySut.receive(1, action); // Assert assertThat(inserted).isZero(); - assertThat(changeCount).isZero(); assertThat(zeroCapacitySut.getStored()).isZero(); } @@ -69,10 +54,8 @@ void shouldReceiveEnergy(final Action action) { if (action == Action.EXECUTE) { assertThat(sut.getStored()).isEqualTo(50); - assertThat(changeCount).isEqualTo(1); } else { assertThat(sut.getStored()).isZero(); - assertThat(changeCount).isZero(); } } @@ -87,10 +70,8 @@ void shouldReceiveEnergyAndReachCapacity(final Action action) { if (action == Action.EXECUTE) { assertThat(sut.getStored()).isEqualTo(100); - assertThat(changeCount).isEqualTo(1); } else { assertThat(sut.getStored()).isZero(); - assertThat(changeCount).isZero(); } } @@ -105,10 +86,8 @@ void shouldReceiveEnergyAndExceedCapacity(final Action action) { if (action == Action.EXECUTE) { assertThat(sut.getStored()).isEqualTo(100); - assertThat(changeCount).isEqualTo(1); } else { assertThat(sut.getStored()).isZero(); - assertThat(changeCount).isZero(); } } @@ -117,14 +96,12 @@ void shouldReceiveEnergyAndExceedCapacity(final Action action) { void shouldNotReceiveEnergyWhenFull(final Action action) { // Arrange sut.receive(100, Action.EXECUTE); - changeCount = 0; // Act final long inserted = sut.receive(100, action); // Assert assertThat(inserted).isZero(); - assertThat(changeCount).isZero(); assertThat(sut.getStored()).isEqualTo(100); } @@ -133,7 +110,6 @@ void shouldNotReceiveEnergyWhenFull(final Action action) { void shouldExtractEnergyPartly(final Action action) { // Arrange sut.receive(100, Action.EXECUTE); - changeCount = 0; // Act final long extracted = sut.extract(99, action); @@ -143,10 +119,8 @@ void shouldExtractEnergyPartly(final Action action) { if (action == Action.EXECUTE) { assertThat(sut.getStored()).isEqualTo(1); - assertThat(changeCount).isEqualTo(1); } else { assertThat(sut.getStored()).isEqualTo(100); - assertThat(changeCount).isZero(); } } @@ -155,7 +129,6 @@ void shouldExtractEnergyPartly(final Action action) { void shouldExtractEnergyCompletely(final Action action) { // Arrange sut.receive(50, Action.EXECUTE); - changeCount = 0; // Act final long extracted = sut.extract(51, action); @@ -165,10 +138,8 @@ void shouldExtractEnergyCompletely(final Action action) { if (action == Action.EXECUTE) { assertThat(sut.getStored()).isZero(); - assertThat(changeCount).isEqualTo(1); } else { assertThat(sut.getStored()).isEqualTo(50); - assertThat(changeCount).isZero(); } } } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/controller/ControllerBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/controller/ControllerBlockEntity.java index a98759460..ca239b878 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/controller/ControllerBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/controller/ControllerBlockEntity.java @@ -1,6 +1,7 @@ package com.refinedmods.refinedstorage2.platform.common.controller; import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage; +import com.refinedmods.refinedstorage2.api.network.impl.energy.EnergyStorageImpl; import com.refinedmods.refinedstorage2.api.network.impl.node.controller.ControllerNetworkNode; import com.refinedmods.refinedstorage2.platform.api.support.energy.TransferableBlockEntityEnergy; import com.refinedmods.refinedstorage2.platform.common.Platform; @@ -49,7 +50,7 @@ private static EnergyStorage createEnergyStorage(final ControllerType type, fina return CreativeEnergyStorage.INSTANCE; } return new BlockEntityEnergyStorage( - Platform.INSTANCE.getConfig().getController().getEnergyCapacity(), + new EnergyStorageImpl(Platform.INSTANCE.getConfig().getController().getEnergyCapacity()), blockEntity ); } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/AbstractPortableGridBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/AbstractPortableGridBlockEntity.java index 8726ae356..ab17a63d0 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/AbstractPortableGridBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/AbstractPortableGridBlockEntity.java @@ -1,6 +1,7 @@ package com.refinedmods.refinedstorage2.platform.common.storage.portablegrid; import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage; +import com.refinedmods.refinedstorage2.api.network.impl.energy.EnergyStorageImpl; import com.refinedmods.refinedstorage2.platform.api.PlatformApi; import com.refinedmods.refinedstorage2.platform.api.configurationcard.ConfigurationCardTarget; import com.refinedmods.refinedstorage2.platform.api.grid.Grid; @@ -97,7 +98,7 @@ private static EnergyStorage createEnergyStorage(final PortableGridType type, fi return CreativeEnergyStorage.INSTANCE; } return new BlockEntityEnergyStorage( - Platform.INSTANCE.getConfig().getPortableGrid().getEnergyCapacity(), + new EnergyStorageImpl(Platform.INSTANCE.getConfig().getPortableGrid().getEnergyCapacity()), blockEntity ); } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/energy/BlockEntityEnergyStorage.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/energy/BlockEntityEnergyStorage.java index 6515f7ffd..eeb98bd13 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/energy/BlockEntityEnergyStorage.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/energy/BlockEntityEnergyStorage.java @@ -1,20 +1,34 @@ package com.refinedmods.refinedstorage2.platform.common.support.energy; -import com.refinedmods.refinedstorage2.api.network.impl.energy.EnergyStorageImpl; +import com.refinedmods.refinedstorage2.api.core.Action; +import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage; +import com.refinedmods.refinedstorage2.api.network.impl.energy.AbstractProxyEnergyStorage; import net.minecraft.world.level.block.entity.BlockEntity; -public class BlockEntityEnergyStorage extends EnergyStorageImpl { +public class BlockEntityEnergyStorage extends AbstractProxyEnergyStorage { private final BlockEntity blockEntity; - public BlockEntityEnergyStorage(final long capacity, final BlockEntity blockEntity) { - super(capacity); + public BlockEntityEnergyStorage(final EnergyStorage delegate, final BlockEntity blockEntity) { + super(delegate); this.blockEntity = blockEntity; } @Override - protected void changed() { - super.changed(); - blockEntity.setChanged(); + public long receive(final long amount, final Action action) { + final long received = super.receive(amount, action); + if (received > 0 && action == Action.EXECUTE) { + blockEntity.setChanged(); + } + return received; + } + + @Override + public long extract(final long amount, final Action action) { + final long extracted = super.extract(amount, action); + if (extracted > 0 && action == Action.EXECUTE) { + blockEntity.setChanged(); + } + return extracted; } }