From a99273db2e2b7c7c730f6a0162a43b8848489f34 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Thu, 18 Apr 2024 16:12:57 +0200 Subject: [PATCH] Always call onMappingDataLoaded in TagRewriter In case they are needed in the future, so that we don't also have to check whether the different methods are actually called --- .../api/protocol/AbstractProtocol.java | 4 +- .../viaversion/api/protocol/Protocol.java | 11 +++ .../api/rewriter/MappingDataListener.java | 29 +++++++ .../viaversion/api/rewriter/Rewriter.java | 5 +- .../viaversion/api/rewriter/TagRewriter.java | 77 +++++++++++++++++++ .../Protocol1_13_1To1_13.java | 8 +- .../Protocol1_13To1_12_2.java | 1 + .../Protocol1_14To1_13_2.java | 1 + .../Protocol1_15To1_14_4.java | 10 ++- .../Protocol1_16_2To1_16_1.java | 24 +++--- .../Protocol1_16To1_15_2.java | 10 ++- .../Protocol1_17To1_16_4.java | 7 +- .../Protocol1_18_2To1_18.java | 1 + .../Protocol1_18To1_17_1.java | 3 +- .../Protocol1_19_3To1_19_1.java | 8 +- .../Protocol1_19_4To1_19_3.java | 8 +- .../Protocol1_19To1_18_2.java | 7 +- .../Protocol1_20_2To1_20.java | 7 +- .../Protocol1_20_3To1_20_2.java | 7 +- .../Protocol1_20_5To1_20_3.java | 5 ++ .../Protocol1_20To1_19_4.java | 7 +- .../viaversion/rewriter/TagRewriter.java | 58 ++++---------- .../template/protocols/Protocol1_99To_98.java | 10 ++- 23 files changed, 236 insertions(+), 72 deletions(-) create mode 100644 api/src/main/java/com/viaversion/viaversion/api/rewriter/MappingDataListener.java create mode 100644 api/src/main/java/com/viaversion/viaversion/api/rewriter/TagRewriter.java diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java index 1a8e313d9..a7cd7da21 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java @@ -38,6 +38,7 @@ import com.viaversion.viaversion.api.protocol.packet.provider.PacketTypeMap; import com.viaversion.viaversion.api.protocol.packet.provider.PacketTypesProvider; import com.viaversion.viaversion.api.protocol.packet.provider.SimplePacketTypesProvider; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; +import com.viaversion.viaversion.api.rewriter.MappingDataListener; import com.viaversion.viaversion.api.rewriter.Rewriter; import com.viaversion.viaversion.exception.CancelException; import com.viaversion.viaversion.exception.InformativeException; @@ -211,6 +212,7 @@ public abstract class AbstractProtocol rewriter) { @@ -219,7 +221,7 @@ public abstract class AbstractProtocol rewriter) { + private void callOnMappingDataLoaded(@Nullable MappingDataListener rewriter) { if (rewriter != null) { rewriter.onMappingDataLoaded(); } diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/Protocol.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/Protocol.java index 58bc62ee0..a18ed43bd 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/Protocol.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/Protocol.java @@ -36,6 +36,8 @@ import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.rewriter.EntityRewriter; import com.viaversion.viaversion.api.rewriter.ItemRewriter; +import com.viaversion.viaversion.api.rewriter.Rewriter; +import com.viaversion.viaversion.api.rewriter.TagRewriter; import org.checkerframework.checker.nullness.qual.Nullable; /** @@ -350,6 +352,15 @@ public interface Protocol { +public interface Rewriter extends MappingDataListener { /** * Registers any packet handlers or rewrites needed. @@ -37,7 +37,4 @@ public interface Rewriter { * @return protocol of the rewriter */ T protocol(); - - default void onMappingDataLoaded() { - } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/rewriter/TagRewriter.java b/api/src/main/java/com/viaversion/viaversion/api/rewriter/TagRewriter.java new file mode 100644 index 000000000..9ea232f69 --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/rewriter/TagRewriter.java @@ -0,0 +1,77 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.rewriter; + +import com.viaversion.viaversion.api.minecraft.RegistryType; +import com.viaversion.viaversion.api.minecraft.TagData; +import com.viaversion.viaversion.api.minecraft.entities.EntityType; +import java.util.List; +import org.checkerframework.checker.nullness.qual.Nullable; + +public interface TagRewriter extends MappingDataListener { + + void removeTags(String registryKey); + + void renameTag(RegistryType type, String registryKey, String renameTo); + + /** + * Adds an empty tag (since the client crashes if a checked tag is not registered). + * + * @param tagType registry tag type + * @param tagId tag id + */ + void addEmptyTag(RegistryType tagType, String tagId); + + void addEmptyTags(RegistryType tagType, String... tagIds); + + /** + * Adds an entity tag type to be filled with the given entity type ids. + * + * @param tagId registry tag type + * @param entities mapped entity types + */ + void addEntityTag(String tagId, EntityType... entities); + + /** + * Adds a tag type to be filled with the given type ids after being mapped to new ids. + * + * @param tagType registry tag type + * @param tagId tag id + * @param unmappedIds unmapped type ids + */ + void addTag(RegistryType tagType, String tagId, int... unmappedIds); + + /** + * Adds a tag type to be filled with the given raw type ids. + * + * @param tagType registry tag type + * @param tagId tag id + * @param ids raw type ids + */ + void addTagRaw(RegistryType tagType, String tagId, int... ids); + + @Nullable + List getNewTags(RegistryType tagType); + + List getOrComputeNewTags(RegistryType tagType); +} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java index 85f0e8920..a8aa61b8d 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java @@ -44,6 +44,7 @@ public class Protocol1_13_1To1_13 extends AbstractProtocol tagRewriter = new TagRewriter<>(this); public Protocol1_13_1To1_13() { super(ClientboundPackets1_13.class, ClientboundPackets1_13.class, ServerboundPackets1_13.class, ServerboundPackets1_13.class); @@ -128,7 +129,7 @@ public class Protocol1_13_1To1_13 extends AbstractProtocol(this).register(ClientboundPackets1_13.TAGS, RegistryType.ITEM); + tagRewriter.register(ClientboundPackets1_13.TAGS, RegistryType.ITEM); new StatisticsRewriter<>(this).register(ClientboundPackets1_13.STATISTICS); } @@ -154,4 +155,9 @@ public class Protocol1_13_1To1_13 extends AbstractProtocol getTagRewriter() { + return tagRewriter; + } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java index 79c2f5484..81e6b46ee 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java @@ -843,6 +843,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol tagRewriter; + private final TagRewriter tagRewriter = new TagRewriter<>(this); public Protocol1_15To1_14_4() { super(ClientboundPackets1_14_4.class, ClientboundPackets1_15.class, ServerboundPackets1_14.class, ServerboundPackets1_14.class); @@ -61,12 +61,13 @@ public class Protocol1_15To1_14_4 extends AbstractProtocol itemRewriter.handleItemToServer(wrapper.passthrough(Type.ITEM1_13_2))); - tagRewriter = new TagRewriter<>(this); tagRewriter.register(ClientboundPackets1_14_4.TAGS, RegistryType.ENTITY); } @Override protected void onMappingDataLoaded() { + super.onMappingDataLoaded(); + int[] shulkerBoxes = new int[17]; int shulkerBoxOffset = 501; for (int i = 0; i < 17; i++) { @@ -94,4 +95,9 @@ public class Protocol1_15To1_14_4 extends AbstractProtocol getTagRewriter() { + return tagRewriter; + } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/Protocol1_16_2To1_16_1.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/Protocol1_16_2To1_16_1.java index e1332ff4b..d1043879a 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/Protocol1_16_2To1_16_1.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/Protocol1_16_2To1_16_1.java @@ -39,7 +39,7 @@ public class Protocol1_16_2To1_16_1 extends AbstractProtocol tagRewriter; + private final TagRewriter tagRewriter = new TagRewriter<>(this); public Protocol1_16_2To1_16_1() { super(ClientboundPackets1_16.class, ClientboundPackets1_16_2.class, ServerboundPackets1_16.class, ServerboundPackets1_16_2.class); @@ -52,7 +52,6 @@ public class Protocol1_16_2To1_16_1 extends AbstractProtocol(this); tagRewriter.register(ClientboundPackets1_16.TAGS, RegistryType.ENTITY); new StatisticsRewriter<>(this).register(ClientboundPackets1_16.STATISTICS); @@ -85,19 +84,21 @@ public class Protocol1_16_2To1_16_1 extends AbstractProtocol tagRewriter.addEmptyTags(RegistryType.ITEM, "minecraft:soul_fire_base_blocks", "minecraft:furnace_materials", "minecraft:crimson_stems", - "minecraft:gold_ores", "minecraft:piglin_loved", "minecraft:piglin_repellents", "minecraft:creeper_drop_music_discs", - "minecraft:logs_that_burn", "minecraft:stone_tool_materials", "minecraft:warped_stems"); + "minecraft:gold_ores", "minecraft:piglin_loved", "minecraft:piglin_repellents", "minecraft:creeper_drop_music_discs", + "minecraft:logs_that_burn", "minecraft:stone_tool_materials", "minecraft:warped_stems"); tagRewriter.addEmptyTags(RegistryType.BLOCK, "minecraft:infiniburn_nether", "minecraft:crimson_stems", - "minecraft:wither_summon_base_blocks", "minecraft:infiniburn_overworld", "minecraft:piglin_repellents", - "minecraft:hoglin_repellents", "minecraft:prevent_mob_spawning_inside", "minecraft:wart_blocks", - "minecraft:stone_pressure_plates", "minecraft:nylium", "minecraft:gold_ores", "minecraft:pressure_plates", - "minecraft:logs_that_burn", "minecraft:strider_warm_blocks", "minecraft:warped_stems", "minecraft:infiniburn_end", - "minecraft:base_stone_nether", "minecraft:base_stone_overworld"); + "minecraft:wither_summon_base_blocks", "minecraft:infiniburn_overworld", "minecraft:piglin_repellents", + "minecraft:hoglin_repellents", "minecraft:prevent_mob_spawning_inside", "minecraft:wart_blocks", + "minecraft:stone_pressure_plates", "minecraft:nylium", "minecraft:gold_ores", "minecraft:pressure_plates", + "minecraft:logs_that_burn", "minecraft:strider_warm_blocks", "minecraft:warped_stems", "minecraft:infiniburn_end", + "minecraft:base_stone_nether", "minecraft:base_stone_overworld"); } @Override @@ -119,4 +120,9 @@ public class Protocol1_16_2To1_16_1 extends AbstractProtocol getTagRewriter() { + return tagRewriter; + } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java index 711a16f7b..355ee403f 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java @@ -61,7 +61,7 @@ public class Protocol1_16To1_15_2 extends AbstractProtocol tagRewriter; + private final TagRewriter tagRewriter = new TagRewriter<>(this); public Protocol1_16To1_15_2() { super(ClientboundPackets1_15.class, ClientboundPackets1_16.class, ServerboundPackets1_14.class, ServerboundPackets1_16.class); @@ -74,7 +74,6 @@ public class Protocol1_16To1_15_2 extends AbstractProtocol(this); tagRewriter.register(ClientboundPackets1_15.TAGS, RegistryType.ENTITY); new StatisticsRewriter<>(this).register(ClientboundPackets1_15.STATISTICS); @@ -213,6 +212,8 @@ public class Protocol1_16To1_15_2 extends AbstractProtocol getTagRewriter() { + return tagRewriter; + } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/Protocol1_17To1_16_4.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/Protocol1_17To1_16_4.java index a9f135a9a..d27d7c858 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/Protocol1_17To1_16_4.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/Protocol1_17To1_16_4.java @@ -188,7 +188,7 @@ public final class Protocol1_17To1_16_4 extends AbstractProtocol getTagRewriter() { + return tagRewriter; + } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18_2to1_18/Protocol1_18_2To1_18.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18_2to1_18/Protocol1_18_2To1_18.java index 30ecdae7c..8d7f61e00 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18_2to1_18/Protocol1_18_2To1_18.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18_2to1_18/Protocol1_18_2To1_18.java @@ -31,6 +31,7 @@ import com.viaversion.viaversion.rewriter.TagRewriter; public final class Protocol1_18_2To1_18 extends AbstractProtocol { + public Protocol1_18_2To1_18() { super(ClientboundPackets1_18.class, ClientboundPackets1_18.class, ServerboundPackets1_17.class, ServerboundPackets1_17.class); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/Protocol1_18To1_17_1.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/Protocol1_18To1_17_1.java index ed1aa7359..e2013a3b7 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/Protocol1_18To1_17_1.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/Protocol1_18To1_17_1.java @@ -43,6 +43,7 @@ public final class Protocol1_18To1_17_1 extends AbstractProtocol tagRewriter = new TagRewriter<>(this); public Protocol1_18To1_17_1() { super(ClientboundPackets1_17_1.class, ClientboundPackets1_18.class, ServerboundPackets1_17.class, ServerboundPackets1_17.class); @@ -58,7 +59,6 @@ public final class Protocol1_18To1_17_1 extends AbstractProtocol tagRewriter = new TagRewriter<>(this); tagRewriter.registerGeneric(ClientboundPackets1_17_1.TAGS); tagRewriter.addEmptyTags(RegistryType.BLOCK, "minecraft:lava_pool_stone_cannot_replace", "minecraft:big_dripleaf_placeable", "minecraft:wolves_spawnable_on", "minecraft:rabbits_spawnable_on", "minecraft:polar_bears_spawnable_on_in_frozen_ocean", "minecraft:parrots_spawnable_on", @@ -85,6 +85,7 @@ public final class Protocol1_18To1_17_1 extends AbstractProtocol tagRewriter = new TagRewriter<>(this); public Protocol1_19_3To1_19_1() { super(ClientboundPackets1_19_1.class, ClientboundPackets1_19_3.class, ServerboundPackets1_19_1.class, ServerboundPackets1_19_3.class); @@ -74,8 +75,6 @@ public final class Protocol1_19_3To1_19_1 extends AbstractProtocol tagRewriter = new TagRewriter<>(this); - // Flint and steel was hardcoded before 1.19.3 to ignite a creeper; has been moved to a tag - adding this ensures offhand doesn't trigger as well tagRewriter.addTagRaw(RegistryType.ITEM, "minecraft:creeper_igniters", 733); // 733 = flint_and_steel 1.19.3 @@ -378,4 +377,9 @@ public final class Protocol1_19_3To1_19_1 extends AbstractProtocol getTagRewriter() { + return tagRewriter; + } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_4to1_19_3/Protocol1_19_4To1_19_3.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_4to1_19_3/Protocol1_19_4To1_19_3.java index dd79cc6cd..d05b33833 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_4to1_19_3/Protocol1_19_4To1_19_3.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_4to1_19_3/Protocol1_19_4To1_19_3.java @@ -45,6 +45,7 @@ public final class Protocol1_19_4To1_19_3 extends AbstractProtocol tagRewriter = new TagRewriter<>(this); public Protocol1_19_4To1_19_3() { super(ClientboundPackets1_19_3.class, ClientboundPackets1_19_4.class, ServerboundPackets1_19_3.class, ServerboundPackets1_19_4.class); @@ -54,7 +55,7 @@ public final class Protocol1_19_4To1_19_3 extends AbstractProtocol(this).registerGeneric(ClientboundPackets1_19_3.TAGS); + tagRewriter.registerGeneric(ClientboundPackets1_19_3.TAGS); new StatisticsRewriter<>(this).register(ClientboundPackets1_19_3.STATISTICS); final SoundRewriter soundRewriter = new SoundRewriter<>(this); @@ -127,4 +128,9 @@ public final class Protocol1_19_4To1_19_3 extends AbstractProtocol getTagRewriter() { + return tagRewriter; + } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19to1_18_2/Protocol1_19To1_18_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19to1_18_2/Protocol1_19To1_18_2.java index b50b492f1..4adfc36c5 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19to1_18_2/Protocol1_19To1_18_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19to1_18_2/Protocol1_19To1_18_2.java @@ -55,6 +55,7 @@ public final class Protocol1_19To1_18_2 extends AbstractProtocol tagRewriter = new TagRewriter<>(this); public Protocol1_19To1_18_2() { super(ClientboundPackets1_18.class, ClientboundPackets1_19.class, ServerboundPackets1_17.class, ServerboundPackets1_19.class); @@ -74,7 +75,6 @@ public final class Protocol1_19To1_18_2 extends AbstractProtocol tagRewriter = new TagRewriter<>(this); tagRewriter.registerGeneric(ClientboundPackets1_18.TAGS); entityRewriter.register(); @@ -331,4 +331,9 @@ public final class Protocol1_19To1_18_2 extends AbstractProtocol getTagRewriter() { + return tagRewriter; + } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_2to1_20/Protocol1_20_2To1_20.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_2to1_20/Protocol1_20_2To1_20.java index 1f1c0e3ad..9e3627b59 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_2to1_20/Protocol1_20_2To1_20.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_2to1_20/Protocol1_20_2To1_20.java @@ -62,6 +62,7 @@ public final class Protocol1_20_2To1_20 extends AbstractProtocol tagRewriter = new TagRewriter<>(this); public Protocol1_20_2To1_20() { super(ClientboundPackets1_19_4.class, ClientboundPackets1_20_2.class, ServerboundPackets1_19_4.class, ServerboundPackets1_20_2.class); @@ -100,7 +101,6 @@ public final class Protocol1_20_2To1_20 extends AbstractProtocol tagRewriter = new TagRewriter<>(this); registerClientbound(ClientboundPackets1_19_4.TAGS, wrapper -> { tagRewriter.getGenericHandler().handle(wrapper); wrapper.resetReader(); @@ -354,4 +354,9 @@ public final class Protocol1_20_2To1_20 extends AbstractProtocol getItemRewriter() { return itemPacketRewriter; } + + @Override + public TagRewriter getTagRewriter() { + return tagRewriter; + } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_3to1_20_2/Protocol1_20_3To1_20_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_3to1_20_2/Protocol1_20_3To1_20_2.java index be605425c..152ba8b48 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_3to1_20_2/Protocol1_20_3To1_20_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_3to1_20_2/Protocol1_20_3To1_20_2.java @@ -61,6 +61,7 @@ public final class Protocol1_20_3To1_20_2 extends AbstractProtocol tagRewriter = new TagRewriter<>(this); public Protocol1_20_3To1_20_2() { super(ClientboundPacket1_20_2.class, ClientboundPacket1_20_3.class, ServerboundPacket1_20_2.class, ServerboundPacket1_20_3.class); @@ -72,7 +73,6 @@ public final class Protocol1_20_3To1_20_2 extends AbstractProtocol tagRewriter = new TagRewriter<>(this); tagRewriter.registerGeneric(ClientboundPackets1_20_2.TAGS); final SoundRewriter soundRewriter = new SoundRewriter<>(this); @@ -383,6 +383,11 @@ public final class Protocol1_20_3To1_20_2 extends AbstractProtocol getTagRewriter() { + return tagRewriter; + } + @Override protected PacketTypesProvider createPacketTypesProvider() { return new SimplePacketTypesProvider<>( diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/Protocol1_20_5To1_20_3.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/Protocol1_20_5To1_20_3.java index f4175ff63..dd9a6c4f5 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/Protocol1_20_5To1_20_3.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/Protocol1_20_5To1_20_3.java @@ -184,6 +184,11 @@ public final class Protocol1_20_5To1_20_3 extends AbstractProtocol getTagRewriter() { + return tagRewriter; + } + @Override protected PacketTypesProvider createPacketTypesProvider() { return new SimplePacketTypesProvider<>( diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20to1_19_4/Protocol1_20To1_19_4.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20to1_19_4/Protocol1_20To1_19_4.java index 9376c8804..691d6339a 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20to1_19_4/Protocol1_20To1_19_4.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20to1_19_4/Protocol1_20To1_19_4.java @@ -39,6 +39,7 @@ public final class Protocol1_20To1_19_4 extends AbstractProtocol tagRewriter = new TagRewriter<>(this); public Protocol1_20To1_19_4() { super(ClientboundPackets1_19_4.class, ClientboundPackets1_19_4.class, ServerboundPackets1_19_4.class, ServerboundPackets1_19_4.class); @@ -48,7 +49,6 @@ public final class Protocol1_20To1_19_4 extends AbstractProtocol tagRewriter = new TagRewriter<>(this); tagRewriter.registerGeneric(ClientboundPackets1_19_4.TAGS); final SoundRewriter soundRewriter = new SoundRewriter<>(this); @@ -101,4 +101,9 @@ public final class Protocol1_20To1_19_4 extends AbstractProtocol getTagRewriter() { + return tagRewriter; + } } \ No newline at end of file diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/TagRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/TagRewriter.java index 63b4c392c..df3f5fd95 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/TagRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/TagRewriter.java @@ -38,7 +38,7 @@ import java.util.Map; import java.util.Set; import org.checkerframework.checker.nullness.qual.Nullable; -public class TagRewriter { +public class TagRewriter implements com.viaversion.viaversion.api.rewriter.TagRewriter { private static final int[] EMPTY_ARRAY = {}; private final Protocol protocol; private final Map> newTags = new EnumMap<>(RegistryType.class); @@ -49,10 +49,12 @@ public class TagRewriter { this.protocol = protocol; } - /** - * Gets new tags from the protocol's {@link MappingData} instance. - */ - public void loadFromMappingData() { + @Override + public void onMappingDataLoaded() { + if (protocol.getMappingData() == null) { + return; + } + for (RegistryType type : RegistryType.getValues()) { List tags = protocol.getMappingData().getTags(type); if (tags != null) { @@ -61,24 +63,22 @@ public class TagRewriter { } } + @Override public void removeTags(final String registryKey) { toRemove.add(registryKey); } + @Override public void renameTag(final RegistryType type, final String registryKey, final String renameTo) { toRename.computeIfAbsent(type, t -> new HashMap<>()).put(registryKey, renameTo); } - /** - * Adds an empty tag (since the client crashes if a checked tag is not registered). - * - * @param tagType registry tag type - * @param tagId tag id - */ + @Override public void addEmptyTag(RegistryType tagType, String tagId) { getOrComputeNewTags(tagType).add(new TagData(tagId, EMPTY_ARRAY)); } + @Override public void addEmptyTags(RegistryType tagType, String... tagIds) { List tagList = getOrComputeNewTags(tagType); for (String id : tagIds) { @@ -86,12 +86,7 @@ public class TagRewriter { } } - /** - * Adds an entity tag type to be filled with the given entity type ids. - * - * @param tagId registry tag type - * @param entities mapped entity types - */ + @Override public void addEntityTag(String tagId, EntityType... entities) { int[] ids = new int[entities.length]; for (int i = 0; i < entities.length; i++) { @@ -100,13 +95,7 @@ public class TagRewriter { addTagRaw(RegistryType.ENTITY, tagId, ids); } - /** - * Adds a tag type to be filled with the given type ids after being mapped to new ids. - * - * @param tagType registry tag type - * @param tagId tag id - * @param unmappedIds unmapped type ids - */ + @Override public void addTag(RegistryType tagType, String tagId, int... unmappedIds) { List newTags = getOrComputeNewTags(tagType); IdRewriteFunction rewriteFunction = getRewriter(tagType); @@ -119,32 +108,15 @@ public class TagRewriter { newTags.add(new TagData(tagId, unmappedIds)); } - /** - * Adds a tag type to be filled with the given raw type ids. - * - * @param tagType registry tag type - * @param tagId tag id - * @param ids raw type ids - */ + @Override public void addTagRaw(RegistryType tagType, String tagId, int... ids) { getOrComputeNewTags(tagType).add(new TagData(tagId, ids)); } - /** - * Pre 1.17 reading of hardcoded registry types. - * - * @param packetType packet type - * @param readUntilType read and process the types until (including) the given registry type - */ public void register(C packetType, @Nullable RegistryType readUntilType) { protocol.registerClientbound(packetType, getHandler(readUntilType)); } - /** - * 1.17+ reading of generic tag types. - * - * @param packetType packet type - */ public void registerGeneric(C packetType) { protocol.registerClientbound(packetType, getGenericHandler()); } @@ -236,10 +208,12 @@ public class TagRewriter { } } + @Override public @Nullable List getNewTags(RegistryType tagType) { return newTags.get(tagType); } + @Override public List getOrComputeNewTags(RegistryType tagType) { return newTags.computeIfAbsent(tagType, type -> new ArrayList<>()); } diff --git a/template/src/main/java/com/viaversion/viaversion/template/protocols/Protocol1_99To_98.java b/template/src/main/java/com/viaversion/viaversion/template/protocols/Protocol1_99To_98.java index e9a8e23df..8ac92b615 100644 --- a/template/src/main/java/com/viaversion/viaversion/template/protocols/Protocol1_99To_98.java +++ b/template/src/main/java/com/viaversion/viaversion/template/protocols/Protocol1_99To_98.java @@ -45,6 +45,7 @@ public final class Protocol1_99To_98 extends AbstractProtocol tagRewriter = new TagRewriter<>(this); public Protocol1_99To_98() { // Passing the class types into the super constructor is needed for automatic packet type id remapping, but can otherwise be omitted @@ -55,7 +56,6 @@ public final class Protocol1_99To_98 extends AbstractProtocol tagRewriter = new TagRewriter<>(this); tagRewriter.registerGeneric(ClientboundPackets1_20_5.TAGS); tagRewriter.registerGeneric(ClientboundConfigurationPackets1_20_5.UPDATE_TAGS); @@ -107,7 +107,8 @@ public final class Protocol1_99To_98 extends AbstractProtocol getTagRewriter() { + return tagRewriter; + } } \ No newline at end of file