From ad8e723005854be821a9d81c9aeb02f323218340 Mon Sep 17 00:00:00 2001 From: KennyTV <28825609+KennyTV@users.noreply.github.com> Date: Fri, 23 Aug 2019 22:14:32 +0200 Subject: [PATCH] Mapping and protocolregistry cleanup --- .../myles/ViaVersion/api/PacketWrapper.java | 2 +- .../myles/ViaVersion/api/data/Mappings.java | 32 ++++++++++- .../api/protocol/ProtocolRegistry.java | 49 ++++++++++------- .../data/MappingData.java | 54 +++---------------- .../data/MappingData.java | 43 +++------------ 5 files changed, 77 insertions(+), 103 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java b/common/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java index 29f583064..d56877e38 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java +++ b/common/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java @@ -224,7 +224,7 @@ public class PacketWrapper { if (id != -1) { Type.VAR_INT.write(buffer, id); } - if (readableObjects.size() > 0) { + if (!readableObjects.isEmpty()) { packetValues.addAll(readableObjects); readableObjects.clear(); } diff --git a/common/src/main/java/us/myles/ViaVersion/api/data/Mappings.java b/common/src/main/java/us/myles/ViaVersion/api/data/Mappings.java index 3999c5a9c..bb70eb659 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/data/Mappings.java +++ b/common/src/main/java/us/myles/ViaVersion/api/data/Mappings.java @@ -1,6 +1,34 @@ package us.myles.ViaVersion.api.data; -public interface Mappings { +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; - int getNewId(int old); +import java.util.Arrays; + +public class Mappings { + protected final short[] oldToNew; + + public Mappings(int size, JsonObject oldMapping, JsonObject newMapping) { + oldToNew = new short[size]; + Arrays.fill(oldToNew, (short) -1); + MappingDataLoader.mapIdentifiers(oldToNew, oldMapping, newMapping); + } + + public Mappings(JsonObject oldMapping, JsonObject newMapping) { + this(oldMapping.entrySet().size(), oldMapping, newMapping); + } + + public Mappings(int size, JsonArray oldMapping, JsonArray newMapping) { + oldToNew = new short[size]; + Arrays.fill(oldToNew, (short) -1); + MappingDataLoader.mapIdentifiers(oldToNew, oldMapping, newMapping); + } + + public Mappings(JsonArray oldMapping, JsonArray newMapping) { + this(oldMapping.size(), oldMapping, newMapping); + } + + public int getNewId(int old) { + return old >= 0 && old < oldToNew.length ? oldToNew[old] : -1; + } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java b/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java index bcc669765..05fec74fa 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java +++ b/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java @@ -43,33 +43,44 @@ public class ProtocolRegistry { static { // Base Protocol registerBaseProtocol(BASE_PROTOCOL, Range.lessThan(Integer.MIN_VALUE)); - registerBaseProtocol(new BaseProtocol1_7(), Range.all()); + registerBaseProtocol(new BaseProtocol1_7(), Range.all()); // Register built in protocols - registerProtocol(new Protocol1_9To1_8(), Collections.singletonList(ProtocolVersion.v1_9.getId()), ProtocolVersion.v1_8.getId()); + registerProtocol(new Protocol1_9To1_8(), ProtocolVersion.v1_9, ProtocolVersion.v1_8); registerProtocol(new Protocol1_9_1To1_9(), Arrays.asList(ProtocolVersion.v1_9_1.getId(), ProtocolVersion.v1_9_2.getId()), ProtocolVersion.v1_9.getId()); - registerProtocol(new Protocol1_9_3To1_9_1_2(), Collections.singletonList(ProtocolVersion.v1_9_3.getId()), ProtocolVersion.v1_9_2.getId()); + registerProtocol(new Protocol1_9_3To1_9_1_2(), ProtocolVersion.v1_9_3, ProtocolVersion.v1_9_2); // Only supported for 1.9.4 server to 1.9 (nothing else) - registerProtocol(new Protocol1_9To1_9_1(), Collections.singletonList(ProtocolVersion.v1_9.getId()), ProtocolVersion.v1_9_2.getId()); + registerProtocol(new Protocol1_9To1_9_1(), ProtocolVersion.v1_9, ProtocolVersion.v1_9_2); registerProtocol(new Protocol1_9_1_2To1_9_3_4(), Arrays.asList(ProtocolVersion.v1_9_1.getId(), ProtocolVersion.v1_9_2.getId()), ProtocolVersion.v1_9_3.getId()); - registerProtocol(new Protocol1_10To1_9_3_4(), Collections.singletonList(ProtocolVersion.v1_10.getId()), ProtocolVersion.v1_9_3.getId()); + registerProtocol(new Protocol1_10To1_9_3_4(), ProtocolVersion.v1_10, ProtocolVersion.v1_9_3); - registerProtocol(new Protocol1_11To1_10(), Collections.singletonList(ProtocolVersion.v1_11.getId()), ProtocolVersion.v1_10.getId()); - registerProtocol(new Protocol1_11_1To1_11(), Collections.singletonList(ProtocolVersion.v1_11_1.getId()), ProtocolVersion.v1_11.getId()); + registerProtocol(new Protocol1_11To1_10(), ProtocolVersion.v1_11, ProtocolVersion.v1_10); + registerProtocol(new Protocol1_11_1To1_11(), ProtocolVersion.v1_11_1, ProtocolVersion.v1_11); - registerProtocol(new Protocol1_12To1_11_1(), Collections.singletonList(ProtocolVersion.v1_12.getId()), ProtocolVersion.v1_11_1.getId()); - registerProtocol(new Protocol1_12_1To1_12(), Collections.singletonList(ProtocolVersion.v1_12_1.getId()), ProtocolVersion.v1_12.getId()); - registerProtocol(new Protocol1_12_2To1_12_1(), Collections.singletonList(ProtocolVersion.v1_12_2.getId()), ProtocolVersion.v1_12_1.getId()); + registerProtocol(new Protocol1_12To1_11_1(), ProtocolVersion.v1_12, ProtocolVersion.v1_11_1); + registerProtocol(new Protocol1_12_1To1_12(), ProtocolVersion.v1_12_1, ProtocolVersion.v1_12); + registerProtocol(new Protocol1_12_2To1_12_1(), ProtocolVersion.v1_12_2, ProtocolVersion.v1_12_1); - registerProtocol(new Protocol1_13To1_12_2(), Collections.singletonList(ProtocolVersion.v1_13.getId()), ProtocolVersion.v1_12_2.getId()); - registerProtocol(new Protocol1_13_1To1_13(), Collections.singletonList(ProtocolVersion.v1_13_1.getId()), ProtocolVersion.v1_13.getId()); - registerProtocol(new Protocol1_13_2To1_13_1(), Collections.singletonList(ProtocolVersion.v1_13_2.getId()), ProtocolVersion.v1_13_1.getId()); + registerProtocol(new Protocol1_13To1_12_2(), ProtocolVersion.v1_13, ProtocolVersion.v1_12_2); + registerProtocol(new Protocol1_13_1To1_13(), ProtocolVersion.v1_13_1, ProtocolVersion.v1_13); + registerProtocol(new Protocol1_13_2To1_13_1(), ProtocolVersion.v1_13_2, ProtocolVersion.v1_13_1); - registerProtocol(new Protocol1_14To1_13_2(), Collections.singletonList(ProtocolVersion.v1_14.getId()), ProtocolVersion.v1_13_2.getId()); - registerProtocol(new Protocol1_14_1To1_14(), Collections.singletonList(ProtocolVersion.v1_14_1.getId()), ProtocolVersion.v1_14.getId()); - registerProtocol(new Protocol1_14_2To1_14_1(), Collections.singletonList(ProtocolVersion.v1_14_2.getId()), ProtocolVersion.v1_14_1.getId()); - registerProtocol(new Protocol1_14_3To1_14_2(), Collections.singletonList(ProtocolVersion.v1_14_3.getId()), ProtocolVersion.v1_14_2.getId()); - registerProtocol(new Protocol1_14_4To1_14_3(), Collections.singletonList(ProtocolVersion.v1_14_4.getId()), ProtocolVersion.v1_14_3.getId()); + registerProtocol(new Protocol1_14To1_13_2(), ProtocolVersion.v1_14, ProtocolVersion.v1_13_2); + registerProtocol(new Protocol1_14_1To1_14(), ProtocolVersion.v1_14_1, ProtocolVersion.v1_14); + registerProtocol(new Protocol1_14_2To1_14_1(), ProtocolVersion.v1_14_2, ProtocolVersion.v1_14_1); + registerProtocol(new Protocol1_14_3To1_14_2(), ProtocolVersion.v1_14_3, ProtocolVersion.v1_14_2); + registerProtocol(new Protocol1_14_4To1_14_3(), ProtocolVersion.v1_14_4, ProtocolVersion.v1_14_3); + } + + /** + * Register a protocol + * + * @param protocol The protocol to register. + * @param supported Supported client versions. + * @param output The output server version it converts to. + */ + public static void registerProtocol(Protocol protocol, ProtocolVersion supported, ProtocolVersion output) { + registerProtocol(protocol, Collections.singletonList(supported.getId()), output.getId()); } /** @@ -81,7 +92,7 @@ public class ProtocolRegistry { */ public static void registerProtocol(Protocol protocol, List supported, Integer output) { // Clear cache as this may make new routes. - if (pathCache.size() > 0) + if (!pathCache.isEmpty()) pathCache.clear(); for (Integer version : supported) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/data/MappingData.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/data/MappingData.java index 95e69966a..e3ea1e6e8 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/data/MappingData.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/data/MappingData.java @@ -16,7 +16,6 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.StandardCharsets; -import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -46,17 +45,14 @@ public class MappingData { loadTags(fluidTags, mapping1_13.getAsJsonObject("fluid_tags")); Via.getPlatform().getLogger().info("Loading 1.12.2 -> 1.13 enchantment mapping..."); loadEnchantments(oldEnchantmentsIds, mapping1_12.getAsJsonObject("enchantments")); - enchantmentMappings = new EnchantmentMappingByteArray(mapping1_12.getAsJsonObject("enchantments"), mapping1_13.getAsJsonObject("enchantments")); + enchantmentMappings = new Mappings(72, mapping1_12.getAsJsonObject("enchantments"), mapping1_13.getAsJsonObject("enchantments")); Via.getPlatform().getLogger().info("Loading 1.12.2 -> 1.13 sound mapping..."); - soundMappings = new SoundMappingShortArray(mapping1_12.getAsJsonArray("sounds"), mapping1_13.getAsJsonArray("sounds")); + soundMappings = new Mappings(662, mapping1_12.getAsJsonArray("sounds"), mapping1_13.getAsJsonArray("sounds")); Via.getPlatform().getLogger().info("Loading translation mappping"); Map translateData = GsonUtil.getGson().fromJson( - new InputStreamReader( - MappingData.class.getClassLoader() - .getResourceAsStream("assets/viaversion/data/mapping-lang-1.12-1.13.json") - ), - (new TypeToken>() { - }).getType()); + new InputStreamReader(MappingData.class.getClassLoader().getResourceAsStream("assets/viaversion/data/mapping-lang-1.12-1.13.json")), + new TypeToken>() { + }.getType()); try { String[] lines; try (Reader reader = new InputStreamReader(MappingData.class.getClassLoader() @@ -100,12 +96,11 @@ public class MappingData { } } - private static class BlockMappingsShortArray implements Mappings { - private short[] oldToNew = new short[4084]; + private static class BlockMappingsShortArray extends Mappings { private BlockMappingsShortArray(JsonObject mapping1_12, JsonObject mapping1_13) { - Arrays.fill(oldToNew, (short) -1); - MappingDataLoader.mapIdentifiers(oldToNew, mapping1_12, mapping1_13); + super(4084, mapping1_12, mapping1_13); + // Map minecraft:snow[layers=1] of 1.12 to minecraft:snow[layers=2] in 1.13 if (Via.getConfig().isSnowCollisionFix()) { oldToNew[1248] = 3416; @@ -119,38 +114,5 @@ public class MappingData { oldToNew[1556] = 3985; // cracked stone bricks oldToNew[1557] = 3986; // chiseled stone bricks } - - @Override - public int getNewId(int old) { - return old >= 0 && old < oldToNew.length ? oldToNew[old] : -1; - } - } - - private static class SoundMappingShortArray implements Mappings { - private short[] oldToNew = new short[662]; - - private SoundMappingShortArray(JsonArray mapping1_12, JsonArray mapping1_13) { - Arrays.fill(oldToNew, (short) -1); - MappingDataLoader.mapIdentifiers(oldToNew, mapping1_12, mapping1_13); - } - - @Override - public int getNewId(int old) { - return old >= 0 && old < oldToNew.length ? oldToNew[old] : -1; - } - } - - private static class EnchantmentMappingByteArray implements Mappings { - private byte[] oldToNew = new byte[72]; - - private EnchantmentMappingByteArray(JsonObject m1_12, JsonObject m1_13) { - Arrays.fill(oldToNew, (byte) -1); - MappingDataLoader.mapIdentifiers(oldToNew, m1_12, m1_13); - } - - @Override - public int getNewId(int old) { - return old >= 0 && old < oldToNew.length ? oldToNew[old] : -1; - } } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/data/MappingData.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/data/MappingData.java index 2899a9332..b6594e4aa 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/data/MappingData.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/data/MappingData.java @@ -9,10 +9,13 @@ import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.MappingDataLoader; import us.myles.ViaVersion.api.data.Mappings; -import java.util.*; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; public class MappingData { - public final static BiMap oldToNewItems = HashBiMap.create(); + public static final BiMap oldToNewItems = HashBiMap.create(); public static Mappings blockStateMappings; public static Mappings blockMappings; public static Mappings soundMappings; @@ -23,13 +26,13 @@ public class MappingData { JsonObject mapping1_14 = MappingDataLoader.loadData("mapping-1.14.json"); Via.getPlatform().getLogger().info("Loading 1.13.2 -> 1.14 blockstate mapping..."); - blockStateMappings = new BlockMappingsShortArray(mapping1_13_2.getAsJsonObject("blockstates"), mapping1_14.getAsJsonObject("blockstates")); + blockStateMappings = new Mappings(mapping1_13_2.getAsJsonObject("blockstates"), mapping1_14.getAsJsonObject("blockstates")); Via.getPlatform().getLogger().info("Loading 1.13.2 -> 1.14 block mapping..."); - blockMappings = new BlockMappingsShortArray(mapping1_13_2.getAsJsonObject("blocks"), mapping1_14.getAsJsonObject("blocks")); + blockMappings = new Mappings(mapping1_13_2.getAsJsonObject("blocks"), mapping1_14.getAsJsonObject("blocks")); Via.getPlatform().getLogger().info("Loading 1.13.2 -> 1.14 item mapping..."); MappingDataLoader.mapIdentifiers(oldToNewItems, mapping1_13_2.getAsJsonObject("items"), mapping1_14.getAsJsonObject("items")); Via.getPlatform().getLogger().info("Loading 1.13.2 -> 1.14 sound mapping..."); - soundMappings = new SoundMappingShortArray(mapping1_13_2.getAsJsonArray("sounds"), mapping1_14.getAsJsonArray("sounds")); + soundMappings = new Mappings(mapping1_13_2.getAsJsonArray("sounds"), mapping1_14.getAsJsonArray("sounds")); Via.getPlatform().getLogger().info("Loading 1.14 blockstates..."); JsonObject blockStates = mapping1_14.getAsJsonObject("blockstates"); @@ -52,34 +55,4 @@ public class MappingData { } } } - - private static class SoundMappingShortArray implements Mappings { - private short[] oldToNew; - - private SoundMappingShortArray(JsonArray mapping1_13_2, JsonArray mapping1_14) { - oldToNew = new short[mapping1_13_2.size()]; - Arrays.fill(oldToNew, (short) -1); - MappingDataLoader.mapIdentifiers(oldToNew, mapping1_13_2, mapping1_14); - } - - @Override - public int getNewId(int old) { - return old >= 0 && old < oldToNew.length ? oldToNew[old] : -1; - } - } - - private static class BlockMappingsShortArray implements Mappings { - private short[] oldToNew; - - private BlockMappingsShortArray(JsonObject mapping1_13_2, JsonObject mapping1_14) { - oldToNew = new short[mapping1_13_2.entrySet().size()]; - Arrays.fill(oldToNew, (short) -1); - MappingDataLoader.mapIdentifiers(oldToNew, mapping1_13_2, mapping1_14); - } - - @Override - public int getNewId(int old) { - return old >= 0 && old < oldToNew.length ? oldToNew[old] : -1; - } - } }