-
Notifications
You must be signed in to change notification settings - Fork 17
/
BlockMap.java
73 lines (59 loc) · 2.07 KB
/
BlockMap.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
package com.griefcraft.lwc;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.Map.Entry;
import org.bukkit.Material;
import com.google.common.base.Preconditions;
import com.griefcraft.bukkit.EntityBlock;
public class BlockMap {
private static BlockMap instance = new BlockMap();
private EnumMap<Material, Integer> blockToIdMap = new EnumMap<>(Material.class);
private Material[] idToBlockMap = new Material[64];
public static BlockMap instance() {
return instance;
}
private BlockMap() {
}
public void init() {
blockToIdMap.clear();
idToBlockMap = new Material[64];
for (Entry<Integer, String> e : LWC.getInstance().getPhysicalDatabase().loadBlockMappings().entrySet()) {
int id = e.getKey();
String name = e.getValue();
Material mat = Material.matchMaterial(name);
if (mat == null) {
System.out.println("Invalid block mapping: " + name);
continue;
}
internalAddMapping(id, mat);
}
}
private void internalAddMapping(int id, Material mat) {
blockToIdMap.put(mat, id);
if (idToBlockMap.length <= id) {
idToBlockMap = Arrays.copyOf(idToBlockMap, Math.max(idToBlockMap.length * 2, id + 1));
}
idToBlockMap[id] = mat;
}
public int getId(Material mat) {
Integer val = blockToIdMap.get(mat);
return val == null ? -1 : val;
}
public Material getMaterial(int id) {
return id >= 0 && id < idToBlockMap.length ? idToBlockMap[id] : null;
}
public int registerOrGetId(Material mat) {
Preconditions.checkNotNull(mat,"mat may not be null");
Integer val = blockToIdMap.get(mat);
if (val != null) {
return val;
}
int id = 1;
while (getMaterial(id) != null || id == EntityBlock.ENTITY_BLOCK_ID) {
id++;
}
internalAddMapping(id, mat);
LWC.getInstance().getPhysicalDatabase().addBlockMapping(id, mat.name());
return id;
}
}