-
Notifications
You must be signed in to change notification settings - Fork 497
/
StackHelper.java
162 lines (147 loc) · 4.6 KB
/
StackHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.inventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
public class StackHelper {
protected StackHelper() {
}
/* STACK MERGING */
/**
* Checks if two ItemStacks are identical enough to be merged
*
* @param stack1 - The first stack
* @param stack2 - The second stack
* @return true if stacks can be merged, false otherwise
*/
public static boolean canStacksMerge(ItemStack stack1, ItemStack stack2) {
if (stack1 == null || stack2 == null) {
return false;
}
if (!stack1.isItemEqual(stack2)) {
return false;
}
if (!ItemStack.areItemStackTagsEqual(stack1, stack2)) {
return false;
}
return true;
}
/**
* Merges mergeSource into mergeTarget
*
* @param mergeSource - The stack to merge into mergeTarget, this stack is
* not modified
* @param mergeTarget - The target merge, this stack is modified if doMerge
* is set
* @param doMerge - To actually do the merge
* @return The number of items that was successfully merged.
*/
public static int mergeStacks(ItemStack mergeSource, ItemStack mergeTarget, boolean doMerge) {
if (!canStacksMerge(mergeSource, mergeTarget)) {
return 0;
}
int mergeCount = Math.min(mergeTarget.getMaxStackSize() - mergeTarget.stackSize, mergeSource.stackSize);
if (mergeCount < 1) {
return 0;
}
if (doMerge) {
mergeTarget.stackSize += mergeCount;
}
return mergeCount;
}
/* ITEM COMPARISONS */
/**
* Determines whether the given ItemStack should be considered equivalent
* for crafting purposes.
*
* @param base The stack to compare to.
* @param comparison The stack to compare.
* @param oreDictionary true to take the Forge OreDictionary into account.
* @return true if comparison should be considered a crafting equivalent for
* base.
*/
public static boolean isCraftingEquivalent(ItemStack base, ItemStack comparison, boolean oreDictionary) {
if (isMatchingItem(base, comparison, true, false)) {
return true;
}
if (oreDictionary) {
int[] idBase = OreDictionary.getOreIDs(base);
if (idBase.length > 0) {
for (int id : idBase) {
for (ItemStack itemstack : OreDictionary.getOres(OreDictionary.getOreName(id))) {
if (comparison.getItem() == itemstack.getItem() && (itemstack.getItemDamage() == OreDictionary.WILDCARD_VALUE || comparison.getItemDamage() == itemstack.getItemDamage())) {
return true;
}
}
}
}
}
return false;
}
public static boolean isCraftingEquivalent(int[] oreIDs, ItemStack comparison) {
if (oreIDs.length > 0) {
for (int id : oreIDs) {
for (ItemStack itemstack : OreDictionary.getOres(OreDictionary.getOreName(id))) {
if (comparison.getItem() == itemstack.getItem() && (itemstack.getItemDamage() == OreDictionary.WILDCARD_VALUE || comparison.getItemDamage() == itemstack.getItemDamage())) {
return true;
}
}
}
}
return false;
}
/**
* Compares item id, damage and NBT. Accepts wildcard damage. Ignores damage
* entirely if the item doesn't have subtypes.
*
* @param base The stack to compare to.
* @param comparison The stack to compare.
* @return true if id, damage and NBT match.
*/
public static boolean isMatchingItem(ItemStack base, ItemStack comparison) {
return isMatchingItem(base, comparison, true, true);
}
/**
* Compares item id, and optionally damage and NBT. Accepts wildcard damage.
* Ignores damage entirely if the item doesn't have subtypes.
*
* @param a ItemStack
* @param b ItemStack
* @param matchDamage
* @param matchNBT
* @return true if matches
*/
public static boolean isMatchingItem(final ItemStack a, final ItemStack b, final boolean matchDamage, final boolean matchNBT) {
if (a == null || b == null) {
return false;
}
if (a.getItem() != b.getItem()) {
return false;
}
if (matchDamage && a.getHasSubtypes()) {
if (!isWildcard(a) && !isWildcard(b)) {
if (a.getItemDamage() != b.getItemDamage()) {
return false;
}
}
}
if (matchNBT) {
if (a.stackTagCompound != null && !a.stackTagCompound.equals(b.stackTagCompound)) {
return false;
}
}
return true;
}
public static boolean isWildcard(ItemStack stack) {
return isWildcard(stack.getItemDamage());
}
public static boolean isWildcard(int damage) {
return damage == -1 || damage == OreDictionary.WILDCARD_VALUE;
}
}