diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f0b305d2..538735bca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 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-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/PlatformApi.java b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/PlatformApi.java index 72e3cf9f6..6f3c71781 100644 --- a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/PlatformApi.java +++ b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/PlatformApi.java @@ -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") @@ -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 getSlotReferenceFactoryRegistry(); diff --git a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/PlatformApiProxy.java b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/PlatformApiProxy.java index 9820d7009..de1375c99 100644 --- a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/PlatformApiProxy.java +++ b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/PlatformApiProxy.java @@ -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 @@ -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(); diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/PlatformApiImpl.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/PlatformApiImpl.java index de8798b0a..4049dbff1 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/PlatformApiImpl.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/PlatformApiImpl.java @@ -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; @@ -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; @@ -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; 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 fc7d3e72d..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,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; @@ -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; @@ -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; @@ -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 ); } @@ -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); } @@ -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 diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/controller/ControllerBlockItem.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/controller/ControllerBlockItem.java index 752f57d1e..d78218c88 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/controller/ControllerBlockItem.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/controller/ControllerBlockItem.java @@ -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; @@ -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() + ); } } 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 64ab1e085..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,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; @@ -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; @@ -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; @@ -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 @@ -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 ); } @@ -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); } @@ -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); } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridBlockItem.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridBlockItem.java index 879c85d66..8090bf465 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridBlockItem.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridBlockItem.java @@ -12,15 +12,14 @@ import com.refinedmods.refinedstorage2.platform.api.support.energy.AbstractEnergyBlockItem; import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.SlotReference; import com.refinedmods.refinedstorage2.platform.common.Platform; +import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities; import com.refinedmods.refinedstorage2.platform.common.storage.Disk; import com.refinedmods.refinedstorage2.platform.common.storage.DiskInventory; import com.refinedmods.refinedstorage2.platform.common.support.energy.CreativeEnergyStorage; -import com.refinedmods.refinedstorage2.platform.common.support.energy.ItemEnergyStorage; -import com.refinedmods.refinedstorage2.platform.common.util.ContainerUtil; import javax.annotation.Nullable; -import net.minecraft.core.BlockPos; +import net.minecraft.nbt.CompoundTag; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResultHolder; @@ -29,11 +28,8 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.state.BlockState; public class PortableGridBlockItem extends AbstractEnergyBlockItem { - private static final String TAG_DISK_INVENTORY = "diskinv"; - private final PortableGridType type; public PortableGridBlockItem(final Block block, final PortableGridType type) { @@ -43,9 +39,8 @@ public PortableGridBlockItem(final Block block, final PortableGridType type) { public static PortableGridBlockItemRenderInfo getRenderInfo(final ItemStack stack, @Nullable final Level level) { - final boolean creative = stack.getItem() instanceof PortableGridBlockItem portableGridBlockItem - && portableGridBlockItem.type == PortableGridType.CREATIVE; - final boolean hasEnergy = creative || ItemEnergyStorage.getStored(stack) > 0; + final boolean creative = isCreative(stack); + final boolean hasEnergy = creative || createEnergyStorage(stack).getStored() > 0; final ItemStack diskStack = getDisk(stack); final boolean active = hasEnergy && !diskStack.isEmpty(); final Disk disk = new Disk( @@ -55,6 +50,11 @@ public static PortableGridBlockItemRenderInfo getRenderInfo(final ItemStack stac return new PortableGridBlockItemRenderInfo(active, disk); } + private static boolean isCreative(final ItemStack stack) { + return stack.getItem() instanceof PortableGridBlockItem portableGridBlockItem + && portableGridBlockItem.type == PortableGridType.CREATIVE; + } + private static StorageState getState(final ItemStack diskStack, final boolean active, @Nullable final Level level) { @@ -71,41 +71,34 @@ private static StorageState getState(final ItemStack diskStack, } private static ItemStack getDisk(final ItemStack stack) { - if (stack.getTag() == null) { - return ItemStack.EMPTY; - } - if (!stack.getTag().contains(TAG_DISK_INVENTORY)) { + final CompoundTag tag = getBlockEntityData(stack); + if (tag == null) { return ItemStack.EMPTY; } - return ContainerUtil.getItemInSlot(stack.getTag().getCompound(TAG_DISK_INVENTORY), 0); + return AbstractPortableGridBlockEntity.getDisk(tag); } static void setDiskInventory(final ItemStack stack, final DiskInventory diskInventory) { - stack.getOrCreateTag().put(TAG_DISK_INVENTORY, ContainerUtil.write(diskInventory)); + final CompoundTag tag = new CompoundTag(); + AbstractPortableGridBlockEntity.writeDiskInventory(tag, diskInventory); + setBlockEntityData( + stack, + isCreative(stack) + ? BlockEntities.INSTANCE.getCreativePortableGrid() + : BlockEntities.INSTANCE.getPortableGrid(), + tag + ); } - public EnergyStorage createEnergyStorage(final ItemStack stack) { + public static EnergyStorage createEnergyStorage(final ItemStack stack) { final EnergyStorage energyStorage = new EnergyStorageImpl( Platform.INSTANCE.getConfig().getPortableGrid().getEnergyCapacity() ); - return PlatformApi.INSTANCE.asItemEnergyStorage(energyStorage, stack); - } - - @Override - protected boolean updateCustomBlockEntityTag( - final BlockPos pos, - final Level level, - @Nullable final Player player, - final ItemStack stack, - final BlockState blockState - ) { - final boolean result = super.updateCustomBlockEntityTag(pos, level, player, stack, blockState); - if (!level.isClientSide() - && level.getBlockEntity(pos) instanceof AbstractPortableGridBlockEntity portableGrid) { - final ItemStack diskStack = getDisk(stack); - portableGrid.getDiskInventory().setItem(0, diskStack); - } - return result; + return PlatformApi.INSTANCE.asBlockItemEnergyStorage( + energyStorage, + stack, + BlockEntities.INSTANCE.getPortableGrid() + ); } @Override @@ -145,8 +138,9 @@ private PortableGridEnergyStorage createEnergyStorageInternal(final ItemStack st private DiskInventory createDiskInventory(final ItemStack stack, final DiskInventoryListenerImpl listener) { final DiskInventory diskInventory = new DiskInventory(listener, 1); - if (stack.getTag() != null && stack.getTag().contains(TAG_DISK_INVENTORY)) { - ContainerUtil.read(stack.getTag().getCompound(TAG_DISK_INVENTORY), diskInventory); + final CompoundTag tag = getBlockEntityData(stack); + if (tag != null) { + AbstractPortableGridBlockEntity.readDiskInventory(tag, diskInventory); } return diskInventory; } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridLootItemFunction.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridLootItemFunction.java index d6335c96c..941eef37d 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridLootItemFunction.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridLootItemFunction.java @@ -1,15 +1,15 @@ package com.refinedmods.refinedstorage2.platform.common.storage.portablegrid; import com.refinedmods.refinedstorage2.platform.common.content.LootFunctions; +import com.refinedmods.refinedstorage2.platform.common.support.energy.EnergyLootItemFunction; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.storage.loot.LootContext; -import net.minecraft.world.level.storage.loot.functions.LootItemFunction; import net.minecraft.world.level.storage.loot.functions.LootItemFunctionType; import net.minecraft.world.level.storage.loot.parameters.LootContextParams; -public class PortableGridLootItemFunction implements LootItemFunction { +public class PortableGridLootItemFunction extends EnergyLootItemFunction { @Override public LootItemFunctionType getType() { return LootFunctions.INSTANCE.getPortableGrid(); @@ -21,6 +21,6 @@ public ItemStack apply(final ItemStack itemStack, final LootContext lootContext) if (blockEntity instanceof AbstractPortableGridBlockEntity portableGrid) { PortableGridBlockItem.setDiskInventory(itemStack, portableGrid.getDiskInventory()); } - return itemStack; + return super.apply(itemStack, lootContext); } } 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; } } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/energy/ItemBlockEnergyStorage.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/energy/ItemBlockEnergyStorage.java new file mode 100644 index 000000000..3756d2955 --- /dev/null +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/energy/ItemBlockEnergyStorage.java @@ -0,0 +1,65 @@ +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 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 { + private static final String TAG_STORED = "stored"; + + private final ItemStack stack; + private final BlockEntityType blockEntityType; + + public ItemBlockEnergyStorage(final EnergyStorage energyStorage, + final ItemStack stack, + final BlockEntityType blockEntityType) { + super(energyStorage); + this.stack = stack; + this.blockEntityType = blockEntityType; + final CompoundTag tag = BlockItem.getBlockEntityData(stack); + if (tag != null) { + readFromTag(energyStorage, tag); + } + } + + @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() { + CompoundTag tag = BlockItem.getBlockEntityData(stack); + if (tag == null) { + tag = new CompoundTag(); + } + writeToTag(tag, getStored()); + BlockItem.setBlockEntityData(stack, blockEntityType, tag); + } + + public static void writeToTag(final CompoundTag tag, final long stored) { + tag.putLong(TAG_STORED, stored); + } + + public static void readFromTag(final EnergyStorage energyStorage, final CompoundTag tag) { + final long stored = tag.getLong(TAG_STORED); + energyStorage.receive(stored, Action.EXECUTE); + } +} diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/energy/ItemEnergyStorage.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/energy/ItemEnergyStorage.java index 47c2adf57..6fd4b568a 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/energy/ItemEnergyStorage.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/energy/ItemEnergyStorage.java @@ -2,50 +2,43 @@ 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.item.ItemStack; -public class ItemEnergyStorage implements EnergyStorage { +public class ItemEnergyStorage extends AbstractProxyEnergyStorage { private static final String TAG_STORED = "stored"; private final ItemStack stack; - private final EnergyStorage delegate; public ItemEnergyStorage(final ItemStack stack, final EnergyStorage delegate) { + super(delegate); this.stack = stack; - this.delegate = delegate; - delegate.receive(getStored(stack), Action.EXECUTE); - } - - public static long getStored(final ItemStack stack) { - return stack.getTag() != null ? stack.getTag().getLong(TAG_STORED) : 0; - } - - @Override - public long getStored() { - return delegate.getStored(); - } - - @Override - public long getCapacity() { - return delegate.getCapacity(); + final long stored = stack.getTag() != null ? stack.getTag().getLong(TAG_STORED) : 0; + if (stored > 0) { + delegate.receive(stored, Action.EXECUTE); + } } @Override public long receive(final long amount, final Action action) { - final long received = delegate.receive(amount, action); + final long received = super.receive(amount, action); if (received > 0 && action == Action.EXECUTE) { - stack.getOrCreateTag().putLong(TAG_STORED, getStored()); + updateStored(); } return received; } @Override public long extract(final long amount, final Action action) { - final long extracted = delegate.extract(amount, action); + final long extracted = super.extract(amount, action); if (extracted > 0 && action == Action.EXECUTE) { - stack.getOrCreateTag().putLong(TAG_STORED, getStored()); + updateStored(); } return extracted; } + + private void updateStored() { + stack.getOrCreateTag().putLong(TAG_STORED, getStored()); + } } diff --git a/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/ModInitializerImpl.java b/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/ModInitializerImpl.java index 535954036..2a39d003a 100644 --- a/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/ModInitializerImpl.java +++ b/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/ModInitializerImpl.java @@ -370,7 +370,7 @@ private void registerEnergyItemProviders() { controller.get() )); EnergyStorage.ITEM.registerForItems( - (stack, context) -> new EnergyStorageAdapter(Items.INSTANCE.getPortableGrid().createEnergyStorage(stack)), + (stack, context) -> new EnergyStorageAdapter(PortableGridBlockItem.createEnergyStorage(stack)), Items.INSTANCE.getPortableGrid() ); } diff --git a/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/ModInitializer.java b/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/ModInitializer.java index a09b377fe..fa72d378f 100644 --- a/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/ModInitializer.java +++ b/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/ModInitializer.java @@ -315,7 +315,7 @@ private void registerCapabilities(final RegisterCapabilitiesEvent event) { )); event.registerItem( Capabilities.EnergyStorage.ITEM, - (stack, ctx) -> new EnergyStorageAdapter(Items.INSTANCE.getPortableGrid().createEnergyStorage(stack)), + (stack, ctx) -> new EnergyStorageAdapter(PortableGridBlockItem.createEnergyStorage(stack)), Items.INSTANCE.getPortableGrid() ); }