SPIGOT-4120: Store BlockData enum mappings per class

This commit is contained in:
md_5 2018-07-24 10:08:34 +10:00
parent ea7b61290a
commit fbf903a1bc

View File

@ -89,7 +89,7 @@ public class CraftBlockData implements BlockData {
this.state = this.state.set(nms, toNMS(bukkit, nms.b()));
}
private static final BiMap<Enum<?>, Enum<?>> nmsToBukkit = HashBiMap.create();
private static final Map<Class, BiMap<Enum<?>, Enum<?>>> classMappings = new HashMap<>();
/**
* Convert an NMS Enum (usually a BlockStateEnum) to its appropriate Bukkit
@ -99,9 +99,14 @@ public class CraftBlockData implements BlockData {
*/
@SuppressWarnings("unchecked")
private static <B extends Enum<B>> B toBukkit(Enum<?> nms, Class<B> bukkit) {
Enum<?> converted = nmsToBukkit.get(nms);
if (converted != null) {
return (B) converted;
Enum<?> converted;
BiMap<Enum<?>, Enum<?>> nmsToBukkit = classMappings.get(nms.getClass());
if (nmsToBukkit != null) {
converted = nmsToBukkit.get(nms);
if (converted != null) {
return (B) converted;
}
}
if (nms instanceof EnumDirection) {
@ -111,6 +116,12 @@ public class CraftBlockData implements BlockData {
}
Preconditions.checkState(converted != null, "Could not convert enum %s->%s", nms, bukkit);
if (nmsToBukkit == null) {
nmsToBukkit = HashBiMap.create();
classMappings.put(nms.getClass(), nmsToBukkit);
}
nmsToBukkit.put(nms, converted);
return (B) converted;
@ -126,9 +137,14 @@ public class CraftBlockData implements BlockData {
*/
@SuppressWarnings("unchecked")
private static <N extends Enum<N> & INamable> N toNMS(Enum<?> bukkit, Class<N> nms) {
Enum<?> converted = nmsToBukkit.inverse().get(bukkit);
if (converted != null) {
return (N) converted;
Enum<?> converted;
BiMap<Enum<?>, Enum<?>> nmsToBukkit = classMappings.get(nms.getClass());
if (nmsToBukkit != null) {
converted = nmsToBukkit.inverse().get(bukkit);
if (converted != null) {
return (N) converted;
}
}
if (bukkit instanceof BlockFace) {
@ -138,6 +154,12 @@ public class CraftBlockData implements BlockData {
}
Preconditions.checkState(converted != null, "Could not convert enum %s->%s", nms, bukkit);
if (nmsToBukkit == null) {
nmsToBukkit = HashBiMap.create();
classMappings.put(nms.getClass(), nmsToBukkit);
}
nmsToBukkit.put(converted, bukkit);
return (N) converted;