/
SubtypeRegistry.java
80 lines (66 loc) · 1.96 KB
/
SubtypeRegistry.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
package mezz.jei;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import mezz.jei.api.ISubtypeRegistry;
import mezz.jei.util.Log;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
public class SubtypeRegistry implements ISubtypeRegistry {
private final Map<Item, ISubtypeInterpreter> interpreters = new HashMap<>();
@Override
public void useNbtForSubtypes(@Nonnull Item... items) {
for (Item item : items) {
registerNbtInterpreter(item, AllNbt.INSTANCE);
}
}
@Override
public void registerNbtInterpreter(@Nullable Item item, @Nullable ISubtypeInterpreter interpreter) {
if (item == null) {
Log.error("Null item", new NullPointerException());
return;
}
if (interpreter == null) {
Log.error("Null interpreter", new NullPointerException());
return;
}
if (interpreters.containsKey(item)) {
Log.error("An interpreter is already registered for this item: {}", item, new IllegalArgumentException());
return;
}
interpreters.put(item, interpreter);
}
@Nullable
@Override
public String getSubtypeInfo(@Nullable ItemStack itemStack) {
if (itemStack == null) {
Log.error("Null itemStack", new NullPointerException());
return null;
}
Item item = itemStack.getItem();
if (item == null) {
Log.error("Null item", new NullPointerException());
return null;
}
ISubtypeInterpreter nbtInterpreter = interpreters.get(item);
if (nbtInterpreter == null) {
return null;
}
return nbtInterpreter.getSubtypeInfo(itemStack);
}
private static class AllNbt implements ISubtypeInterpreter {
public static final AllNbt INSTANCE = new AllNbt();
private AllNbt() { }
@Nullable
@Override
public String getSubtypeInfo(@Nonnull ItemStack itemStack) {
NBTTagCompound nbtTagCompound = itemStack.getTagCompound();
if (nbtTagCompound == null) {
return null;
}
return nbtTagCompound.toString();
}
}
}