added compatibility for vanilla's weird composter special case

Closes #47
This commit is contained in:
Ell 2020-10-24 15:00:11 +02:00
parent abc5a96b96
commit 072fc3b924
3 changed files with 28 additions and 3 deletions

View file

@ -2,10 +2,14 @@ package de.ellpeck.prettypipes;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.network.PipeItem; 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.gui.screen.Screen;
import net.minecraft.client.resources.I18n; import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity; 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.InventoryHelper;
import net.minecraft.inventory.container.Container; import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot; import net.minecraft.inventory.container.Slot;
@ -23,6 +27,7 @@ import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld; import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.wrapper.SidedInvWrapper;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import java.util.ArrayList; import java.util.ArrayList;
@ -130,6 +135,17 @@ public final class Utility {
return items; 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 { public interface IMergeItemStack {
boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex, boolean reverseDirection); boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex, boolean reverseDirection);
} }

View file

@ -201,15 +201,19 @@ public class PipeBlock extends ContainerBlock {
BlockPos offset = pos.offset(direction); BlockPos offset = pos.offset(direction);
if (!world.isBlockLoaded(offset)) if (!world.isBlockLoaded(offset))
return ConnectionType.DISCONNECTED; return ConnectionType.DISCONNECTED;
Direction opposite = direction.getOpposite();
TileEntity tile = world.getTileEntity(offset); TileEntity tile = world.getTileEntity(offset);
if (tile != null) { 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) if (connectable != null)
return connectable.getConnectionType(pos, direction); 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) if (handler != null)
return ConnectionType.CONNECTED; return ConnectionType.CONNECTED;
} }
IItemHandler blockHandler = Utility.getBlockItemHandler(world, offset, opposite);
if (blockHandler != null)
return ConnectionType.CONNECTED;
BlockState offState = world.getBlockState(offset); BlockState offState = world.getBlockState(offset);
if (hasLegsTo(world, offState, offset, direction)) { if (hasLegsTo(world, offState, offset, direction)) {
if (DIRECTIONS.values().stream().noneMatch(d -> state.get(d) == ConnectionType.LEGS)) if (DIRECTIONS.values().stream().noneMatch(d -> state.get(d) == ConnectionType.LEGS))

View file

@ -13,6 +13,8 @@ import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory; 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.Container;
import net.minecraft.inventory.container.INamedContainerProvider; import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.Item; import net.minecraft.item.Item;
@ -304,7 +306,10 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide
} }
public IItemHandler getItemHandler(Direction dir) { 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> T getNeighborCap(Direction dir, Capability<T> cap) { public <T> T getNeighborCap(Direction dir, Capability<T> cap) {