Skip to content

Commit

Permalink
Merge pull request #479 from refinedmods/fix/GH-477/wrenching-other
Browse files Browse the repository at this point in the history
refactor: introduce listening energy storage
  • Loading branch information
raoulvdberge authored Feb 19, 2024
2 parents 8360d8a + d1402fd commit bdbbc6c
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.refinedmods.refinedstorage2.api.network.impl.energy;

import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage;

public abstract class AbstractListeningEnergyStorage extends AbstractProxyEnergyStorage {
protected AbstractListeningEnergyStorage(final EnergyStorage delegate) {
super(delegate);
}

@Override
public long receive(final long amount, final Action action) {
final long received = super.receive(amount, action);
if (received > 0 && action == Action.EXECUTE) {
onStoredChanged(getStored());
}
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) {
onStoredChanged(getStored());
}
return extracted;
}

protected abstract void onStoredChanged(long stored);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package com.refinedmods.refinedstorage2.api.network.impl.energy;

import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import static org.assertj.core.api.Assertions.assertThat;

class ListeningEnergyStorageTest {
EnergyStorage sut;
int changeCount;

@BeforeEach
void setUp() {
sut = new AbstractListeningEnergyStorage(new EnergyStorageImpl(100)) {
@Override
protected void onStoredChanged(final long stored) {
changeCount++;
}
};
}

@ParameterizedTest
@EnumSource(Action.class)
void shouldReceiveEnergy(final Action action) {
// Act
final long inserted = sut.receive(50, action);

// Assert
assertThat(inserted).isEqualTo(50);

if (action == Action.EXECUTE) {
assertThat(sut.getStored()).isEqualTo(50);
assertThat(changeCount).isEqualTo(1);
} else {
assertThat(sut.getStored()).isZero();
assertThat(changeCount).isZero();
}
}

@ParameterizedTest
@EnumSource(Action.class)
void shouldReceiveEnergyAndReachCapacity(final Action action) {
// Act
final long inserted = sut.receive(100, action);

// Assert
assertThat(inserted).isEqualTo(100);

if (action == Action.EXECUTE) {
assertThat(sut.getStored()).isEqualTo(100);
assertThat(changeCount).isEqualTo(1);
} else {
assertThat(sut.getStored()).isZero();
assertThat(changeCount).isZero();
}
}

@ParameterizedTest
@EnumSource(Action.class)
void shouldReceiveEnergyAndExceedCapacity(final Action action) {
// Act
final long inserted = sut.receive(101, action);

// Assert
assertThat(inserted).isEqualTo(100);

if (action == Action.EXECUTE) {
assertThat(sut.getStored()).isEqualTo(100);
assertThat(changeCount).isEqualTo(1);
} else {
assertThat(sut.getStored()).isZero();
assertThat(changeCount).isZero();
}
}

@ParameterizedTest
@EnumSource(Action.class)
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);
}

@ParameterizedTest
@EnumSource(Action.class)
void shouldExtractEnergyPartly(final Action action) {
// Arrange
sut.receive(100, Action.EXECUTE);
changeCount = 0;

// Act
final long extracted = sut.extract(99, action);

// Assert
assertThat(extracted).isEqualTo(99);

if (action == Action.EXECUTE) {
assertThat(sut.getStored()).isEqualTo(1);
assertThat(changeCount).isEqualTo(1);
} else {
assertThat(sut.getStored()).isEqualTo(100);
assertThat(changeCount).isZero();
}
}

@ParameterizedTest
@EnumSource(Action.class)
void shouldExtractEnergyCompletely(final Action action) {
// Arrange
sut.receive(50, Action.EXECUTE);
changeCount = 0;

// Act
final long extracted = sut.extract(51, action);

// Assert
assertThat(extracted).isEqualTo(50);

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,12 +1,11 @@
package com.refinedmods.refinedstorage2.platform.common.support.energy;

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 com.refinedmods.refinedstorage2.api.network.impl.energy.AbstractListeningEnergyStorage;

import net.minecraft.world.level.block.entity.BlockEntity;

public class BlockEntityEnergyStorage extends AbstractProxyEnergyStorage {
public class BlockEntityEnergyStorage extends AbstractListeningEnergyStorage {
private final BlockEntity blockEntity;

public BlockEntityEnergyStorage(final EnergyStorage delegate, final BlockEntity blockEntity) {
Expand All @@ -15,20 +14,7 @@ public BlockEntityEnergyStorage(final EnergyStorage delegate, final BlockEntity
}

@Override
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;
protected void onStoredChanged(final long stored) {
blockEntity.setChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

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 com.refinedmods.refinedstorage2.api.network.impl.energy.AbstractListeningEnergyStorage;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntityType;

public class ItemBlockEnergyStorage extends AbstractProxyEnergyStorage {
public class ItemBlockEnergyStorage extends AbstractListeningEnergyStorage {
private static final String TAG_STORED = "stored";

private final ItemStack stack;
Expand All @@ -28,29 +28,12 @@ public ItemBlockEnergyStorage(final EnergyStorage energyStorage,
}

@Override
public long receive(final long amount, final Action action) {
final long received = super.receive(amount, action);
if (received > 0 && action == Action.EXECUTE) {
updateStored();
}
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) {
updateStored();
}
return extracted;
}

private void updateStored() {
protected void onStoredChanged(final long stored) {
CompoundTag tag = BlockItem.getBlockEntityData(stack);
if (tag == null) {
tag = new CompoundTag();
}
writeToTag(tag, getStored());
writeToTag(tag, stored);
BlockItem.setBlockEntityData(stack, blockEntityType, tag);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

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 com.refinedmods.refinedstorage2.api.network.impl.energy.AbstractListeningEnergyStorage;

import net.minecraft.world.item.ItemStack;

public class ItemEnergyStorage extends AbstractProxyEnergyStorage {
public class ItemEnergyStorage extends AbstractListeningEnergyStorage {
private static final String TAG_STORED = "stored";

private final ItemStack stack;
Expand All @@ -21,24 +21,7 @@ public ItemEnergyStorage(final ItemStack stack, final EnergyStorage delegate) {
}

@Override
public long receive(final long amount, final Action action) {
final long received = super.receive(amount, action);
if (received > 0 && action == Action.EXECUTE) {
updateStored();
}
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) {
updateStored();
}
return extracted;
}

private void updateStored() {
stack.getOrCreateTag().putLong(TAG_STORED, getStored());
protected void onStoredChanged(final long stored) {
stack.getOrCreateTag().putLong(TAG_STORED, stored);
}
}

0 comments on commit bdbbc6c

Please sign in to comment.