From a191d0de1664289fe5d20b762e2c612aa2529b56 Mon Sep 17 00:00:00 2001 From: KennyTV <28825609+KennyTV@users.noreply.github.com> Date: Wed, 15 May 2019 13:32:29 +0200 Subject: [PATCH] Reduce code duplication MappingData in (also regarding future versions) --- .../api/data/MappingDataLoader.java | 100 +++++++++++++ .../myles/ViaVersion/api/data/Mappings.java | 6 + .../Protocol1_13To1_12_2.java | 2 +- .../providers/BlockConnectionProvider.java | 2 +- .../data/MappingData.java | 134 +++--------------- .../packets/InventoryPackets.java | 2 +- .../packets/WorldPackets.java | 4 +- .../storage/BlockConnectionStorage.java | 2 +- .../Protocol1_14_1To1_14.java | 18 +-- .../Protocol1_14To1_13_2.java | 6 +- .../data/MappingData.java | 124 +++------------- .../packets/WorldPackets.java | 6 +- 12 files changed, 164 insertions(+), 242 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/api/data/MappingDataLoader.java create mode 100644 common/src/main/java/us/myles/ViaVersion/api/data/Mappings.java diff --git a/common/src/main/java/us/myles/ViaVersion/api/data/MappingDataLoader.java b/common/src/main/java/us/myles/ViaVersion/api/data/MappingDataLoader.java new file mode 100644 index 000000000..66332acea --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/data/MappingDataLoader.java @@ -0,0 +1,100 @@ +package us.myles.ViaVersion.api.data; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.util.GsonUtil; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Map; + +public class MappingDataLoader { + + public static JsonObject loadData(String name) { + InputStream stream = MappingDataLoader.class.getClassLoader().getResourceAsStream("assets/viaversion/data/" + name); + InputStreamReader reader = new InputStreamReader(stream); + try { + return GsonUtil.getGson().fromJson(reader, JsonObject.class); + } finally { + try { + reader.close(); + } catch (IOException ignored) { + // Ignored + } + } + } + + public static void mapIdentifiers(Map output, JsonObject oldIdentifiers, JsonObject newIdentifiers) { + for (Map.Entry entry : oldIdentifiers.entrySet()) { + Map.Entry value = findValue(newIdentifiers, entry.getValue().getAsString()); + if (value == null) { + if (!Via.getConfig().isSuppress1_13ConversionErrors() || Via.getManager().isDebug()) { + Via.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( "); + } + continue; + } + output.put(Integer.parseInt(entry.getKey()), Integer.parseInt(value.getKey())); + } + } + + public static void mapIdentifiers(short[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers) { + for (Map.Entry entry : oldIdentifiers.entrySet()) { + Map.Entry value = findValue(newIdentifiers, entry.getValue().getAsString()); + if (value == null) { + if (!Via.getConfig().isSuppress1_13ConversionErrors() || Via.getManager().isDebug()) { + Via.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( "); + } + continue; + } + output[Integer.parseInt(entry.getKey())] = Short.parseShort(value.getKey()); + } + } + + public static void mapIdentifiers(short[] output, JsonArray oldIdentifiers, JsonArray newIdentifiers) { + for (int i = 0; i < oldIdentifiers.size(); i++) { + JsonElement v = oldIdentifiers.get(i); + Integer index = findIndex(newIdentifiers, v.getAsString()); + if (index == null) { + if (!Via.getConfig().isSuppress1_13ConversionErrors() || Via.getManager().isDebug()) { + Via.getPlatform().getLogger().warning("No key for " + v + " :( "); + } + continue; + } + output[i] = index.shortValue(); + } + } + + public static void mapIdentifiers(byte[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers) { + for (Map.Entry entry : oldIdentifiers.entrySet()) { + Map.Entry value = MappingDataLoader.findValue(newIdentifiers, entry.getValue().getAsString()); + if (value == null) { + Via.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( "); + continue; + } + output[Integer.parseInt(entry.getKey())] = Byte.parseByte(value.getKey()); + } + } + + public static Map.Entry findValue(JsonObject object, String needle) { + for (Map.Entry entry : object.entrySet()) { + String value = entry.getValue().getAsString(); + if (value.equals(needle)) { + return entry; + } + } + return null; + } + + public static Integer findIndex(JsonArray array, String value) { + for (int i = 0; i < array.size(); i++) { + JsonElement v = array.get(i); + if (v.getAsString().equals(value)) { + return i; + } + } + return null; + } +} 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 new file mode 100644 index 000000000..3999c5a9c --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/data/Mappings.java @@ -0,0 +1,6 @@ +package us.myles.ViaVersion.api.data; + +public interface Mappings { + + int getNewId(int old); +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java index 3fdcbcbf4..539021720 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java @@ -1159,7 +1159,7 @@ public class Protocol1_13To1_12_2 extends Protocol { } private int getNewSoundID(final int oldID) { - return MappingData.soundMappings.getNewSound(oldID); + return MappingData.soundMappings.getNewId(oldID); } // Based on method from https://github.com/Bukkit/Bukkit/blob/master/src/main/java/org/bukkit/ChatColor.java diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/providers/BlockConnectionProvider.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/providers/BlockConnectionProvider.java index 4cac4c0fa..26330d7d9 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/providers/BlockConnectionProvider.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/providers/BlockConnectionProvider.java @@ -9,7 +9,7 @@ public class BlockConnectionProvider implements Provider { public int getBlockdata(UserConnection connection, Position position) { int oldId = getWorldBlockData(connection, position); - return MappingData.blockMappings.getNewBlock(oldId); + return MappingData.blockMappings.getNewId(oldId); } public int getWorldBlockData(UserConnection connection, Position position) { 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 45c1efd1f..02b93706d 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 @@ -8,10 +8,11 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.data.MappingDataLoader; +import us.myles.ViaVersion.api.data.Mappings; import us.myles.ViaVersion.util.GsonUtil; import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.StandardCharsets; @@ -27,18 +28,18 @@ public class MappingData { public static BiMap oldEnchantmentsIds = HashBiMap.create(); public static Map translateMapping = new HashMap<>(); public static Map mojangTranslation = new HashMap<>(); - public static EnchantmentMappings enchantmentMappings; - public static SoundMappings soundMappings; - public static BlockMappings blockMappings; + public static Mappings enchantmentMappings; + public static Mappings soundMappings; + public static Mappings blockMappings; public static void init() { - JsonObject mapping1_12 = loadData("mapping-1.12.json"); - JsonObject mapping1_13 = loadData("mapping-1.13.json"); + JsonObject mapping1_12 = MappingDataLoader.loadData("mapping-1.12.json"); + JsonObject mapping1_13 = MappingDataLoader.loadData("mapping-1.13.json"); Via.getPlatform().getLogger().info("Loading 1.12.2 -> 1.13 block mapping..."); blockMappings = new BlockMappingsShortArray(mapping1_12.getAsJsonObject("blocks"), mapping1_13.getAsJsonObject("blocks")); Via.getPlatform().getLogger().info("Loading 1.12.2 -> 1.13 item mapping..."); - mapIdentifiers(oldToNewItems, mapping1_12.getAsJsonObject("items"), mapping1_13.getAsJsonObject("items")); + MappingDataLoader.mapIdentifiers(oldToNewItems, mapping1_12.getAsJsonObject("items"), mapping1_13.getAsJsonObject("items")); Via.getPlatform().getLogger().info("Loading new 1.13 tags..."); loadTags(blockTags, mapping1_13.getAsJsonObject("block_tags")); loadTags(itemTags, mapping1_13.getAsJsonObject("item_tags")); @@ -55,7 +56,8 @@ public class MappingData { MappingData.class.getClassLoader() .getResourceAsStream("assets/viaversion/data/mapping-lang-1.12-1.13.json") ), - (new TypeToken>(){}).getType()); + (new TypeToken>() { + }).getType()); try { String[] lines; try (Reader reader = new InputStreamReader(MappingData.class.getClassLoader() @@ -82,72 +84,6 @@ public class MappingData { } } - public static JsonObject loadData(String name) { - InputStream stream = MappingData.class.getClassLoader().getResourceAsStream("assets/viaversion/data/" + name); - InputStreamReader reader = new InputStreamReader(stream); - try { - JsonObject jsonObject = GsonUtil.getGson().fromJson(reader, JsonObject.class); - return jsonObject; - } finally { - try { - reader.close(); - } catch (IOException ignored) { - // Ignored - } - } - } - - private static void mapIdentifiers(Map output, JsonObject oldIdentifiers, JsonObject newIdentifiers) { - for (Map.Entry entry : oldIdentifiers.entrySet()) { - Map.Entry value = findValue(newIdentifiers, entry.getValue().getAsString()); - if (value == null) { - if (!Via.getConfig().isSuppress1_13ConversionErrors() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( "); - } - continue; - } - output.put(Integer.parseInt(entry.getKey()), Integer.parseInt(value.getKey())); - } - } - - private static void mapIdentifiers(short[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers) { - for (Map.Entry entry : oldIdentifiers.entrySet()) { - Map.Entry value = findValue(newIdentifiers, entry.getValue().getAsString()); - if (value == null) { - if (!Via.getConfig().isSuppress1_13ConversionErrors() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( "); - } - continue; - } - output[Integer.parseInt(entry.getKey())] = Short.parseShort(value.getKey()); - } - } - - private static void mapIdentifiers(byte[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers) { - for (Map.Entry entry : oldIdentifiers.entrySet()) { - Map.Entry value = findValue(newIdentifiers, entry.getValue().getAsString()); - if (value == null) { - Via.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( "); - continue; - } - output[Integer.parseInt(entry.getKey())] = Byte.parseByte(value.getKey()); - } - } - - private static void mapIdentifiers(short[] output, JsonArray oldIdentifiers, JsonArray newIdentifiers) { - for (int i = 0; i < oldIdentifiers.size(); i++) { - JsonElement v = oldIdentifiers.get(i); - Integer index = findIndex(newIdentifiers, v.getAsString()); - if (index == null) { - if (!Via.getConfig().isSuppress1_13ConversionErrors() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().warning("No key for " + v + " :( "); - } - continue; - } - output[i] = index.shortValue(); - } - } - private static void loadTags(Map output, JsonObject newTags) { for (Map.Entry entry : newTags.entrySet()) { JsonArray ids = entry.getValue().getAsJsonArray(); @@ -165,36 +101,12 @@ public class MappingData { } } - private static Map.Entry findValue(JsonObject object, String needle) { - for (Map.Entry entry : object.entrySet()) { - String value = entry.getValue().getAsString(); - if (value.equals(needle)) { - return entry; - } - } - return null; - } - - private static Integer findIndex(JsonArray array, String value) { - for (int i = 0; i < array.size(); i++) { - JsonElement v = array.get(i); - if (v.getAsString().equals(value)) { - return i; - } - } - return null; - } - - public interface BlockMappings { - int getNewBlock(int old); - } - - private static class BlockMappingsShortArray implements BlockMappings { + private static class BlockMappingsShortArray implements Mappings { private short[] oldToNew = new short[4084]; private BlockMappingsShortArray(JsonObject mapping1_12, JsonObject mapping1_13) { Arrays.fill(oldToNew, (short) -1); - mapIdentifiers(oldToNew, mapping1_12, mapping1_13); + MappingDataLoader.mapIdentifiers(oldToNew, 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; @@ -202,43 +114,35 @@ public class MappingData { } @Override - public int getNewBlock(int old) { + public int getNewId(int old) { return old >= 0 && old < oldToNew.length ? oldToNew[old] : -1; } } - public interface SoundMappings { - int getNewSound(int old); - } - - private static class SoundMappingShortArray implements SoundMappings { + 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); - mapIdentifiers(oldToNew, mapping1_12, mapping1_13); + MappingDataLoader.mapIdentifiers(oldToNew, mapping1_12, mapping1_13); } @Override - public int getNewSound(int old) { + public int getNewId(int old) { return old >= 0 && old < oldToNew.length ? oldToNew[old] : -1; } } - public interface EnchantmentMappings { - int getNewEnchantment(int old); - } - - private static class EnchantmentMappingByteArray implements EnchantmentMappings { + 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); - mapIdentifiers(oldToNew, m1_12, m1_13); + MappingDataLoader.mapIdentifiers(oldToNew, m1_12, m1_13); } @Override - public int getNewEnchantment(int old) { + 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_13to1_12_2/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java index 2d8ad2522..6bfb3aec9 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java @@ -81,7 +81,7 @@ public class InventoryPackets { public void handle(PacketWrapper wrapper) throws Exception { short property = wrapper.get(Type.SHORT, 0); if (property >= 4 && property <= 6) { // Enchantment id - wrapper.set(Type.SHORT, 1, (short) MappingData.enchantmentMappings.getNewEnchantment( + wrapper.set(Type.SHORT, 1, (short) MappingData.enchantmentMappings.getNewId( wrapper.get(Type.SHORT, 1) )); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/WorldPackets.java index a5b82467d..17680c8e0 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/WorldPackets.java @@ -496,11 +496,11 @@ public class WorldPackets { if (oldId < 0) { oldId = 0; // Some plugins use negative numbers to clear blocks, remap them to air. } - int newId = MappingData.blockMappings.getNewBlock(oldId); + int newId = MappingData.blockMappings.getNewId(oldId); if (newId != -1) { return newId; } - newId = MappingData.blockMappings.getNewBlock(oldId & ~0xF); // Remove data + newId = MappingData.blockMappings.getNewId(oldId & ~0xF); // Remove data if (newId != -1) { if (!Via.getConfig().isSuppress1_13ConversionErrors() || Via.getManager().isDebug()) { Via.getPlatform().getLogger().warning("Missing block " + oldId); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/BlockConnectionStorage.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/BlockConnectionStorage.java index 176e66563..96de6245a 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/BlockConnectionStorage.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/BlockConnectionStorage.java @@ -28,7 +28,7 @@ public class BlockConnectionStorage extends StoredObject { } reverseBlockMappings = new HashMap<>(); for (int i = 0; i < 4096; i++) { - int newBlock = MappingData.blockMappings.getNewBlock(i); + int newBlock = MappingData.blockMappings.getNewId(i); if (newBlock != -1) reverseBlockMappings.put((short) newBlock, (short) i); } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/Protocol1_14_1To1_14.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/Protocol1_14_1To1_14.java index 0758c64e1..298913ccc 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/Protocol1_14_1To1_14.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/Protocol1_14_1To1_14.java @@ -8,15 +8,15 @@ import us.myles.ViaVersion.protocols.protocol1_14_1to1_14.storage.EntityTracker1 public class Protocol1_14_1To1_14 extends Protocol { - @Override - protected void registerPackets() { - put(new MetadataRewriter1_14_1To1_14()); + @Override + protected void registerPackets() { + put(new MetadataRewriter1_14_1To1_14()); - EntityPackets.register(this); - } + EntityPackets.register(this); + } - @Override - public void init(UserConnection userConnection) { - userConnection.put(new EntityTracker1_14_1(userConnection)); - } + @Override + public void init(UserConnection userConnection) { + userConnection.put(new EntityTracker1_14_1(userConnection)); + } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java index f99f45c6e..4cce427be 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java @@ -296,7 +296,7 @@ public class Protocol1_14To1_13_2 extends Protocol { } public static int getNewSoundId(int id) { - int newId = MappingData.soundMappings.getNewSound(id); + int newId = MappingData.soundMappings.getNewId(id); if (newId == -1) { Via.getPlatform().getLogger().warning("Missing 1.14 sound for 1.13.2 sound " + id); return 0; @@ -305,7 +305,7 @@ public class Protocol1_14To1_13_2 extends Protocol { } public static int getNewBlockStateId(int id) { - int newId = MappingData.blockStateMappings.getNewBlock(id); + int newId = MappingData.blockStateMappings.getNewId(id); if (newId == -1) { Via.getPlatform().getLogger().warning("Missing 1.14 blockstate for 1.13.2 blockstate " + id); return 0; @@ -314,7 +314,7 @@ public class Protocol1_14To1_13_2 extends Protocol { } public static int getNewBlockId(int id) { - int newId = MappingData.blockMappings.getNewBlock(id); + int newId = MappingData.blockMappings.getNewId(id); if (newId == -1) { Via.getPlatform().getLogger().warning("Missing 1.14 block for 1.13.2 block " + id); return 0; 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 f85760213..739a43336 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 @@ -6,34 +6,28 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import us.myles.ViaVersion.api.Via; -import us.myles.ViaVersion.util.GsonUtil; +import us.myles.ViaVersion.api.data.MappingDataLoader; +import us.myles.ViaVersion.api.data.Mappings; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; public class MappingData { public static BiMap oldToNewItems = HashBiMap.create(); - public static BlockMappings blockStateMappings; - public static BlockMappings blockMappings; - public static SoundMappings soundMappings; + public static Mappings blockStateMappings; + public static Mappings blockMappings; + public static Mappings soundMappings; public static Set motionBlocking; public static void init() { - JsonObject mapping1_13_2 = loadData("mapping-1.13.2.json"); - JsonObject mapping1_14 = loadData("mapping-1.14.json"); + JsonObject mapping1_13_2 = MappingDataLoader.loadData("mapping-1.13.2.json"); + 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")); Via.getPlatform().getLogger().info("Loading 1.13.2 -> 1.14 block mapping..."); blockMappings = new BlockMappingsShortArray(mapping1_13_2.getAsJsonObject("blocks"), mapping1_14.getAsJsonObject("blocks")); Via.getPlatform().getLogger().info("Loading 1.13.2 -> 1.14 item mapping..."); - mapIdentifiers(oldToNewItems, mapping1_13_2.getAsJsonObject("items"), mapping1_14.getAsJsonObject("items")); + 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")); @@ -45,128 +39,46 @@ public class MappingData { } Via.getPlatform().getLogger().info("Loading 1.14 heightmap data..."); - JsonObject heightMapData = loadData("heightMapData-1.14.json"); + JsonObject heightMapData = MappingDataLoader.loadData("heightMapData-1.14.json"); JsonArray motionBlocking = heightMapData.getAsJsonArray("MOTION_BLOCKING"); - MappingData.motionBlocking = new HashSet<>(motionBlocking.size()); + us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData.motionBlocking = new HashSet<>(motionBlocking.size()); for (JsonElement blockState : motionBlocking) { String key = blockState.getAsString(); Integer id = blockStateMap.get(key); if (id == null) { Via.getPlatform().getLogger().warning("Unknown blockstate " + key + " :("); } else { - MappingData.motionBlocking.add(id); + us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData.motionBlocking.add(id); } } } - public static JsonObject loadData(String name) { - InputStream stream = MappingData.class.getClassLoader().getResourceAsStream("assets/viaversion/data/" + name); - InputStreamReader reader = new InputStreamReader(stream); - try { - return GsonUtil.getGson().fromJson(reader, JsonObject.class); - } finally { - try { - reader.close(); - } catch (IOException ignored) { - // Ignored - } - } - } - - private static void mapIdentifiers(Map output, JsonObject oldIdentifiers, JsonObject newIdentifiers) { - for (Map.Entry entry : oldIdentifiers.entrySet()) { - Map.Entry value = findValue(newIdentifiers, entry.getValue().getAsString()); - if (value == null) { - if (!Via.getConfig().isSuppress1_13ConversionErrors() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( "); - } - continue; - } - output.put(Integer.parseInt(entry.getKey()), Integer.parseInt(value.getKey())); - } - } - - private static void mapIdentifiers(short[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers) { - for (Map.Entry entry : oldIdentifiers.entrySet()) { - Map.Entry value = findValue(newIdentifiers, entry.getValue().getAsString()); - if (value == null) { - if (!Via.getConfig().isSuppress1_13ConversionErrors() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( "); - } - continue; - } - output[Integer.parseInt(entry.getKey())] = Short.parseShort(value.getKey()); - } - } - - private static void mapIdentifiers(short[] output, JsonArray oldIdentifiers, JsonArray newIdentifiers) { - for (int i = 0; i < oldIdentifiers.size(); i++) { - JsonElement v = oldIdentifiers.get(i); - Integer index = findIndex(newIdentifiers, v.getAsString()); - if (index == null) { - if (!Via.getConfig().isSuppress1_13ConversionErrors() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().warning("No key for " + v + " :( "); - } - continue; - } - output[i] = index.shortValue(); - } - } - - private static Map.Entry findValue(JsonObject object, String needle) { - for (Map.Entry entry : object.entrySet()) { - String value = entry.getValue().getAsString(); - if (value.equals(needle)) { - return entry; - } - } - return null; - } - - private static Integer findIndex(JsonArray array, String value) { - for (int i = 0; i < array.size(); i++) { - JsonElement v = array.get(i); - if (v.getAsString().equals(value)) { - return i; - } - } - return null; - } - - public interface SoundMappings { - int getNewSound(int old); - } - - private static class SoundMappingShortArray implements SoundMappings { + 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); - mapIdentifiers(oldToNew, mapping1_13_2, mapping1_14); + MappingDataLoader.mapIdentifiers(oldToNew, mapping1_13_2, mapping1_14); } @Override - public int getNewSound(int old) { + public int getNewId(int old) { return old >= 0 && old < oldToNew.length ? oldToNew[old] : -1; } } - public interface BlockMappings { - int getNewBlock(int old); - } - - private static class BlockMappingsShortArray implements BlockMappings { + 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); - mapIdentifiers(oldToNew, mapping1_13_2, mapping1_14); + MappingDataLoader.mapIdentifiers(oldToNew, mapping1_13_2, mapping1_14); } @Override - public int getNewBlock(int old) { + 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/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java index f4fd5c29d..0301339e9 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java @@ -23,9 +23,9 @@ import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; public class WorldPackets { - private static final int AIR = MappingData.blockStateMappings.getNewBlock(0); - private static final int VOID_AIR = MappingData.blockStateMappings.getNewBlock(8591); - private static final int CAVE_AIR = MappingData.blockStateMappings.getNewBlock(8592); + private static final int AIR = MappingData.blockStateMappings.getNewId(0); + private static final int VOID_AIR = MappingData.blockStateMappings.getNewId(8591); + private static final int CAVE_AIR = MappingData.blockStateMappings.getNewId(8592); public static final int SERVERSIDE_VIEW_DISTANCE = 64; public static void register(final Protocol protocol) {