Skip to content

Commit

Permalink
refactor: use energy proxy for block entity change detection
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Feb 19, 2024
1 parent 3c6874b commit c4c807a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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);
}

Expand All @@ -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);
Expand All @@ -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();
}
}

Expand All @@ -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);
Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit c4c807a

Please sign in to comment.