From d36def89e160b5c0464ad4e609f983579ab35325 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Sun, 16 Aug 2020 16:24:06 +0200 Subject: [PATCH] Abstract mapping loading --- bukkit-legacy/pom.xml | 2 +- bukkit/pom.xml | 2 +- bungee/pom.xml | 2 +- common/pom.xml | 2 +- .../ViaVersion/api/data/MappingData.java | 128 ++++ .../api/data/MappingDataLoader.java | 18 +- .../myles/ViaVersion/api/data/Mappings.java | 11 +- .../ViaVersion/api/protocol/Protocol.java | 45 +- .../api/protocol/SimpleProtocol.java | 4 - .../api/rewriters/BlockRewriter.java | 22 +- .../api/rewriters/ComponentRewriter.java | 4 + .../api/rewriters/ItemRewriter.java | 4 +- .../api/rewriters/MetadataRewriter.java | 6 +- .../api/rewriters/SoundRewriter.java | 6 +- .../api/rewriters/StatisticsRewriter.java | 22 +- .../ViaVersion/api/rewriters/TagRewriter.java | 18 +- .../Protocol1_11To1_10.java | 3 +- .../Protocol1_12To1_11_1.java | 3 +- .../Protocol1_13_1To1_13.java | 36 +- .../MetadataRewriter1_13_1To1_13.java | 4 +- .../packets/EntityPackets.java | 2 +- .../packets/InventoryPackets.java | 34 +- .../packets/WorldPackets.java | 7 +- .../Protocol1_13To1_12_2.java | 128 ++-- .../blockconnections/ConnectionData.java | 2 +- .../providers/BlockConnectionProvider.java | 4 +- .../data/ComponentRewriter1_13.java | 6 +- .../data/MappingData.java | 120 ++-- .../packets/InventoryPackets.java | 25 +- .../packets/WorldPackets.java | 5 +- .../storage/BlockConnectionStorage.java | 4 +- .../Protocol1_14_1To1_14.java | 3 +- .../Protocol1_14To1_13_2.java | 57 +- .../data/MappingData.java | 48 +- .../MetadataRewriter1_14To1_13_2.java | 10 +- .../packets/EntityPackets.java | 2 +- .../packets/InventoryPackets.java | 20 +- .../packets/WorldPackets.java | 20 +- .../Protocol1_15To1_14_4.java | 41 +- .../data/MappingData.java | 33 +- .../MetadataRewriter1_15To1_14_4.java | 2 +- .../packets/EntityPackets.java | 2 +- .../packets/InventoryPackets.java | 26 +- .../packets/WorldPackets.java | 11 +- .../Protocol1_16_2To1_16_1.java | 41 +- .../data/MappingData.java | 36 +- .../MetadataRewriter1_16_2To1_16_1.java | 2 +- .../packets/EntityPackets.java | 8 +- .../packets/InventoryPackets.java | 22 +- .../packets/WorldPackets.java | 8 +- .../Protocol1_16To1_15_2.java | 40 +- .../data/MappingData.java | 34 +- .../MetadataRewriter1_16To1_15_2.java | 2 +- .../packets/EntityPackets.java | 5 +- .../packets/InventoryPackets.java | 33 +- .../packets/WorldPackets.java | 9 +- .../protocol1_9to1_8/Protocol1_9To1_8.java | 3 +- .../assets/viaversion/data/mapping-1.13.json | 597 +++++++++++++++++- .../data/mappingdiff-1.13to1.13.2.json | 64 ++ ...6.2.json => mappingdiff-1.16to1.16.2.json} | 0 fabric/pom.xml | 2 +- jar/pom.xml | 2 +- pom.xml | 2 +- sponge-legacy/pom.xml | 2 +- sponge/pom.xml | 2 +- velocity/pom.xml | 2 +- 66 files changed, 1264 insertions(+), 606 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/api/data/MappingData.java create mode 100644 common/src/main/resources/assets/viaversion/data/mappingdiff-1.13to1.13.2.json rename common/src/main/resources/assets/viaversion/data/{mappingdiff-1.16.1to1.16.2.json => mappingdiff-1.16to1.16.2.json} (100%) diff --git a/bukkit-legacy/pom.xml b/bukkit-legacy/pom.xml index c90d1c429..c4769f0b5 100644 --- a/bukkit-legacy/pom.xml +++ b/bukkit-legacy/pom.xml @@ -5,7 +5,7 @@ viaversion-parent us.myles - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT 4.0.0 diff --git a/bukkit/pom.xml b/bukkit/pom.xml index 3289405fd..e259abe70 100644 --- a/bukkit/pom.xml +++ b/bukkit/pom.xml @@ -5,7 +5,7 @@ viaversion-parent us.myles - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT 4.0.0 diff --git a/bungee/pom.xml b/bungee/pom.xml index 2fb8595f5..d649993e3 100644 --- a/bungee/pom.xml +++ b/bungee/pom.xml @@ -5,7 +5,7 @@ viaversion-parent us.myles - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT 4.0.0 diff --git a/common/pom.xml b/common/pom.xml index 4f316aaf2..17638b0f5 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -5,7 +5,7 @@ viaversion-parent us.myles - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT 4.0.0 diff --git a/common/src/main/java/us/myles/ViaVersion/api/data/MappingData.java b/common/src/main/java/us/myles/ViaVersion/api/data/MappingData.java new file mode 100644 index 000000000..5b4538874 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/data/MappingData.java @@ -0,0 +1,128 @@ +package us.myles.ViaVersion.api.data; + +import com.google.gson.JsonObject; +import org.jetbrains.annotations.Nullable; +import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.util.Int2IntBiMap; + +public class MappingData { + protected final String oldVersion; + protected final String newVersion; + protected final boolean hasDiffFile; + protected Int2IntBiMap itemMappings; + protected Mappings blockMappings; + protected Mappings blockStateMappings; + protected Mappings soundMappings; + protected Mappings statisticsMappings; + protected boolean loadItems = true; + + public MappingData(String oldVersion, String newVersion) { + this(oldVersion, newVersion, false); + } + + public MappingData(String oldVersion, String newVersion, boolean hasDiffFile) { + this.oldVersion = oldVersion; + this.newVersion = newVersion; + this.hasDiffFile = hasDiffFile; + } + + public void load() { + Via.getPlatform().getLogger().info("Loading " + oldVersion + " -> " + newVersion + " mappings..."); + JsonObject diffmapping = hasDiffFile ? loadDiffFile() : null; + JsonObject oldMappings = MappingDataLoader.loadData("mapping-" + oldVersion + ".json", true); + JsonObject newMappings = MappingDataLoader.loadData("mapping-" + newVersion + ".json", true); + + blockMappings = loadFromObject(oldMappings, newMappings, diffmapping, "blocks"); + blockStateMappings = loadFromObject(oldMappings, newMappings, diffmapping, "blockstates"); + soundMappings = loadFromArray(oldMappings, newMappings, diffmapping, "sounds"); + statisticsMappings = loadFromArray(oldMappings, newMappings, diffmapping, "statistics"); + if (loadItems && newMappings.has("items")) { + itemMappings = new Int2IntBiMap(); + itemMappings.defaultReturnValue(-1); + MappingDataLoader.mapIdentifiers(itemMappings, oldMappings.getAsJsonObject("items"), newMappings.getAsJsonObject("items"), + diffmapping != null ? diffmapping.getAsJsonObject("items") : null); + } + + loadExtras(oldMappings, newMappings, diffmapping); + } + + public int getNewBlockStateId(int id) { + return checkValidity(blockStateMappings.getNewId(id), "blockstate"); + } + + public int getNewBlockId(int id) { + return checkValidity(blockMappings.getNewId(id), "block"); + } + + public int getNewItemId(int id) { + return checkValidity(itemMappings.get(id), "item"); + } + + public int getOldItemId(int id) { + int oldId = itemMappings.inverse().get(id); + return oldId != -1 ? oldId : 1; + } + + @Nullable + public Int2IntBiMap getItemMappings() { + return itemMappings; + } + + @Nullable + public Mappings getBlockMappings() { + return blockMappings; + } + + @Nullable + public Mappings getBlockStateMappings() { + return blockStateMappings; + } + + @Nullable + public Mappings getSoundMappings() { + return soundMappings; + } + + @Nullable + public Mappings getStatisticsMappings() { + return statisticsMappings; + } + + @Nullable + protected Mappings loadFromArray(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings, String key) { + if (!oldMappings.has(key) || !newMappings.has(key)) return null; + + JsonObject diff = diffMappings != null ? diffMappings.getAsJsonObject(key) : null; + return new Mappings(oldMappings.getAsJsonArray(key), newMappings.getAsJsonArray(key), diff); + } + + @Nullable + protected Mappings loadFromObject(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings, String key) { + if (!oldMappings.has(key) || !newMappings.has(key)) return null; + + JsonObject diff = diffMappings != null ? diffMappings.getAsJsonObject(key) : null; + return new Mappings(oldMappings.getAsJsonObject(key), newMappings.getAsJsonObject(key), diff); + } + + protected JsonObject loadDiffFile() { + return MappingDataLoader.loadData("mappingdiff-" + oldVersion + "to" + newVersion + ".json"); + } + + protected int checkValidity(int id, String type) { + if (id == -1) { + Via.getPlatform().getLogger().warning(String.format("Missing %s %s for %s %s %d", newVersion, type, oldVersion, type, id)); + return 0; + } + return id; + } + + /** + * To be overridden. + * + * @param oldMappings old mappings + * @param newMappings new mappings + * @param diffMappings diff mappings if present + */ + protected void loadExtras(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings) { + } +} 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 index 528618268..076f60c4e 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/data/MappingDataLoader.java +++ b/common/src/main/java/us/myles/ViaVersion/api/data/MappingDataLoader.java @@ -48,6 +48,7 @@ public class MappingDataLoader { /** * Loads the file from the plugin folder if present, else from the bundled resources. */ + @Nullable public static JsonObject loadFromDataDir(String name) { File file = new File(Via.getPlatform().getDataFolder(), name); if (!file.exists()) return loadData(name); @@ -68,6 +69,7 @@ public class MappingDataLoader { /** * Loads the file from the bundled resources. Uses the cache if enabled. */ + @Nullable public static JsonObject loadData(String name) { return loadData(name, false); } @@ -77,6 +79,7 @@ public class MappingDataLoader { * * @param cacheIfEnabled whether loaded files should be cached */ + @Nullable public static JsonObject loadData(String name, boolean cacheIfEnabled) { if (cacheJsonMappings) { JsonObject cached = MAPPINGS_CACHE.get(name); @@ -86,6 +89,8 @@ public class MappingDataLoader { } InputStream stream = getResource(name); + if (stream == null) return null; + InputStreamReader reader = new InputStreamReader(stream); try { JsonObject object = GsonUtil.getGson().fromJson(reader, JsonObject.class); @@ -106,9 +111,9 @@ public class MappingDataLoader { mapIdentifiers(output, oldIdentifiers, newIdentifiers, null); } - public static void mapIdentifiers(Map output, JsonObject oldIdentifiers, JsonObject newIdentifiers, JsonObject diffIdentifiers) { + public static void mapIdentifiers(Map output, JsonObject oldIdentifiers, JsonObject newIdentifiers, @Nullable JsonObject diffIdentifiers) { for (Map.Entry entry : oldIdentifiers.entrySet()) { - Map.Entry value = mapIdentifierEntry(entry, oldIdentifiers, newIdentifiers, diffIdentifiers); + Map.Entry value = mapIdentifierEntry(entry, newIdentifiers, diffIdentifiers); if (value != null) { output.put(Integer.parseInt(entry.getKey()), Integer.parseInt(value.getKey())); } @@ -119,9 +124,9 @@ public class MappingDataLoader { MappingDataLoader.mapIdentifiers(output, oldIdentifiers, newIdentifiers, null); } - public static void mapIdentifiers(short[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers, JsonObject diffIdentifiers) { + public static void mapIdentifiers(short[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers, @Nullable JsonObject diffIdentifiers) { for (Map.Entry entry : oldIdentifiers.entrySet()) { - Map.Entry value = mapIdentifierEntry(entry, oldIdentifiers, newIdentifiers, diffIdentifiers); + Map.Entry value = mapIdentifierEntry(entry, newIdentifiers, diffIdentifiers); if (value != null) { output[Integer.parseInt(entry.getKey())] = Short.parseShort(value.getKey()); } @@ -129,7 +134,7 @@ public class MappingDataLoader { } @Nullable - private static Map.Entry mapIdentifierEntry(Map.Entry entry, JsonObject oldIdentifiers, JsonObject newIdentifiers, JsonObject diffIdentifiers) { + private static Map.Entry mapIdentifierEntry(Map.Entry entry, JsonObject newIdentifiers, @Nullable JsonObject diffIdentifiers) { Map.Entry value = findValue(newIdentifiers, entry.getValue().getAsString()); if (value == null) { // Search in diff mappings @@ -153,7 +158,7 @@ public class MappingDataLoader { mapIdentifiers(output, oldIdentifiers, newIdentifiers, null, warnOnMissing); } - public static void mapIdentifiers(short[] output, JsonArray oldIdentifiers, JsonArray newIdentifiers, JsonObject diffIdentifiers, boolean warnOnMissing) { + public static void mapIdentifiers(short[] output, JsonArray oldIdentifiers, JsonArray newIdentifiers, @Nullable JsonObject diffIdentifiers, boolean warnOnMissing) { for (int i = 0; i < oldIdentifiers.size(); i++) { JsonElement value = oldIdentifiers.get(i); Integer index = findIndex(newIdentifiers, value.getAsString()); @@ -208,6 +213,7 @@ public class MappingDataLoader { return null; } + @Nullable public static InputStream getResource(String name) { return MappingDataLoader.class.getClassLoader().getResourceAsStream("assets/viaversion/data/" + name); } 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 bb2ac20b9..3a8f167cf 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 @@ -2,13 +2,14 @@ package us.myles.ViaVersion.api.data; import com.google.gson.JsonArray; import com.google.gson.JsonObject; +import org.jetbrains.annotations.Nullable; import java.util.Arrays; public class Mappings { protected final short[] oldToNew; - protected Mappings(short[] oldToNew) { + public Mappings(short[] oldToNew) { this.oldToNew = oldToNew; } @@ -21,13 +22,13 @@ public class Mappings { * @param newMapping mappings to map to * @param diffMapping extra mappings that will be used/scanned when an entry cannot be found */ - public Mappings(int size, JsonObject oldMapping, JsonObject newMapping, JsonObject diffMapping) { + public Mappings(int size, JsonObject oldMapping, JsonObject newMapping, @Nullable JsonObject diffMapping) { oldToNew = new short[size]; Arrays.fill(oldToNew, (short) -1); MappingDataLoader.mapIdentifiers(oldToNew, oldMapping, newMapping, diffMapping); } - public Mappings(JsonObject oldMapping, JsonObject newMapping, JsonObject diffMapping) { + public Mappings(JsonObject oldMapping, JsonObject newMapping, @Nullable JsonObject diffMapping) { this(oldMapping.entrySet().size(), oldMapping, newMapping, diffMapping); } @@ -86,4 +87,8 @@ public class Mappings { public int getNewId(int old) { return old >= 0 && old < oldToNew.length ? oldToNew[old] : -1; } + + public short[] getOldToNew() { + return oldToNew; + } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java b/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java index 362e45371..f6d8d60e3 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java +++ b/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java @@ -4,6 +4,7 @@ import com.google.common.base.Preconditions; import org.jetbrains.annotations.Nullable; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.data.MappingData; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.platform.providers.ViaProviders; import us.myles.ViaVersion.api.remapper.PacketRemapper; @@ -36,14 +37,9 @@ public abstract class Protocol newClientboundPacketEnum; protected final Class oldServerboundPacketEnum; protected final Class newServerboundPacketEnum; - protected final boolean hasMappingDataToLoad; protected Protocol() { - this(null, null, null, null, false); - } - - protected Protocol(boolean hasMappingDataToLoad) { - this(null, null, null, null, hasMappingDataToLoad); + this(null, null, null, null); } /** @@ -51,21 +47,10 @@ public abstract class Protocol oldClientboundPacketEnum, @Nullable Class clientboundPacketEnum, @Nullable Class oldServerboundPacketEnum, @Nullable Class serverboundPacketEnum) { - this(oldClientboundPacketEnum, clientboundPacketEnum, oldServerboundPacketEnum, serverboundPacketEnum, false); - } - - /** - * Creates a protocol with automated id mapping if the respective enums are not null. - * - * @param hasMappingDataToLoad whether an async executor should call the {@Link #loadMappingData} method - */ - protected Protocol(@Nullable Class oldClientboundPacketEnum, @Nullable Class clientboundPacketEnum, - @Nullable Class oldServerboundPacketEnum, @Nullable Class serverboundPacketEnum, boolean hasMappingDataToLoad) { this.oldClientboundPacketEnum = oldClientboundPacketEnum; this.newClientboundPacketEnum = clientboundPacketEnum; this.oldServerboundPacketEnum = oldServerboundPacketEnum; this.newServerboundPacketEnum = serverboundPacketEnum; - this.hasMappingDataToLoad = hasMappingDataToLoad; registerPackets(); // Register the rest of the ids with no handlers if necessary @@ -157,11 +142,19 @@ public abstract class Protocol * To be overridden if needed. */ - protected void loadMappingData() { + protected void onMappingDataLoaded() { } /** @@ -387,10 +380,6 @@ public abstract class Protocol positionType; - public BlockRewriter(Protocol protocol, Type positionType, IdRewriteFunction blockStateRewriter, IdRewriteFunction blockRewriter) { + public BlockRewriter(Protocol protocol, Type positionType) { this.protocol = protocol; this.positionType = positionType; - this.blockStateRewriter = blockStateRewriter; - this.blockRewriter = blockRewriter; } public void registerBlockAction(ClientboundPacketType packetType) { @@ -33,7 +29,7 @@ public class BlockRewriter { map(Type.VAR_INT); // Block id - /!\ NOT BLOCK STATE handler(wrapper -> { int id = wrapper.get(Type.VAR_INT, 0); - int mappedId = blockRewriter.rewrite(id); + int mappedId = protocol.getMappingData().getNewBlockId(id); if (mappedId == -1) { // Block (action) has been removed wrapper.cancel(); @@ -52,7 +48,7 @@ public class BlockRewriter { public void registerMap() { map(positionType); map(Type.VAR_INT); - handler(wrapper -> wrapper.set(Type.VAR_INT, 0, blockStateRewriter.rewrite(wrapper.get(Type.VAR_INT, 0)))); + handler(wrapper -> wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(wrapper.get(Type.VAR_INT, 0)))); } }); } @@ -65,7 +61,7 @@ public class BlockRewriter { map(Type.INT); // 1 - Chunk Z handler(wrapper -> { for (BlockChangeRecord record : wrapper.passthrough(Type.BLOCK_CHANGE_RECORD_ARRAY)) { - record.setBlockId(blockStateRewriter.rewrite(record.getBlockId())); + record.setBlockId(protocol.getMappingData().getNewBlockStateId(record.getBlockId())); } }); } @@ -79,7 +75,7 @@ public class BlockRewriter { map(Type.LONG); // Chunk position handler(wrapper -> { for (BlockChangeRecord record : wrapper.passthrough(Type.VAR_LONG_BLOCK_CHANGE_RECORD_ARRAY)) { - record.setBlockId(blockStateRewriter.rewrite(record.getBlockId())); + record.setBlockId(protocol.getMappingData().getNewBlockStateId(record.getBlockId())); } }); } @@ -91,7 +87,7 @@ public class BlockRewriter { registerBlockChange(packetType); } - public void registerEffect(ClientboundPacketType packetType, int playRecordId, int blockBreakId, IdRewriteFunction itemIdRewriteFunction) { + public void registerEffect(ClientboundPacketType packetType, int playRecordId, int blockBreakId) { protocol.registerOutgoing(packetType, new PacketRemapper() { @Override public void registerMap() { @@ -102,9 +98,9 @@ public class BlockRewriter { int id = wrapper.get(Type.INT, 0); int data = wrapper.get(Type.INT, 1); if (id == playRecordId) { // Play record - wrapper.set(Type.INT, 1, itemIdRewriteFunction.rewrite(data)); + wrapper.set(Type.INT, 1, protocol.getMappingData().getNewItemId(data)); } else if (id == blockBreakId) { // Block break + block break sound - wrapper.set(Type.INT, 1, blockStateRewriter.rewrite(data)); + wrapper.set(Type.INT, 1, protocol.getMappingData().getNewBlockStateId(data)); } }); } @@ -136,7 +132,7 @@ public class BlockRewriter { if (id == -1) return; if (id == blockId || id == fallingDustId) { int data = wrapper.passthrough(Type.VAR_INT); - wrapper.set(Type.VAR_INT, 0, blockStateRewriter.rewrite(data)); + wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data)); } else if (id == itemId) { // Has to be like this, until we make *everything* object oriented inside of each protocol :( itemRewriteFunction.rewrite(wrapper.passthrough(itemType)); diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/ComponentRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/ComponentRewriter.java index 38b10c76e..7362f43b2 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/rewriters/ComponentRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/ComponentRewriter.java @@ -180,4 +180,8 @@ public class ComponentRewriter { processText(jsonElement); } } + + public T getProtocol() { + return (T) protocol; + } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/ItemRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/ItemRewriter.java index df7cfb31b..cc2634bcc 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/rewriters/ItemRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/ItemRewriter.java @@ -106,13 +106,13 @@ public class ItemRewriter { }); } - public void registerSetCooldown(ClientboundPacketType packetType, IdRewriteFunction itemIDRewriteFunction) { + public void registerSetCooldown(ClientboundPacketType packetType) { protocol.registerOutgoing(packetType, new PacketRemapper() { @Override public void registerMap() { handler(wrapper -> { int itemId = wrapper.read(Type.VAR_INT); - wrapper.write(Type.VAR_INT, itemIDRewriteFunction.rewrite(itemId)); + wrapper.write(Type.VAR_INT, protocol.getMappingData().getNewItemId(itemId)); }); } }); diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java index 0cb3bb732..46c603efe 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java @@ -20,7 +20,7 @@ import java.util.logging.Logger; public abstract class MetadataRewriter { private final Class entityTrackerClass; - private final Protocol protocol; + protected final Protocol protocol; private Int2IntMap typeMapping; protected MetadataRewriter(Protocol protocol, Class entityTrackerClass) { @@ -64,7 +64,7 @@ public abstract class MetadataRewriter { }); } - public void registerSpawnTrackerWithData(ClientboundPacketType packetType, EntityType fallingBlockType, IdRewriteFunction itemRewriter) { + public void registerSpawnTrackerWithData(ClientboundPacketType packetType, EntityType fallingBlockType) { protocol.registerOutgoing(packetType, new PacketRemapper() { @Override public void registerMap() { @@ -82,7 +82,7 @@ public abstract class MetadataRewriter { int entityId = wrapper.get(Type.VAR_INT, 0); EntityType entityType = wrapper.user().get(entityTrackerClass).getEntity(entityId); if (entityType == fallingBlockType) { - wrapper.set(Type.INT, 0, itemRewriter.rewrite(wrapper.get(Type.INT, 0))); + wrapper.set(Type.INT, 0, protocol.getMappingData().getNewItemId(wrapper.get(Type.INT, 0))); } }); } diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/SoundRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/SoundRewriter.java index 831bab655..768b53fcb 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/rewriters/SoundRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/SoundRewriter.java @@ -7,9 +7,13 @@ import us.myles.ViaVersion.api.type.Type; public class SoundRewriter { protected final Protocol protocol; - // Can't hold the mappings instance here since it's loaded later protected final IdRewriteFunction idRewriter; + public SoundRewriter(Protocol protocol) { + this.protocol = protocol; + this.idRewriter = id -> protocol.getMappingData().getSoundMappings().getNewId(id); + } + public SoundRewriter(Protocol protocol, IdRewriteFunction idRewriter) { this.protocol = protocol; this.idRewriter = idRewriter; diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/StatisticsRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/StatisticsRewriter.java index 2449a7a1f..06ac809bf 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/rewriters/StatisticsRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/StatisticsRewriter.java @@ -8,24 +8,12 @@ import us.myles.ViaVersion.api.type.Type; public class StatisticsRewriter { private final Protocol protocol; - private final IdRewriteFunction blockRewriter; - private final IdRewriteFunction itemRewriter; private final IdRewriteFunction entityRewriter; - private final IdRewriteFunction statisticsIdRewriter; private final int customStatsCategory = 8; // Make this changeable if it differs in a future version - public StatisticsRewriter(Protocol protocol, - @Nullable IdRewriteFunction blockRewriter, @Nullable IdRewriteFunction itemRewriter, @Nullable IdRewriteFunction entityRewriter, - @Nullable IdRewriteFunction statisticsIdRewriter) { + public StatisticsRewriter(Protocol protocol, @Nullable IdRewriteFunction entityRewriter) { this.protocol = protocol; - this.blockRewriter = blockRewriter; - this.itemRewriter = itemRewriter; this.entityRewriter = entityRewriter; - this.statisticsIdRewriter = statisticsIdRewriter; - } - - public StatisticsRewriter(Protocol protocol, @Nullable IdRewriteFunction blockRewriter, @Nullable IdRewriteFunction itemRewriter, @Nullable IdRewriteFunction entityRewriter) { - this(protocol, blockRewriter, itemRewriter, entityRewriter, null); } public void register(ClientboundPacketType packetType) { @@ -39,9 +27,9 @@ public class StatisticsRewriter { int categoryId = wrapper.read(Type.VAR_INT); int statisticId = wrapper.read(Type.VAR_INT); int value = wrapper.read(Type.VAR_INT); - if (categoryId == customStatsCategory && statisticsIdRewriter != null) { + if (categoryId == customStatsCategory && protocol.getMappingData().getStatisticsMappings() != null) { // Rewrite custom statistics id - statisticId = statisticsIdRewriter.rewrite(statisticId); + statisticId = protocol.getMappingData().getStatisticsMappings().getNewId(statisticId); if (statisticId == -1) { // Remove entry newSize--; @@ -73,9 +61,9 @@ public class StatisticsRewriter { protected IdRewriteFunction getRewriter(RegistryType type) { switch (type) { case BLOCK: - return blockRewriter; + return protocol.getMappingData().getBlockMappings() != null ? id -> protocol.getMappingData().getNewBlockId(id) : null; case ITEM: - return itemRewriter; + return protocol.getMappingData().getItemMappings() != null ? id -> protocol.getMappingData().getNewItemId(id) : null; case ENTITY: return entityRewriter; } diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagRewriter.java index 49afcefc9..c5933e5fb 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagRewriter.java @@ -2,7 +2,9 @@ package us.myles.ViaVersion.api.rewriters; import it.unimi.dsi.fastutil.ints.IntArrayList; import it.unimi.dsi.fastutil.ints.IntList; +import org.jetbrains.annotations.Nullable; import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.data.MappingData; import us.myles.ViaVersion.api.protocol.ClientboundPacketType; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketRemapper; @@ -14,18 +16,14 @@ import java.util.List; public class TagRewriter { private static final int[] EMPTY_ARRAY = {}; private final Protocol protocol; - private final IdRewriteFunction blockRewriter; - private final IdRewriteFunction itemRewriter; private final IdRewriteFunction entityRewriter; private final List newBlockTags = new ArrayList<>(); private final List newItemTags = new ArrayList<>(); private final List newEntityTags = new ArrayList<>(); // add fluid tag list if needed at some point - public TagRewriter(Protocol protocol, IdRewriteFunction blockRewriter, IdRewriteFunction itemRewriter, IdRewriteFunction entityRewriter) { + public TagRewriter(Protocol protocol, @Nullable IdRewriteFunction entityRewriter) { this.protocol = protocol; - this.blockRewriter = blockRewriter; - this.itemRewriter = itemRewriter; this.entityRewriter = entityRewriter; } @@ -58,8 +56,9 @@ public class TagRewriter { @Override public void registerMap() { handler(wrapper -> { - handle(wrapper, blockRewriter, newBlockTags); - handle(wrapper, itemRewriter, newItemTags); + MappingData mappingData = protocol.getMappingData(); + handle(wrapper, id -> mappingData != null ? mappingData.getNewBlockId(id) : null, newBlockTags); + handle(wrapper, id -> mappingData != null ? mappingData.getNewItemId(id) : null, newItemTags); if (entityRewriter == null && newEntityTags.isEmpty()) return; @@ -122,12 +121,13 @@ public class TagRewriter { } } + @Nullable private IdRewriteFunction getRewriter(RegistryType tagType) { switch (tagType) { case BLOCK: - return blockRewriter; + return protocol.getMappingData().getBlockMappings() != null ? id -> protocol.getMappingData().getNewBlockId(id) : null; case ITEM: - return itemRewriter; + return protocol.getMappingData().getItemMappings() != null ? id -> protocol.getMappingData().getNewItemId(id) : null; case ENTITY: return entityRewriter; case FLUID: diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java index 7aef4ebe4..d323f97b0 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java @@ -13,6 +13,7 @@ import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.ValueCreator; import us.myles.ViaVersion.api.remapper.ValueTransformer; +import us.myles.ViaVersion.api.rewriters.MetadataRewriter; import us.myles.ViaVersion.api.rewriters.SoundRewriter; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Types1_9; @@ -39,7 +40,7 @@ public class Protocol1_11To1_10 extends Protocol { + public static final MappingData MAPPINGS = new MappingData("1.13", "1.13.2", true); + public Protocol1_13_1To1_13() { super(ClientboundPackets1_13.class, ClientboundPackets1_13.class, ServerboundPackets1_13.class, ServerboundPackets1_13.class); } @Override protected void registerPackets() { - new MetadataRewriter1_13_1To1_13(this); + MetadataRewriter metadataRewriter = new MetadataRewriter1_13_1To1_13(this); EntityPackets.register(this); InventoryPackets.register(this); @@ -130,7 +134,7 @@ public class Protocol1_13_1To1_13 extends Protocol { + new StatisticsRewriter(this, id -> { int newId = id; if (newId > 22) { newId += 2; @@ -169,26 +173,8 @@ public class Protocol1_13_1To1_13 extends Protocol 8573) { - blockId += 17; - } else if (blockId > 8463) { - blockId += 16; - } else if (blockId > 8458) { - blockId = 8470 + (blockId - 8459) * 2; - } else if (blockId > 1126) { - blockId += 1; - } - - return blockId; - } - - public static int getNewBlockId(final int oldBlockId) { - int blockId = oldBlockId; - if (oldBlockId >= 561) { - blockId += 5; - } - return blockId; + @Override + public MappingData getMappingData() { + return MAPPINGS; } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/metadata/MetadataRewriter1_13_1To1_13.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/metadata/MetadataRewriter1_13_1To1_13.java index c8364df67..39523e86f 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/metadata/MetadataRewriter1_13_1To1_13.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/metadata/MetadataRewriter1_13_1To1_13.java @@ -27,7 +27,7 @@ public class MetadataRewriter1_13_1To1_13 extends MetadataRewriter { } else if (metadata.getMetaType() == MetaType1_13.BlockID) { // Convert to new block id int data = (int) metadata.getValue(); - metadata.setValue(Protocol1_13_1To1_13.getNewBlockStateId(data)); + metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); } if (type == null) return; @@ -35,7 +35,7 @@ public class MetadataRewriter1_13_1To1_13 extends MetadataRewriter { if (type.isOrHasParent(Entity1_13Types.EntityType.MINECART_ABSTRACT) && metadata.getId() == 9) { // New block format int data = (int) metadata.getValue(); - metadata.setValue(Protocol1_13_1To1_13.getNewBlockStateId(data)); + metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); } if (type.isOrHasParent(Entity1_13Types.EntityType.ABSTRACT_ARROW) && metadata.getId() >= 7) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/EntityPackets.java index 01282ba56..182a3d09e 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/EntityPackets.java @@ -40,7 +40,7 @@ public class EntityPackets { if (entType != null) { if (entType.is(Entity1_13Types.EntityType.FALLING_BLOCK)) { int data = wrapper.get(Type.INT, 0); - wrapper.set(Type.INT, 0, Protocol1_13_1To1_13.getNewBlockStateId(data)); + wrapper.set(Type.INT, 0, protocol.getMappingData().getNewBlockStateId(data)); } // Register Type ID wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/InventoryPackets.java index 7f730745c..a55f06c40 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/InventoryPackets.java @@ -2,26 +2,34 @@ package us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.minecraft.item.Item; -import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.rewriters.ItemRewriter; import us.myles.ViaVersion.api.rewriters.RecipeRewriter; import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.Protocol1_13_1To1_13; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2; public class InventoryPackets { - public static void register(Protocol protocol) { + public static void register(Protocol1_13_1To1_13 protocol) { ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer); - - itemRewriter.registerSetCooldown(ClientboundPackets1_13.COOLDOWN, InventoryPackets::getNewItemId); itemRewriter.registerSetSlot(ClientboundPackets1_13.SET_SLOT, Type.FLAT_ITEM); itemRewriter.registerWindowItems(ClientboundPackets1_13.WINDOW_ITEMS, Type.FLAT_ITEM_ARRAY); itemRewriter.registerAdvancements(ClientboundPackets1_13.ADVANCEMENTS, Type.FLAT_ITEM); + protocol.registerOutgoing(ClientboundPackets1_13.COOLDOWN, new PacketRemapper() { + @Override + public void registerMap() { + handler(wrapper -> { + int itemId = wrapper.read(Type.VAR_INT); + wrapper.write(Type.VAR_INT, protocol.getMappingData().getNewItemId(itemId)); + }); + } + }); + protocol.registerOutgoing(ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() { @Override public void registerMap() { @@ -80,25 +88,11 @@ public class InventoryPackets { public static void toClient(Item item) { if (item == null) return; - item.setIdentifier(getNewItemId(item.getIdentifier())); - } - - public static int getNewItemId(int itemId) { - if (itemId >= 443) { - return itemId + 5; - } - return itemId; + item.setIdentifier(Protocol1_13_1To1_13.MAPPINGS.getNewItemId(item.getIdentifier())); } public static void toServer(Item item) { if (item == null) return; - item.setIdentifier(getOldItemId(item.getIdentifier())); - } - - public static int getOldItemId(int newId) { - if (newId >= 448) { - return newId - 5; - } - return newId; + item.setIdentifier(Protocol1_13_1To1_13.MAPPINGS.getOldItemId(item.getIdentifier())); } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/WorldPackets.java index 13dd7ae20..746663d35 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/WorldPackets.java @@ -8,7 +8,6 @@ import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.rewriters.BlockRewriter; import us.myles.ViaVersion.api.type.Type; -import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.Protocol1_13_1To1_13; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; @@ -16,7 +15,7 @@ import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; public class WorldPackets { public static void register(Protocol protocol) { - BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION, Protocol1_13_1To1_13::getNewBlockStateId, Protocol1_13_1To1_13::getNewBlockId); + BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION); protocol.registerOutgoing(ClientboundPackets1_13.CHUNK_DATA, new PacketRemapper() { @Override @@ -30,7 +29,7 @@ public class WorldPackets { for (ChunkSection section : chunk.getSections()) { if (section == null) continue; for (int i = 0; i < section.getPaletteSize(); i++) { - section.setPaletteEntry(i, Protocol1_13_1To1_13.getNewBlockStateId(section.getPaletteEntry(i))); + section.setPaletteEntry(i, protocol.getMappingData().getNewBlockStateId(section.getPaletteEntry(i))); } } } @@ -41,7 +40,7 @@ public class WorldPackets { blockRewriter.registerBlockAction(ClientboundPackets1_13.BLOCK_ACTION); blockRewriter.registerBlockChange(ClientboundPackets1_13.BLOCK_CHANGE); blockRewriter.registerMultiBlockChange(ClientboundPackets1_13.MULTI_BLOCK_CHANGE); - blockRewriter.registerEffect(ClientboundPackets1_13.EFFECT, 1010, 2001, InventoryPackets::getNewItemId); + blockRewriter.registerEffect(ClientboundPackets1_13.EFFECT, 1010, 2001); protocol.registerOutgoing(ClientboundPackets1_13.JOIN_GAME, new PacketRemapper() { @Override 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 92e7f39ea..18e687517 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 @@ -17,6 +17,7 @@ import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.ValueCreator; import us.myles.ViaVersion.api.remapper.ValueTransformer; +import us.myles.ViaVersion.api.rewriters.MetadataRewriter; import us.myles.ViaVersion.api.rewriters.SoundRewriter; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; @@ -25,7 +26,11 @@ import us.myles.ViaVersion.protocols.protocol1_12_1to1_12.ServerboundPackets1_12 import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.ConnectionData; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.providers.BlockConnectionProvider; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.providers.PacketBlockConnectionProvider; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.*; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.BlockIdData; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.MappingData; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.RecipeData; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.StatisticData; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.StatisticMappings; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.metadata.MetadataRewriter1_13To1_12_2; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.EntityPackets; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.InventoryPackets; @@ -39,12 +44,18 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.TabCompleteTra import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; import us.myles.ViaVersion.util.GsonUtil; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; public class Protocol1_13To1_12_2 extends Protocol { + public static final MappingData MAPPINGS = new MappingData(); + public Protocol1_13To1_12_2() { - super(ClientboundPackets1_12_1.class, ClientboundPackets1_13.class, ServerboundPackets1_12_1.class, ServerboundPackets1_13.class, true); + super(ClientboundPackets1_12_1.class, ClientboundPackets1_13.class, ServerboundPackets1_12_1.class, ServerboundPackets1_13.class); } public static final PacketHandler POS_TO_3_INT = wrapper -> { @@ -54,58 +65,55 @@ public class Protocol1_13To1_12_2 extends Protocol { + // Send fake declare commands + w.create(0x11, new ValueCreator() { + @Override + public void write(PacketWrapper wrapper) { + wrapper.write(Type.VAR_INT, 2); // Size + // Write root node + wrapper.write(Type.VAR_INT, 0); // Mark as command + wrapper.write(Type.VAR_INT, 1); // 1 child + wrapper.write(Type.VAR_INT, 1); // Child is at 1 - // Write arg node - wrapper.write(Type.VAR_INT, 0x02 | 0x04 | 0x10); // Mark as command - wrapper.write(Type.VAR_INT, 0); // No children - // Extra data - wrapper.write(Type.STRING, "args"); // Arg name - wrapper.write(Type.STRING, "brigadier:string"); - wrapper.write(Type.VAR_INT, 2); // Greedy - wrapper.write(Type.STRING, "minecraft:ask_server"); // Ask server + // Write arg node + wrapper.write(Type.VAR_INT, 0x02 | 0x04 | 0x10); // Mark as command + wrapper.write(Type.VAR_INT, 0); // No children + // Extra data + wrapper.write(Type.STRING, "args"); // Arg name + wrapper.write(Type.STRING, "brigadier:string"); + wrapper.write(Type.VAR_INT, 2); // Greedy + wrapper.write(Type.STRING, "minecraft:ask_server"); // Ask server - wrapper.write(Type.VAR_INT, 0); // Root node index + wrapper.write(Type.VAR_INT, 0); // Root node index + } + }).send(Protocol1_13To1_12_2.class); + + // Send tags packet + w.create(0x55, new ValueCreator() { + @Override + public void write(PacketWrapper wrapper) throws Exception { + wrapper.write(Type.VAR_INT, MAPPINGS.getBlockTags().size()); // block tags + for (Map.Entry tag : MAPPINGS.getBlockTags().entrySet()) { + wrapper.write(Type.STRING, tag.getKey()); + // Needs copy as other protocols may modify it + wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, toPrimitive(tag.getValue())); } - }).send(Protocol1_13To1_12_2.class); - - // Send tags packet - w.create(0x55, new ValueCreator() { - @Override - public void write(PacketWrapper wrapper) throws Exception { - wrapper.write(Type.VAR_INT, MappingData.blockTags.size()); // block tags - for (Map.Entry tag : MappingData.blockTags.entrySet()) { - wrapper.write(Type.STRING, tag.getKey()); - // Needs copy as other protocols may modify it - wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, toPrimitive(tag.getValue())); - } - wrapper.write(Type.VAR_INT, MappingData.itemTags.size()); // item tags - for (Map.Entry tag : MappingData.itemTags.entrySet()) { - wrapper.write(Type.STRING, tag.getKey()); - // Needs copy as other protocols may modify it - wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, toPrimitive(tag.getValue())); - } - wrapper.write(Type.VAR_INT, MappingData.fluidTags.size()); // fluid tags - for (Map.Entry tag : MappingData.fluidTags.entrySet()) { - wrapper.write(Type.STRING, tag.getKey()); - // Needs copy as other protocols may modify it - wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, toPrimitive(tag.getValue())); - } + wrapper.write(Type.VAR_INT, MAPPINGS.getItemTags().size()); // item tags + for (Map.Entry tag : MAPPINGS.getItemTags().entrySet()) { + wrapper.write(Type.STRING, tag.getKey()); + // Needs copy as other protocols may modify it + wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, toPrimitive(tag.getValue())); } - }).send(Protocol1_13To1_12_2.class); - } + wrapper.write(Type.VAR_INT, MAPPINGS.getFluidTags().size()); // fluid tags + for (Map.Entry tag : MAPPINGS.getFluidTags().entrySet()) { + wrapper.write(Type.STRING, tag.getKey()); + // Needs copy as other protocols may modify it + wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, toPrimitive(tag.getValue())); + } + } + }).send(Protocol1_13To1_12_2.class); }; // These are arbitrary rewrite values, it just needs an invalid color code character. @@ -140,7 +148,7 @@ public class Protocol1_13To1_12_2 extends Protocol> 12; @@ -679,7 +687,7 @@ public class Protocol1_13To1_12_2 extends Protocol MappingData.soundMappings.getNewId(id)).registerSound(ClientboundPackets1_12_1.SOUND); + new SoundRewriter(this).registerSound(ClientboundPackets1_12_1.SOUND); registerOutgoing(ClientboundPackets1_12_1.TAB_LIST, new PacketRemapper() { @Override @@ -1015,8 +1023,7 @@ public class Protocol1_13To1_12_2 extends Protocol blockState : blocks1_13.entrySet()) { int id = Integer.parseInt(blockState.getKey()); String key = blockState.getValue().getAsString(); 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 aa3d5351e..89f70eabe 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 @@ -2,13 +2,13 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.prov import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.platform.providers.Provider; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.MappingData; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; public class BlockConnectionProvider implements Provider { public int getBlockData(UserConnection connection, int x, int y, int z) { int oldId = getWorldBlockData(connection, x, y, z); - return MappingData.blockMappings.getNewId(oldId); + return Protocol1_13To1_12_2.MAPPINGS.getBlockMappings().getNewId(oldId); } public int getWorldBlockData(UserConnection connection, int x, int y, int z) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/data/ComponentRewriter1_13.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/data/ComponentRewriter1_13.java index 7ce412e99..8f5fb41ea 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/data/ComponentRewriter1_13.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/data/ComponentRewriter1_13.java @@ -11,6 +11,7 @@ import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.nbt.BinaryTagIO; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.rewriters.ComponentRewriter; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.InventoryPackets; import java.io.IOException; @@ -103,9 +104,10 @@ public class ComponentRewriter1_13 extends ComponentRewriter { protected void handleTranslate(JsonObject object, String translate) { super.handleTranslate(object, translate); String newTranslate; - newTranslate = MappingData.translateMapping.get(translate); + Protocol1_13To1_12_2 protocol = getProtocol(); + newTranslate = protocol.getMappingData().getTranslateMapping().get(translate); if (newTranslate == null) { - newTranslate = MappingData.mojangTranslation.get(translate); + newTranslate = protocol.getMappingData().getMojangTranslation().get(translate); } if (newTranslate != null) { object.addProperty("translate", newTranslate); 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 7c7848a8a..97fa88eb3 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 @@ -7,11 +7,11 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; +import org.jetbrains.annotations.Nullable; 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 us.myles.ViaVersion.util.Int2IntBiMap; import java.io.IOException; import java.io.InputStreamReader; @@ -20,34 +20,44 @@ import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; -public class MappingData { - public static final Int2IntBiMap oldToNewItems = new Int2IntBiMap(); - public static final Map blockTags = new HashMap<>(); - public static final Map itemTags = new HashMap<>(); - public static final Map fluidTags = new HashMap<>(); - public static final BiMap oldEnchantmentsIds = HashBiMap.create(); - public static final Map translateMapping = new HashMap<>(); - public static final Map mojangTranslation = new HashMap<>(); - public static final BiMap channelMappings = HashBiMap.create(); // 1.12->1.13 - public static Mappings enchantmentMappings; - public static Mappings soundMappings; - public static Mappings blockMappings; +public class MappingData extends us.myles.ViaVersion.api.data.MappingData { + private final Map blockTags = new HashMap<>(); + private final Map itemTags = new HashMap<>(); + private final Map fluidTags = new HashMap<>(); + private final BiMap oldEnchantmentsIds = HashBiMap.create(); + private final Map translateMapping = new HashMap<>(); + private final Map mojangTranslation = new HashMap<>(); + private final BiMap channelMappings = HashBiMap.create(); // 1.12->1.13 + private Mappings enchantmentMappings; - public static void init() { - Via.getPlatform().getLogger().info("Loading 1.12.2 -> 1.13 mappings..."); - JsonObject mapping1_12 = MappingDataLoader.loadData("mapping-1.12.json", true); - JsonObject mapping1_13 = MappingDataLoader.loadData("mapping-1.13.json", true); + public MappingData() { + super("1.12", "1.13"); + } - oldToNewItems.defaultReturnValue(-1); - blockMappings = new BlockMappingsShortArray(mapping1_12.getAsJsonObject("blocks"), mapping1_13.getAsJsonObject("blocks")); - MappingDataLoader.mapIdentifiers(oldToNewItems, mapping1_12.getAsJsonObject("items"), mapping1_13.getAsJsonObject("items")); - loadTags(blockTags, mapping1_13.getAsJsonObject("block_tags")); - loadTags(itemTags, mapping1_13.getAsJsonObject("item_tags")); - loadTags(fluidTags, mapping1_13.getAsJsonObject("fluid_tags")); + @Override + public void loadExtras(JsonObject oldMappings, JsonObject newMappings, JsonObject diffMappings) { + loadTags(blockTags, newMappings.getAsJsonObject("block_tags")); + loadTags(itemTags, newMappings.getAsJsonObject("item_tags")); + loadTags(fluidTags, newMappings.getAsJsonObject("fluid_tags")); - loadEnchantments(oldEnchantmentsIds, mapping1_12.getAsJsonObject("enchantments")); - enchantmentMappings = new Mappings(72, mapping1_12.getAsJsonObject("enchantments"), mapping1_13.getAsJsonObject("enchantments")); - soundMappings = new Mappings(662, mapping1_12.getAsJsonArray("sounds"), mapping1_13.getAsJsonArray("sounds")); + loadEnchantments(oldEnchantmentsIds, oldMappings.getAsJsonObject("enchantments")); + enchantmentMappings = new Mappings(72, oldMappings.getAsJsonObject("enchantments"), newMappings.getAsJsonObject("enchantments")); + + // Map minecraft:snow[layers=1] of 1.12 to minecraft:snow[layers=2] in 1.13 + if (Via.getConfig().isSnowCollisionFix()) { + blockMappings.getOldToNew()[1248] = 3416; + } + + // Remap infested blocks, as they are instantly breakabale in 1.13+ and can't be broken by those clients on older servers + if (Via.getConfig().isInfestedBlocksFix()) { + short[] oldToNew = blockMappings.getOldToNew(); + oldToNew[1552] = 1; // stone + oldToNew[1553] = 14; // cobblestone + oldToNew[1554] = 3983; // stone bricks + oldToNew[1555] = 3984; // mossy stone bricks + oldToNew[1556] = 3985; // cracked stone bricks + oldToNew[1557] = 3986; // chiseled stone bricks + } JsonObject object = MappingDataLoader.loadFromDataDir("channelmappings-1.13.json"); if (object != null) { @@ -94,6 +104,16 @@ public class MappingData { } } + @Override + protected Mappings loadFromObject(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings, String key) { + if (key.equals("blocks")) { + // Need to use a custom size since there are larger gaps in ids + return new Mappings(4084, oldMappings.getAsJsonObject("blocks"), newMappings.getAsJsonObject("blockstates")); + } else { + return super.loadFromObject(oldMappings, newMappings, diffMappings, key); + } + } + public static String validateNewChannel(String newId) { if (!isValid1_13Channel(newId)) { return null; // Not valid @@ -110,7 +130,7 @@ public class MappingData { return channelId.matches("([0-9a-z_.-]+):([0-9a-z_/.-]+)"); } - private static void loadTags(Map output, JsonObject newTags) { + private void loadTags(Map output, JsonObject newTags) { for (Map.Entry entry : newTags.entrySet()) { JsonArray ids = entry.getValue().getAsJsonArray(); Integer[] idsArray = new Integer[ids.size()]; @@ -121,31 +141,41 @@ public class MappingData { } } - private static void loadEnchantments(Map output, JsonObject enchantments) { + private void loadEnchantments(Map output, JsonObject enchantments) { for (Map.Entry enchantment : enchantments.entrySet()) { output.put(Short.parseShort(enchantment.getKey()), enchantment.getValue().getAsString()); } } - private static class BlockMappingsShortArray extends Mappings { + public Map getBlockTags() { + return blockTags; + } - private BlockMappingsShortArray(JsonObject mapping1_12, JsonObject mapping1_13) { - super(4084, mapping1_12, mapping1_13); + public Map getItemTags() { + return itemTags; + } - // Map minecraft:snow[layers=1] of 1.12 to minecraft:snow[layers=2] in 1.13 - if (Via.getConfig().isSnowCollisionFix()) { - oldToNew[1248] = 3416; - } + public Map getFluidTags() { + return fluidTags; + } - // Remap infested blocks, as they are instantly breakabale in 1.13+ and can't be broken by those clients on older servers - if (Via.getConfig().isInfestedBlocksFix()) { - oldToNew[1552] = 1; // stone - oldToNew[1553] = 14; // cobblestone - oldToNew[1554] = 3983; // stone bricks - oldToNew[1555] = 3984; // mossy stone bricks - oldToNew[1556] = 3985; // cracked stone bricks - oldToNew[1557] = 3986; // chiseled stone bricks - } - } + public BiMap getOldEnchantmentsIds() { + return oldEnchantmentsIds; + } + + public Map getTranslateMapping() { + return translateMapping; + } + + public Map getMojangTranslation() { + return mojangTranslation; + } + + public BiMap getChannelMappings() { + return channelMappings; + } + + public Mappings getEnchantmentMappings() { + return enchantmentMappings; } } 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 7ad4d6998..a9bd3eec0 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 @@ -12,7 +12,6 @@ import com.google.common.primitives.Ints; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.minecraft.item.Item; -import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.rewriters.ItemRewriter; @@ -35,7 +34,7 @@ import java.util.Optional; public class InventoryPackets { private static final String NBT_TAG_NAME = "ViaVersion|" + Protocol1_13To1_12_2.class.getSimpleName(); - public static void register(Protocol protocol) { + public static void register(Protocol1_13To1_12_2 protocol) { ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer); protocol.registerOutgoing(ClientboundPackets1_12_1.SET_SLOT, new PacketRemapper() { @@ -69,7 +68,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.getNewId(wrapper.get(Type.SHORT, 1))); + wrapper.set(Type.SHORT, 1, (short) protocol.getMappingData().getEnchantmentMappings().getNewId(wrapper.get(Type.SHORT, 1))); } } }); @@ -315,7 +314,7 @@ public class InventoryPackets { if (enchEntry instanceof CompoundTag) { CompoundTag enchantmentEntry = new CompoundTag(""); short oldId = ((Number) ((CompoundTag) enchEntry).get("id").getValue()).shortValue(); - String newId = MappingData.oldEnchantmentsIds.get(oldId); + String newId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().get(oldId); if (newId == null) { newId = "viaversion:legacy/" + oldId; } @@ -334,7 +333,7 @@ public class InventoryPackets { if (enchEntry instanceof CompoundTag) { CompoundTag enchantmentEntry = new CompoundTag(""); short oldId = ((Number) ((CompoundTag) enchEntry).get("id").getValue()).shortValue(); - String newId = MappingData.oldEnchantmentsIds.get(oldId); + String newId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().get(oldId); if (newId == null) { newId = "viaversion:legacy/" + oldId; } @@ -420,14 +419,14 @@ public class InventoryPackets { } } - if (!MappingData.oldToNewItems.containsKey(rawId)) { + if (!Protocol1_13To1_12_2.MAPPINGS.getItemMappings().containsKey(rawId)) { if (!isDamageable(item.getIdentifier()) && item.getIdentifier() != 358) { // Map if (tag == null) item.setTag(tag = new CompoundTag("tag")); tag.put(new IntTag(NBT_TAG_NAME, originalId)); // Data will be lost, saving original id } if (item.getIdentifier() == 31 && item.getData() == 0) { // Shrub was removed rawId = 32 << 4; // Dead Bush - } else if (MappingData.oldToNewItems.containsKey(rawId & ~0xF)) { + } else if (Protocol1_13To1_12_2.MAPPINGS.getItemMappings().containsKey(rawId & ~0xF)) { rawId &= ~0xF; // Remove data } else { if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { @@ -437,7 +436,7 @@ public class InventoryPackets { } } - item.setIdentifier(MappingData.oldToNewItems.get(rawId)); + item.setIdentifier(Protocol1_13To1_12_2.MAPPINGS.getItemMappings().get(rawId)); item.setData((short) 0); } @@ -463,7 +462,7 @@ public class InventoryPackets { case "bungeecord:main": return null; default: - String mappedChannel = MappingData.channelMappings.get(old); + String mappedChannel = Protocol1_13To1_12_2.MAPPINGS.getChannelMappings().get(old); if (mappedChannel != null) return mappedChannel; return MappingData.isValid1_13Channel(old) ? old : null; } @@ -489,7 +488,7 @@ public class InventoryPackets { } if (rawId == null) { - int oldId = MappingData.oldToNewItems.inverse().get(item.getIdentifier()); + int oldId = Protocol1_13To1_12_2.MAPPINGS.getItemMappings().inverse().get(item.getIdentifier()); if (oldId != -1) { // Handle spawn eggs Optional eggEntityId = SpawnEggRewriter.getEntityId(oldId); @@ -576,7 +575,7 @@ public class InventoryPackets { if (enchantmentEntry instanceof CompoundTag) { CompoundTag enchEntry = new CompoundTag(""); String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue(); - Short oldId = MappingData.oldEnchantmentsIds.inverse().get(newId); + Short oldId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().inverse().get(newId); if (oldId == null && newId.startsWith("viaversion:legacy/")) { oldId = Short.valueOf(newId.substring(18)); } @@ -602,7 +601,7 @@ public class InventoryPackets { if (enchantmentEntry instanceof CompoundTag) { CompoundTag enchEntry = new CompoundTag(""); String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue(); - Short oldId = MappingData.oldEnchantmentsIds.inverse().get(newId); + Short oldId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().inverse().get(newId); if (oldId == null && newId.startsWith("viaversion:legacy/")) { oldId = Short.valueOf(newId.substring(18)); } @@ -695,7 +694,7 @@ public class InventoryPackets { case "bungeecord:main": return "BungeeCord"; default: - String mappedChannel = MappingData.channelMappings.inverse().get(newId); + String mappedChannel = Protocol1_13To1_12_2.MAPPINGS.getChannelMappings().inverse().get(newId); if (mappedChannel != null) return mappedChannel; return newId.length() > 20 ? newId.substring(0, 20) : newId; } 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 8de58d31f..30943fe06 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 @@ -19,7 +19,6 @@ import us.myles.ViaVersion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12 import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.ConnectionData; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.ConnectionHandler; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.NamedSoundRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.ParticleRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider; @@ -535,11 +534,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.getNewId(oldId); + int newId = Protocol1_13To1_12_2.MAPPINGS.getBlockMappings().getNewId(oldId); if (newId != -1) { return newId; } - newId = MappingData.blockMappings.getNewId(oldId & ~0xF); // Remove data + newId = Protocol1_13To1_12_2.MAPPINGS.getBlockMappings().getNewId(oldId & ~0xF); // Remove data if (newId != -1) { if (!Via.getConfig().isSuppressConversionWarnings() || 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 5b3c90eff..22eeba912 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 @@ -6,7 +6,7 @@ import us.myles.ViaVersion.api.data.StoredObject; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.minecraft.Position; import us.myles.ViaVersion.api.minecraft.chunks.NibbleArray; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.MappingData; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.WorldPackets; import java.lang.reflect.Constructor; @@ -30,7 +30,7 @@ public class BlockConnectionStorage extends StoredObject { Arrays.fill(REVERSE_BLOCK_MAPPINGS, (short) -1); for (int i = 0; i < 4096; i++) { - int newBlock = MappingData.blockMappings.getNewId(i); + int newBlock = Protocol1_13To1_12_2.MAPPINGS.getBlockMappings().getNewId(i); if (newBlock != -1) { REVERSE_BLOCK_MAPPINGS[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 1007b427d..20886fcb1 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 @@ -2,6 +2,7 @@ package us.myles.ViaVersion.protocols.protocol1_14_1to1_14; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; +import us.myles.ViaVersion.api.rewriters.MetadataRewriter; import us.myles.ViaVersion.protocols.protocol1_14_1to1_14.metadata.MetadataRewriter1_14_1To1_14; import us.myles.ViaVersion.protocols.protocol1_14_1to1_14.packets.EntityPackets; import us.myles.ViaVersion.protocols.protocol1_14_1to1_14.storage.EntityTracker1_14_1; @@ -16,7 +17,7 @@ public class Protocol1_14_1To1_14 extends Protocol { + public static final MappingData MAPPINGS = new MappingData(); + public Protocol1_14To1_13_2() { - super(ClientboundPackets1_13.class, ClientboundPackets1_14.class, ServerboundPackets1_13.class, ServerboundPackets1_14.class, true); + super(ClientboundPackets1_13.class, ClientboundPackets1_14.class, ServerboundPackets1_13.class, ServerboundPackets1_14.class); } @Override protected void registerPackets() { - MetadataRewriter1_14To1_13_2 metadataRewriter = new MetadataRewriter1_14To1_13_2(this); + MetadataRewriter metadataRewriter = new MetadataRewriter1_14To1_13_2(this); InventoryPackets.register(this); EntityPackets.register(this); WorldPackets.register(this); PlayerPackets.register(this); - new SoundRewriter(this, id -> MappingData.soundMappings.getNewId(id)).registerSound(ClientboundPackets1_13.SOUND); - new StatisticsRewriter(this, Protocol1_14To1_13_2::getNewBlockId, - InventoryPackets::getNewItemId, metadataRewriter::getNewEntityId, id -> MappingData.statisticsMappings.getNewId(id)).register(ClientboundPackets1_13.STATISTICS); + new SoundRewriter(this).registerSound(ClientboundPackets1_13.SOUND); + new StatisticsRewriter(this, metadataRewriter::getNewEntityId).register(ClientboundPackets1_13.STATISTICS); ComponentRewriter componentRewriter = new ComponentRewriter1_14(this); componentRewriter.registerChatMessage(ClientboundPackets1_13.CHAT_MESSAGE); @@ -56,21 +57,21 @@ public class Protocol1_14To1_13_2 extends Protocol 1.14 mappings..."); - JsonObject mapping1_13_2 = MappingDataLoader.loadData("mapping-1.13.2.json", true); - JsonObject mapping1_14 = MappingDataLoader.loadData("mapping-1.14.json", true); + public MappingData() { + super("1.13.2", "1.14"); + } - oldToNewItems.defaultReturnValue(-1); - blockStateMappings = new Mappings(mapping1_13_2.getAsJsonObject("blockstates"), mapping1_14.getAsJsonObject("blockstates")); - blockMappings = new Mappings(mapping1_13_2.getAsJsonObject("blocks"), mapping1_14.getAsJsonObject("blocks")); - MappingDataLoader.mapIdentifiers(oldToNewItems, mapping1_13_2.getAsJsonObject("items"), mapping1_14.getAsJsonObject("items")); - soundMappings = new Mappings(mapping1_13_2.getAsJsonArray("sounds"), mapping1_14.getAsJsonArray("sounds")); - statisticsMappings = new Mappings(mapping1_13_2.getAsJsonArray("statistics"), mapping1_14.getAsJsonArray("statistics")); - - JsonObject blockStates = mapping1_14.getAsJsonObject("blockstates"); + @Override + public void loadExtras(JsonObject oldMappings, JsonObject newMappings, JsonObject diffMappings) { + JsonObject blockStates = newMappings.getAsJsonObject("blockstates"); Map blockStateMap = new HashMap<>(blockStates.entrySet().size()); for (Map.Entry entry : blockStates.entrySet()) { blockStateMap.put(entry.getValue().getAsString(), Integer.parseInt(entry.getKey())); @@ -42,23 +29,24 @@ public class MappingData { JsonObject heightMapData = MappingDataLoader.loadData("heightMapData-1.14.json"); JsonArray motionBlocking = heightMapData.getAsJsonArray("MOTION_BLOCKING"); - MappingData.motionBlocking = new IntOpenHashSet(motionBlocking.size(), 1F); + this.motionBlocking = new IntOpenHashSet(motionBlocking.size(), 1F); 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.intValue()); + this.motionBlocking.add(id.intValue()); } } if (Via.getConfig().isNonFullBlockLightFix()) { nonFullBlocks = new IntOpenHashSet(1611, 1F); - for (Map.Entry blockstates : mapping1_13_2.getAsJsonObject("blockstates").entrySet()) { + for (Map.Entry blockstates : oldMappings.getAsJsonObject("blockstates").entrySet()) { final String state = blockstates.getValue().getAsString(); - if (state.contains("_slab") || state.contains("_stairs") || state.contains("_wall[")) + if (state.contains("_slab") || state.contains("_stairs") || state.contains("_wall[")) { nonFullBlocks.add(blockStateMappings.getNewId(Integer.parseInt(blockstates.getKey()))); + } } nonFullBlocks.add(blockStateMappings.getNewId(8163)); // grass path for (int i = 3060; i <= 3067; i++) { // farmland @@ -66,4 +54,12 @@ public class MappingData { } } } + + public IntSet getMotionBlocking() { + return motionBlocking; + } + + public IntSet getNonFullBlocks() { + return nonFullBlocks; + } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/metadata/MetadataRewriter1_14To1_13_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/metadata/MetadataRewriter1_14To1_13_2.java index 3be171d24..3a9491147 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/metadata/MetadataRewriter1_14To1_13_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/metadata/MetadataRewriter1_14To1_13_2.java @@ -38,7 +38,7 @@ public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter { } else if (metadata.getMetaType() == MetaType1_14.BlockID) { // Convert to new block id int data = (int) metadata.getValue(); - metadata.setValue(Protocol1_14To1_13_2.getNewBlockStateId(data)); + metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); } if (type == null) return; @@ -95,7 +95,7 @@ public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter { if (metadata.getId() == 10) { // New block format int data = (int) metadata.getValue(); - metadata.setValue(Protocol1_14To1_13_2.getNewBlockStateId(data)); + metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); } } else if (type.is(Entity1_14Types.EntityType.HORSE)) { if (metadata.getId() == 18) { @@ -104,11 +104,11 @@ public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter { int armorType = (int) metadata.getValue(); Item armorItem = null; if (armorType == 1) { //iron armor - armorItem = new Item(InventoryPackets.getNewItemId(727), (byte) 1, (short) 0, null); + armorItem = new Item(protocol.getMappingData().getNewItemId(727), (byte) 1, (short) 0, null); } else if (armorType == 2) { //gold armor - armorItem = new Item(InventoryPackets.getNewItemId(728), (byte) 1, (short) 0, null); + armorItem = new Item(protocol.getMappingData().getNewItemId(728), (byte) 1, (short) 0, null); } else if (armorType == 3) { //diamond armor - armorItem = new Item(InventoryPackets.getNewItemId(729), (byte) 1, (short) 0, null); + armorItem = new Item(protocol.getMappingData().getNewItemId(729), (byte) 1, (short) 0, null); } PacketWrapper equipmentPacket = new PacketWrapper(0x46, null, connection); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java index e81c7e5f8..487c0940a 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java @@ -55,7 +55,7 @@ public class EntityPackets { if (type1_14 != null) { int data = wrapper.get(Type.INT, 0); if (type1_14.is(Entity1_14Types.EntityType.FALLING_BLOCK)) { - wrapper.set(Type.INT, 0, Protocol1_14To1_13_2.getNewBlockStateId(data)); + wrapper.set(Type.INT, 0, protocol.getMappingData().getNewBlockStateId(data)); } else if (type1_14.is(Entity1_14Types.EntityType.MINECART)) { // default is 0 = rideable minecart switch (data) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java index a55b548ac..aec307a4c 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java @@ -47,7 +47,7 @@ public class InventoryPackets { public static void register(Protocol protocol) { ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer); - itemRewriter.registerSetCooldown(ClientboundPackets1_13.COOLDOWN, InventoryPackets::getNewItemId); + itemRewriter.registerSetCooldown(ClientboundPackets1_13.COOLDOWN); itemRewriter.registerAdvancements(ClientboundPackets1_13.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM); protocol.registerOutgoing(ClientboundPackets1_13.OPEN_WINDOW, null, new PacketRemapper() { @@ -234,7 +234,7 @@ public class InventoryPackets { public static void toClient(Item item) { if (item == null) return; - item.setIdentifier(getNewItemId(item.getIdentifier())); + item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getNewItemId(item.getIdentifier())); CompoundTag tag; if ((tag = item.getTag()) != null) { @@ -256,18 +256,9 @@ public class InventoryPackets { } } - public static int getNewItemId(int id) { - int newId = MappingData.oldToNewItems.get(id); - if (newId == -1) { - Via.getPlatform().getLogger().warning("Missing 1.14 item for 1.13.2 item " + id); - return 1; - } - return newId; - } - public static void toServer(Item item) { if (item == null) return; - item.setIdentifier(getOldItemId(item.getIdentifier())); + item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getOldItemId(item.getIdentifier())); CompoundTag tag; if ((tag = item.getTag()) != null) { @@ -297,9 +288,4 @@ public class InventoryPackets { } } } - - public static int getOldItemId(int id) { - int oldId = MappingData.oldToNewItems.inverse().get(id); - return oldId != -1 ? oldId : 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 2e745a704..bc6cbeb51 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 @@ -9,7 +9,6 @@ import us.myles.ViaVersion.api.minecraft.BlockFace; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; import us.myles.ViaVersion.api.minecraft.chunks.NibbleArray; -import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.ValueCreator; @@ -18,7 +17,6 @@ import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; -import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.metadata.MetadataRewriter1_14To1_13_2; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type; @@ -38,8 +36,8 @@ public class WorldPackets { Arrays.fill(FULL_LIGHT, (byte) 0xff); } - public static void register(final Protocol protocol) { - BlockRewriter blockRewriter = new BlockRewriter(protocol, null, Protocol1_14To1_13_2::getNewBlockStateId, Protocol1_14To1_13_2::getNewBlockId); + public static void register(Protocol1_14To1_13_2 protocol) { + BlockRewriter blockRewriter = new BlockRewriter(protocol, null); protocol.registerOutgoing(ClientboundPackets1_13.BLOCK_BREAK_ANIMATION, new PacketRemapper() { @Override @@ -65,7 +63,7 @@ public class WorldPackets { handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { - wrapper.set(Type.VAR_INT, 0, Protocol1_14To1_13_2.getNewBlockId(wrapper.get(Type.VAR_INT, 0))); + wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockId(wrapper.get(Type.VAR_INT, 0))); } }); } @@ -80,7 +78,7 @@ public class WorldPackets { public void handle(PacketWrapper wrapper) throws Exception { int id = wrapper.get(Type.VAR_INT, 0); - wrapper.set(Type.VAR_INT, 0, Protocol1_14To1_13_2.getNewBlockStateId(id)); + wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(id)); } }); } @@ -144,7 +142,7 @@ public class WorldPackets { boolean hasBlock = false; for (int i = 0; i < section.getPaletteSize(); i++) { int old = section.getPaletteEntry(i); - int newId = Protocol1_14To1_13_2.getNewBlockStateId(old); + int newId = protocol.getMappingData().getNewBlockStateId(old); if (!hasBlock && newId != air && newId != voidAir && newId != caveAir) { // air, void_air, cave_air hasBlock = true; } @@ -164,12 +162,12 @@ public class WorldPackets { nonAirBlockCount++; worldSurface[x + z * 16] = y + s * 16 + 1; // +1 (top of the block) } - if (MappingData.motionBlocking.contains(id)) { + if (protocol.getMappingData().getMotionBlocking().contains(id)) { motionBlocking[x + z * 16] = y + s * 16 + 1; // +1 (top of the block) } // Manually update light for non full blocks (block light must not be sent) - if (Via.getConfig().isNonFullBlockLightFix() && MappingData.nonFullBlocks.contains(id)) { + if (Via.getConfig().isNonFullBlockLightFix() && protocol.getMappingData().getNonFullBlocks().contains(id)) { setNonFullLight(chunk, section, s, x, y, z); } } @@ -265,9 +263,9 @@ public class WorldPackets { int id = wrapper.get(Type.INT, 0); int data = wrapper.get(Type.INT, 1); if (id == 1010) { // Play record - wrapper.set(Type.INT, 1, InventoryPackets.getNewItemId(data)); + wrapper.set(Type.INT, 1, protocol.getMappingData().getNewItemId(data)); } else if (id == 2001) { // Block break + block break sound - wrapper.set(Type.INT, 1, Protocol1_14To1_13_2.getNewBlockStateId(data)); + wrapper.set(Type.INT, 1, protocol.getMappingData().getNewBlockStateId(data)); } } }); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java index 3163e6180..293357869 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java @@ -1,9 +1,9 @@ package us.myles.ViaVersion.protocols.protocol1_15to1_14_4; -import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.rewriters.MetadataRewriter; import us.myles.ViaVersion.api.rewriters.RegistryType; import us.myles.ViaVersion.api.rewriters.SoundRewriter; import us.myles.ViaVersion.api.rewriters.StatisticsRewriter; @@ -21,27 +21,27 @@ import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.storage.EntityTracker1 public class Protocol1_15To1_14_4 extends Protocol { + public static final MappingData MAPPINGS = new MappingData(); private TagRewriter tagRewriter; public Protocol1_15To1_14_4() { - super(ClientboundPackets1_14.class, ClientboundPackets1_15.class, ServerboundPackets1_14.class, ServerboundPackets1_14.class, true); + super(ClientboundPackets1_14.class, ClientboundPackets1_15.class, ServerboundPackets1_14.class, ServerboundPackets1_14.class); } @Override protected void registerPackets() { - MetadataRewriter1_15To1_14_4 metadataRewriter = new MetadataRewriter1_15To1_14_4(this); + MetadataRewriter metadataRewriter = new MetadataRewriter1_15To1_14_4(this); EntityPackets.register(this); PlayerPackets.register(this); WorldPackets.register(this); InventoryPackets.register(this); - SoundRewriter soundRewriter = new SoundRewriter(this, id -> MappingData.soundMappings.getNewId(id)); + SoundRewriter soundRewriter = new SoundRewriter(this); soundRewriter.registerSound(ClientboundPackets1_14.ENTITY_SOUND); // Entity Sound Effect (added somewhere in 1.14) soundRewriter.registerSound(ClientboundPackets1_14.SOUND); - new StatisticsRewriter(this, Protocol1_15To1_14_4::getNewBlockId, InventoryPackets::getNewItemId, - metadataRewriter::getNewEntityId, id -> MappingData.statisticsMappings.getNewId(id)).register(ClientboundPackets1_14.STATISTICS); + new StatisticsRewriter(this, metadataRewriter::getNewEntityId).register(ClientboundPackets1_14.STATISTICS); registerIncoming(ServerboundPackets1_14.EDIT_BOOK, new PacketRemapper() { @Override @@ -50,14 +50,12 @@ public class Protocol1_15To1_14_4 extends Protocol 1.15 mappings..."); - JsonObject diffmapping = MappingDataLoader.loadData("mappingdiff-1.14to1.15.json"); - JsonObject mapping1_14 = MappingDataLoader.loadData("mapping-1.14.json", true); - JsonObject mapping1_15 = MappingDataLoader.loadData("mapping-1.15.json", true); + public MappingData() { + super("1.14", "1.15", true); + } - oldToNewItems.defaultReturnValue(-1); - blockStateMappings = new Mappings(mapping1_14.getAsJsonObject("blockstates"), mapping1_15.getAsJsonObject("blockstates"), diffmapping.getAsJsonObject("blockstates")); - blockMappings = new Mappings(mapping1_14.getAsJsonObject("blocks"), mapping1_15.getAsJsonObject("blocks")); - MappingDataLoader.mapIdentifiers(oldToNewItems, mapping1_14.getAsJsonObject("items"), mapping1_15.getAsJsonObject("items")); - soundMappings = new Mappings(mapping1_14.getAsJsonArray("sounds"), mapping1_15.getAsJsonArray("sounds"), false); - statisticsMappings = new Mappings(mapping1_14.getAsJsonArray("statistics"), mapping1_15.getAsJsonArray("statistics")); + @Override + protected Mappings loadFromArray(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings, String key) { + if (!key.equals("sounds")) { + return super.loadFromArray(oldMappings, newMappings, diffMappings, key); + } + + // Ignore removed sounds + return new Mappings(oldMappings.getAsJsonArray(key), newMappings.getAsJsonArray(key), false); } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/metadata/MetadataRewriter1_15To1_14_4.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/metadata/MetadataRewriter1_15To1_14_4.java index 18994e9ed..ce6b06845 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/metadata/MetadataRewriter1_15To1_14_4.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/metadata/MetadataRewriter1_15To1_14_4.java @@ -27,7 +27,7 @@ public class MetadataRewriter1_15To1_14_4 extends MetadataRewriter { } else if (metadata.getMetaType() == MetaType1_14.BlockID) { // Convert to new block id int data = (int) metadata.getValue(); - metadata.setValue(Protocol1_15To1_14_4.getNewBlockStateId(data)); + metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); } if (type == null) return; diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/EntityPackets.java index 776c91dbb..7074c0ac1 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/EntityPackets.java @@ -18,7 +18,7 @@ public class EntityPackets { public static void register(Protocol1_15To1_14_4 protocol) { MetadataRewriter1_15To1_14_4 metadataRewriter = protocol.get(MetadataRewriter1_15To1_14_4.class); - metadataRewriter.registerSpawnTrackerWithData(ClientboundPackets1_14.SPAWN_ENTITY, Entity1_15Types.EntityType.FALLING_BLOCK, Protocol1_15To1_14_4::getNewBlockStateId); + metadataRewriter.registerSpawnTrackerWithData(ClientboundPackets1_14.SPAWN_ENTITY, Entity1_15Types.EntityType.FALLING_BLOCK); protocol.registerOutgoing(ClientboundPackets1_14.SPAWN_MOB, new PacketRemapper() { @Override diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/InventoryPackets.java index 52ed0eebd..5dcd6fa99 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/InventoryPackets.java @@ -1,21 +1,19 @@ package us.myles.ViaVersion.protocols.protocol1_15to1_14_4.packets; -import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.minecraft.item.Item; -import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.rewriters.ItemRewriter; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14; -import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.data.MappingData; +import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4; public class InventoryPackets { - public static void register(Protocol protocol) { + public static void register(Protocol1_15To1_14_4 protocol) { ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer); - itemRewriter.registerSetCooldown(ClientboundPackets1_14.COOLDOWN, InventoryPackets::getNewItemId); + itemRewriter.registerSetCooldown(ClientboundPackets1_14.COOLDOWN); itemRewriter.registerWindowItems(ClientboundPackets1_14.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY); itemRewriter.registerTradeList(ClientboundPackets1_14.TRADE_LIST, Type.FLAT_VAR_INT_ITEM); itemRewriter.registerSetSlot(ClientboundPackets1_14.SET_SLOT, Type.FLAT_VAR_INT_ITEM); @@ -30,25 +28,11 @@ public class InventoryPackets { public static void toClient(Item item) { if (item == null) return; - item.setIdentifier(getNewItemId(item.getIdentifier())); + item.setIdentifier(Protocol1_15To1_14_4.MAPPINGS.getNewItemId(item.getIdentifier())); } public static void toServer(Item item) { if (item == null) return; - item.setIdentifier(getOldItemId(item.getIdentifier())); - } - - public static int getNewItemId(int id) { - int newId = MappingData.oldToNewItems.get(id); - if (newId == -1) { - Via.getPlatform().getLogger().warning("Missing 1.15 item for 1.14 item " + id); - return 1; - } - return newId; - } - - public static int getOldItemId(int id) { - int oldId = MappingData.oldToNewItems.inverse().get(id); - return oldId != -1 ? oldId : 1; + item.setIdentifier(Protocol1_15To1_14_4.MAPPINGS.getOldItemId(item.getIdentifier())); } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/WorldPackets.java index 61b2e2e00..42729f2a4 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/WorldPackets.java @@ -3,7 +3,6 @@ package us.myles.ViaVersion.protocols.protocol1_15to1_14_4.packets; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; -import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.rewriters.BlockRewriter; @@ -15,8 +14,8 @@ import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.types.Chunk1_15Type; public class WorldPackets { - public static void register(Protocol protocol) { - BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14, Protocol1_15To1_14_4::getNewBlockStateId, Protocol1_15To1_14_4::getNewBlockId); + public static void register(Protocol1_15To1_14_4 protocol) { + BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14); blockRewriter.registerBlockAction(ClientboundPackets1_14.BLOCK_ACTION); blockRewriter.registerBlockChange(ClientboundPackets1_14.BLOCK_CHANGE); @@ -59,7 +58,7 @@ public class WorldPackets { if (section == null) continue; for (int i = 0; i < section.getPaletteSize(); i++) { int old = section.getPaletteEntry(i); - int newId = Protocol1_15To1_14_4.getNewBlockStateId(old); + int newId = protocol.getMappingData().getNewBlockStateId(old); section.setPaletteEntry(i, newId); } } @@ -68,7 +67,7 @@ public class WorldPackets { } }); - blockRewriter.registerEffect(ClientboundPackets1_14.EFFECT, 1010, 2001, InventoryPackets::getNewItemId); + blockRewriter.registerEffect(ClientboundPackets1_14.EFFECT, 1010, 2001); protocol.registerOutgoing(ClientboundPackets1_14.SPAWN_PARTICLE, new PacketRemapper() { @Override public void registerMap() { @@ -88,7 +87,7 @@ public class WorldPackets { int id = wrapper.get(Type.INT, 0); if (id == 3 || id == 23) { int data = wrapper.passthrough(Type.VAR_INT); - wrapper.set(Type.VAR_INT, 0, Protocol1_15To1_14_4.getNewBlockStateId(data)); + wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data)); } else if (id == 32) { InventoryPackets.toClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/Protocol1_16_2To1_16_1.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/Protocol1_16_2To1_16_1.java index abdd3491e..0bc4db549 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/Protocol1_16_2To1_16_1.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/Protocol1_16_2To1_16_1.java @@ -1,9 +1,9 @@ package us.myles.ViaVersion.protocols.protocol1_16_2to1_16_1; -import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.rewriters.MetadataRewriter; import us.myles.ViaVersion.api.rewriters.RegistryType; import us.myles.ViaVersion.api.rewriters.SoundRewriter; import us.myles.ViaVersion.api.rewriters.StatisticsRewriter; @@ -20,27 +20,27 @@ import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16 public class Protocol1_16_2To1_16_1 extends Protocol { + public static final MappingData MAPPINGS = new MappingData(); private TagRewriter tagRewriter; public Protocol1_16_2To1_16_1() { - super(ClientboundPackets1_16.class, ClientboundPackets1_16_2.class, ServerboundPackets1_16.class, ServerboundPackets1_16_2.class, true); + super(ClientboundPackets1_16.class, ClientboundPackets1_16_2.class, ServerboundPackets1_16.class, ServerboundPackets1_16_2.class); } @Override protected void registerPackets() { - MetadataRewriter1_16_2To1_16_1 metadataRewriter = new MetadataRewriter1_16_2To1_16_1(this); + MetadataRewriter metadataRewriter = new MetadataRewriter1_16_2To1_16_1(this); EntityPackets.register(this); WorldPackets.register(this); InventoryPackets.register(this); - tagRewriter = new TagRewriter(this, Protocol1_16_2To1_16_1::getNewBlockId, InventoryPackets::getNewItemId, metadataRewriter::getNewEntityId); + tagRewriter = new TagRewriter(this, metadataRewriter::getNewEntityId); tagRewriter.register(ClientboundPackets1_16.TAGS); - new StatisticsRewriter(this, Protocol1_16_2To1_16_1::getNewBlockId, InventoryPackets::getNewItemId, - metadataRewriter::getNewEntityId).register(ClientboundPackets1_16.STATISTICS); + new StatisticsRewriter(this, metadataRewriter::getNewEntityId).register(ClientboundPackets1_16.STATISTICS); - SoundRewriter soundRewriter = new SoundRewriter(this, id -> MappingData.soundMappings.getNewId(id)); + SoundRewriter soundRewriter = new SoundRewriter(this); soundRewriter.registerSound(ClientboundPackets1_16.SOUND); soundRewriter.registerSound(ClientboundPackets1_16.ENTITY_SOUND); @@ -77,9 +77,7 @@ public class Protocol1_16_2To1_16_1 extends Protocol dimensionDataMap = new HashMap<>(); - public static Int2IntBiMap oldToNewItems = new Int2IntBiMap(); - public static Mappings blockMappings; - public static Mappings blockStateMappings; - public static Mappings soundMappings; +public class MappingData extends us.myles.ViaVersion.api.data.MappingData { + private final Map dimensionDataMap = new HashMap<>(); + private CompoundTag dimensionRegistry; - public static void init() { - Via.getPlatform().getLogger().info("Loading 1.16.1 -> 1.16.2 mappings..."); - JsonObject diffmapping = MappingDataLoader.loadData("mappingdiff-1.16.1to1.16.2.json"); - JsonObject mapping1_16 = MappingDataLoader.loadData("mapping-1.16.json", true); - JsonObject mapping1_16_2 = MappingDataLoader.loadData("mapping-1.16.2.json", true); + public MappingData() { + super("1.16", "1.16.2", true); + } + @Override + public void loadExtras(JsonObject oldMappings, JsonObject newMappings, JsonObject diffMappings) { try { dimensionRegistry = BinaryTagIO.readCompressedInputStream(MappingDataLoader.getResource("dimension-registry-1.16.2.nbt")); } catch (IOException e) { @@ -36,12 +30,6 @@ public class MappingData { e.printStackTrace(); } - oldToNewItems.defaultReturnValue(-1); - blockStateMappings = new Mappings(mapping1_16.getAsJsonObject("blockstates"), mapping1_16_2.getAsJsonObject("blockstates"), diffmapping.getAsJsonObject("blockstates")); - blockMappings = new Mappings(mapping1_16.getAsJsonObject("blocks"), mapping1_16_2.getAsJsonObject("blocks")); - MappingDataLoader.mapIdentifiers(oldToNewItems, mapping1_16.getAsJsonObject("items"), mapping1_16_2.getAsJsonObject("items")); - soundMappings = new Mappings(mapping1_16.getAsJsonArray("sounds"), mapping1_16_2.getAsJsonArray("sounds")); - // Data of each dimension ListTag dimensions = ((CompoundTag) dimensionRegistry.get("minecraft:dimension_type")).get("value"); for (Tag dimension : dimensions) { @@ -51,4 +39,12 @@ public class MappingData { dimensionDataMap.put(((StringTag) dimensionCompound.get("name")).getValue(), dimensionData); } } + + public Map getDimensionDataMap() { + return dimensionDataMap; + } + + public CompoundTag getDimensionRegistry() { + return dimensionRegistry; + } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/metadata/MetadataRewriter1_16_2To1_16_1.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/metadata/MetadataRewriter1_16_2To1_16_1.java index bbe793b25..73ecb9771 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/metadata/MetadataRewriter1_16_2To1_16_1.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/metadata/MetadataRewriter1_16_2To1_16_1.java @@ -27,7 +27,7 @@ public class MetadataRewriter1_16_2To1_16_1 extends MetadataRewriter { InventoryPackets.toClient((Item) metadata.getValue()); } else if (metadata.getMetaType() == MetaType1_14.BlockID) { int data = (int) metadata.getValue(); - metadata.setValue(Protocol1_16_2To1_16_1.getNewBlockStateId(data)); + metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); } if (type == null) return; diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/packets/EntityPackets.java index 1bf7e7abd..b17787e5c 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/packets/EntityPackets.java @@ -7,7 +7,6 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Types1_14; import us.myles.ViaVersion.protocols.protocol1_16_2to1_16_1.Protocol1_16_2To1_16_1; -import us.myles.ViaVersion.protocols.protocol1_16_2to1_16_1.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_16_2to1_16_1.metadata.MetadataRewriter1_16_2To1_16_1; import us.myles.ViaVersion.protocols.protocol1_16_2to1_16_1.storage.EntityTracker1_16_2; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; @@ -16,8 +15,7 @@ public class EntityPackets { public static void register(Protocol1_16_2To1_16_1 protocol) { MetadataRewriter1_16_2To1_16_1 metadataRewriter = protocol.get(MetadataRewriter1_16_2To1_16_1.class); - - metadataRewriter.registerSpawnTrackerWithData(ClientboundPackets1_16.SPAWN_ENTITY, Entity1_16_2Types.EntityType.FALLING_BLOCK, Protocol1_16_2To1_16_1::getNewBlockStateId); + metadataRewriter.registerSpawnTrackerWithData(ClientboundPackets1_16.SPAWN_ENTITY, Entity1_16_2Types.EntityType.FALLING_BLOCK); metadataRewriter.registerTracker(ClientboundPackets1_16.SPAWN_MOB); metadataRewriter.registerTracker(ClientboundPackets1_16.SPAWN_PLAYER, Entity1_16_2Types.EntityType.PLAYER); metadataRewriter.registerMetadataRewriter(ClientboundPackets1_16.ENTITY_METADATA, Types1_14.METADATA_LIST); @@ -39,7 +37,7 @@ public class EntityPackets { handler(wrapper -> { // Throw away the old dimension registry, extra conversion would be too hard of a hit wrapper.read(Type.NBT); - wrapper.write(Type.NBT, MappingData.dimensionRegistry); + wrapper.write(Type.NBT, protocol.getMappingData().getDimensionRegistry()); // Instead of the dimension's resource key, it now just wants the data directly String dimensionType = wrapper.read(Type.STRING); @@ -67,7 +65,7 @@ public class EntityPackets { } public static CompoundTag getDimensionData(String dimensionType) { - CompoundTag tag = MappingData.dimensionDataMap.get(dimensionType); + CompoundTag tag = Protocol1_16_2To1_16_1.MAPPINGS.getDimensionDataMap().get(dimensionType); if (tag == null) { Via.getPlatform().getLogger().severe("Could not get dimension data of " + dimensionType); throw new NullPointerException("Dimension data for " + dimensionType + " is null!"); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/packets/InventoryPackets.java index 4da15cd53..669584d1d 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/packets/InventoryPackets.java @@ -1,13 +1,11 @@ package us.myles.ViaVersion.protocols.protocol1_16_2to1_16_1.packets; -import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.rewriters.ItemRewriter; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.protocol1_16_2to1_16_1.Protocol1_16_2To1_16_1; import us.myles.ViaVersion.protocols.protocol1_16_2to1_16_1.ServerboundPackets1_16_2; -import us.myles.ViaVersion.protocols.protocol1_16_2to1_16_1.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16; @@ -16,7 +14,7 @@ public class InventoryPackets { public static void register(Protocol1_16_2To1_16_1 protocol) { ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer); - itemRewriter.registerSetCooldown(ClientboundPackets1_16.COOLDOWN, InventoryPackets::getNewItemId); + itemRewriter.registerSetCooldown(ClientboundPackets1_16.COOLDOWN); itemRewriter.registerWindowItems(ClientboundPackets1_16.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY); itemRewriter.registerTradeList(ClientboundPackets1_16.TRADE_LIST, Type.FLAT_VAR_INT_ITEM); itemRewriter.registerSetSlot(ClientboundPackets1_16.SET_SLOT, Type.FLAT_VAR_INT_ITEM); @@ -55,25 +53,11 @@ public class InventoryPackets { public static void toClient(Item item) { if (item == null) return; - item.setIdentifier(getNewItemId(item.getIdentifier())); + item.setIdentifier(Protocol1_16_2To1_16_1.MAPPINGS.getNewItemId(item.getIdentifier())); } public static void toServer(Item item) { if (item == null) return; - item.setIdentifier(getOldItemId(item.getIdentifier())); - } - - public static int getNewItemId(int id) { - int newId = MappingData.oldToNewItems.get(id); - if (newId == -1) { - Via.getPlatform().getLogger().warning("Missing 1.16.2 item for 1.16 item " + id); - return 1; - } - return newId; - } - - public static int getOldItemId(int id) { - int oldId = MappingData.oldToNewItems.inverse().get(id); - return oldId != -1 ? oldId : 1; + item.setIdentifier(Protocol1_16_2To1_16_1.MAPPINGS.getOldItemId(item.getIdentifier())); } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/packets/WorldPackets.java index 2b7ad08bc..c19a21e5e 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/packets/WorldPackets.java @@ -23,7 +23,7 @@ public class WorldPackets { private static final BlockChangeRecord[] EMPTY_RECORDS = new BlockChangeRecord[0]; public static void register(Protocol protocol) { - BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14, Protocol1_16_2To1_16_1::getNewBlockStateId, Protocol1_16_2To1_16_1::getNewBlockId); + BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14); blockRewriter.registerBlockAction(ClientboundPackets1_16.BLOCK_ACTION); blockRewriter.registerBlockChange(ClientboundPackets1_16.BLOCK_CHANGE); @@ -41,7 +41,7 @@ public class WorldPackets { if (section == null) continue; for (int i = 0; i < section.getPaletteSize(); i++) { int old = section.getPaletteEntry(i); - section.setPaletteEntry(i, Protocol1_16_2To1_16_1.getNewBlockStateId(old)); + section.setPaletteEntry(i, protocol.getMappingData().getNewBlockStateId(old)); } } }); @@ -71,7 +71,7 @@ public class WorldPackets { } // Absolute y -> relative chunk section y - int blockId = Protocol1_16_2To1_16_1.getNewBlockStateId(record.getBlockId()); + int blockId = protocol.getMappingData().getNewBlockStateId(record.getBlockId()); list.add(new BlockChangeRecord1_16_2(record.getSectionX(), record.getSectionY(), record.getSectionZ(), blockId)); } @@ -90,7 +90,7 @@ public class WorldPackets { } }); - blockRewriter.registerEffect(ClientboundPackets1_16.EFFECT, 1010, 2001, InventoryPackets::getNewItemId); + blockRewriter.registerEffect(ClientboundPackets1_16.EFFECT, 1010, 2001); blockRewriter.registerSpawnParticle(ClientboundPackets1_16.SPAWN_PARTICLE, 3, 23, 34, null, InventoryPackets::toClient, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java index c5e5198f5..259e468e4 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java @@ -9,6 +9,7 @@ import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.rewriters.ComponentRewriter; +import us.myles.ViaVersion.api.rewriters.MetadataRewriter; import us.myles.ViaVersion.api.rewriters.RegistryType; import us.myles.ViaVersion.api.rewriters.SoundRewriter; import us.myles.ViaVersion.api.rewriters.StatisticsRewriter; @@ -35,25 +36,25 @@ import java.util.UUID; public class Protocol1_16To1_15_2 extends Protocol { private static final UUID ZERO_UUID = new UUID(0, 0); + public static final MappingData MAPPINGS = new MappingData(); private TagRewriter tagRewriter; public Protocol1_16To1_15_2() { - super(ClientboundPackets1_15.class, ClientboundPackets1_16.class, ServerboundPackets1_14.class, ServerboundPackets1_16.class, true); + super(ClientboundPackets1_15.class, ClientboundPackets1_16.class, ServerboundPackets1_14.class, ServerboundPackets1_16.class); } @Override protected void registerPackets() { - MetadataRewriter1_16To1_15_2 metadataRewriter = new MetadataRewriter1_16To1_15_2(this); + MetadataRewriter metadataRewriter = new MetadataRewriter1_16To1_15_2(this); EntityPackets.register(this); WorldPackets.register(this); InventoryPackets.register(this); - tagRewriter = new TagRewriter(this, Protocol1_16To1_15_2::getNewBlockId, InventoryPackets::getNewItemId, metadataRewriter::getNewEntityId); + tagRewriter = new TagRewriter(this, metadataRewriter::getNewEntityId); tagRewriter.register(ClientboundPackets1_15.TAGS); - new StatisticsRewriter(this, Protocol1_16To1_15_2::getNewBlockId, InventoryPackets::getNewItemId, - metadataRewriter::getNewEntityId, id -> MappingData.statisticsMappings.getNewId(id)).register(ClientboundPackets1_15.STATISTICS); + new StatisticsRewriter(this, metadataRewriter::getNewEntityId).register(ClientboundPackets1_15.STATISTICS); // Login Success registerOutgoing(State.LOGIN, 0x02, 0x02, new PacketRemapper() { @@ -124,7 +125,7 @@ public class Protocol1_16To1_15_2 extends Protocol MappingData.soundMappings.getNewId(id)); + SoundRewriter soundRewriter = new SoundRewriter(this); soundRewriter.registerSound(ClientboundPackets1_15.SOUND); soundRewriter.registerSound(ClientboundPackets1_15.ENTITY_SOUND); @@ -206,9 +207,7 @@ public class Protocol1_16To1_15_2 extends Protocol attributeMappings = HashBiMap.create(); - public static Mappings blockMappings; - public static Mappings blockStateMappings; - public static Mappings soundMappings; - public static Mappings statisticsMappings; +public class MappingData extends us.myles.ViaVersion.api.data.MappingData { + private final BiMap attributeMappings = HashBiMap.create(); - public static void init() { - Via.getPlatform().getLogger().info("Loading 1.15 -> 1.16 mappings..."); - JsonObject diffmapping = MappingDataLoader.loadData("mappingdiff-1.15to1.16.json"); - JsonObject mapping1_15 = MappingDataLoader.loadData("mapping-1.15.json", true); - JsonObject mapping1_16 = MappingDataLoader.loadData("mapping-1.16.json", true); - - oldToNewItems.defaultReturnValue(-1); - blockStateMappings = new Mappings(mapping1_15.getAsJsonObject("blockstates"), mapping1_16.getAsJsonObject("blockstates"), diffmapping.getAsJsonObject("blockstates")); - blockMappings = new Mappings(mapping1_15.getAsJsonObject("blocks"), mapping1_16.getAsJsonObject("blocks")); - MappingDataLoader.mapIdentifiers(oldToNewItems, mapping1_15.getAsJsonObject("items"), mapping1_16.getAsJsonObject("items"), diffmapping.getAsJsonObject("items")); - soundMappings = new Mappings(mapping1_15.getAsJsonArray("sounds"), mapping1_16.getAsJsonArray("sounds"), diffmapping.getAsJsonObject("sounds")); - statisticsMappings = new Mappings(mapping1_15.getAsJsonArray("statistics"), mapping1_16.getAsJsonArray("statistics")); + public MappingData() { + super("1.15", "1.16", true); + } + @Override + protected void loadExtras(JsonObject oldMappings, JsonObject newMappings, JsonObject diffMappings) { attributeMappings.put("generic.maxHealth", "minecraft:generic.max_health"); attributeMappings.put("zombie.spawnReinforcements", "minecraft:zombie.spawn_reinforcements"); attributeMappings.put("horse.jumpStrength", "minecraft:horse.jump_strength"); @@ -41,4 +25,8 @@ public class MappingData { attributeMappings.put("generic.attackSpeed", "minecraft:generic.attack_speed"); attributeMappings.put("generic.armorToughness", "minecraft:generic.armor_toughness"); } + + public BiMap getAttributeMappings() { + return attributeMappings; + } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/metadata/MetadataRewriter1_16To1_15_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/metadata/MetadataRewriter1_16To1_15_2.java index 4febfe109..d3732eaa8 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/metadata/MetadataRewriter1_16To1_15_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/metadata/MetadataRewriter1_16To1_15_2.java @@ -30,7 +30,7 @@ public class MetadataRewriter1_16To1_15_2 extends MetadataRewriter { InventoryPackets.toClient((Item) metadata.getValue()); } else if (metadata.getMetaType() == MetaType1_14.BlockID) { int data = (int) metadata.getValue(); - metadata.setValue(Protocol1_16To1_15_2.getNewBlockStateId(data)); + metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); } if (type == null) return; diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java index 9ecd65a97..b3b91a35f 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java @@ -17,7 +17,6 @@ import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15 import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16; -import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.metadata.MetadataRewriter1_16To1_15_2; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.storage.EntityTracker1_16; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16; @@ -156,7 +155,7 @@ public class EntityPackets { } }); - metadataRewriter.registerSpawnTrackerWithData(ClientboundPackets1_15.SPAWN_ENTITY, Entity1_16Types.EntityType.FALLING_BLOCK, Protocol1_16To1_15_2::getNewBlockStateId); + metadataRewriter.registerSpawnTrackerWithData(ClientboundPackets1_15.SPAWN_ENTITY, Entity1_16Types.EntityType.FALLING_BLOCK); metadataRewriter.registerTracker(ClientboundPackets1_15.SPAWN_MOB); metadataRewriter.registerTracker(ClientboundPackets1_15.SPAWN_PLAYER, Entity1_16Types.EntityType.PLAYER); metadataRewriter.registerMetadataRewriter(ClientboundPackets1_15.ENTITY_METADATA, Types1_14.METADATA_LIST); @@ -215,7 +214,7 @@ public class EntityPackets { for (int i = 0; i < size; i++) { // Attributes have been renamed and are now namespaced identifiers String key = wrapper.read(Type.STRING); - String attributeIdentifier = MappingData.attributeMappings.get(key); + String attributeIdentifier = protocol.getMappingData().getAttributeMappings().get(key); if (attributeIdentifier == null) { attributeIdentifier = "minecraft:" + key; if (!us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.MappingData.isValid1_13Channel(attributeIdentifier)) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java index 1683a6e5e..743b7477a 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java @@ -6,24 +6,22 @@ import com.github.steveice10.opennbt.tag.builtin.ListTag; import com.github.steveice10.opennbt.tag.builtin.LongTag; import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.Tag; -import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.minecraft.item.Item; -import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.rewriters.ItemRewriter; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.UUIDIntArrayType; -import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14; +import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15; +import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16; -import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16; import java.util.UUID; public class InventoryPackets { - public static void register(Protocol protocol) { + public static void register(Protocol1_16To1_15_2 protocol) { ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer); protocol.registerOutgoing(ClientboundPackets1_15.OPEN_WINDOW, new PacketRemapper() { @@ -76,7 +74,7 @@ public class InventoryPackets { } }); - itemRewriter.registerSetCooldown(ClientboundPackets1_15.COOLDOWN, InventoryPackets::getNewItemId); + itemRewriter.registerSetCooldown(ClientboundPackets1_15.COOLDOWN); itemRewriter.registerWindowItems(ClientboundPackets1_15.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY); itemRewriter.registerTradeList(ClientboundPackets1_15.TRADE_LIST, Type.FLAT_VAR_INT_ITEM); itemRewriter.registerSetSlot(ClientboundPackets1_15.SET_SLOT, Type.FLAT_VAR_INT_ITEM); @@ -137,13 +135,13 @@ public class InventoryPackets { } oldToNewAttributes(item); - item.setIdentifier(getNewItemId(item.getIdentifier())); + item.setIdentifier(Protocol1_16To1_15_2.MAPPINGS.getNewItemId(item.getIdentifier())); } public static void toServer(Item item) { if (item == null) return; - item.setIdentifier(getOldItemId(item.getIdentifier())); + item.setIdentifier(Protocol1_16To1_15_2.MAPPINGS.getOldItemId(item.getIdentifier())); if (item.getIdentifier() == 771 && item.getTag() != null) { CompoundTag tag = item.getTag(); @@ -200,7 +198,7 @@ public class InventoryPackets { } public static void rewriteAttributeName(CompoundTag compoundTag, String entryName, boolean inverse) { - StringTag attributeNameTag = compoundTag.get("AttributeName"); + StringTag attributeNameTag = compoundTag.get(entryName); if (attributeNameTag == null) return; String attributeName = attributeNameTag.getValue(); @@ -208,23 +206,10 @@ public class InventoryPackets { attributeName = "minecraft:" + attributeName; } - String mappedAttribute = (inverse ? MappingData.attributeMappings.inverse() : MappingData.attributeMappings).get(attributeName); + String mappedAttribute = (inverse ? Protocol1_16To1_15_2.MAPPINGS.getAttributeMappings().inverse() + : Protocol1_16To1_15_2.MAPPINGS.getAttributeMappings()).get(attributeName); if (mappedAttribute == null) return; attributeNameTag.setValue(mappedAttribute); } - - public static int getNewItemId(int id) { - int newId = MappingData.oldToNewItems.get(id); - if (newId == -1) { - Via.getPlatform().getLogger().warning("Missing 1.16 item for 1.15.2 item " + id); - return 1; - } - return newId; - } - - public static int getOldItemId(int id) { - int oldId = MappingData.oldToNewItems.inverse().get(id); - return oldId != -1 ? oldId : 1; - } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/WorldPackets.java index c83e3915b..6da3a35c2 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/WorldPackets.java @@ -8,7 +8,6 @@ import com.github.steveice10.opennbt.tag.builtin.Tag; import us.myles.ViaVersion.api.minecraft.Position; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; -import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.rewriters.BlockRewriter; import us.myles.ViaVersion.api.type.Type; @@ -23,8 +22,8 @@ import java.util.UUID; public class WorldPackets { - public static void register(Protocol protocol) { - BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14, Protocol1_16To1_15_2::getNewBlockStateId, Protocol1_16To1_15_2::getNewBlockId); + public static void register(Protocol1_16To1_15_2 protocol) { + BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14); blockRewriter.registerBlockAction(ClientboundPackets1_15.BLOCK_ACTION); blockRewriter.registerBlockChange(ClientboundPackets1_15.BLOCK_CHANGE); @@ -54,7 +53,7 @@ public class WorldPackets { if (section == null) continue; for (int i = 0; i < section.getPaletteSize(); i++) { int old = section.getPaletteEntry(i); - section.setPaletteEntry(i, Protocol1_16To1_15_2.getNewBlockStateId(old)); + section.setPaletteEntry(i, protocol.getMappingData().getNewBlockStateId(old)); } } @@ -86,7 +85,7 @@ public class WorldPackets { } }); - blockRewriter.registerEffect(ClientboundPackets1_15.EFFECT, 1010, 2001, InventoryPackets::getNewItemId); + blockRewriter.registerEffect(ClientboundPackets1_15.EFFECT, 1010, 2001); blockRewriter.registerSpawnParticle(ClientboundPackets1_15.SPAWN_PARTICLE, 3, 23, 32, WorldPackets::getNewParticleId, InventoryPackets::toClient, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java index 1325aba2d..b506881dc 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java @@ -10,6 +10,7 @@ import us.myles.ViaVersion.api.platform.providers.ViaProviders; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.ValueTransformer; +import us.myles.ViaVersion.api.rewriters.MetadataRewriter; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_8.ClientboundPackets1_8; @@ -94,7 +95,7 @@ public class Protocol1_9To1_8 extends Protocol viaversion-parent us.myles - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT 4.0.0 diff --git a/jar/pom.xml b/jar/pom.xml index f0b5ddecd..b38fafd8b 100644 --- a/jar/pom.xml +++ b/jar/pom.xml @@ -5,7 +5,7 @@ viaversion-parent us.myles - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT 4.0.0 viaversion-jar diff --git a/pom.xml b/pom.xml index c07cc79fc..22777567c 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ us.myles viaversion-parent - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT pom viaversion-parent diff --git a/sponge-legacy/pom.xml b/sponge-legacy/pom.xml index 290ab2852..10e34b5d9 100644 --- a/sponge-legacy/pom.xml +++ b/sponge-legacy/pom.xml @@ -5,7 +5,7 @@ viaversion-parent us.myles - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT 4.0.0 diff --git a/sponge/pom.xml b/sponge/pom.xml index 59824438d..0d3b60523 100644 --- a/sponge/pom.xml +++ b/sponge/pom.xml @@ -5,7 +5,7 @@ viaversion-parent us.myles - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT 4.0.0 diff --git a/velocity/pom.xml b/velocity/pom.xml index 24a04bc9c..df84eff02 100644 --- a/velocity/pom.xml +++ b/velocity/pom.xml @@ -5,7 +5,7 @@ viaversion-parent us.myles - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT 4.0.0