From 3aa35395f4ee27e338bc4fee6468bb7b2d3a20b2 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Thu, 3 Dec 2020 11:14:35 +0100 Subject: [PATCH] Fix tag processing in older versions, small refactor --- .../api/rewriters/RegistryType.java | 8 +++- .../ViaVersion/api/rewriters/TagRewriter.java | 45 ++++++++++++------- .../Protocol1_13_1To1_13.java | 3 +- .../Protocol1_15To1_14_4.java | 2 +- .../Protocol1_16_2To1_16_1.java | 2 +- .../Protocol1_16To1_15_2.java | 2 +- .../Protocol1_17To1_16_4.java | 2 +- 7 files changed, 41 insertions(+), 23 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/RegistryType.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/RegistryType.java index a103590a9..973063bac 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/rewriters/RegistryType.java +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/RegistryType.java @@ -6,5 +6,11 @@ public enum RegistryType { ITEM, FLUID, ENTITY, - GAME_EVENT + GAME_EVENT; + + private static final RegistryType[] VALUES = values(); + + public static RegistryType[] getValues() { + return VALUES; + } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagRewriter.java index 44f9f7303..ae5ce62e6 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagRewriter.java @@ -51,28 +51,37 @@ public class TagRewriter { newTags.add(new TagData(id, oldIds)); } - public void register(ClientboundPacketType packetType) { - register(packetType, false); - } - - public void register(ClientboundPacketType packetType, boolean hasGameEvents) { + /** + * @param packetType packet type + * @param readUntilType read and process the types until (including) the given registry type + */ + public void register(ClientboundPacketType packetType, @Nullable RegistryType readUntilType) { protocol.registerOutgoing(packetType, new PacketRemapper() { @Override public void registerMap() { - handler(getHandler(hasGameEvents)); + handler(getHandler(readUntilType)); } }); } - public PacketHandler getHandler(boolean hasGameEvents) { + /** + * Registers the handler, reading and processing until and including the entity tags. + * + * @param packetType packet type + */ + public void register(ClientboundPacketType packetType) { + register(packetType, RegistryType.ENTITY); + } + + public PacketHandler getHandler(@Nullable RegistryType readUntilType) { return wrapper -> { - MappingData mappingData = protocol.getMappingData(); - handle(wrapper, id -> mappingData != null ? mappingData.getNewBlockId(id) : null, getNewTags(RegistryType.BLOCK)); - handle(wrapper, id -> mappingData != null ? mappingData.getNewItemId(id) : null, getNewTags(RegistryType.ITEM)); - handle(wrapper, null, getNewTags(RegistryType.FLUID)); - handle(wrapper, entityRewriter, getNewTags(RegistryType.ENTITY)); - if (hasGameEvents) { - handle(wrapper, null, getNewTags(RegistryType.GAME_EVENT)); + for (RegistryType type : RegistryType.getValues()) { + handle(wrapper, getRewriter(type), getNewTags(type)); + + // Stop iterating + if (type == readUntilType) { + break; + } } }; } @@ -102,7 +111,7 @@ public class TagRewriter { } // Send new tags if present - if (newTags != null && !newTags.isEmpty()) { + if (newTags != null) { for (TagData tag : newTags) { wrapper.write(Type.STRING, tag.identifier); wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, tag.entries); @@ -121,14 +130,16 @@ public class TagRewriter { @Nullable private IdRewriteFunction getRewriter(RegistryType tagType) { + MappingData mappingData = protocol.getMappingData(); switch (tagType) { case BLOCK: - return protocol.getMappingData().getBlockMappings() != null ? id -> protocol.getMappingData().getNewBlockId(id) : null; + return mappingData != null && mappingData.getBlockMappings() != null ? mappingData::getNewBlockId : null; case ITEM: - return protocol.getMappingData().getItemMappings() != null ? id -> protocol.getMappingData().getNewItemId(id) : null; + return mappingData != null && mappingData.getItemMappings() != null ? mappingData::getNewItemId : null; case ENTITY: return entityRewriter; case FLUID: + case GAME_EVENT: default: return null; } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java index f680b6f61..508132fb6 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java @@ -8,6 +8,7 @@ import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.ValueTransformer; +import us.myles.ViaVersion.api.rewriters.RegistryType; import us.myles.ViaVersion.api.rewriters.StatisticsRewriter; import us.myles.ViaVersion.api.rewriters.TagRewriter; import us.myles.ViaVersion.api.type.Type; @@ -123,7 +124,7 @@ public class Protocol1_13_1To1_13 extends Protocol { // New Game Event tags type wrapper.write(Type.VAR_INT, NEW_GAME_EVENT_TAGS.length);