From 072fc3b924d16d17ecd3e03dbf3b2f3cdc3c4411 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 24 Oct 2020 15:00:11 +0200 Subject: [PATCH] added compatibility for vanilla's weird composter special case Closes #47 --- .../java/de/ellpeck/prettypipes/Utility.java | 16 ++++++++++++++++ .../de/ellpeck/prettypipes/pipe/PipeBlock.java | 8 ++++++-- .../ellpeck/prettypipes/pipe/PipeTileEntity.java | 7 ++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/ellpeck/prettypipes/Utility.java b/src/main/java/de/ellpeck/prettypipes/Utility.java index 573b23e..7514365 100644 --- a/src/main/java/de/ellpeck/prettypipes/Utility.java +++ b/src/main/java/de/ellpeck/prettypipes/Utility.java @@ -2,10 +2,14 @@ package de.ellpeck.prettypipes; import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.network.PipeItem; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.resources.I18n; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.ServerPlayerEntity; +import net.minecraft.inventory.ISidedInventory; +import net.minecraft.inventory.ISidedInventoryProvider; import net.minecraft.inventory.InventoryHelper; import net.minecraft.inventory.container.Container; import net.minecraft.inventory.container.Slot; @@ -23,6 +27,7 @@ import net.minecraft.world.World; import net.minecraft.world.server.ServerWorld; import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.items.IItemHandler; +import net.minecraftforge.items.wrapper.SidedInvWrapper; import org.apache.commons.lang3.tuple.Pair; import java.util.ArrayList; @@ -130,6 +135,17 @@ public final class Utility { return items; } + public static IItemHandler getBlockItemHandler(World world, BlockPos pos, Direction direction) { + BlockState state = world.getBlockState(pos); + Block block = state.getBlock(); + if (!(block instanceof ISidedInventoryProvider)) + return null; + ISidedInventory inventory = ((ISidedInventoryProvider) block).createInventory(state, world, pos); + if (inventory == null) + return null; + return new SidedInvWrapper(inventory, direction); + } + public interface IMergeItemStack { boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex, boolean reverseDirection); } diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java index fa9c653..5255e4e 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java @@ -201,15 +201,19 @@ public class PipeBlock extends ContainerBlock { BlockPos offset = pos.offset(direction); if (!world.isBlockLoaded(offset)) return ConnectionType.DISCONNECTED; + Direction opposite = direction.getOpposite(); TileEntity tile = world.getTileEntity(offset); if (tile != null) { - IPipeConnectable connectable = tile.getCapability(Registry.pipeConnectableCapability, direction.getOpposite()).orElse(null); + IPipeConnectable connectable = tile.getCapability(Registry.pipeConnectableCapability, opposite).orElse(null); if (connectable != null) return connectable.getConnectionType(pos, direction); - IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, direction.getOpposite()).orElse(null); + IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, opposite).orElse(null); if (handler != null) return ConnectionType.CONNECTED; } + IItemHandler blockHandler = Utility.getBlockItemHandler(world, offset, opposite); + if (blockHandler != null) + return ConnectionType.CONNECTED; BlockState offState = world.getBlockState(offset); if (hasLegsTo(world, offState, offset, direction)) { if (DIRECTIONS.values().stream().noneMatch(d -> state.get(d) == ConnectionType.LEGS)) diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/PipeTileEntity.java b/src/main/java/de/ellpeck/prettypipes/pipe/PipeTileEntity.java index 2c4038d..f61a77a 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/PipeTileEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/PipeTileEntity.java @@ -13,6 +13,8 @@ import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.inventory.ISidedInventory; +import net.minecraft.inventory.ISidedInventoryProvider; import net.minecraft.inventory.container.Container; import net.minecraft.inventory.container.INamedContainerProvider; import net.minecraft.item.Item; @@ -304,7 +306,10 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide } public IItemHandler getItemHandler(Direction dir) { - return this.getNeighborCap(dir, CapabilityItemHandler.ITEM_HANDLER_CAPABILITY); + IItemHandler handler = this.getNeighborCap(dir, CapabilityItemHandler.ITEM_HANDLER_CAPABILITY); + if (handler != null) + return handler; + return Utility.getBlockItemHandler(this.world, this.pos.offset(dir), dir.getOpposite()); } public T getNeighborCap(Direction dir, Capability cap) {