From a1cc6d20b7c5e1700b830144ae21edf619153a87 Mon Sep 17 00:00:00 2001 From: raoulvdberge Date: Sun, 18 Feb 2024 18:33:06 +0100 Subject: [PATCH] fix: losing disk when using wrench dismantling on the portable grid --- CHANGELOG.md | 4 ++ .../AbstractPortableGridBlockEntity.java | 24 ++++++-- .../portablegrid/PortableGridBlockItem.java | 55 ++++++++----------- .../PortableGridLootItemFunction.java | 6 +- 4 files changed, 50 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f0b305d2..0d7e208bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed + +- Fixed losing disk when using Wrench dismantling on the Portable Grid. + ## [2.0.0-milestone.3.3] - 2024-02-17 ### Added 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..ec53ef457 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 @@ -34,6 +34,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; @@ -75,6 +76,23 @@ 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; @@ -137,9 +155,7 @@ 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); - } + readDiskInventory(tag, diskInventory); if (tag.contains(TAG_STORED)) { energyStorage.receive(tag.getLong(TAG_STORED), Action.EXECUTE); } @@ -169,7 +185,7 @@ protected void onClientDriveStateUpdated() { @Override public void saveAdditional(final CompoundTag tag) { super.saveAdditional(tag); - tag.put(TAG_DISK_INVENTORY, ContainerUtil.write(diskInventory)); + writeDiskInventory(tag, diskInventory); tag.putLong(TAG_STORED, 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..ecd46232b 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,15 @@ 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 +29,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,8 +40,7 @@ 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 creative = isCreative(stack); final boolean hasEnergy = creative || ItemEnergyStorage.getStored(stack) > 0; final ItemStack diskStack = getDisk(stack); final boolean active = hasEnergy && !diskStack.isEmpty(); @@ -55,6 +51,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,17 +72,23 @@ 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) { @@ -91,23 +98,6 @@ public EnergyStorage createEnergyStorage(final ItemStack stack) { 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; - } - @Override public InteractionResultHolder use(final Level level, final Player player, final InteractionHand hand) { final ItemStack stack = player.getItemInHand(hand); @@ -145,8 +135,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); } }