Skip to content

Commit

Permalink
Merge pull request #478 from refinedmods/fix/GH-477/wrenching
Browse files Browse the repository at this point in the history
Transfer energy in wrench dismantling
  • Loading branch information
raoulvdberge authored Feb 19, 2024
2 parents a7b97e4 + c4c807a commit 8360d8a
Show file tree
Hide file tree
Showing 16 changed files with 203 additions and 126 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- Fixed losing disk when using Wrench dismantling on the Portable Grid.
- Fixed losing energy when using Wrench dismantling on the Portable Grid and the Controller.

## [2.0.0-milestone.3.3] - 2024-02-17

### Added
Expand Down
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
Expand Up @@ -53,6 +53,7 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntityType;
import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.1.0")
Expand Down Expand Up @@ -165,6 +166,12 @@ GridScrollingStrategy createGridScrollingStrategy(AbstractContainerMenu containe

EnergyStorage asItemEnergyStorage(EnergyStorage energyStorage, ItemStack stack);

EnergyStorage asBlockItemEnergyStorage(
EnergyStorage energyStorage,
ItemStack stack,
BlockEntityType<?> blockEntityType
);

NetworkBoundItemHelper getNetworkBoundItemHelper();

PlatformRegistry<SlotReferenceFactory> getSlotReferenceFactoryRegistry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntityType;

public class PlatformApiProxy implements PlatformApi {
@Nullable
Expand Down Expand Up @@ -324,6 +325,13 @@ public EnergyStorage asItemEnergyStorage(final EnergyStorage energyStorage, fina
return ensureLoaded().asItemEnergyStorage(energyStorage, stack);
}

@Override
public EnergyStorage asBlockItemEnergyStorage(final EnergyStorage energyStorage,
final ItemStack stack,
final BlockEntityType<?> blockEntityType) {
return ensureLoaded().asBlockItemEnergyStorage(energyStorage, stack, blockEntityType);
}

@Override
public NetworkBoundItemHelper getNetworkBoundItemHelper() {
return ensureLoaded().getNetworkBoundItemHelper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import com.refinedmods.refinedstorage2.platform.common.storagemonitor.CompositeStorageMonitorExtractionStrategy;
import com.refinedmods.refinedstorage2.platform.common.storagemonitor.CompositeStorageMonitorInsertionStrategy;
import com.refinedmods.refinedstorage2.platform.common.support.energy.EnergyItemHelperImpl;
import com.refinedmods.refinedstorage2.platform.common.support.energy.ItemBlockEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.support.energy.ItemEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.support.network.ConnectionProviderImpl;
import com.refinedmods.refinedstorage2.platform.common.support.network.bounditem.CompositeSlotReferenceProvider;
Expand Down Expand Up @@ -101,6 +102,7 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.saveddata.SavedData;

import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createIdentifier;
Expand Down Expand Up @@ -468,6 +470,13 @@ public EnergyStorage asItemEnergyStorage(final EnergyStorage energyStorage,
return new ItemEnergyStorage(stack, energyStorage);
}

@Override
public EnergyStorage asBlockItemEnergyStorage(final EnergyStorage energyStorage,
final ItemStack stack,
final BlockEntityType<?> blockEntityType) {
return new ItemBlockEnergyStorage(energyStorage, stack, blockEntityType);
}

@Override
public NetworkBoundItemHelper getNetworkBoundItemHelper() {
return networkBoundItemHelper;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.refinedmods.refinedstorage2.platform.common.controller;

import com.refinedmods.refinedstorage2.api.core.Action;
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 All @@ -10,6 +10,7 @@
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ExtendedMenuProvider;
import com.refinedmods.refinedstorage2.platform.common.support.energy.BlockEntityEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.support.energy.CreativeEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.support.energy.ItemBlockEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.support.network.AbstractRedstoneModeNetworkNodeContainerBlockEntity;

import com.google.common.util.concurrent.RateLimiter;
Expand All @@ -31,7 +32,6 @@ public class ControllerBlockEntity extends AbstractRedstoneModeNetworkNodeContai
implements ExtendedMenuProvider, TransferableBlockEntityEnergy {
private static final Logger LOGGER = LoggerFactory.getLogger(ControllerBlockEntity.class);

private static final String TAG_STORED = "stored";
private static final String TAG_CAPACITY = "capacity";

private final ControllerType type;
Expand All @@ -50,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 Expand Up @@ -78,7 +78,7 @@ public void updateEnergyTypeInLevel(final BlockState state) {
@Override
public void saveAdditional(final CompoundTag tag) {
super.saveAdditional(tag);
tag.putLong(TAG_STORED, getNode().getActualStored());
ItemBlockEnergyStorage.writeToTag(tag, getNode().getActualStored());
saveRenderingInfo(tag);
}

Expand All @@ -89,9 +89,7 @@ private void saveRenderingInfo(final CompoundTag tag) {
@Override
public void load(final CompoundTag tag) {
super.load(tag);
if (tag.contains(TAG_STORED)) {
energyStorage.receive(tag.getLong(TAG_STORED), Action.EXECUTE);
}
ItemBlockEnergyStorage.readFromTag(energyStorage, tag);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.refinedmods.refinedstorage2.platform.api.support.HelpTooltipComponent;
import com.refinedmods.refinedstorage2.platform.api.support.energy.AbstractEnergyBlockItem;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;

import java.util.Optional;

Expand Down Expand Up @@ -44,6 +45,10 @@ public EnergyStorage createEnergyStorage(final ItemStack stack) {
final EnergyStorage energyStorage = new EnergyStorageImpl(
Platform.INSTANCE.getConfig().getController().getEnergyCapacity()
);
return PlatformApi.INSTANCE.asItemEnergyStorage(energyStorage, stack);
return PlatformApi.INSTANCE.asBlockItemEnergyStorage(
energyStorage,
stack,
BlockEntities.INSTANCE.getController()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.refinedmods.refinedstorage2.platform.common.storage.portablegrid;

import com.refinedmods.refinedstorage2.api.core.Action;
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 All @@ -18,6 +18,7 @@
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ExtendedMenuProvider;
import com.refinedmods.refinedstorage2.platform.common.support.energy.BlockEntityEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.support.energy.CreativeEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.support.energy.ItemBlockEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.util.ContainerUtil;

import javax.annotation.Nullable;
Expand All @@ -34,6 +35,7 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
Expand All @@ -47,7 +49,6 @@ public abstract class AbstractPortableGridBlockEntity extends BlockEntity implem

private static final String TAG_DISK_INVENTORY = "inv";
private static final String TAG_DISKS = "disks";
private static final String TAG_STORED = "stored";
private static final String TAG_REDSTONE_MODE = "rm";

@Nullable
Expand Down Expand Up @@ -75,12 +76,29 @@ public boolean isGridActive() {
};
}

static void readDiskInventory(final CompoundTag tag, final DiskInventory diskInventory) {
if (tag.contains(TAG_DISK_INVENTORY)) {
ContainerUtil.read(tag.getCompound(TAG_DISK_INVENTORY), diskInventory);
}
}

static void writeDiskInventory(final CompoundTag tag, final DiskInventory diskInventory) {
tag.put(TAG_DISK_INVENTORY, ContainerUtil.write(diskInventory));
}

static ItemStack getDisk(final CompoundTag tag) {
if (!tag.contains(TAG_DISK_INVENTORY)) {
return ItemStack.EMPTY;
}
return ContainerUtil.getItemInSlot(tag.getCompound(TAG_DISK_INVENTORY), 0);
}

private static EnergyStorage createEnergyStorage(final PortableGridType type, final BlockEntity blockEntity) {
if (type == PortableGridType.CREATIVE) {
return CreativeEnergyStorage.INSTANCE;
}
return new BlockEntityEnergyStorage(
Platform.INSTANCE.getConfig().getPortableGrid().getEnergyCapacity(),
new EnergyStorageImpl(Platform.INSTANCE.getConfig().getPortableGrid().getEnergyCapacity()),
blockEntity
);
}
Expand Down Expand Up @@ -137,12 +155,8 @@ private void initialize(final Level level) {
@Override
public void load(final CompoundTag tag) {
fromClientTag(tag);
if (tag.contains(TAG_DISK_INVENTORY)) {
ContainerUtil.read(tag.getCompound(TAG_DISK_INVENTORY), diskInventory);
}
if (tag.contains(TAG_STORED)) {
energyStorage.receive(tag.getLong(TAG_STORED), Action.EXECUTE);
}
readDiskInventory(tag, diskInventory);
ItemBlockEnergyStorage.readFromTag(energyStorage, tag);
readConfiguration(tag);
super.load(tag);
}
Expand All @@ -169,8 +183,8 @@ protected void onClientDriveStateUpdated() {
@Override
public void saveAdditional(final CompoundTag tag) {
super.saveAdditional(tag);
tag.put(TAG_DISK_INVENTORY, ContainerUtil.write(diskInventory));
tag.putLong(TAG_STORED, energyStorage.getStored());
writeDiskInventory(tag, diskInventory);
ItemBlockEnergyStorage.writeToTag(tag, energyStorage.getStored());
writeConfiguration(tag);
}

Expand Down
Loading

0 comments on commit 8360d8a

Please sign in to comment.