From e3e707bd0a8ecf15baa171f7d8c24372dbec96d4 Mon Sep 17 00:00:00 2001 From: hilburn Date: Tue, 6 Jan 2015 18:21:58 +0000 Subject: [PATCH] Implement IInventoryConnection on ItemConduits much better than the old way --- .../api/inventory/IInventoryConnection.java | 24 ++++++ .../cofh/api/inventory/IInventoryHandler.java | 82 +++++++++++++++++++ .../api/inventory/IInventoryRetainer.java | 12 +++ .../java/cofh/api/inventory/package-info.java | 10 +++ .../enderio/conduit/item/ItemConduit.java | 16 ++-- 5 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 src/main/java/cofh/api/inventory/IInventoryConnection.java create mode 100644 src/main/java/cofh/api/inventory/IInventoryHandler.java create mode 100644 src/main/java/cofh/api/inventory/IInventoryRetainer.java create mode 100644 src/main/java/cofh/api/inventory/package-info.java diff --git a/src/main/java/cofh/api/inventory/IInventoryConnection.java b/src/main/java/cofh/api/inventory/IInventoryConnection.java new file mode 100644 index 00000000..99ea70bf --- /dev/null +++ b/src/main/java/cofh/api/inventory/IInventoryConnection.java @@ -0,0 +1,24 @@ +package cofh.api.inventory; + +import net.minecraftforge.common.util.ForgeDirection; + +/** + * Implement this interface on TileEntities which should connect to item transportation blocks. + */ +public interface IInventoryConnection { + + /** + * @param from + * Side to which a connector would connect + * @return DEFAULT if the connector should decide how to connect; FORCE if the connector should always connect; DENY if the connector should never connect. + */ + public ConnectionType canConnectInventory(ForgeDirection from); + + public static enum ConnectionType { + DEFAULT, FORCE, DENY; + + public final boolean canConnect = ordinal() != 2; + public final boolean forceConnect = ordinal() == 1; + } + +} diff --git a/src/main/java/cofh/api/inventory/IInventoryHandler.java b/src/main/java/cofh/api/inventory/IInventoryHandler.java new file mode 100644 index 00000000..4cb753c9 --- /dev/null +++ b/src/main/java/cofh/api/inventory/IInventoryHandler.java @@ -0,0 +1,82 @@ +package cofh.api.inventory; + +import java.util.List; + +import net.minecraft.item.ItemStack; +import net.minecraftforge.common.util.ForgeDirection; + +/** + * Implement this interface on TileEntities which should handle items. + * + * A reference implementation is provided {@link TileInventoryHandler}. + * + * @author King Lemming + * + */ +public interface IInventoryHandler extends IInventoryConnection { + + /** + * Insert an ItemStack into the IInventoryHandler, internal distribution is left entirely to the IInventoryHandler. This returns what is remaining of the + * original stack - a null return means that the entire stack was accepted! + * + * @param from + * Orientation the item is inserted from. + * @param item + * ItemStack to be inserted. The size of this stack corresponds to the maximum amount to insert. + * @param simulate + * If TRUE, the insertion will only be simulated. + * @return An ItemStack representing how much is remaining after the item was inserted (or would have been, if simulated) into the container inventory. + */ + ItemStack insertItem(ForgeDirection from, ItemStack item, boolean simulate); + + /** + * Extract an ItemStack from an IInventoryHandler, internal distribution is left entirely to the IInventoryHandler. This returns the resulting stack - a + * null return means that nothing was extracted! + * + * @param from + * Orientation the item is extracted from. + * @param item + * ItemStack to be extracted. The size of this stack corresponds to the maximum amount to extract. If this is null, then a null ItemStack should + * immediately be returned. + * @param simulate + * If TRUE, the extraction will only be simulated. + * @return An ItemStack representing how much was extracted (or would have been, if simulated) from the container inventory. + */ + ItemStack extractItem(ForgeDirection from, ItemStack item, boolean simulate); + + /** + * Extract an ItemStack from an IInventoryHandler, internal distribution is left entirely to the IInventoryHandler. This returns the resulting stack - a + * null return means that nothing was extracted! + * + * @param from + * Orientation the item is extracted from. + * @param maxExtract + * Maximum number of items to extract. (The returned ItemStack should have a stackSize no higher than this.) + * @param simulate + * If TRUE, the extraction will only be simulated. + * @return An ItemStack representing how much was extracted (or would have been, if simulated) from the container inventory. + */ + ItemStack extractItem(ForgeDirection from, int maxExtract, boolean simulate); + + /** + * Get the contents of the IInventoryHandler's inventory. This returns a COPY. This should only return non-null ItemStacks, and an empty List if the + * inventory has nothing. + */ + List getInventoryContents(ForgeDirection from); + + /** + * Get the size (number of internal slots) of the IInventoryHandler's inventory. + */ + int getSizeInventory(ForgeDirection from); + + /** + * Returns whether or not the IInventoryHandler's inventory is empty (for a given side). + */ + boolean isEmpty(ForgeDirection from); + + /** + * Returns whether or not the IInventoryHandler's inventory is full (for a given side). + */ + boolean isFull(ForgeDirection from); + +} diff --git a/src/main/java/cofh/api/inventory/IInventoryRetainer.java b/src/main/java/cofh/api/inventory/IInventoryRetainer.java new file mode 100644 index 00000000..30309fb2 --- /dev/null +++ b/src/main/java/cofh/api/inventory/IInventoryRetainer.java @@ -0,0 +1,12 @@ +package cofh.api.inventory; + +/** + * Marks a block which will retain its inventory when broken. + * + * @author King Lemming + * + */ +public interface IInventoryRetainer { + + // There's nothing else to go here at the moment. Potentially a conditional at a later date. +} diff --git a/src/main/java/cofh/api/inventory/package-info.java b/src/main/java/cofh/api/inventory/package-info.java new file mode 100644 index 00000000..a11a175c --- /dev/null +++ b/src/main/java/cofh/api/inventory/package-info.java @@ -0,0 +1,10 @@ +/** + * (C) 2014 Team CoFH / CoFH / Cult of the Full Hub + * http://www.teamcofh.com + */ +@API(apiVersion = CoFHAPIProps.VERSION, owner = "CoFHAPI", provides = "CoFHAPI|inventory") +package cofh.api.inventory; + +import cofh.api.CoFHAPIProps; +import cpw.mods.fml.common.API; + diff --git a/src/main/java/crazypants/enderio/conduit/item/ItemConduit.java b/src/main/java/crazypants/enderio/conduit/item/ItemConduit.java index 2fd7abf0..6a87e831 100644 --- a/src/main/java/crazypants/enderio/conduit/item/ItemConduit.java +++ b/src/main/java/crazypants/enderio/conduit/item/ItemConduit.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import cofh.api.inventory.IInventoryConnection; import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; @@ -542,16 +543,15 @@ public class ItemConduit extends AbstractConduit implements IItemConduit { @Override public boolean canConnectToExternal(ForgeDirection direction, boolean ignoreDisabled) { IInventory inv = getExternalInventory(direction); - if(inv instanceof ISidedInventory) { + if (inv==null) return false; + else if (inv instanceof IInventoryConnection){ + return ((IInventoryConnection)inv).canConnectInventory(direction.getOpposite()).canConnect; + } + else if(inv instanceof ISidedInventory) { int[] slots = ((ISidedInventory) inv).getAccessibleSlotsFromSide(direction.getOpposite().ordinal()); - //TODO: The MFR thing is a horrible hack. Blocks like the harvester make no slots accessible but will push into a connected - //conduit. I could just return true for sided inventories but this will lead to confusing connections in some cases. - //Therefore, bad hack for now. - - return (slots != null && slots.length != 0) || inv.getClass().getName().startsWith("powercrystals.minefactoryreloaded") || - inv.getClass().getName().endsWith("TileTesseract"); + return slots!=null && slots.length>0; } else { - return inv != null; + return true; } }