Add functionality in IItemHandler to test if a slot can accept an item (#5033)

This commit is contained in:
Cody Brittain authored and LexManos committed 2018-07-28 17:38:18 -07:00
1 parent eb870bc84c
commit 4acfca27f8
10 files changed
+105 -28

No files matched your search

@@ -19,7 +19,10 @@
package net.minecraftforge.items;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.IFluidHandler;
import javax.annotation.Nonnull;
@@ -35,16 +38,18 @@ public interface IItemHandler
/**
* Returns the ItemStack in a given slot.
*
* The result's stack size may be greater than the itemstacks max size.
* The result's stack size may be greater than the itemstack's max size.
*
* If the result is empty, then the slot is empty.
*
* <p/>
* IMPORTANT: This ItemStack MUST NOT be modified. This method is not for
* altering an inventories contents. Any implementers who are able to detect
* <p>
* <strong>IMPORTANT:</strong> This ItemStack <em>MUST NOT</em> be modified. This method is not for
* altering an inventory's contents. Any implementers who are able to detect
* modification through this method should throw an exception.
* <p/>
* SERIOUSLY: DO NOT MODIFY THE RETURNED ITEMSTACK
* </p>
* <p>
* <strong><em>SERIOUSLY: DO NOT MODIFY THE RETURNED ITEMSTACK</em></strong>
* </p>
*
* @param slot Slot to query
* @return ItemStack in given slot. Empty Itemstack if the slot is empty.
@@ -53,9 +58,11 @@ public interface IItemHandler
ItemStack getStackInSlot(int slot);
/**
* <p>
* Inserts an ItemStack into the given slot and return the remainder.
* The ItemStack should not be modified in this function!
* Note: This behaviour is subtly different from IFluidHandlers.fill()
* The ItemStack <em>should not</em> be modified in this function!
* </p>
* Note: This behaviour is subtly different from {@link IFluidHandler#fill(FluidStack, boolean)}
*
* @param slot Slot to insert into.
* @param stack ItemStack to insert. This must not be modified by the item handler.
@@ -69,11 +76,13 @@ public interface IItemHandler
/**
* Extracts an ItemStack from the given slot.
* <p>
* The returned value must be empty if nothing is extracted,
* otherwise it's stack size must less than than amount and {@link ItemStack#getMaxStackSize()}.
* otherwise its stack size must be less than or equal to {@code amount} and {@link ItemStack#getMaxStackSize()}.
* </p>
*
* @param slot Slot to extract from.
* @param amount Amount to extract (may be greater than the current stacks max limit)
* @param amount Amount to extract (may be greater than the current stack's max limit)
* @param simulate If true, the extraction is only simulated
* @return ItemStack extracted from the slot, must be empty if nothing can be extracted.
* The returned ItemStack can be safely modified after, so item handlers should return a new or copied stack.
@@ -88,4 +97,26 @@ public interface IItemHandler
* @return The maximum stack size allowed in the slot.
*/
int getSlotLimit(int slot);
/**
* <p>
* This function re-implements the vanilla function {@link IInventory#isItemValidForSlot(int, ItemStack)}.
* It should be used instead of simulated insertions in cases where the contents and state of the inventory are
* irrelevant, mainly for the purpose of automation and logic (for instance, testing if a minecart can wait
* to deposit its items into a full inventory, or if the items in the minecart can never be placed into the
* inventory and should move on).
* </p>
* <ul>
* <li>isItemValid is false when insertion of the item is never valid.</li>
* <li>When isItemValid is true, no assumptions can be made and insertion must be simulated case-by-case.</li>
* <li>The actual items in the inventory, its fullness, or any other state are <strong>not</strong> considered by isItemValid.</li>
* </ul>
* @param slot Slot to query for validity
* @param stack Stack to test with for validity
*
* @return true if the slot can insert the ItemStack, not considering the current state of the inventory.
* false if the slot can never insert the ItemStack in any situation.
*/
//todo Make non-default and/or replace in 1.13
default boolean isItemValid(int slot, @Nonnull ItemStack stack) { return true; }
}