From 947e5c3e07330b3c821b47b0cb91690d528716be Mon Sep 17 00:00:00 2001 From: KennyTV Date: Sun, 16 Aug 2020 20:17:04 +0200 Subject: [PATCH] Abstract mapping data loading --- all/pom.xml | 2 +- bukkit/pom.xml | 2 +- bungee/pom.xml | 2 +- core/pom.xml | 2 +- .../viabackwards/api/BackwardsProtocol.java | 17 ++- .../api/data/BackwardsMappings.java | 118 ++++++++++++++++++ .../viabackwards/api/data/VBItemMappings.java | 56 --------- .../api/data/VBMappingDataLoader.java | 50 ++++++++ .../viabackwards/api/data/VBMappings.java | 4 + .../api/data/VBSoundMappings.java | 43 ------- .../api/rewriters/EntityRewriter.java | 5 +- .../api/rewriters/ItemRewriter.java | 41 +++--- .../api/rewriters/ItemRewriterBase.java | 19 +-- .../rewriters/LegacyBlockItemRewriter.java | 9 +- .../api/rewriters/SoundRewriter.java | 19 +-- .../Protocol1_12_2To1_13.java | 10 +- .../block_entity_handlers/PistonHandler.java | 4 +- .../data/BackwardsMappings.java | 65 +++++----- .../data/ParticleMapping.java | 3 +- .../packets/BlockItemPackets1_13.java | 46 +++---- .../packets/EntityPackets1_13.java | 2 +- .../packets/PlayerPacket1_13.java | 3 +- .../packets/SoundPackets1_13.java | 7 +- .../Protocol1_13_1To1_13_2.java | 2 +- .../Protocol1_13_2To1_14.java | 40 ++---- .../data/BackwardsMappings.java | 31 ----- .../packets/BlockItemPackets1_14.java | 40 ++---- .../packets/EntityPackets1_14.java | 4 +- .../packets/SoundPackets1_14.java | 6 +- .../Protocol1_13To1_13_1.java | 51 +++----- .../packets/EntityPackets1_13_1.java | 6 +- .../packets/InventoryPackets1_13_1.java | 23 +--- .../packets/WorldPackets1_13_1.java | 7 +- .../Protocol1_14_4To1_15.java | 39 ++---- .../data/BackwardsMappings.java | 31 ----- .../packets/BlockItemPackets1_15.java | 34 +---- .../packets/EntityPackets1_15.java | 4 +- .../Protocol1_15_2To1_16.java | 37 ++---- .../data/BackwardsMappings.java | 40 ++---- .../packets/BlockItemPackets1_16.java | 32 +---- .../packets/EntityPackets1_16.java | 7 +- .../Protocol1_16_1To1_16_2.java | 39 ++---- .../data/BackwardsMappings.java | 29 ----- .../packets/BlockItemPackets1_16_2.java | 35 +----- .../packets/EntityPackets1_16_2.java | 4 +- ...2.2to1.13.json => mapping-1.12to1.13.json} | 0 .../data/mapping-1.13to1.13.2.json | 20 +++ ...4.4to1.15.json => mapping-1.14to1.15.json} | 0 fabric/pom.xml | 2 +- pom.xml | 4 +- sponge/pom.xml | 2 +- velocity/pom.xml | 2 +- 52 files changed, 429 insertions(+), 671 deletions(-) create mode 100644 core/src/main/java/nl/matsv/viabackwards/api/data/BackwardsMappings.java delete mode 100644 core/src/main/java/nl/matsv/viabackwards/api/data/VBItemMappings.java delete mode 100644 core/src/main/java/nl/matsv/viabackwards/api/data/VBSoundMappings.java delete mode 100644 core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/data/BackwardsMappings.java delete mode 100644 core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/data/BackwardsMappings.java delete mode 100644 core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_16_1to1_16_2/data/BackwardsMappings.java rename core/src/main/resources/assets/viabackwards/data/{mapping-1.12.2to1.13.json => mapping-1.12to1.13.json} (100%) create mode 100644 core/src/main/resources/assets/viabackwards/data/mapping-1.13to1.13.2.json rename core/src/main/resources/assets/viabackwards/data/{mapping-1.14.4to1.15.json => mapping-1.14to1.15.json} (100%) diff --git a/all/pom.xml b/all/pom.xml index a2651aa4..2b30620b 100644 --- a/all/pom.xml +++ b/all/pom.xml @@ -16,7 +16,7 @@ viabackwards-parent nl.matsv - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT viabackwards-all diff --git a/bukkit/pom.xml b/bukkit/pom.xml index 4dcceaa1..8b971781 100644 --- a/bukkit/pom.xml +++ b/bukkit/pom.xml @@ -16,7 +16,7 @@ viabackwards-parent nl.matsv - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT viabackwards-bukkit diff --git a/bungee/pom.xml b/bungee/pom.xml index d70b3593..a6740d79 100644 --- a/bungee/pom.xml +++ b/bungee/pom.xml @@ -16,7 +16,7 @@ viabackwards-parent nl.matsv - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT viabackwards-bungee diff --git a/core/pom.xml b/core/pom.xml index 964dbda3..e9f295a8 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -16,7 +16,7 @@ viabackwards-parent nl.matsv - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT viabackwards-core diff --git a/core/src/main/java/nl/matsv/viabackwards/api/BackwardsProtocol.java b/core/src/main/java/nl/matsv/viabackwards/api/BackwardsProtocol.java index 42d70c44..eb38485e 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/BackwardsProtocol.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/BackwardsProtocol.java @@ -1,5 +1,6 @@ package nl.matsv.viabackwards.api; +import nl.matsv.viabackwards.api.data.BackwardsMappings; import org.jetbrains.annotations.Nullable; import us.myles.ViaVersion.api.protocol.ClientboundPacketType; import us.myles.ViaVersion.api.protocol.Protocol; @@ -12,18 +13,9 @@ public abstract class BackwardsProtocol oldClientboundPacketEnum, @Nullable Class clientboundPacketEnum, @Nullable Class oldServerboundPacketEnum, @Nullable Class serverboundPacketEnum) { - super(oldClientboundPacketEnum, clientboundPacketEnum, oldServerboundPacketEnum, serverboundPacketEnum, false); - } - - protected BackwardsProtocol(@Nullable Class oldClientboundPacketEnum, @Nullable Class clientboundPacketEnum, - @Nullable Class oldServerboundPacketEnum, @Nullable Class serverboundPacketEnum, boolean hasMappingDatatToLoad) { - super(oldClientboundPacketEnum, clientboundPacketEnum, oldServerboundPacketEnum, serverboundPacketEnum, hasMappingDatatToLoad); + super(oldClientboundPacketEnum, clientboundPacketEnum, oldServerboundPacketEnum, serverboundPacketEnum); } /** @@ -32,4 +24,9 @@ public abstract class BackwardsProtocol protocolClass, Runnable runnable) { ProtocolRegistry.addMappingLoaderFuture(getClass(), protocolClass, runnable); } + + @Override + public BackwardsMappings getMappingData() { + return null; + } } diff --git a/core/src/main/java/nl/matsv/viabackwards/api/data/BackwardsMappings.java b/core/src/main/java/nl/matsv/viabackwards/api/data/BackwardsMappings.java new file mode 100644 index 00000000..3205a469 --- /dev/null +++ b/core/src/main/java/nl/matsv/viabackwards/api/data/BackwardsMappings.java @@ -0,0 +1,118 @@ +package nl.matsv.viabackwards.api.data; + +import com.google.common.base.Preconditions; +import nl.matsv.viabackwards.api.BackwardsProtocol; +import org.jetbrains.annotations.Nullable; +import us.myles.ViaVersion.api.data.MappingData; +import us.myles.ViaVersion.api.data.Mappings; +import us.myles.ViaVersion.api.protocol.Protocol; +import us.myles.ViaVersion.api.protocol.ProtocolRegistry; +import us.myles.viaversion.libs.fastutil.ints.Int2ObjectMap; +import us.myles.viaversion.libs.gson.JsonObject; + +import java.util.Map; + +public class BackwardsMappings extends MappingData { + + private final Class vvProtocolClass; + private Int2ObjectMap backwardsItemMappings; + private Map backwardsSoundMappings; + + public BackwardsMappings(String oldVersion, String newVersion, @Nullable Class vvProtocolClass) { + this(oldVersion, newVersion, vvProtocolClass, false); + } + + public BackwardsMappings(String oldVersion, String newVersion, @Nullable Class vvProtocolClass, boolean hasDiffFile) { + super(oldVersion, newVersion, hasDiffFile); + Preconditions.checkArgument(!vvProtocolClass.isAssignableFrom(BackwardsProtocol.class)); + this.vvProtocolClass = vvProtocolClass; + // Just re-use ViaVersion's item id map + loadItems = false; + } + + @Override + public void load() { + // Load them later + } + + public void loadVBMappings() { + super.load(); + } + + @Override + protected void loadExtras(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings) { + if (diffMappings != null) { + JsonObject diffItems = diffMappings.getAsJsonObject("items"); + if (diffItems != null) { + backwardsItemMappings = VBMappingDataLoader.loadItemMappings(oldMappings.getAsJsonObject("items"), newMappings.getAsJsonObject("items"), diffItems); + } + + JsonObject diffSounds = diffMappings.getAsJsonObject("sounds"); + if (diffSounds != null) { + backwardsSoundMappings = VBMappingDataLoader.objectToMap(diffSounds); + } + } + + // Just re-use ViaVersion's item id map + if (vvProtocolClass != null) { + itemMappings = ProtocolRegistry.getProtocol(vvProtocolClass).getMappingData().getItemMappings().inverse(); + } + + loadVBExtras(oldMappings, newMappings); + } + + @Override + @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 VBMappings(oldMappings.getAsJsonArray(key), newMappings.getAsJsonArray(key), diff, shouldWarnOnMissing(key)); + } + + @Override + @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 VBMappings(oldMappings.getAsJsonObject(key), newMappings.getAsJsonObject(key), diff, shouldWarnOnMissing(key)); + } + + @Override + protected JsonObject loadDiffFile() { + return VBMappingDataLoader.loadFromDataDir("mapping-" + newVersion + "to" + oldVersion + ".json"); + } + + protected boolean shouldWarnOnMissing(String key) { + return !key.equals("blocks") && !key.equals("statistics"); + } + + protected void loadVBExtras(JsonObject oldMappings, JsonObject newMappings) { + } + + @Nullable + public MappedItem getMappedItem(int id) { + return backwardsItemMappings != null ? backwardsItemMappings.get(id) : null; + } + + @Nullable + public String getMappedNamedSound(String id) { + return backwardsSoundMappings != null ? backwardsSoundMappings.get(id) : null; + } + + @Override + protected int checkValidity(int id, String type) { + return id; + } + + @Nullable + public Int2ObjectMap getBackwardsItemMappings() { + return backwardsItemMappings; + } + + @Nullable + public Map getBackwardsSoundMappings() { + return backwardsSoundMappings; + } +} diff --git a/core/src/main/java/nl/matsv/viabackwards/api/data/VBItemMappings.java b/core/src/main/java/nl/matsv/viabackwards/api/data/VBItemMappings.java deleted file mode 100644 index a8dd67a2..00000000 --- a/core/src/main/java/nl/matsv/viabackwards/api/data/VBItemMappings.java +++ /dev/null @@ -1,56 +0,0 @@ -package nl.matsv.viabackwards.api.data; - -import nl.matsv.viabackwards.ViaBackwards; -import org.jetbrains.annotations.Nullable; -import us.myles.ViaVersion.api.Via; -import us.myles.ViaVersion.api.data.MappingDataLoader; -import us.myles.viaversion.libs.fastutil.ints.Int2ObjectMap; -import us.myles.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap; -import us.myles.viaversion.libs.gson.JsonElement; -import us.myles.viaversion.libs.gson.JsonObject; - -import java.util.HashMap; -import java.util.Map; - -/** - * Backwards mappings for newly (!) added items. - */ -public class VBItemMappings { - - private final Int2ObjectMap itemMapping; - - public VBItemMappings(JsonObject oldMapping, JsonObject newMapping, JsonObject diffMapping) { - Map itemMapping = new HashMap<>(); - for (Map.Entry entry : diffMapping.entrySet()) { - JsonObject object = entry.getValue().getAsJsonObject(); - String mappedIdName = object.getAsJsonPrimitive("id").getAsString(); - Map.Entry value = MappingDataLoader.findValue(newMapping, mappedIdName); - if (value == null) { - if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - ViaBackwards.getPlatform().getLogger().warning("No key for " + mappedIdName + " :( "); - } - continue; - } - - Map.Entry oldEntry = MappingDataLoader.findValue(oldMapping, entry.getKey()); - if (oldEntry == null) { - if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - ViaBackwards.getPlatform().getLogger().warning("No old entry for " + mappedIdName + " :( "); - } - continue; - } - - int id = Integer.parseInt(oldEntry.getKey()); - int mappedId = Integer.parseInt(value.getKey()); - String name = object.getAsJsonPrimitive("name").getAsString(); - itemMapping.put(id, new MappedItem(mappedId, name)); - } - - this.itemMapping = new Int2ObjectOpenHashMap<>(itemMapping, 1F); - } - - @Nullable - public MappedItem getMappedItem(int id) { - return itemMapping.get(id); - } -} diff --git a/core/src/main/java/nl/matsv/viabackwards/api/data/VBMappingDataLoader.java b/core/src/main/java/nl/matsv/viabackwards/api/data/VBMappingDataLoader.java index a6135e1c..c2caecb4 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/data/VBMappingDataLoader.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/data/VBMappingDataLoader.java @@ -4,6 +4,8 @@ import nl.matsv.viabackwards.ViaBackwards; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.MappingDataLoader; import us.myles.ViaVersion.util.GsonUtil; +import us.myles.viaversion.libs.fastutil.ints.Int2ObjectMap; +import us.myles.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap; import us.myles.viaversion.libs.gson.JsonArray; import us.myles.viaversion.libs.gson.JsonElement; import us.myles.viaversion.libs.gson.JsonIOException; @@ -16,6 +18,7 @@ import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.HashMap; import java.util.Map; public class VBMappingDataLoader { @@ -43,6 +46,7 @@ public class VBMappingDataLoader { try (InputStreamReader reader = new InputStreamReader(stream)) { return GsonUtil.getGson().fromJson(reader, JsonObject.class); } catch (IOException e) { + e.printStackTrace(); return null; } } @@ -122,4 +126,50 @@ public class VBMappingDataLoader { output[i] = index.shortValue(); } } + + public static Map objectToMap(JsonObject object) { + Map mappings = new HashMap<>(); + for (Map.Entry entry : object.entrySet()) { + String key = entry.getKey(); + if (key.indexOf(':') == -1) { + key = "minecraft:" + key; + } + String value = entry.getValue().getAsString(); + if (value.indexOf(':') == -1) { + value = "minecraft:" + value; + } + mappings.put(key, value); + } + return mappings; + } + + public static Int2ObjectMap loadItemMappings(JsonObject oldMapping, JsonObject newMapping, JsonObject diffMapping) { + Map itemMapping = new HashMap<>(); + for (Map.Entry entry : diffMapping.entrySet()) { + JsonObject object = entry.getValue().getAsJsonObject(); + String mappedIdName = object.getAsJsonPrimitive("id").getAsString(); + Map.Entry value = MappingDataLoader.findValue(newMapping, mappedIdName); + if (value == null) { + if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { + ViaBackwards.getPlatform().getLogger().warning("No key for " + mappedIdName + " :( "); + } + continue; + } + + Map.Entry oldEntry = MappingDataLoader.findValue(oldMapping, entry.getKey()); + if (oldEntry == null) { + if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { + ViaBackwards.getPlatform().getLogger().warning("No old entry for " + mappedIdName + " :( "); + } + continue; + } + + int id = Integer.parseInt(oldEntry.getKey()); + int mappedId = Integer.parseInt(value.getKey()); + String name = object.getAsJsonPrimitive("name").getAsString(); + itemMapping.put(id, new MappedItem(mappedId, name)); + } + + return new Int2ObjectOpenHashMap<>(itemMapping, 1F); + } } diff --git a/core/src/main/java/nl/matsv/viabackwards/api/data/VBMappings.java b/core/src/main/java/nl/matsv/viabackwards/api/data/VBMappings.java index 3f88a209..69df14df 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/data/VBMappings.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/data/VBMappings.java @@ -32,6 +32,10 @@ public class VBMappings extends Mappings { super(create(oldMapping, newMapping, diffMapping, true)); } + public VBMappings(JsonArray oldMapping, JsonArray newMapping, JsonObject diffMapping, boolean warnOnMissing) { + super(create(oldMapping, newMapping, diffMapping, warnOnMissing)); + } + private static short[] create(int size, JsonObject oldMapping, JsonObject newMapping, JsonObject diffMapping, boolean warnOnMissing) { short[] oldToNew = new short[size]; Arrays.fill(oldToNew, (short) -1); diff --git a/core/src/main/java/nl/matsv/viabackwards/api/data/VBSoundMappings.java b/core/src/main/java/nl/matsv/viabackwards/api/data/VBSoundMappings.java deleted file mode 100644 index 130f2a05..00000000 --- a/core/src/main/java/nl/matsv/viabackwards/api/data/VBSoundMappings.java +++ /dev/null @@ -1,43 +0,0 @@ -package nl.matsv.viabackwards.api.data; - -import org.jetbrains.annotations.Nullable; -import us.myles.ViaVersion.api.data.Mappings; -import us.myles.viaversion.libs.gson.JsonArray; -import us.myles.viaversion.libs.gson.JsonElement; -import us.myles.viaversion.libs.gson.JsonObject; - -import java.util.HashMap; -import java.util.Map; - -/** - * Backwards mappings for sound ids and names. - */ -public class VBSoundMappings { - - private final Map namedSoundMappings = new HashMap<>(); - private final Mappings idMappings; - - public VBSoundMappings(JsonArray oldSounds, JsonArray newSounds, JsonObject diffMapping) { - idMappings = new VBMappings(oldSounds, newSounds, diffMapping); - for (Map.Entry entry : diffMapping.entrySet()) { - String key = entry.getKey(); - if (key.indexOf(':') == -1) { - key = "minecraft:" + key; - } - String value = entry.getValue().getAsString(); - if (value.indexOf(':') == -1) { - value = "minecraft:" + value; - } - namedSoundMappings.put(key, value); - } - } - - @Nullable - public String getNewId(String oldId) { - return namedSoundMappings.get(oldId); - } - - public int getNewId(int oldId) { - return idMappings.getNewId(oldId); - } -} diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriter.java index b35cbf49..68af1844 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriter.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriter.java @@ -10,7 +10,6 @@ import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_14; import us.myles.ViaVersion.api.protocol.ClientboundPacketType; import us.myles.ViaVersion.api.remapper.PacketRemapper; -import us.myles.ViaVersion.api.rewriters.IdRewriteFunction; import us.myles.ViaVersion.api.type.Type; import java.util.List; @@ -25,7 +24,7 @@ public abstract class EntityRewriter extends Entity super(protocol, displayType, 2); } - public void registerSpawnTrackerWithData(ClientboundPacketType packetType, EntityType fallingBlockType, IdRewriteFunction blockStateRewriter) { + public void registerSpawnTrackerWithData(ClientboundPacketType packetType, EntityType fallingBlockType) { protocol.registerOutgoing(packetType, new PacketRemapper() { @Override public void registerMap() { @@ -42,7 +41,7 @@ public abstract class EntityRewriter extends Entity EntityType entityType = setOldEntityId(wrapper); if (entityType == fallingBlockType) { int blockState = wrapper.get(Type.INT, 0); - wrapper.set(Type.INT, 0, blockStateRewriter.rewrite(blockState)); + wrapper.set(Type.INT, 0, protocol.getMappingData().getNewBlockStateId(blockState)); } }); } diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/ItemRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/ItemRewriter.java index 6e4eb028..c3fad074 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/ItemRewriter.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/ItemRewriter.java @@ -4,29 +4,20 @@ import nl.matsv.viabackwards.api.BackwardsProtocol; import nl.matsv.viabackwards.api.data.MappedItem; import org.jetbrains.annotations.Nullable; import us.myles.ViaVersion.api.minecraft.item.Item; -import us.myles.ViaVersion.api.rewriters.IdRewriteFunction; import us.myles.viaversion.libs.opennbt.tag.builtin.ByteTag; import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag; +import us.myles.viaversion.libs.opennbt.tag.builtin.IntTag; import us.myles.viaversion.libs.opennbt.tag.builtin.ListTag; import us.myles.viaversion.libs.opennbt.tag.builtin.StringTag; import us.myles.viaversion.libs.opennbt.tag.builtin.Tag; public abstract class ItemRewriter extends ItemRewriterBase { - private final MappedItemFunction mappedItemFunction; private final TranslatableRewriter translatableRewriter; - protected ItemRewriter(T protocol, @Nullable TranslatableRewriter translatableRewriter, - @Nullable IdRewriteFunction oldRewriter, @Nullable IdRewriteFunction newRewriter, MappedItemFunction mappedItemFunction) { - super(protocol, oldRewriter, newRewriter, true); - this.translatableRewriter = translatableRewriter; - this.mappedItemFunction = mappedItemFunction; - } - - protected ItemRewriter(T protocol, @Nullable TranslatableRewriter translatableRewriter, MappedItemFunction mappedItemFunction) { + protected ItemRewriter(T protocol, @Nullable TranslatableRewriter translatableRewriter) { super(protocol, true); this.translatableRewriter = translatableRewriter; - this.mappedItemFunction = mappedItemFunction; } @Override @@ -71,19 +62,21 @@ public abstract class ItemRewriter extends ItemRewr } } - MappedItem data = mappedItemFunction.get(item.getIdentifier()); + MappedItem data = protocol.getMappingData().getMappedItem(item.getIdentifier()); if (data == null) { // Just rewrite the id return super.handleItemToClient(item); } - // Set remapped id - item.setIdentifier(data.getId()); - - // Set custom name - only done if there is no original one if (item.getTag() == null) { item.setTag(new CompoundTag("")); } + + // Save original id, set remapped id + item.getTag().put(new IntTag(nbtTagName + "|id", item.getIdentifier())); + item.setIdentifier(data.getId()); + + // Set custom name - only done if there is no original one if (display == null) { item.getTag().put(display = new CompoundTag("display")); } @@ -94,10 +87,18 @@ public abstract class ItemRewriter extends ItemRewr return item; } - @FunctionalInterface - public interface MappedItemFunction { + @Override + @Nullable + public Item handleItemToServer(Item item) { + if (item == null) return null; - @Nullable - MappedItem get(int id); + super.handleItemToServer(item); + if (item.getTag() != null) { + IntTag originalId = item.getTag().remove(nbtTagName + "|id"); + if (originalId != null) { + item.setIdentifier(originalId.getValue()); + } + } + return item; } } diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/ItemRewriterBase.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/ItemRewriterBase.java index 5c4841b6..ef19c2db 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/ItemRewriterBase.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/ItemRewriterBase.java @@ -3,7 +3,6 @@ package nl.matsv.viabackwards.api.rewriters; import nl.matsv.viabackwards.api.BackwardsProtocol; import org.jetbrains.annotations.Nullable; import us.myles.ViaVersion.api.minecraft.item.Item; -import us.myles.ViaVersion.api.rewriters.IdRewriteFunction; import us.myles.viaversion.libs.opennbt.conversion.builtin.CompoundTagConverter; import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag; import us.myles.viaversion.libs.opennbt.tag.builtin.ListTag; @@ -13,28 +12,20 @@ import us.myles.viaversion.libs.opennbt.tag.builtin.Tag; public abstract class ItemRewriterBase extends Rewriter { protected static final CompoundTagConverter CONVERTER = new CompoundTagConverter(); - protected final IdRewriteFunction toClientRewriter; - protected final IdRewriteFunction toServerRewriter; protected final String nbtTagName; protected final boolean jsonNameFormat; - protected ItemRewriterBase(T protocol, @Nullable IdRewriteFunction toClientRewriter, @Nullable IdRewriteFunction toServerRewriter, boolean jsonNameFormat) { + protected ItemRewriterBase(T protocol, boolean jsonNameFormat) { super(protocol); - this.toClientRewriter = toClientRewriter; - this.toServerRewriter = toServerRewriter; this.jsonNameFormat = jsonNameFormat; nbtTagName = "VB|" + protocol.getClass().getSimpleName(); } - protected ItemRewriterBase(T protocol, boolean jsonNameFormat) { - this(protocol, null, null, jsonNameFormat); - } - @Nullable public Item handleItemToClient(Item item) { if (item == null) return null; - if (toClientRewriter != null) { - item.setIdentifier(toClientRewriter.rewrite(item.getIdentifier())); + if (protocol.getMappingData() != null) { + item.setIdentifier(protocol.getMappingData().getNewItemId(item.getIdentifier())); } return item; } @@ -42,8 +33,8 @@ public abstract class ItemRewriterBase extends Rewr @Nullable public Item handleItemToServer(Item item) { if (item == null) return null; - if (toServerRewriter != null) { - item.setIdentifier(toServerRewriter.rewrite(item.getIdentifier())); + if (protocol.getMappingData() != null) { + item.setIdentifier(protocol.getMappingData().getOldItemId(item.getIdentifier())); } restoreDisplayTag(item); return item; diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/LegacyBlockItemRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/LegacyBlockItemRewriter.java index 2dfe399b..a5a6bec7 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/LegacyBlockItemRewriter.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/LegacyBlockItemRewriter.java @@ -20,7 +20,6 @@ import org.jetbrains.annotations.Nullable; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; import us.myles.ViaVersion.api.minecraft.item.Item; -import us.myles.ViaVersion.api.rewriters.IdRewriteFunction; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; import us.myles.viaversion.libs.fastutil.ints.Int2ObjectMap; import us.myles.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap; @@ -79,13 +78,9 @@ public abstract class LegacyBlockItemRewriter exten } } - protected LegacyBlockItemRewriter(T protocol, @Nullable IdRewriteFunction oldRewriter, @Nullable IdRewriteFunction newRewriter) { - super(protocol, oldRewriter, newRewriter, false); - replacementData = LEGACY_MAPPINGS.get(protocol.getClass().getSimpleName().split("To")[1].replace("_", ".")); - } - protected LegacyBlockItemRewriter(T protocol) { - this(protocol, null, null); + super(protocol, false); + replacementData = LEGACY_MAPPINGS.get(protocol.getClass().getSimpleName().split("To")[1].replace("_", ".")); } @Override diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundRewriter.java index 092d54fc..0fedd858 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundRewriter.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundRewriter.java @@ -3,22 +3,15 @@ package nl.matsv.viabackwards.api.rewriters; import nl.matsv.viabackwards.api.BackwardsProtocol; import us.myles.ViaVersion.api.protocol.ClientboundPacketType; import us.myles.ViaVersion.api.remapper.PacketRemapper; -import us.myles.ViaVersion.api.rewriters.IdRewriteFunction; import us.myles.ViaVersion.api.type.Type; -import java.util.function.Function; - public class SoundRewriter extends us.myles.ViaVersion.api.rewriters.SoundRewriter { - private final Function stringIdRewriter; + private final BackwardsProtocol protocol; - public SoundRewriter(BackwardsProtocol protocol, IdRewriteFunction idRewriter, Function stringIdRewriter) { - super(protocol, idRewriter); - this.stringIdRewriter = stringIdRewriter; - } - - public SoundRewriter(BackwardsProtocol protocol, IdRewriteFunction idRewriter) { - this(protocol, idRewriter, null); + public SoundRewriter(BackwardsProtocol protocol) { + super(protocol); + this.protocol = protocol; } public void registerNamedSound(ClientboundPacketType packetType) { @@ -28,7 +21,7 @@ public class SoundRewriter extends us.myles.ViaVersion.api.rewriters.SoundRewrit map(Type.STRING); // Sound identifier handler(wrapper -> { String soundId = wrapper.get(Type.STRING, 0); - String mappedId = stringIdRewriter.apply(soundId); + String mappedId = protocol.getMappingData().getMappedNamedSound(soundId); if (mappedId == null) return; if (!mappedId.isEmpty()) { wrapper.set(Type.STRING, 0, mappedId); @@ -53,7 +46,7 @@ public class SoundRewriter extends us.myles.ViaVersion.api.rewriters.SoundRewrit } String soundId = wrapper.read(Type.STRING); - String mappedId = stringIdRewriter.apply(soundId); + String mappedId = protocol.getMappingData().getMappedNamedSound(soundId); if (mappedId == null) { // No mapping found wrapper.write(Type.STRING, soundId); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java index b47fae02..97dca18f 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java @@ -36,6 +36,7 @@ import us.myles.viaversion.libs.gson.JsonObject; public class Protocol1_12_2To1_13 extends BackwardsProtocol { + public static final BackwardsMappings MAPPINGS = new BackwardsMappings(); private BlockItemPackets1_13 blockItemPackets; public Protocol1_12_2To1_13() { @@ -45,7 +46,7 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol { - BackwardsMappings.init(); + MAPPINGS.loadVBMappings(); PaintingMapping.init(); Via.getManager().getProviders().register(BackwardsBlockEntityProvider.class, new BackwardsBlockEntityProvider()); }); @@ -54,7 +55,7 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol statisticMappings = new Int2ObjectOpenHashMap<>(); - public static final Map translateMappings = new HashMap<>(); - public static BlockMappingsShortArray blockMappings; - public static VBSoundMappings soundMappings; - public static VBItemMappings itemMappings; - public static Mappings enchantmentMappings; +public class BackwardsMappings extends nl.matsv.viabackwards.api.data.BackwardsMappings { + private final Int2ObjectMap statisticMappings = new Int2ObjectOpenHashMap<>(); + private final Map translateMappings = new HashMap<>(); + private Mappings enchantmentMappings; - public static void init() { - ViaBackwards.getPlatform().getLogger().info("Loading 1.13 -> 1.12.2 mappings..."); - JsonObject mapping1_12 = MappingDataLoader.getMappingsCache().get("mapping-1.12.json"); - JsonObject mapping1_13 = MappingDataLoader.getMappingsCache().get("mapping-1.13.json"); - JsonObject mapping1_12_2to1_13 = VBMappingDataLoader.loadFromDataDir("mapping-1.12.2to1.13.json"); - - blockMappings = new BlockMappingsShortArray(mapping1_13.getAsJsonObject("blocks"), mapping1_12.getAsJsonObject("blocks"), mapping1_12_2to1_13.getAsJsonObject("blockstates")); - itemMappings = new VBItemMappings(mapping1_13.getAsJsonObject("items"), mapping1_12.getAsJsonObject("items"), mapping1_12_2to1_13.getAsJsonObject("items")); - soundMappings = new VBSoundMappings(mapping1_13.getAsJsonArray("sounds"), mapping1_12.getAsJsonArray("sounds"), mapping1_12_2to1_13.getAsJsonObject("sounds")); - enchantmentMappings = new VBMappings(mapping1_13.getAsJsonObject("enchantments"), mapping1_12.getAsJsonObject("enchantments"), false); + public BackwardsMappings() { + super("1.13", "1.12", Protocol1_13To1_12_2.class, true); + } + @Override + public void loadVBExtras(JsonObject oldMappings, JsonObject newMappings) { + enchantmentMappings = new VBMappings(oldMappings.getAsJsonObject("enchantments"), newMappings.getAsJsonObject("enchantments"), false); for (Map.Entry entry : StatisticMappings.CUSTOM_STATS.entrySet()) { statisticMappings.put(entry.getValue().intValue(), entry.getKey()); } - for (Map.Entry entry : MappingData.translateMapping.entrySet()) { + for (Map.Entry entry : Protocol1_13To1_12_2.MAPPINGS.getTranslateMapping().entrySet()) { translateMappings.put(entry.getValue(), entry.getKey()); } } @@ -93,16 +84,28 @@ public class BackwardsMappings { } } - public static class BlockMappingsShortArray { - private final short[] oldToNew = new short[8582]; - - private BlockMappingsShortArray(JsonObject newIdentifiers, JsonObject oldIdentifiers, JsonObject mapping) { + @Override + @Nullable + protected Mappings loadFromObject(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings, String key) { + if (key.equals("blockstates")) { + short[] oldToNew = new short[8582]; Arrays.fill(oldToNew, (short) -1); - mapIdentifiers(oldToNew, newIdentifiers, oldIdentifiers, mapping); - } - - public int getNewId(int old) { - return old >= 0 && old < oldToNew.length ? oldToNew[old] : -1; + mapIdentifiers(oldToNew, oldMappings.getAsJsonObject("blockstates"), newMappings.getAsJsonObject("blocks"), diffMappings.getAsJsonObject("blockstates")); + return new Mappings(oldToNew); + } else { + return super.loadFromObject(oldMappings, newMappings, diffMappings, key); } } + + public Int2ObjectMap getStatisticMappings() { + return statisticMappings; + } + + public Map getTranslateMappings() { + return translateMappings; + } + + public Mappings getEnchantmentMappings() { + return enchantmentMappings; + } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/ParticleMapping.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/ParticleMapping.java index 9daca4ba..97f6b0d9 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/ParticleMapping.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/ParticleMapping.java @@ -11,7 +11,6 @@ package nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13; -import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.packets.BlockItemPackets1_13; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.type.Type; @@ -35,7 +34,7 @@ public class ParticleMapping { } private int[] rewrite(int newType) { - int blockType = BlockItemPackets1_13.toOldId(newType); + int blockType = Protocol1_12_2To1_13.MAPPINGS.getNewBlockStateId(newType); int type = blockType >> 4; int meta = blockType & 15; diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java index a903bdef..47c853da 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java @@ -15,7 +15,6 @@ import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.rewriters.EnchantmentRewriter; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.block_entity_handlers.FlowerPotHandler; -import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.providers.BackwardsBlockEntityProvider; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.storage.BackwardsBlockStorage; import us.myles.ViaVersion.api.PacketWrapper; @@ -33,8 +32,8 @@ import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.protocol1_12_1to1_12.ServerboundPackets1_12_1; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; 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.SpawnEggRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type; import us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type; @@ -61,23 +60,10 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It private final String extraNbtTag; public BlockItemPackets1_13(Protocol1_12_2To1_13 protocol) { - super(protocol, null, id -> BackwardsMappings.itemMappings.getMappedItem(id)); + super(protocol, null); extraNbtTag = "VB|" + protocol.getClass().getSimpleName() + "|2"; } - public static int toOldId(int oldId) { - if (oldId < 0) { - oldId = 0; // Some plugins use negative numbers to clear blocks, remap them to air. - } - int newId = BackwardsMappings.blockMappings.getNewId(oldId); - if (newId != -1) - return newId; - - ViaBackwards.getPlatform().getLogger().warning("Missing block completely " + oldId); - // Default stone - return 1 << 4; - } - public static boolean isDamageable(int id) { return id >= 256 && id <= 259 // iron shovel, pickaxe, axe, flint and steel || id == 261 // bow @@ -101,7 +87,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It @Override public void handle(PacketWrapper wrapper) throws Exception { int itemId = wrapper.read(Type.VAR_INT); - int oldId = MappingData.oldToNewItems.inverse().get(itemId); + int oldId = protocol.getMappingData().getItemMappings().get(itemId); if (oldId != -1) { Optional eggEntityId = SpawnEggRewriter.getEntityId(oldId); if (eggEntityId.isPresent()) { @@ -220,7 +206,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It BackwardsBlockStorage storage = wrapper.user().get(BackwardsBlockStorage.class); storage.checkAndStore(position, blockState); - wrapper.write(Type.VAR_INT, toOldId(blockState)); + wrapper.write(Type.VAR_INT, protocol.getMappingData().getNewBlockStateId(blockState)); // Flower pot special treatment flowerPotSpecialTreatment(wrapper.user(), blockState, position); @@ -257,7 +243,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It flowerPotSpecialTreatment(wrapper.user(), block, position); // Change to old id - record.setBlockId(toOldId(block)); + record.setBlockId(protocol.getMappingData().getNewBlockStateId(block)); } } }); @@ -357,7 +343,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It for (int p = 0; p < section.getPaletteSize(); p++) { int old = section.getPaletteEntry(p); if (old != 0) { - int oldId = toOldId(old); + int oldId = protocol.getMappingData().getNewBlockStateId(old); section.setPaletteEntry(p, oldId); } } @@ -413,9 +399,9 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It int id = wrapper.get(Type.INT, 0); int data = wrapper.get(Type.INT, 1); if (id == 1010) { // Play record - wrapper.set(Type.INT, 1, MappingData.oldToNewItems.inverse().get(data) >> 4); + wrapper.set(Type.INT, 1, protocol.getMappingData().getItemMappings().get(data) >> 4); } else if (id == 2001) { // Block break + block break sound - data = toOldId(data); + data = protocol.getMappingData().getNewBlockStateId(data); int blockId = data >> 4; int blockData = data & 0xF; wrapper.set(Type.INT, 1, (blockId & 0xFFF) | (blockData << 12)); @@ -478,7 +464,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It // Enchantment table if (property >= 4 && property <= 6) { short oldId = wrapper.get(Type.SHORT, 1); - wrapper.set(Type.SHORT, 1, (short) BackwardsMappings.enchantmentMappings.getNewId(oldId)); + wrapper.set(Type.SHORT, 1, (short) protocol.getMappingData().getEnchantmentMappings().getNewId(oldId)); } }); } @@ -543,7 +529,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It // No custom mapping found, look at VV mappings if (item.getIdentifier() == originalId) { - int oldId = MappingData.oldToNewItems.inverse().get(item.getIdentifier()); + int oldId = protocol.getMappingData().getItemMappings().get(item.getIdentifier()); if (oldId != -1) { rawId = itemIdToRaw(oldId, item, tag); } else if (item.getIdentifier() == 362) { // base/colorless shulker box @@ -668,7 +654,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It lore.add(new StringTag("", mappedEnchantmentId + " " + EnchantmentRewriter.getRomanNumber(level))); noMapped.add(enchantmentEntry); } else if (!newId.isEmpty()) { - Short oldId = MappingData.oldEnchantmentsIds.inverse().get(newId); + Short oldId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().inverse().get(newId); if (oldId == null) { if (!newId.startsWith("viaversion:legacy/")) { // Custom enchant (?) @@ -837,7 +823,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It item.setIdentifier(identifier); int newId = -1; - if (!MappingData.oldToNewItems.containsKey(rawId)) { + if (!protocol.getMappingData().getItemMappings().inverse().containsKey(rawId)) { if (!isDamageable(item.getIdentifier()) && item.getIdentifier() != 358) { // Map if (tag == null) item.setTag(tag = new CompoundTag("tag")); tag.put(new IntTag(extraNbtTag, originalId)); // Data will be lost, saving original id @@ -847,7 +833,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It newId = 362; // directly set the new id -> base/colorless shulker box } else if (item.getIdentifier() == 31 && item.getData() == 0) { // Shrub was removed rawId = 32 << 4; // Dead Bush - } else if (MappingData.oldToNewItems.containsKey(rawId & ~0xF)) { + } else if (protocol.getMappingData().getItemMappings().inverse().containsKey(rawId & ~0xF)) { rawId &= ~0xF; // Remove data } else { if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { @@ -858,7 +844,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It } if (newId == -1) { - newId = MappingData.oldToNewItems.get(rawId); + newId = protocol.getMappingData().getItemMappings().inverse().get(rawId); } item.setIdentifier(newId); @@ -920,7 +906,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It if (dummyEnchant && oldId == 0 && level == 0) { continue; //Skip dummy enchatment } - String newId = MappingData.oldEnchantmentsIds.get(oldId); + String newId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().get(oldId); if (newId == null) { newId = "viaversion:legacy/" + oldId; } @@ -1004,7 +990,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It // Create the flowerpot PacketWrapper blockCreate = new PacketWrapper(0x0B, null, user); blockCreate.write(Type.POSITION, position); - blockCreate.write(Type.VAR_INT, toOldId(blockState)); + blockCreate.write(Type.VAR_INT, Protocol1_12_2To1_13.MAPPINGS.getNewBlockStateId(blockState)); blockCreate.send(Protocol1_12_2To1_13.class, true); // Send a block entity update diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java index 7c61d492..2641e755 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java @@ -89,7 +89,7 @@ public class EntityPackets1_13 extends LegacyEntityRewriter> 4) & 0xFFF) | ((combined & 0xF) << 12); wrapper.set(Type.INT, 0, combined); } else if (type == Entity1_13Types.ObjectType.ITEM_FRAME) { diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/PlayerPacket1_13.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/PlayerPacket1_13.java index 494f23a4..964b0bf2 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/PlayerPacket1_13.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/PlayerPacket1_13.java @@ -4,7 +4,6 @@ import com.google.common.base.Joiner; import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.rewriters.Rewriter; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13; -import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.ParticleMapping; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.storage.TabCompleteStorage; import nl.matsv.viabackwards.utils.ChatUtil; @@ -542,7 +541,7 @@ public class PlayerPacket1_13 extends Rewriter { newSize--; continue; case 8: - name = BackwardsMappings.statisticMappings.get(statisticId); + name = protocol.getMappingData().getStatisticMappings().get(statisticId); if (name == null) { wrapper.read(Type.VAR_INT); newSize--; diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/SoundPackets1_13.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/SoundPackets1_13.java index ac5a0154..898e45b4 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/SoundPackets1_13.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/SoundPackets1_13.java @@ -3,7 +3,6 @@ package nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.packets; import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.rewriters.Rewriter; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13; -import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.NamedSoundMapping; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.remapper.PacketRemapper; @@ -27,7 +26,7 @@ public class SoundPackets1_13 extends Rewriter { handler(wrapper -> { String newSound = wrapper.get(Type.STRING, 0); String oldSound = NamedSoundMapping.getOldId(newSound); - if (oldSound != null || (oldSound = BackwardsMappings.soundMappings.getNewId(newSound)) != null) { + if (oldSound != null || (oldSound = protocol.getMappingData().getMappedNamedSound(newSound)) != null) { wrapper.set(Type.STRING, 0, oldSound); } else if (!Via.getConfig().isSuppressConversionWarnings()) { ViaBackwards.getPlatform().getLogger().warning("Unknown named sound in 1.13->1.12 protocol: " + newSound); @@ -52,7 +51,7 @@ public class SoundPackets1_13 extends Rewriter { String sound; if ((flags & 0x02) != 0) { - sound = BackwardsMappings.soundMappings.getNewId(wrapper.read(Type.STRING)); + sound = protocol.getMappingData().getMappedNamedSound(wrapper.read(Type.STRING)); if (sound == null) { sound = ""; } @@ -72,7 +71,7 @@ public class SoundPackets1_13 extends Rewriter { map(Type.VAR_INT); handler(wrapper -> { int newSound = wrapper.get(Type.VAR_INT, 0); - int oldSound = BackwardsMappings.soundMappings.getNewId(newSound); + int oldSound = protocol.getMappingData().getSoundMappings().getNewId(newSound); if (oldSound == -1) { wrapper.cancel(); } else { diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_1to1_13_2/Protocol1_13_1To1_13_2.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_1to1_13_2/Protocol1_13_1To1_13_2.java index 98743305..56b5f8a2 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_1to1_13_2/Protocol1_13_1To1_13_2.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_1to1_13_2/Protocol1_13_1To1_13_2.java @@ -74,7 +74,7 @@ public class Protocol1_13_1To1_13_2 extends BackwardsProtocol { + new StatisticsRewriter(this, id -> { int newId = id; if (newId > 40) { if (id == 41) return -1; diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java index e9fd5650..b41e8cc8 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java @@ -1,10 +1,9 @@ package nl.matsv.viabackwards.protocol.protocol1_13_2to1_14; -import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.BackwardsProtocol; +import nl.matsv.viabackwards.api.data.BackwardsMappings; import nl.matsv.viabackwards.api.entities.storage.EntityTracker; import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter; -import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.packets.BlockItemPackets1_14; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.packets.EntityPackets1_14; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.packets.PlayerPackets1_14; @@ -21,11 +20,11 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13 import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; -import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; public class Protocol1_13_2To1_14 extends BackwardsProtocol { + public static final BackwardsMappings MAPPINGS = new BackwardsMappings("1.14", "1.13.2", Protocol1_14To1_13_2.class, true); private BlockItemPackets1_14 blockItemPackets; private EntityPackets1_14 entityPackets; @@ -35,7 +34,7 @@ public class Protocol1_13_2To1_14 extends BackwardsProtocol BackwardsMappings.blockMappings.getNewId(id), id -> MappingData.oldToNewItems.inverse().get(id), - entityPackets::getOldEntityId, id -> BackwardsMappings.statisticsMappings.getNewId(id)).register(ClientboundPackets1_14.STATISTICS); + new StatisticsRewriter(this, entityPackets::getOldEntityId).register(ClientboundPackets1_14.STATISTICS); cancelOutgoing(ClientboundPackets1_14.UPDATE_VIEW_POSITION); cancelOutgoing(ClientboundPackets1_14.UPDATE_VIEW_DISTANCE); @@ -73,7 +71,7 @@ public class Protocol1_13_2To1_14 extends BackwardsProtocol 1.13.2 mappings..."); - JsonObject mapping1_13_2 = MappingDataLoader.getMappingsCache().get("mapping-1.13.2.json"); - JsonObject mapping1_14 = MappingDataLoader.getMappingsCache().get("mapping-1.14.json"); - JsonObject mapping1_13_2to1_14 = VBMappingDataLoader.loadFromDataDir("mapping-1.13.2to1.14.json"); - - blockStateMappings = new VBMappings(mapping1_14.getAsJsonObject("blockstates"), mapping1_13_2.getAsJsonObject("blockstates"), mapping1_13_2to1_14.getAsJsonObject("blockstates")); - blockMappings = new VBMappings(mapping1_14.getAsJsonObject("blocks"), mapping1_13_2.getAsJsonObject("blocks"), mapping1_13_2to1_14.getAsJsonObject("blocks"), false); - itemMappings = new VBItemMappings(mapping1_14.getAsJsonObject("items"), mapping1_13_2.getAsJsonObject("items"), mapping1_13_2to1_14.getAsJsonObject("items")); - soundMappings = new VBSoundMappings(mapping1_14.getAsJsonArray("sounds"), mapping1_13_2.getAsJsonArray("sounds"), mapping1_13_2to1_14.getAsJsonObject("sounds")); - statisticsMappings = new Mappings(mapping1_14.getAsJsonArray("statistics"), mapping1_13_2.getAsJsonArray("statistics"), false); - } -} diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java index fed9095e..8407388b 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java @@ -6,7 +6,6 @@ import nl.matsv.viabackwards.api.entities.storage.EntityTracker; import nl.matsv.viabackwards.api.rewriters.EnchantmentRewriter; import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14; -import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; @@ -31,7 +30,7 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13 import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; -import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData; +import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; import us.myles.viaversion.libs.gson.JsonElement; @@ -51,7 +50,7 @@ public class BlockItemPackets1_14 extends nl.matsv.viabackwards.api.rewriters.It private EnchantmentRewriter enchantmentRewriter; public BlockItemPackets1_14(Protocol1_13_2To1_14 protocol, TranslatableRewriter translatableRewriter) { - super(protocol, translatableRewriter, BlockItemPackets1_14::getOldItemId, BlockItemPackets1_14::getNewItemId, id -> BackwardsMappings.itemMappings.getMappedItem(id)); + super(protocol, translatableRewriter); } @Override @@ -176,9 +175,9 @@ public class BlockItemPackets1_14 extends nl.matsv.viabackwards.api.rewriters.It }); ItemRewriter itemRewriter = new ItemRewriter(protocol, this::handleItemToClient, this::handleItemToServer); - BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION, Protocol1_13_2To1_14::getNewBlockStateId, Protocol1_13_2To1_14::getNewBlockId); + BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION); - itemRewriter.registerSetCooldown(ClientboundPackets1_14.COOLDOWN, BlockItemPackets1_14::getOldItemId); + itemRewriter.registerSetCooldown(ClientboundPackets1_14.COOLDOWN); itemRewriter.registerWindowItems(ClientboundPackets1_14.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY); itemRewriter.registerSetSlot(ClientboundPackets1_14.SET_SLOT, Type.FLAT_VAR_INT_ITEM); itemRewriter.registerAdvancements(ClientboundPackets1_14.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM); @@ -358,7 +357,7 @@ public class BlockItemPackets1_14 extends nl.matsv.viabackwards.api.rewriters.It map(Type.UNSIGNED_BYTE); // Action param map(Type.VAR_INT); // Block id - /!\ NOT BLOCK STATE handler(wrapper -> { - int mappedId = Protocol1_13_2To1_14.getNewBlockId(wrapper.get(Type.VAR_INT, 0)); + int mappedId = protocol.getMappingData().getNewBlockId(wrapper.get(Type.VAR_INT, 0)); if (mappedId == -1) { wrapper.cancel(); return; @@ -378,7 +377,7 @@ public class BlockItemPackets1_14 extends nl.matsv.viabackwards.api.rewriters.It public void handle(PacketWrapper wrapper) throws Exception { int id = wrapper.get(Type.VAR_INT, 0); - wrapper.set(Type.VAR_INT, 0, Protocol1_13_2To1_14.getNewBlockStateId(id)); + wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(id)); } }); } @@ -443,7 +442,7 @@ public class BlockItemPackets1_14 extends nl.matsv.viabackwards.api.rewriters.It for (int y = 0; y < 16; y++) { for (int z = 0; z < 16; z++) { int id = section.getFlatBlock(x, y, z); - if (MappingData.nonFullBlocks.contains(id)) { + if (Protocol1_14To1_13_2.MAPPINGS.getNonFullBlocks().contains(id)) { section.getBlockLightNibbleArray().set(x, y, z, 0); } } @@ -453,7 +452,7 @@ public class BlockItemPackets1_14 extends nl.matsv.viabackwards.api.rewriters.It for (int j = 0; j < section.getPaletteSize(); j++) { int old = section.getPaletteEntry(j); - int newId = Protocol1_13_2To1_14.getNewBlockStateId(old); + int newId = protocol.getMappingData().getNewBlockStateId(old); section.setPaletteEntry(j, newId); } } @@ -488,9 +487,9 @@ public class BlockItemPackets1_14 extends nl.matsv.viabackwards.api.rewriters.It int id = wrapper.get(Type.INT, 0); int data = wrapper.get(Type.INT, 1); if (id == 1010) { // Play record - wrapper.set(Type.INT, 1, data = BlockItemPackets1_14.getOldItemId(data)); + wrapper.set(Type.INT, 1, protocol.getMappingData().getNewItemId(data)); } else if (id == 2001) { // Block break + block break sound - wrapper.set(Type.INT, 1, data = Protocol1_13_2To1_14.getNewBlockStateId(data)); + wrapper.set(Type.INT, 1, protocol.getMappingData().getNewBlockStateId(data)); } } }); @@ -584,23 +583,4 @@ public class BlockItemPackets1_14 extends nl.matsv.viabackwards.api.rewriters.It } return item; } - - - public static int getNewItemId(int id) { - int newId = MappingData.oldToNewItems.get(id); - if (newId == -1) { - ViaBackwards.getPlatform().getLogger().warning("Missing 1.14 item for 1.13.2 item " + id); - return 1; - } - return newId; - } - - public static int getOldItemId(int id) { - int oldId = MappingData.oldToNewItems.inverse().get(id); - if (oldId == -1) { - ViaBackwards.getPlatform().getLogger().warning("Missing 1.13.2 item for 1.14 item " + id); - return 1; - } - return oldId; - } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/EntityPackets1_14.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/EntityPackets1_14.java index a3644b6e..6382939c 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/EntityPackets1_14.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/EntityPackets1_14.java @@ -172,7 +172,7 @@ public class EntityPackets1_14 extends LegacyEntityRewriter { @Override protected void registerPackets() { - SoundRewriter soundRewriter = new SoundRewriter(protocol, - id -> BackwardsMappings.soundMappings.getNewId(id), stringId -> BackwardsMappings.soundMappings.getNewId(stringId)); + SoundRewriter soundRewriter = new SoundRewriter(protocol); soundRewriter.registerSound(ClientboundPackets1_14.SOUND); soundRewriter.registerNamedSound(ClientboundPackets1_14.NAMED_SOUND); soundRewriter.registerStopSound(ClientboundPackets1_14.STOP_SOUND); @@ -34,7 +32,7 @@ public class SoundPackets1_14 extends Rewriter { wrapper.cancel(); int soundId = wrapper.read(Type.VAR_INT); - int newId = BackwardsMappings.soundMappings.getNewId(soundId); + int newId = protocol.getMappingData().getSoundMappings().getNewId(soundId); if (newId == -1) return; int category = wrapper.read(Type.VAR_INT); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13to1_13_1/Protocol1_13To1_13_1.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13to1_13_1/Protocol1_13To1_13_1.java index d698e809..60ec3c3f 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13to1_13_1/Protocol1_13To1_13_1.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13to1_13_1/Protocol1_13To1_13_1.java @@ -1,6 +1,7 @@ package nl.matsv.viabackwards.protocol.protocol1_13to1_13_1; import nl.matsv.viabackwards.api.BackwardsProtocol; +import nl.matsv.viabackwards.api.data.BackwardsMappings; import nl.matsv.viabackwards.api.entities.storage.EntityTracker; import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter; import nl.matsv.viabackwards.protocol.protocol1_13to1_13_1.packets.EntityPackets1_13_1; @@ -14,18 +15,23 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.ValueTransformer; import us.myles.ViaVersion.api.rewriters.TagRewriter; 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_9_3to1_9_1_2.storage.ClientWorld; public class Protocol1_13To1_13_1 extends BackwardsProtocol { + public static final BackwardsMappings MAPPINGS = new BackwardsMappings("1.13.2", "1.13", Protocol1_13_1To1_13.class, true); + public Protocol1_13To1_13_1() { super(ClientboundPackets1_13.class, ClientboundPackets1_13.class, ServerboundPackets1_13.class, ServerboundPackets1_13.class); } @Override protected void registerPackets() { + executeAsyncAfterLoaded(Protocol1_13_1To1_13.class, MAPPINGS::loadVBMappings); + new EntityPackets1_13_1(this).register(); InventoryPackets1_13_1.register(this); WorldPackets1_13_1.register(this); @@ -161,42 +167,7 @@ public class Protocol1_13To1_13_1 extends BackwardsProtocol 8590) { - blockId -= 17; - } else if (blockId > 8588) { - blockId = 8573; - } else if (blockId > 8479) { - blockId -= 16; - } else if (blockId > 8469 && blockId % 2 == 0) { - if (blockId % 2 == 0) { - blockId = 8459 + (blockId - 8470) / 2; - } else { - blockId = 0; //TODO replace new blocks - } - } else if (blockId > 8463) { - blockId = 0; //TODO replace new blocks - } else if (blockId > 1127) { - blockId -= 1; - } else if (blockId == 1127) { - blockId = 1126; - } - - return blockId; - } - - public static int getNewBlockId(int blockId) { - if (blockId > 565) { - blockId -= 5; - } else if (blockId > 561) { - blockId = 0; // Replacements not needed - } - - return blockId; + new TagRewriter(this, null).register(ClientboundPackets1_13.TAGS); } @Override @@ -208,7 +179,13 @@ public class Protocol1_13To1_13_1 extends BackwardsProtocol= 443) { - return itemId + 5; - } - return itemId; + item.setIdentifier(Protocol1_13To1_13_1.MAPPINGS.getNewItemId(item.getIdentifier())); } public static void toServer(Item item) { if (item == null) return; - item.setIdentifier(getNewItemId(item.getIdentifier())); - } - - // 1.13 Item Id - public static int getOldItemId(int newId) { - if (newId >= 448) { - return newId - 5; - } - return newId; + item.setIdentifier(Protocol1_13To1_13_1.MAPPINGS.getOldItemId(item.getIdentifier())); } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13to1_13_1/packets/WorldPackets1_13_1.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13to1_13_1/packets/WorldPackets1_13_1.java index d1e6c153..79d1ae77 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13to1_13_1/packets/WorldPackets1_13_1.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13to1_13_1/packets/WorldPackets1_13_1.java @@ -1,6 +1,5 @@ package nl.matsv.viabackwards.protocol.protocol1_13to1_13_1.packets; -import nl.matsv.viabackwards.protocol.protocol1_13to1_13_1.Protocol1_13To1_13_1; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; @@ -16,7 +15,7 @@ import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; public class WorldPackets1_13_1 { public static void register(Protocol protocol) { - BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION, Protocol1_13To1_13_1::getNewBlockStateId, Protocol1_13To1_13_1::getNewBlockId); + BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION); protocol.registerOutgoing(ClientboundPackets1_13.CHUNK_DATA, new PacketRemapper() { @Override @@ -30,7 +29,7 @@ public class WorldPackets1_13_1 { for (ChunkSection section : chunk.getSections()) { if (section != null) { for (int i = 0; i < section.getPaletteSize(); i++) { - section.setPaletteEntry(i, Protocol1_13To1_13_1.getNewBlockStateId(section.getPaletteEntry(i))); + section.setPaletteEntry(i, protocol.getMappingData().getNewBlockStateId(section.getPaletteEntry(i))); } } } @@ -42,7 +41,7 @@ public class WorldPackets1_13_1 { 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, InventoryPackets1_13_1::getOldItemId); + blockRewriter.registerEffect(ClientboundPackets1_13.EFFECT, 1010, 2001); blockRewriter.registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, 3, 20, 27, InventoryPackets1_13_1::toClient, Type.FLAT_ITEM, Type.FLOAT); } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java index a5bc4d16..f8f391a0 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java @@ -1,11 +1,10 @@ package nl.matsv.viabackwards.protocol.protocol1_14_4to1_15; -import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.BackwardsProtocol; +import nl.matsv.viabackwards.api.data.BackwardsMappings; import nl.matsv.viabackwards.api.entities.storage.EntityTracker; import nl.matsv.viabackwards.api.rewriters.SoundRewriter; import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter; -import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.EntityTypeMapping; import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.ImmediateRespawn; import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.packets.BlockItemPackets1_15; @@ -20,10 +19,10 @@ 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_15to1_14_4.ClientboundPackets1_15; import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4; -import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.data.MappingData; public class Protocol1_14_4To1_15 extends BackwardsProtocol { + public static final BackwardsMappings MAPPINGS = new BackwardsMappings("1.15", "1.14", Protocol1_15To1_14_4.class, true); private BlockItemPackets1_15 blockItemPackets; public Protocol1_14_4To1_15() { @@ -32,7 +31,7 @@ public class Protocol1_14_4To1_15 extends BackwardsProtocol BackwardsMappings.soundMappings.getNewId(id), stringId -> BackwardsMappings.soundMappings.getNewId(stringId)); + SoundRewriter soundRewriter = new SoundRewriter(this); soundRewriter.registerSound(ClientboundPackets1_15.SOUND); soundRewriter.registerSound(ClientboundPackets1_15.ENTITY_SOUND); soundRewriter.registerNamedSound(ClientboundPackets1_15.NAMED_SOUND); @@ -79,29 +77,9 @@ public class Protocol1_14_4To1_15 extends BackwardsProtocol BackwardsMappings.blockMappings.getNewId(id), - id -> MappingData.oldToNewItems.inverse().get(id), EntityTypeMapping::getOldEntityId).register(ClientboundPackets1_15.TAGS); + new TagRewriter(this, EntityTypeMapping::getOldEntityId).register(ClientboundPackets1_15.TAGS); - new StatisticsRewriter(this, id -> BackwardsMappings.blockMappings.getNewId(id), id -> MappingData.oldToNewItems.inverse().get(id), - EntityTypeMapping::getOldEntityId, id -> BackwardsMappings.statisticsMappings.getNewId(id)).register(ClientboundPackets1_15.STATISTICS); - } - - public static int getNewBlockStateId(int id) { - int newId = BackwardsMappings.blockStateMappings.getNewId(id); - if (newId == -1) { - ViaBackwards.getPlatform().getLogger().warning("Missing 1.15 blockstate id for 1.14.4 block " + id); - return 0; - } - return newId; - } - - public static int getNewBlockId(int id) { - int newId = BackwardsMappings.blockMappings.getNewId(id); - if (newId == -1) { - ViaBackwards.getPlatform().getLogger().warning("Missing 1.15 block id for 1.14.4 block " + id); - return id; - } - return newId; + new StatisticsRewriter(this, EntityTypeMapping::getOldEntityId).register(ClientboundPackets1_15.STATISTICS); } @Override @@ -118,4 +96,9 @@ public class Protocol1_14_4To1_15 extends BackwardsProtocol 1.14.4 mappings..."); - JsonObject mapping1_14 = MappingDataLoader.getMappingsCache().get("mapping-1.14.json"); - JsonObject mapping1_15 = MappingDataLoader.getMappingsCache().get("mapping-1.15.json"); - JsonObject mapping1_14to1_15 = VBMappingDataLoader.loadFromDataDir("mapping-1.14.4to1.15.json"); - - blockStateMappings = new VBMappings(mapping1_15.getAsJsonObject("blockstates"), mapping1_14.getAsJsonObject("blockstates"), mapping1_14to1_15.getAsJsonObject("blockstates")); - blockMappings = new VBMappings(mapping1_15.getAsJsonObject("blocks"), mapping1_14.getAsJsonObject("blocks"), mapping1_14to1_15.getAsJsonObject("blocks"), false); - itemMappings = new VBItemMappings(mapping1_15.getAsJsonObject("items"), mapping1_14.getAsJsonObject("items"), mapping1_14to1_15.getAsJsonObject("items")); - soundMappings = new VBSoundMappings(mapping1_15.getAsJsonArray("sounds"), mapping1_14.getAsJsonArray("sounds"), mapping1_14to1_15.getAsJsonObject("sounds")); - statisticsMappings = new Mappings(mapping1_15.getAsJsonArray("statistics"), mapping1_14.getAsJsonArray("statistics"), false); - } -} \ No newline at end of file diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/BlockItemPackets1_15.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/BlockItemPackets1_15.java index f8cbac38..0633ee58 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/BlockItemPackets1_15.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/BlockItemPackets1_15.java @@ -1,9 +1,7 @@ package nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.packets; -import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter; import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.Protocol1_14_4To1_15; -import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.ParticleMapping; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; @@ -18,19 +16,18 @@ 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_14to1_13_2.types.Chunk1_14Type; import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15; -import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.types.Chunk1_15Type; public class BlockItemPackets1_15 extends nl.matsv.viabackwards.api.rewriters.ItemRewriter { public BlockItemPackets1_15(Protocol1_14_4To1_15 protocol, TranslatableRewriter translatableRewriter) { - super(protocol, translatableRewriter, BlockItemPackets1_15::getOldItemId, BlockItemPackets1_15::getNewItemId, id -> BackwardsMappings.itemMappings.getMappedItem(id)); + super(protocol, translatableRewriter); } @Override protected void registerPackets() { ItemRewriter itemRewriter = new ItemRewriter(protocol, this::handleItemToClient, this::handleItemToServer); - BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14, Protocol1_14_4To1_15::getNewBlockStateId, Protocol1_14_4To1_15::getNewBlockId); + BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14); new RecipeRewriter1_14(protocol, this::handleItemToClient).registerDefaultHandler(ClientboundPackets1_15.DECLARE_RECIPES); @@ -41,7 +38,7 @@ public class BlockItemPackets1_15 extends nl.matsv.viabackwards.api.rewriters.It } }); - itemRewriter.registerSetCooldown(ClientboundPackets1_15.COOLDOWN, BlockItemPackets1_15::getOldItemId); + itemRewriter.registerSetCooldown(ClientboundPackets1_15.COOLDOWN); itemRewriter.registerWindowItems(ClientboundPackets1_15.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY); itemRewriter.registerSetSlot(ClientboundPackets1_15.SET_SLOT, Type.FLAT_VAR_INT_ITEM); itemRewriter.registerTradeList(ClientboundPackets1_15.TRADE_LIST, Type.FLAT_VAR_INT_ITEM); @@ -92,7 +89,7 @@ public class BlockItemPackets1_15 extends nl.matsv.viabackwards.api.rewriters.It if (section == null) continue; for (int j = 0; j < section.getPaletteSize(); j++) { int old = section.getPaletteEntry(j); - int newId = Protocol1_14_4To1_15.getNewBlockStateId(old); + int newId = protocol.getMappingData().getNewBlockStateId(old); section.setPaletteEntry(j, newId); } } @@ -101,7 +98,7 @@ public class BlockItemPackets1_15 extends nl.matsv.viabackwards.api.rewriters.It } }); - blockRewriter.registerEffect(ClientboundPackets1_15.EFFECT, 1010, 2001, BlockItemPackets1_15::getOldItemId); + blockRewriter.registerEffect(ClientboundPackets1_15.EFFECT, 1010, 2001); protocol.registerOutgoing(ClientboundPackets1_15.SPAWN_PARTICLE, new PacketRemapper() { @Override @@ -127,7 +124,7 @@ public class BlockItemPackets1_15 extends nl.matsv.viabackwards.api.rewriters.It if (id == 3 || id == 23) { int data = wrapper.passthrough(Type.VAR_INT); - wrapper.set(Type.VAR_INT, 0, Protocol1_14_4To1_15.getNewBlockStateId(data)); + wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data)); } else if (id == 32) { Item item = handleItemToClient(wrapper.read(Type.FLAT_VAR_INT_ITEM)); wrapper.write(Type.FLAT_VAR_INT_ITEM, item); @@ -137,23 +134,4 @@ public class BlockItemPackets1_15 extends nl.matsv.viabackwards.api.rewriters.It } }); } - - public static int getNewItemId(int id) { - int newId = MappingData.oldToNewItems.get(id); - if (newId == -1) { - ViaBackwards.getPlatform().getLogger().warning("Missing 1.15 item for 1.14.4 item " + id); - return 1; - } - return newId; - } - - - public static int getOldItemId(int id) { - int oldId = MappingData.oldToNewItems.inverse().get(id); - if (oldId == -1) { - ViaBackwards.getPlatform().getLogger().warning("Missing 1.14.4 item for 1.15 item " + id); - return 1; - } - return oldId; - } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/EntityPackets1_15.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/EntityPackets1_15.java index 68820e08..fdb5e1fc 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/EntityPackets1_15.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/EntityPackets1_15.java @@ -58,7 +58,7 @@ public class EntityPackets1_15 extends EntityRewriter { } }); - registerSpawnTrackerWithData(ClientboundPackets1_15.SPAWN_ENTITY, Entity1_15Types.EntityType.FALLING_BLOCK, Protocol1_14_4To1_15::getNewBlockStateId); + registerSpawnTrackerWithData(ClientboundPackets1_15.SPAWN_ENTITY, Entity1_15Types.EntityType.FALLING_BLOCK); protocol.registerOutgoing(ClientboundPackets1_15.SPAWN_MOB, new PacketRemapper() { @Override @@ -191,7 +191,7 @@ public class EntityPackets1_15 extends EntityRewriter { meta.setValue(protocol.getBlockItemPackets().handleItemToClient(item)); } else if (type == MetaType1_14.BlockID) { int blockstate = (int) meta.getValue(); - meta.setValue(Protocol1_14_4To1_15.getNewBlockStateId(blockstate)); + meta.setValue(protocol.getMappingData().getNewBlockStateId(blockstate)); } else if (type == MetaType1_14.PARTICLE) { Particle particle = (Particle) meta.getValue(); particle.setId(ParticleMapping.getOldId(particle.getId())); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java index d7d7e136..4047575f 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java @@ -1,6 +1,5 @@ package nl.matsv.viabackwards.protocol.protocol1_15_2to1_16; -import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.BackwardsProtocol; import nl.matsv.viabackwards.api.entities.storage.EntityTracker; import nl.matsv.viabackwards.api.rewriters.SoundRewriter; @@ -21,7 +20,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_9_3to1_9_1_2.storage.ClientWorld; import us.myles.ViaVersion.util.GsonUtil; import us.myles.viaversion.libs.gson.JsonElement; @@ -31,6 +29,7 @@ import java.util.UUID; public class Protocol1_15_2To1_16 extends BackwardsProtocol { + public static final BackwardsMappings MAPPINGS = new BackwardsMappings(); private BlockItemPackets1_16 blockItemPackets; private TranslatableRewriter translatableRewriter; @@ -40,7 +39,7 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol BackwardsMappings.soundMappings.getNewId(id), stringId -> BackwardsMappings.soundMappings.getNewId(stringId)); + SoundRewriter soundRewriter = new SoundRewriter(this); soundRewriter.registerSound(ClientboundPackets1_16.SOUND); soundRewriter.registerSound(ClientboundPackets1_16.ENTITY_SOUND); soundRewriter.registerNamedSound(ClientboundPackets1_16.NAMED_SOUND); @@ -114,11 +112,9 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol BackwardsMappings.blockMappings.getNewId(id), id -> - MappingData.oldToNewItems.inverse().get(id), entityPackets::getOldEntityId).register(ClientboundPackets1_16.TAGS); + new TagRewriter(this, entityPackets::getOldEntityId).register(ClientboundPackets1_16.TAGS); - new StatisticsRewriter(this, id -> BackwardsMappings.blockMappings.getNewId(id), id -> MappingData.oldToNewItems.inverse().get(id), - entityPackets::getOldEntityId, id -> BackwardsMappings.statisticsMappings.getNewId(id)).register(ClientboundPackets1_16.STATISTICS); + new StatisticsRewriter(this, entityPackets::getOldEntityId).register(ClientboundPackets1_16.STATISTICS); registerIncoming(ServerboundPackets1_14.ENTITY_ACTION, new PacketRemapper() { @Override @@ -175,24 +171,6 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol attributeMappings = new HashMap<>(); +public class BackwardsMappings extends nl.matsv.viabackwards.api.data.BackwardsMappings { + private final Map attributeMappings = new HashMap<>(); - public static void init() { - ViaBackwards.getPlatform().getLogger().info("Loading 1.16 -> 1.15.2 mappings..."); - JsonObject mapping1_15 = MappingDataLoader.getMappingsCache().get("mapping-1.15.json"); - JsonObject mapping1_16 = MappingDataLoader.getMappingsCache().get("mapping-1.16.json"); - JsonObject mapping1_15to1_16 = VBMappingDataLoader.loadFromDataDir("mapping-1.15to1.16.json"); + public BackwardsMappings() { + super("1.16", "1.15", Protocol1_16To1_15_2.class, true); + } - blockStateMappings = new VBMappings(mapping1_16.getAsJsonObject("blockstates"), mapping1_15.getAsJsonObject("blockstates"), mapping1_15to1_16.getAsJsonObject("blockstates")); - blockMappings = new VBMappings(mapping1_16.getAsJsonObject("blocks"), mapping1_15.getAsJsonObject("blocks"), mapping1_15to1_16.getAsJsonObject("blocks"), false); - itemMappings = new VBItemMappings(mapping1_16.getAsJsonObject("items"), mapping1_15.getAsJsonObject("items"), mapping1_15to1_16.getAsJsonObject("items")); - soundMappings = new VBSoundMappings(mapping1_16.getAsJsonArray("sounds"), mapping1_15.getAsJsonArray("sounds"), mapping1_15to1_16.getAsJsonObject("sounds")); - statisticsMappings = new Mappings(mapping1_16.getAsJsonArray("statistics"), mapping1_15.getAsJsonArray("statistics"), false); - - for (Map.Entry entry : MappingData.attributeMappings.entrySet()) { + @Override + protected void loadVBExtras(JsonObject oldMappings, JsonObject newMappings) { + for (Map.Entry entry : Protocol1_16To1_15_2.MAPPINGS.getAttributeMappings().entrySet()) { attributeMappings.put(entry.getValue(), entry.getKey()); } } + + public Map getAttributeMappings() { + return attributeMappings; + } } \ No newline at end of file diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java index c5ebefbf..aca90795 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java @@ -1,10 +1,8 @@ package nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.packets; -import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.rewriters.EnchantmentRewriter; import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter; import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16; -import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.data.MapColorRewriter; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.minecraft.Position; @@ -21,7 +19,6 @@ import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_1 import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15; import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.types.Chunk1_15Type; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; -import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.types.Chunk1_16Type; import us.myles.ViaVersion.util.CompactArrayUtil; @@ -40,14 +37,13 @@ public class BlockItemPackets1_16 extends nl.matsv.viabackwards.api.rewriters.It private EnchantmentRewriter enchantmentRewriter; public BlockItemPackets1_16(Protocol1_15_2To1_16 protocol, TranslatableRewriter translatableRewriter) { - super(protocol, translatableRewriter, - BlockItemPackets1_16::getOldItemId, BlockItemPackets1_16::getNewItemId, id -> BackwardsMappings.itemMappings.getMappedItem(id)); + super(protocol, translatableRewriter); } @Override protected void registerPackets() { ItemRewriter itemRewriter = new ItemRewriter(protocol, this::handleItemToClient, this::handleItemToServer); - BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14, Protocol1_15_2To1_16::getNewBlockStateId, Protocol1_15_2To1_16::getNewBlockId); + BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14); RecipeRewriter1_14 recipeRewriter = new RecipeRewriter1_14(protocol, this::handleItemToClient); // Remove new smithing type, only in this handler @@ -80,7 +76,7 @@ public class BlockItemPackets1_16 extends nl.matsv.viabackwards.api.rewriters.It } }); - itemRewriter.registerSetCooldown(ClientboundPackets1_16.COOLDOWN, BlockItemPackets1_16::getOldItemId); + itemRewriter.registerSetCooldown(ClientboundPackets1_16.COOLDOWN); itemRewriter.registerWindowItems(ClientboundPackets1_16.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY); itemRewriter.registerSetSlot(ClientboundPackets1_16.SET_SLOT, Type.FLAT_VAR_INT_ITEM); itemRewriter.registerTradeList(ClientboundPackets1_16.TRADE_LIST, Type.FLAT_VAR_INT_ITEM); @@ -145,7 +141,7 @@ public class BlockItemPackets1_16 extends nl.matsv.viabackwards.api.rewriters.It if (section == null) continue; for (int j = 0; j < section.getPaletteSize(); j++) { int old = section.getPaletteEntry(j); - section.setPaletteEntry(j, Protocol1_15_2To1_16.getNewBlockStateId(old)); + section.setPaletteEntry(j, protocol.getMappingData().getNewBlockStateId(old)); } } @@ -179,7 +175,7 @@ public class BlockItemPackets1_16 extends nl.matsv.viabackwards.api.rewriters.It } }); - blockRewriter.registerEffect(ClientboundPackets1_16.EFFECT, 1010, 2001, BlockItemPackets1_16::getOldItemId); + blockRewriter.registerEffect(ClientboundPackets1_16.EFFECT, 1010, 2001); blockRewriter.registerSpawnParticle(ClientboundPackets1_16.SPAWN_PARTICLE, 3, 23, 34, BlockItemPackets1_16::getNewParticleId, this::handleItemToClient, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE); @@ -375,24 +371,6 @@ public class BlockItemPackets1_16 extends nl.matsv.viabackwards.api.rewriters.It return item; } - public static int getNewItemId(int id) { - int newId = MappingData.oldToNewItems.get(id); - if (newId == -1) { - ViaBackwards.getPlatform().getLogger().warning("Missing 1.16 item for 1.15 item " + id); - return 1; - } - return newId; - } - - public static int getOldItemId(int id) { - int oldId = MappingData.oldToNewItems.inverse().get(id); - if (oldId == -1) { - ViaBackwards.getPlatform().getLogger().warning("Missing 1.15 item for 1.16 item " + id); - return 1; - } - return oldId; - } - private static final class EquipmentData { private final int slot; private final Item item; diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java index 5cde4f4b..a00480a8 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java @@ -3,7 +3,6 @@ package nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.packets; import nl.matsv.viabackwards.api.rewriters.EntityRewriter; import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.ParticleMapping; import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16; -import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.data.BackwardsMappings; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.entities.Entity1_15Types; import us.myles.ViaVersion.api.entities.Entity1_16Types; @@ -45,7 +44,7 @@ public class EntityPackets1_16 extends EntityRewriter { @Override protected void registerPackets() { - registerSpawnTrackerWithData(ClientboundPackets1_16.SPAWN_ENTITY, Entity1_16Types.EntityType.FALLING_BLOCK, Protocol1_15_2To1_16::getNewBlockStateId); + registerSpawnTrackerWithData(ClientboundPackets1_16.SPAWN_ENTITY, Entity1_16Types.EntityType.FALLING_BLOCK); registerSpawnTracker(ClientboundPackets1_16.SPAWN_MOB); protocol.registerOutgoing(ClientboundPackets1_16.RESPAWN, new PacketRemapper() { @@ -128,7 +127,7 @@ public class EntityPackets1_16 extends EntityRewriter { int size = wrapper.passthrough(Type.INT); for (int i = 0; i < size; i++) { String attributeIdentifier = wrapper.read(Type.STRING); - String oldKey = BackwardsMappings.attributeMappings.get(attributeIdentifier); + String oldKey = protocol.getMappingData().getAttributeMappings().get(attributeIdentifier); wrapper.write(Type.STRING, oldKey != null ? oldKey : attributeIdentifier.replace("minecraft:", "")); wrapper.passthrough(Type.DOUBLE); @@ -191,7 +190,7 @@ public class EntityPackets1_16 extends EntityRewriter { if (type == MetaType1_14.Slot) { meta.setValue(protocol.getBlockItemPackets().handleItemToClient((Item) meta.getValue())); } else if (type == MetaType1_14.BlockID) { - meta.setValue(Protocol1_15_2To1_16.getNewBlockStateId((int) meta.getValue())); + meta.setValue(protocol.getMappingData().getNewBlockStateId((int) meta.getValue())); } else if (type == MetaType1_14.PARTICLE) { Particle particle = (Particle) meta.getValue(); particle.setId(ParticleMapping.getOldId(particle.getId())); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_16_1to1_16_2/Protocol1_16_1To1_16_2.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_16_1to1_16_2/Protocol1_16_1To1_16_2.java index a91fe2dc..a1b8e180 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_16_1to1_16_2/Protocol1_16_1To1_16_2.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_16_1to1_16_2/Protocol1_16_1To1_16_2.java @@ -1,11 +1,10 @@ package nl.matsv.viabackwards.protocol.protocol1_16_1to1_16_2; -import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.BackwardsProtocol; +import nl.matsv.viabackwards.api.data.BackwardsMappings; import nl.matsv.viabackwards.api.entities.storage.EntityTracker; import nl.matsv.viabackwards.api.rewriters.SoundRewriter; import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter; -import nl.matsv.viabackwards.protocol.protocol1_16_1to1_16_2.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_16_1to1_16_2.packets.BlockItemPackets1_16_2; import nl.matsv.viabackwards.protocol.protocol1_16_1to1_16_2.packets.EntityPackets1_16_2; import us.myles.ViaVersion.api.PacketWrapper; @@ -18,12 +17,12 @@ import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2; 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.ServerboundPackets1_16; public class Protocol1_16_1To1_16_2 extends BackwardsProtocol { + public static final BackwardsMappings MAPPINGS = new BackwardsMappings("1.16.2", "1.16", Protocol1_16_2To1_16_1.class, true); private BlockItemPackets1_16_2 blockItemPackets; private TranslatableRewriter translatableRewriter; @@ -33,7 +32,7 @@ public class Protocol1_16_1To1_16_2 extends BackwardsProtocol BackwardsMappings.soundMappings.getNewId(id), stringId -> BackwardsMappings.soundMappings.getNewId(stringId)); + SoundRewriter soundRewriter = new SoundRewriter(this); soundRewriter.registerSound(ClientboundPackets1_16_2.SOUND); soundRewriter.registerSound(ClientboundPackets1_16_2.ENTITY_SOUND); soundRewriter.registerNamedSound(ClientboundPackets1_16_2.NAMED_SOUND); @@ -92,29 +90,9 @@ public class Protocol1_16_1To1_16_2 extends BackwardsProtocol BackwardsMappings.blockMappings.getNewId(id), id -> - MappingData.oldToNewItems.inverse().get(id), entityPackets::getOldEntityId).register(ClientboundPackets1_16_2.TAGS); + new TagRewriter(this, entityPackets::getOldEntityId).register(ClientboundPackets1_16_2.TAGS); - new StatisticsRewriter(this, id -> BackwardsMappings.blockMappings.getNewId(id), id -> - MappingData.oldToNewItems.inverse().get(id), entityPackets::getOldEntityId).register(ClientboundPackets1_16_2.STATISTICS); - } - - public static int getNewBlockStateId(int id) { - int newId = BackwardsMappings.blockStateMappings.getNewId(id); - if (newId == -1) { - ViaBackwards.getPlatform().getLogger().warning("Missing 1.16 blockstate id for 1.16.2 block " + id); - return 0; - } - return newId; - } - - public static int getNewBlockId(int id) { - int newId = BackwardsMappings.blockMappings.getNewId(id); - if (newId == -1) { - ViaBackwards.getPlatform().getLogger().warning("Missing 1.16 block id for 1.16.2 block " + id); - return id; - } - return newId; + new StatisticsRewriter(this, entityPackets::getOldEntityId).register(ClientboundPackets1_16_2.STATISTICS); } @Override @@ -132,4 +110,9 @@ public class Protocol1_16_1To1_16_2 extends BackwardsProtocol 1.16.1 mappings..."); - JsonObject mapping1_16 = MappingDataLoader.getMappingsCache().get("mapping-1.16.json"); - JsonObject mapping1_16_2 = MappingDataLoader.getMappingsCache().get("mapping-1.16.2.json"); - JsonObject mapping1_16to1_16_2 = VBMappingDataLoader.loadFromDataDir("mapping-1.16to1.16.2.json"); - - blockStateMappings = new VBMappings(mapping1_16_2.getAsJsonObject("blockstates"), mapping1_16.getAsJsonObject("blockstates"), mapping1_16to1_16_2.getAsJsonObject("blockstates")); - blockMappings = new VBMappings(mapping1_16_2.getAsJsonObject("blocks"), mapping1_16.getAsJsonObject("blocks"), mapping1_16to1_16_2.getAsJsonObject("blocks"), false); - itemMappings = new VBItemMappings(mapping1_16_2.getAsJsonObject("items"), mapping1_16.getAsJsonObject("items"), mapping1_16to1_16_2.getAsJsonObject("items")); - soundMappings = new VBSoundMappings(mapping1_16_2.getAsJsonArray("sounds"), mapping1_16.getAsJsonArray("sounds"), mapping1_16to1_16_2.getAsJsonObject("sounds")); - } -} \ No newline at end of file diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_16_1to1_16_2/packets/BlockItemPackets1_16_2.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_16_1to1_16_2/packets/BlockItemPackets1_16_2.java index 20aae21e..81d3c2d0 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_16_1to1_16_2/packets/BlockItemPackets1_16_2.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_16_1to1_16_2/packets/BlockItemPackets1_16_2.java @@ -1,9 +1,7 @@ package nl.matsv.viabackwards.protocol.protocol1_16_1to1_16_2.packets; -import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter; import nl.matsv.viabackwards.protocol.protocol1_16_1to1_16_2.Protocol1_16_1To1_16_2; -import nl.matsv.viabackwards.protocol.protocol1_16_1to1_16_2.data.BackwardsMappings; import us.myles.ViaVersion.api.minecraft.BlockChangeRecord; import us.myles.ViaVersion.api.minecraft.BlockChangeRecord1_8; import us.myles.ViaVersion.api.minecraft.Position; @@ -14,7 +12,6 @@ import us.myles.ViaVersion.api.rewriters.BlockRewriter; import us.myles.ViaVersion.api.rewriters.ItemRewriter; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2; -import us.myles.ViaVersion.protocols.protocol1_16_2to1_16_1.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16; @@ -29,18 +26,17 @@ import us.myles.viaversion.libs.opennbt.tag.builtin.Tag; public class BlockItemPackets1_16_2 extends nl.matsv.viabackwards.api.rewriters.ItemRewriter { public BlockItemPackets1_16_2(Protocol1_16_1To1_16_2 protocol, TranslatableRewriter translatableRewriter) { - super(protocol, translatableRewriter, - BlockItemPackets1_16_2::getOldItemId, BlockItemPackets1_16_2::getNewItemId, id -> BackwardsMappings.itemMappings.getMappedItem(id)); + super(protocol, translatableRewriter); } @Override protected void registerPackets() { ItemRewriter itemRewriter = new ItemRewriter(protocol, this::handleItemToClient, this::handleItemToServer); - BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14, Protocol1_16_1To1_16_2::getNewBlockStateId, Protocol1_16_1To1_16_2::getNewBlockId); + BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14); new RecipeRewriter1_16(protocol, this::handleItemToClient).registerDefaultHandler(ClientboundPackets1_16_2.DECLARE_RECIPES); - itemRewriter.registerSetCooldown(ClientboundPackets1_16_2.COOLDOWN, BlockItemPackets1_16_2::getOldItemId); + itemRewriter.registerSetCooldown(ClientboundPackets1_16_2.COOLDOWN); itemRewriter.registerWindowItems(ClientboundPackets1_16_2.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY); itemRewriter.registerSetSlot(ClientboundPackets1_16_2.SET_SLOT, Type.FLAT_VAR_INT_ITEM); itemRewriter.registerEntityEquipmentArray(ClientboundPackets1_16_2.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM); @@ -82,7 +78,7 @@ public class BlockItemPackets1_16_2 extends nl.matsv.viabackwards.api.rewriters. if (section == null) continue; for (int j = 0; j < section.getPaletteSize(); j++) { int old = section.getPaletteEntry(j); - section.setPaletteEntry(j, Protocol1_16_1To1_16_2.getNewBlockStateId(old)); + section.setPaletteEntry(j, protocol.getMappingData().getNewBlockStateId(old)); } } @@ -92,7 +88,6 @@ public class BlockItemPackets1_16_2 extends nl.matsv.viabackwards.api.rewriters. IntTag x = blockEntity.get("x"); IntTag y = blockEntity.get("y"); IntTag z = blockEntity.get("z"); - if (x != null && y != null && z != null) { handleBlockEntity(blockEntity, new Position(x.getValue(), y.getValue().shortValue(), z.getValue())); } @@ -129,7 +124,7 @@ public class BlockItemPackets1_16_2 extends nl.matsv.viabackwards.api.rewriters. wrapper.write(Type.BLOCK_CHANGE_RECORD_ARRAY, blockChangeRecord); for (int i = 0; i < blockChangeRecord.length; i++) { BlockChangeRecord record = blockChangeRecord[i]; - int blockId = Protocol1_16_1To1_16_2.getNewBlockStateId(record.getBlockId()); + int blockId = protocol.getMappingData().getNewBlockStateId(record.getBlockId()); // Relative y -> absolute y blockChangeRecord[i] = new BlockChangeRecord1_8(record.getSectionX(), record.getY(chunkY), record.getSectionZ(), blockId); } @@ -137,7 +132,7 @@ public class BlockItemPackets1_16_2 extends nl.matsv.viabackwards.api.rewriters. } }); - blockRewriter.registerEffect(ClientboundPackets1_16_2.EFFECT, 1010, 2001, BlockItemPackets1_16_2::getOldItemId); + blockRewriter.registerEffect(ClientboundPackets1_16_2.EFFECT, 1010, 2001); blockRewriter.registerSpawnParticle(ClientboundPackets1_16_2.SPAWN_PARTICLE, 3, 23, 34, null, this::handleItemToClient, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE); @@ -177,22 +172,4 @@ public class BlockItemPackets1_16_2 extends nl.matsv.viabackwards.api.rewriters. skullOwnerCompoundTag.put(new IntArrayTag("Id", uuidIntArray)); } } - - public static int getNewItemId(int id) { - int newId = MappingData.oldToNewItems.get(id); - if (newId == -1) { - ViaBackwards.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); - if (oldId == -1) { - ViaBackwards.getPlatform().getLogger().warning("Missing 1.16 item for 1.16.2 item " + id); - return 1; - } - return oldId; - } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_16_1to1_16_2/packets/EntityPackets1_16_2.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_16_1to1_16_2/packets/EntityPackets1_16_2.java index a75c5e57..b509b8c3 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_16_1to1_16_2/packets/EntityPackets1_16_2.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_16_1to1_16_2/packets/EntityPackets1_16_2.java @@ -31,7 +31,7 @@ public class EntityPackets1_16_2 extends EntityRewriter @Override protected void registerPackets() { - registerSpawnTrackerWithData(ClientboundPackets1_16_2.SPAWN_ENTITY, Entity1_16_2Types.EntityType.FALLING_BLOCK, Protocol1_16_1To1_16_2::getNewBlockStateId); + registerSpawnTrackerWithData(ClientboundPackets1_16_2.SPAWN_ENTITY, Entity1_16_2Types.EntityType.FALLING_BLOCK); registerSpawnTracker(ClientboundPackets1_16_2.SPAWN_MOB); registerExtraTracker(ClientboundPackets1_16_2.SPAWN_EXPERIENCE_ORB, Entity1_16_2Types.EntityType.EXPERIENCE_ORB); registerExtraTracker(ClientboundPackets1_16_2.SPAWN_PAINTING, Entity1_16_2Types.EntityType.PAINTING); @@ -97,7 +97,7 @@ public class EntityPackets1_16_2 extends EntityRewriter if (type == MetaType1_14.Slot) { meta.setValue(protocol.getBlockItemPackets().handleItemToClient((Item) meta.getValue())); } else if (type == MetaType1_14.BlockID) { - meta.setValue(Protocol1_16_1To1_16_2.getNewBlockStateId((int) meta.getValue())); + meta.setValue(protocol.getMappingData().getNewBlockStateId((int) meta.getValue())); } else if (type == MetaType1_14.OptChat) { JsonElement text = meta.getCastedValue(); if (text != null) { diff --git a/core/src/main/resources/assets/viabackwards/data/mapping-1.12.2to1.13.json b/core/src/main/resources/assets/viabackwards/data/mapping-1.12to1.13.json similarity index 100% rename from core/src/main/resources/assets/viabackwards/data/mapping-1.12.2to1.13.json rename to core/src/main/resources/assets/viabackwards/data/mapping-1.12to1.13.json diff --git a/core/src/main/resources/assets/viabackwards/data/mapping-1.13to1.13.2.json b/core/src/main/resources/assets/viabackwards/data/mapping-1.13to1.13.2.json new file mode 100644 index 00000000..5f3ca8e8 --- /dev/null +++ b/core/src/main/resources/assets/viabackwards/data/mapping-1.13to1.13.2.json @@ -0,0 +1,20 @@ +{ + "blockstates": { + "minecraft:tnt[unstable=false]": "minecraft:tnt", + "minecraft:tnt[unstable=true]": "minecraft:tnt", + "minecraft:oak_sign": "minecraft:sign[", + "minecraft:oak_wall_sign": "minecraft:wall_sign[", + "minecraft:smooth_stone_slab": "minecraft:stone_slab[", + "minecraft:dead_tube_coral": "minecraft:tube_coral", + "minecraft:dead_brain_coral": "minecraft:brain_coral", + "minecraft:dead_bubble_coral": "minecraft:bubble_coral", + "minecraft:dead_fire_coral": "minecraft:fire_coral", + "minecraft:dead_horn_coral": "minecraft:horn_coral", + "minecraft:tube_coral": "minecraft:tube_coral", + "minecraft:brain_coral": "minecraft:brain_coral", + "minecraft:bubble_coral": "minecraft:bubble_coral", + "minecraft:fire_coral": "minecraft:fire_coral", + "minecraft:horn_coral": "minecraft:horn_coral", + "minecraft:conduit": "minecraft:conduit" + } +} \ No newline at end of file diff --git a/core/src/main/resources/assets/viabackwards/data/mapping-1.14.4to1.15.json b/core/src/main/resources/assets/viabackwards/data/mapping-1.14to1.15.json similarity index 100% rename from core/src/main/resources/assets/viabackwards/data/mapping-1.14.4to1.15.json rename to core/src/main/resources/assets/viabackwards/data/mapping-1.14to1.15.json diff --git a/fabric/pom.xml b/fabric/pom.xml index 947647ad..0e58de6c 100644 --- a/fabric/pom.xml +++ b/fabric/pom.xml @@ -16,7 +16,7 @@ viabackwards-parent nl.matsv - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT viabackwards-fabric diff --git a/pom.xml b/pom.xml index dea1c6f2..f3dd9f10 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ nl.matsv viabackwards-parent - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT pom Allow older clients to join newer server versions. @@ -65,7 +65,7 @@ us.myles viaversion - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT provided diff --git a/sponge/pom.xml b/sponge/pom.xml index da7f9b7b..89626aae 100644 --- a/sponge/pom.xml +++ b/sponge/pom.xml @@ -16,7 +16,7 @@ viabackwards-parent nl.matsv - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT viabackwards-sponge diff --git a/velocity/pom.xml b/velocity/pom.xml index 1361a25a..d87fe656 100644 --- a/velocity/pom.xml +++ b/velocity/pom.xml @@ -16,7 +16,7 @@ viabackwards-parent nl.matsv - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT viabackwards-velocity