Skip to content

Commit

Permalink
fix: losing disk when using wrench dismantling on the portable grid
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Feb 18, 2024
1 parent a7b97e4 commit a1cc6d2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 39 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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();
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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<ItemStack> use(final Level level, final Player player, final InteractionHand hand) {
final ItemStack stack = player.getItemInHand(hand);
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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);
}
}

0 comments on commit a1cc6d2

Please sign in to comment.