From ff91dd708279868482a65dc6fd79078063f638f1 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Wed, 2 Dec 2020 22:31:02 +0100 Subject: [PATCH] Finish 20w49a --- .../api/rewriters/RegistryType.java | 3 +- .../ViaVersion/api/rewriters/TagRewriter.java | 66 +++++++++---------- .../Protocol1_17To1_16_4.java | 19 +++++- 3 files changed, 50 insertions(+), 38 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 711bcc1b2..a103590a9 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 @@ -5,5 +5,6 @@ public enum RegistryType { BLOCK, ITEM, FLUID, - ENTITY + ENTITY, + GAME_EVENT } 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 c5933e5fb..44f9f7303 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 @@ -7,20 +7,20 @@ import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.data.MappingData; import us.myles.ViaVersion.api.protocol.ClientboundPacketType; import us.myles.ViaVersion.api.protocol.Protocol; +import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import java.util.ArrayList; +import java.util.EnumMap; import java.util.List; +import java.util.Map; public class TagRewriter { private static final int[] EMPTY_ARRAY = {}; private final Protocol protocol; private final IdRewriteFunction entityRewriter; - private final List newBlockTags = new ArrayList<>(); - private final List newItemTags = new ArrayList<>(); - private final List newEntityTags = new ArrayList<>(); - // add fluid tag list if needed at some point + private final Map> newTags = new EnumMap<>(RegistryType.class); public TagRewriter(Protocol protocol, @Nullable IdRewriteFunction entityRewriter) { this.protocol = protocol; @@ -31,18 +31,18 @@ public class TagRewriter { * Adds an empty tag (since the client crashes if a checked tag is not registered.) */ public void addEmptyTag(RegistryType tagType, String id) { - getNewTags(tagType).add(new TagData(id, EMPTY_ARRAY)); + getOrComputeNewTags(tagType).add(new TagData(id, EMPTY_ARRAY)); } public void addEmptyTags(RegistryType tagType, String... ids) { - List tagList = getNewTags(tagType); + List tagList = getOrComputeNewTags(tagType); for (String id : ids) { tagList.add(new TagData(id, EMPTY_ARRAY)); } } public void addTag(RegistryType tagType, String id, int... oldIds) { - List newTags = getNewTags(tagType); + List newTags = getOrComputeNewTags(tagType); IdRewriteFunction rewriteFunction = getRewriter(tagType); for (int i = 0; i < oldIds.length; i++) { int oldId = oldIds[i]; @@ -52,29 +52,32 @@ public class TagRewriter { } public void register(ClientboundPacketType packetType) { + register(packetType, false); + } + + public void register(ClientboundPacketType packetType, boolean hasGameEvents) { protocol.registerOutgoing(packetType, new PacketRemapper() { @Override public void registerMap() { - handler(wrapper -> { - MappingData mappingData = protocol.getMappingData(); - handle(wrapper, id -> mappingData != null ? mappingData.getNewBlockId(id) : null, newBlockTags); - handle(wrapper, id -> mappingData != null ? mappingData.getNewItemId(id) : null, newItemTags); - - if (entityRewriter == null && newEntityTags.isEmpty()) return; - - int fluidTagsSize = wrapper.passthrough(Type.VAR_INT); - for (int i = 0; i < fluidTagsSize; i++) { - wrapper.passthrough(Type.STRING); - wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); - } - - handle(wrapper, entityRewriter, newEntityTags); - }); + handler(getHandler(hasGameEvents)); } }); } - private void handle(PacketWrapper wrapper, IdRewriteFunction rewriteFunction, List newTags) throws Exception { + public PacketHandler getHandler(boolean hasGameEvents) { + 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)); + } + }; + } + + private void handle(PacketWrapper wrapper, @Nullable IdRewriteFunction rewriteFunction, List newTags) throws Exception { int tagsSize = wrapper.read(Type.VAR_INT); wrapper.write(Type.VAR_INT, newTags != null ? tagsSize + newTags.size() : tagsSize); // add new tags count @@ -107,18 +110,13 @@ public class TagRewriter { } } + @Nullable private List getNewTags(RegistryType tagType) { - switch (tagType) { - case BLOCK: - return newBlockTags; - case ITEM: - return newItemTags; - case ENTITY: - return newEntityTags; - case FLUID: - default: - return null; - } + return newTags.get(tagType); + } + + private List getOrComputeNewTags(RegistryType tagType) { + return newTags.computeIfAbsent(tagType, type -> new ArrayList<>()); } @Nullable diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_17to1_16_4/Protocol1_17To1_16_4.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_17to1_16_4/Protocol1_17To1_16_4.java index cd44809f5..57cae9147 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_17to1_16_4/Protocol1_17To1_16_4.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_17to1_16_4/Protocol1_17To1_16_4.java @@ -22,6 +22,7 @@ import us.myles.ViaVersion.protocols.protocol1_17to1_16_4.storage.EntityTracker1 public class Protocol1_17To1_16_4 extends Protocol { public static final MappingData MAPPINGS = new MappingData("1.16.2", "1.17", true); + private static final String[] NEW_GAME_EVENT_TAGS = {"minecraft:ignore_vibrations_stepping_carefully", "minecraft:vibrations"}; private TagRewriter tagRewriter; public Protocol1_17To1_16_4() { @@ -37,7 +38,20 @@ public class Protocol1_17To1_16_4 extends Protocol { + // New Game Event tags type + wrapper.write(Type.VAR_INT, NEW_GAME_EVENT_TAGS.length); + for (String tag : NEW_GAME_EVENT_TAGS) { + wrapper.write(Type.STRING, tag); + wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[0]); + } + }); + } + }); new StatisticsRewriter(this, null).register(ClientboundPackets1_16_2.STATISTICS); @@ -82,8 +96,7 @@ public class Protocol1_17To1_16_4 extends Protocol