/
SubtypeRegistry.java
122 lines (105 loc) · 3.44 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
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
package mezz.jei;
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;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidTankProperties;
public class SubtypeRegistry implements ISubtypeRegistry {
private final Map<Item, ISubtypeInterpreter> interpreters = new HashMap<Item, ISubtypeInterpreter>();
@Override
public void useNbtForSubtypes(Item... items) {
for (Item item : items) {
registerSubtypeInterpreter(item, AllNbt.INSTANCE);
}
}
@Override
public void registerNbtInterpreter(@Nullable Item item, @Nullable ISubtypeInterpreter interpreter) {
registerSubtypeInterpreter(item, interpreter);
}
@Override
public void registerSubtypeInterpreter(@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 nbtInterpreter.getSubtypeInfo(itemStack);
}
if (itemStack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null)) {
IFluidHandler capability = itemStack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null);
IFluidTankProperties[] tankPropertiesList = capability.getTankProperties();
StringBuilder info = new StringBuilder();
for (IFluidTankProperties tankProperties : tankPropertiesList) {
String contentsName = getContentsName(tankProperties);
if (contentsName != null) {
info.append(contentsName).append(";");
} else {
info.append("empty").append(";");
}
}
if (info.length() > 0) {
if (itemStack.getHasSubtypes()) {
info.append("m=").append(itemStack.getMetadata());
}
return info.toString();
}
}
return null;
}
@Nullable
private static String getContentsName(IFluidTankProperties fluidTankProperties) {
FluidStack contents = fluidTankProperties.getContents();
if (contents != null) {
Fluid fluid = contents.getFluid();
if (fluid != null) {
return fluid.getName();
}
}
return null;
}
private static class AllNbt implements ISubtypeInterpreter {
public static final AllNbt INSTANCE = new AllNbt();
private AllNbt() {
}
@Nullable
@Override
public String getSubtypeInfo(ItemStack itemStack) {
NBTTagCompound nbtTagCompound = itemStack.getTagCompound();
if (nbtTagCompound == null || nbtTagCompound.hasNoTags()) {
return null;
}
return nbtTagCompound.toString();
}
}
}