/
StackHelper.java
68 lines (54 loc) · 1.8 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
package mezz.jei.util;
import javax.annotation.Nullable;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import mezz.jei.api.ingredients.subtypes.ISubtypeManager;
import mezz.jei.api.helpers.IStackHelper;
public class StackHelper implements IStackHelper {
private final ISubtypeManager subtypeManager;
public StackHelper(ISubtypeManager subtypeManager) {
this.subtypeManager = subtypeManager;
}
/**
* Similar to ItemStack.areItemStacksEqual but ignores NBT on items without subtypes, and uses the {@link ISubtypeManager}
*/
@Override
public boolean isEquivalent(@Nullable ItemStack lhs, @Nullable ItemStack rhs) {
if (lhs == rhs) {
return true;
}
if (lhs == null || rhs == null) {
return false;
}
if (lhs.getItem() != rhs.getItem()) {
return false;
}
String keyLhs = getUniqueIdentifierForStack(lhs, UidMode.NORMAL);
String keyRhs = getUniqueIdentifierForStack(rhs, UidMode.NORMAL);
return keyLhs.equals(keyRhs);
}
public String getUniqueIdentifierForStack(ItemStack stack) {
return getUniqueIdentifierForStack(stack, UidMode.NORMAL);
}
public String getUniqueIdentifierForStack(ItemStack stack, UidMode mode) {
ErrorUtil.checkNotEmpty(stack, "stack");
Item item = stack.getItem();
ResourceLocation registryName = item.getRegistryName();
if (registryName == null) {
String stackInfo = ErrorUtil.getItemStackInfo(stack);
throw new IllegalStateException("Item has no registry name: " + stackInfo);
}
String result = registryName.toString();
if (mode == UidMode.NORMAL) {
String subtypeInfo = subtypeManager.getSubtypeInfo(stack);
if (subtypeInfo != null && !subtypeInfo.isEmpty()) {
result = result + ':' + subtypeInfo;
}
}
return result;
}
public enum UidMode {
NORMAL, WILDCARD
}
}