1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-01 06:03:00 +00:00

Correctly handle double chests on Fabric

Closes #1279. The perils of ignoring the transfer API :(.
This commit is contained in:
Jonathan Coates
2022-12-29 21:48:59 +00:00
parent 3047e3cdf4
commit 7f34aff6bb
4 changed files with 170 additions and 2 deletions

View File

@@ -8,6 +8,7 @@ package dan200.computercraft.shared.peripheral.generic;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.asm.NamedMethod;
import dan200.computercraft.core.asm.PeripheralMethod;
import dan200.computercraft.shared.peripheral.generic.methods.InventoryMethods;
import net.fabricmc.fabric.api.transfer.v1.item.InventoryStorage;
import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage;
import net.minecraft.core.BlockPos;
@@ -32,7 +33,10 @@ public class GenericPeripheralProvider {
private static final List<Lookup<?>> lookups = List.of(
(world, pos, state, blockEntity, context) -> {
// Try to avoid using the sided version of InventoryStorage where possible.
if (blockEntity instanceof Container container) return InventoryStorage.of(container, null);
if (blockEntity instanceof Container container && (container = InventoryMethods.extractContainer(container)) != null) {
return InventoryStorage.of(container, null);
}
return ItemStorage.SIDED.find(world, pos, state, blockEntity, context);
}
);

View File

@@ -21,7 +21,9 @@ import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage;
import net.minecraft.world.Container;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.ChestBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.ChestBlockEntity;
import javax.annotation.Nullable;
import java.util.HashMap;
@@ -120,6 +122,12 @@ public class InventoryMethods implements GenericPeripheral {
return moveItem(from, fromSlot - 1, to, toSlot.orElse(0) - 1, actualLimit);
}
public static @Nullable Container extractContainer(Container container) {
return container instanceof ChestBlockEntity chest && chest.getBlockState().getBlock() instanceof ChestBlock chestBlock
? ChestBlock.getContainer(chestBlock, chest.getBlockState(), chest.getLevel(), chest.getBlockPos(), true)
: container;
}
@Nullable
private static InventoryStorage extractHandler(IPeripheral peripheral) {
var object = peripheral.getTarget();
@@ -128,7 +136,9 @@ public class InventoryMethods implements GenericPeripheral {
if (object instanceof BlockEntity blockEntity && blockEntity.isRemoved()) return null;
if (object instanceof InventoryStorage storage) return storage;
if (object instanceof Container container) return InventoryStorage.of(container, null);
if (object instanceof Container container && (container = extractContainer(container)) != null) {
return InventoryStorage.of(container, null);
}
if (object instanceof BlockEntity blockEntity && direction != null) {
var found = ItemStorage.SIDED.find(blockEntity.getLevel(), blockEntity.getBlockPos(), blockEntity.getBlockState(), blockEntity, direction);