-
Notifications
You must be signed in to change notification settings - Fork 497
/
ListOreDictionaryCache.java
95 lines (80 loc) · 2.92 KB
/
ListOreDictionaryCache.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
package buildcraft.core.list;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import net.minecraftforge.oredict.OreDictionary;
public final class ListOreDictionaryCache {
public static final ListOreDictionaryCache INSTANCE = new ListOreDictionaryCache();
private static final String[] TYPE_KEYWORDS = { "Tiny", "Dense", "Small" };
private final Map<String, Set<Integer>> namingCache = new HashMap<>();
private final Set<String> registeredNames = new HashSet<>();
private ListOreDictionaryCache() {
}
public Set<Integer> getListOfPartialMatches(String part) {
return namingCache.get(part);
}
private void addToNamingCache(String s, int id) {
if (s == null) {
return;
}
Set<Integer> ll = namingCache.get(s);
if (ll == null) {
ll = new HashSet<>();
ll.add(id);
namingCache.put(s, ll);
} else {
ll.add(id);
}
}
public static String getType(String name) {
// Rules for finding type:
// - Split just before the last uppercase character found.
int splitLocation = name.length() - 1;
while (splitLocation >= 0) {
if (Character.isUpperCase(name.codePointAt(splitLocation))) {
break;
} else {
splitLocation--;
}
}
return splitLocation >= 0 ? name.substring(0, splitLocation) : name; // No null - this handles things like
// "record".
}
public static String getMaterial(String name) {
// Rules for finding material:
// - For every uppercase character, check if the character is not in
// TYPE_KEYWORDS. This is used to skip things like "plate[DenseIron]"
// or "dust[TinyRedstone]". That part should be the material still.
int splitLocation = 0;
String t = null;
while (splitLocation < name.length()) {
if (!Character.isUpperCase(name.codePointAt(splitLocation))) {
splitLocation++;
} else {
t = name.substring(splitLocation);
for (String s : TYPE_KEYWORDS) {
if (t.startsWith(s)) {
t = null;
break;
}
}
if (t != null) {
break;
} else {
splitLocation++;
}
}
}
return splitLocation < name.length() ? t : null;
}
public void registerName(String name) {
if (registeredNames.contains(name)) {
return;
}
int oreID = OreDictionary.getOreID(name);
addToNamingCache(getType(name), oreID);
addToNamingCache(getMaterial(name), oreID);
registeredNames.add(name);
}
}