|
| 1 | +/* |
| 2 | + * Minecraft Forge, Patchwork Project |
| 3 | + * Copyright (c) 2016-2020, 2019-2020 |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation version 2.1 |
| 8 | + * of the License. |
| 9 | + * |
| 10 | + * This library is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this library; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | + */ |
| 19 | + |
| 20 | +package net.minecraftforge.items; |
| 21 | + |
| 22 | +import javax.annotation.Nonnull; |
| 23 | + |
| 24 | +import net.minecraft.inventory.Inventory; |
| 25 | +import net.minecraft.item.ItemStack; |
| 26 | + |
| 27 | +public interface IItemHandler { |
| 28 | + /** |
| 29 | + * Returns the number of slots available. |
| 30 | + * |
| 31 | + * @return The number of slots available |
| 32 | + */ |
| 33 | + int getSlots(); |
| 34 | + |
| 35 | + /** |
| 36 | + * Returns the ItemStack in a given slot. |
| 37 | + * |
| 38 | + * <p>The result's stack size may be greater than the itemstack's max size. |
| 39 | + * |
| 40 | + * <p>If the result is empty, then the slot is empty. |
| 41 | + * |
| 42 | + * <p><strong>IMPORTANT:</strong> This ItemStack <em>MUST NOT</em> be modified. This method is not for |
| 43 | + * altering an inventory's contents. Any implementers who are able to detect |
| 44 | + * modification through this method should throw an exception. |
| 45 | + * |
| 46 | + * <p><strong><em>SERIOUSLY: DO NOT MODIFY THE RETURNED ITEMSTACK</em></strong> |
| 47 | + * |
| 48 | + * @param slot Slot to query |
| 49 | + * @return ItemStack in given slot. Empty Itemstack if the slot is empty. |
| 50 | + */ |
| 51 | + @Nonnull |
| 52 | + ItemStack getStackInSlot(int slot); |
| 53 | + |
| 54 | + /** |
| 55 | + * <p>Inserts an ItemStack into the given slot and return the remainder. |
| 56 | + * The ItemStack <em>should not</em> be modified in this function! |
| 57 | + * </p> |
| 58 | + * Note: This behaviour is subtly different from net.minecraftforge.fluids.capability.IFluidHandler#fill(net.minecraftforge.fluids.FluidStack, boolean) |
| 59 | + * |
| 60 | + * @param slot Slot to insert into. |
| 61 | + * @param stack ItemStack to insert. This must not be modified by the item handler. |
| 62 | + * @param simulate If true, the insertion is only simulated |
| 63 | + * @return The remaining ItemStack that was not inserted (if the entire stack is accepted, then return an empty ItemStack). |
| 64 | + * May be the same as the input ItemStack if unchanged, otherwise a new ItemStack. |
| 65 | + * The returned ItemStack can be safely modified after. |
| 66 | + */ |
| 67 | + @Nonnull |
| 68 | + ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate); |
| 69 | + |
| 70 | + /** |
| 71 | + * Extracts an ItemStack from the given slot. |
| 72 | + * <p>The returned value must be empty if nothing is extracted, |
| 73 | + * otherwise its stack size must be less than or equal to {@code amount} and {@link ItemStack#getMaxCount()} ()}. |
| 74 | + * </p> |
| 75 | + * |
| 76 | + * @param slot Slot to extract from. |
| 77 | + * @param amount Amount to extract (may be greater than the current stack's max limit) |
| 78 | + * @param simulate If true, the extraction is only simulated |
| 79 | + * @return ItemStack extracted from the slot, must be empty if nothing can be extracted. |
| 80 | + * The returned ItemStack can be safely modified after, so item handlers should return a new or copied stack. |
| 81 | + */ |
| 82 | + @Nonnull |
| 83 | + ItemStack extractItem(int slot, int amount, boolean simulate); |
| 84 | + |
| 85 | + /** |
| 86 | + * Retrieves the maximum stack size allowed to exist in the given slot. |
| 87 | + * |
| 88 | + * @param slot Slot to query. |
| 89 | + * @return The maximum stack size allowed in the slot. |
| 90 | + */ |
| 91 | + int getSlotLimit(int slot); |
| 92 | + |
| 93 | + /** |
| 94 | + * <p>This function re-implements the vanilla function {@link Inventory#isValidInvStack(int, ItemStack)}. |
| 95 | + * It should be used instead of simulated insertions in cases where the contents and state of the inventory are |
| 96 | + * irrelevant, mainly for the purpose of automation and logic (for instance, testing if a minecart can wait |
| 97 | + * to deposit its items into a full inventory, or if the items in the minecart can never be placed into the |
| 98 | + * inventory and should move on). |
| 99 | + * </p> |
| 100 | + * <ul> |
| 101 | + * <li>isItemValid is false when insertion of the item is never valid.</li> |
| 102 | + * <li>When isItemValid is true, no assumptions can be made and insertion must be simulated case-by-case.</li> |
| 103 | + * <li>The actual items in the inventory, its fullness, or any other state are <strong>not</strong> considered by isItemValid.</li> |
| 104 | + * </ul> |
| 105 | + * |
| 106 | + * @param slot Slot to query for validity |
| 107 | + * @param stack Stack to test with for validity |
| 108 | + * @return true if the slot can insert the ItemStack, not considering the current state of the inventory. |
| 109 | + * false if the slot can never insert the ItemStack in any situation. |
| 110 | + */ |
| 111 | + boolean isItemValid(int slot, @Nonnull ItemStack stack); |
| 112 | +} |
0 commit comments