Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 4789f0a

Browse files
IItemHandler support (#142)
* IItemHandler support * Remove a sneaky debug print * Apparently this can cause a memory leak, TIL * Update patchwork-items/src/main/java/net/minecraftforge/items/ItemHandlerHelper.java Co-authored-by: Glitch <glitchieproductionsofficial@gmail.com>
1 parent c92e748 commit 4789f0a

14 files changed

Lines changed: 967 additions & 0 deletions

File tree

patchwork-items/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
archivesBaseName = "patchwork-items"
2+
version = getSubprojectVersion(project, "0.1.0")
3+
4+
dependencies {
5+
compile project(path: ':patchwork-capabilities', configuration: 'dev')
6+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 net.minecraftforge.common.capabilities.Capability;
23+
import net.minecraftforge.common.capabilities.CapabilityManager;
24+
25+
import net.minecraft.item.ItemStack;
26+
import net.minecraft.nbt.CompoundTag;
27+
import net.minecraft.nbt.ListTag;
28+
import net.minecraft.nbt.Tag;
29+
import net.minecraft.util.math.Direction;
30+
31+
import net.patchworkmc.api.capability.CapabilityRegisteredCallback;
32+
33+
public class CapabilityItemHandler {
34+
public static Capability<IItemHandler> ITEM_HANDLER_CAPABILITY = null;
35+
36+
public static void register() {
37+
CapabilityRegisteredCallback.event(IItemHandler.class).register(cap -> ITEM_HANDLER_CAPABILITY = cap);
38+
39+
CapabilityManager.INSTANCE.register(IItemHandler.class, new Capability.IStorage<IItemHandler>() {
40+
@Override
41+
public Tag writeNBT(Capability<IItemHandler> capability, IItemHandler instance, Direction side) {
42+
ListTag nbtTagList = new ListTag();
43+
int size = instance.getSlots();
44+
45+
for (int i = 0; i < size; i++) {
46+
ItemStack stack = instance.getStackInSlot(i);
47+
48+
if (!stack.isEmpty()) {
49+
CompoundTag itemTag = new CompoundTag();
50+
itemTag.putInt("Slot", i);
51+
stack.toTag(itemTag);
52+
nbtTagList.add(itemTag);
53+
}
54+
}
55+
56+
return nbtTagList;
57+
}
58+
59+
@Override
60+
public void readNBT(Capability<IItemHandler> capability, IItemHandler instance, Direction side, Tag base) {
61+
if (!(instance instanceof IItemHandlerModifiable)) {
62+
throw new RuntimeException("IItemHandler instance does not implement IItemHandlerModifiable");
63+
}
64+
65+
IItemHandlerModifiable itemHandlerModifiable = (IItemHandlerModifiable) instance;
66+
ListTag tagList = (ListTag) base;
67+
68+
for (int i = 0; i < tagList.size(); i++) {
69+
CompoundTag itemTags = tagList.getCompound(i);
70+
int j = itemTags.getInt("Slot");
71+
72+
if (j >= 0 && j < instance.getSlots()) {
73+
itemHandlerModifiable.setStackInSlot(j, ItemStack.fromTag(itemTags));
74+
}
75+
}
76+
}
77+
}, ItemStackHandler::new);
78+
}
79+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.item.ItemStack;
25+
26+
public interface IItemHandlerModifiable extends IItemHandler {
27+
/**
28+
* Overrides the stack in the given slot. This method is used by the
29+
* standard Forge helper methods and classes. It is not intended for
30+
* general use by other mods, and the handler may throw an error if it
31+
* is called unexpectedly.
32+
*
33+
* @param slot Slot to modify
34+
* @param stack ItemStack to set slot to (may be empty).
35+
* @throws RuntimeException if the handler is called in a way that the handler
36+
* was not expecting.
37+
*/
38+
void setStackInSlot(int slot, @Nonnull ItemStack stack);
39+
}

0 commit comments

Comments
 (0)