/
OreDictionaryHelper.java
193 lines (169 loc) · 7.1 KB
/
OreDictionaryHelper.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Work under Copyright. Licensed under the EUPL.
* See the project README.md and LICENSE.txt for more information.
*/
package net.dries007.tfc.util;
import java.util.function.Predicate;
import com.google.common.base.CaseFormat;
import com.google.common.base.Converter;
import com.google.common.base.Joiner;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;
import org.apache.commons.lang3.ArrayUtils;
import net.minecraft.block.Block;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
import net.dries007.tfc.api.types.Rock;
import static net.minecraftforge.oredict.OreDictionary.WILDCARD_VALUE;
/**
* This is not the best example of good coding practice, but I do think it works rather well.
* The reason for the delayed registration it because now the helper's functions can be called in the constructor of
* the blocks/items (BEFORE they are actually in registries). At this point you cannot yet make an itemstack.
* Storing based on RegistryName is also not possible, as they don't have one yet.
*/
public class OreDictionaryHelper
{
private static final Multimap<Thing, String> MAP = HashMultimap.create();
private static final Converter<String, String> UPPER_UNDERSCORE_TO_LOWER_CAMEL = CaseFormat.UPPER_UNDERSCORE.converterTo(CaseFormat.LOWER_CAMEL);
private static final Joiner JOINER_UNDERSCORE = Joiner.on('_').skipNulls();
private static boolean done = false;
public static String toString(Object... parts)
{
return UPPER_UNDERSCORE_TO_LOWER_CAMEL.convert(JOINER_UNDERSCORE.join(parts));
}
public static String toString(Iterable<Object> parts)
{
return UPPER_UNDERSCORE_TO_LOWER_CAMEL.convert(JOINER_UNDERSCORE.join(parts));
}
public static String toString(Object[] prefix, Object... parts)
{
return toString(ImmutableList.builder().add(prefix).add(parts).build());
}
public static void register(Block thing, Object... parts)
{
register(new Thing(thing), parts);
}
public static void register(Item thing, Object... parts)
{
register(new Thing(thing), parts);
}
public static void registerMeta(Item thing, int meta, Object... parts)
{
register(new Thing(thing, meta), parts);
}
public static void registerRockType(Block thing, Rock.Type type, Rock rock, Object... prefixParts)
{
registerRockType(new Thing(thing), type, rock, prefixParts);
}
public static void registerRockType(Item thing, Rock.Type type, Rock rock, Object... prefixParts)
{
registerRockType(new Thing(thing), type, rock, prefixParts);
}
public static void init()
{
done = true;
MAP.forEach((t, s) -> OreDictionary.registerOre(s, t.toItemStack()));
MAP.clear(); // No need to keep this stuff around
}
private static Predicate<ItemStack> createPredicateStack(String... names)
{
return input -> {
if (input.isEmpty()) return false;
int[] ids = OreDictionary.getOreIDs(input);
for (String name : names)
if (ArrayUtils.contains(ids, OreDictionary.getOreID(name)))
return true;
return false;
};
}
public static Predicate<EntityItem> createPredicateItemEntity(String... names)
{
return input -> input.isEntityAlive() && createPredicateStack(names).test(input.getItem());
}
private static void register(Thing thing, Object... parts)
{
if (done) throw new IllegalStateException("Cannot use the helper to register after init has past.");
MAP.put(thing, toString(parts));
}
private static void registerRockType(Thing thing, Rock.Type type, Rock rock, Object... prefixParts)
{
switch (type)
{
case RAW:
MAP.put(thing, toString(prefixParts, "stone"));
MAP.put(thing, toString(prefixParts, "stone", rock));
MAP.put(thing, toString(prefixParts, "stone", rock.getRockCategory()));
break;
case SMOOTH:
MAP.put(thing, toString(prefixParts, "stone"));
MAP.put(thing, toString(prefixParts, "stone", "polished"));
MAP.put(thing, toString(prefixParts, "stone", rock));
MAP.put(thing, toString(prefixParts, "stone", rock, "polished"));
MAP.put(thing, toString(prefixParts, "stone", rock.getRockCategory()));
MAP.put(thing, toString(prefixParts, "stone", rock.getRockCategory(), "polished"));
break;
case COBBLE:
MAP.put(thing, toString(prefixParts, "cobblestone"));
MAP.put(thing, toString(prefixParts, "cobblestone", rock));
MAP.put(thing, toString(prefixParts, "cobblestone", rock.getRockCategory()));
break;
case BRICKS:
MAP.put(thing, toString(prefixParts, "stone", "brick"));
MAP.put(thing, toString(prefixParts, "stone", rock, "brick"));
MAP.put(thing, toString(prefixParts, "stone", rock.getRockCategory(), "brick"));
break;
case CLAY_GRASS:
MAP.put(thing, toString(prefixParts, "clay"));
MAP.put(thing, toString(prefixParts, "clay", rock));
MAP.put(thing, toString(prefixParts, "clay", rock.getRockCategory()));
MAP.put(thing, toString(prefixParts, "clay", "grass"));
MAP.put(thing, toString(prefixParts, "clay", rock, "grass"));
MAP.put(thing, toString(prefixParts, "clay", rock.getRockCategory(), "grass"));
break;
case DRY_GRASS:
MAP.put(thing, toString(prefixParts, type, "dry"));
MAP.put(thing, toString(prefixParts, type, rock, "dry"));
MAP.put(thing, toString(prefixParts, type, rock.getRockCategory(), "dry"));
break;
case SAND:
case GRAVEL:
case DIRT:
case GRASS:
case CLAY:
default:
MAP.put(thing, toString(prefixParts, type));
MAP.put(thing, toString(prefixParts, type, rock));
MAP.put(thing, toString(prefixParts, type, rock.getRockCategory()));
}
}
private static class Thing
{
private final Block block;
private final Item item;
private final int meta;
private Thing(Block thing)
{
block = thing;
item = null;
meta = WILDCARD_VALUE;
}
private Thing(Item thing)
{
this(thing, WILDCARD_VALUE);
}
private Thing(Item thing, int meta)
{
item = thing;
this.meta = meta;
block = null;
}
private ItemStack toItemStack()
{
//noinspection ConstantConditions
return (block == null) ? new ItemStack(item, 1, meta) : new ItemStack(block, 1, meta);
}
}
}