-
Notifications
You must be signed in to change notification settings - Fork 17
/
BlockMap.java
110 lines (96 loc) · 4.19 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
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
package com.griefcraft.lwc;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.HashMap;
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];
HashMap<Integer, String> existingMappings = LWC.getInstance().getPhysicalDatabase().loadBlockMappings();
HashMap<String, Integer> inverseMappings = new HashMap<>();
for (Entry<Integer, String> e : existingMappings.entrySet()) {
inverseMappings.put(e.getValue(), e.getKey());
}
for (Entry<Integer, String> e : existingMappings.entrySet()) {
int id = e.getKey();
String name = e.getValue();
Material mat = Material.getMaterial(name);
if (mat == null) {
// try to convert to 1.13 materials
mat = Material.getMaterial(name, true);
if (name.equals("BURNING_FURNACE")) {
mat = Material.FURNACE;
} else if(name.equals("STANDING_BANNER")) {
mat = Material.AIR;
}
if (mat == null || mat == Material.AIR) {
// -1 = unknown, will be updated on next protection access
if (mat == null) {
LWC.getInstance().getPlugin().getLogger().severe("Invalid block mapping: " + name);
} else {
LWC.getInstance().getPlugin().getLogger().info("Updating block mapping from " + name + " to UNKNOWN");
}
LWC.getInstance().getPhysicalDatabase().mergeBlockMapping(id, -1);
inverseMappings.remove(name);
continue;
} else {
LWC.getInstance().getPlugin().getLogger().info("Updating block mapping from " + name + " to " + mat.name());
Integer mergeId = inverseMappings.get(mat.name());
if (mergeId != null) {
// we have a material collision (several materials to one) -> merge them
LWC.getInstance().getPlugin().getLogger().info("Merging block mapping with " + mergeId);
LWC.getInstance().getPhysicalDatabase().mergeBlockMapping(id, mergeId.intValue());
inverseMappings.remove(name);
continue;
} else {
// material was renamed
LWC.getInstance().getPhysicalDatabase().updateBlockMappingName(id, mat.name());
inverseMappings.remove(name);
inverseMappings.put(mat.name(), id);
}
}
}
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;
}
}