From 2a71612aea5786069aeb1bae41c3da57a18c9e1c Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sun, 25 Aug 2024 18:42:21 +0200 Subject: [PATCH] Small cleanup of a few rewriters --- .../protocol/packet/PacketWrapperImpl.java | 4 ++ .../v1_16_4to1_17/Protocol1_16_4To1_17.java | 4 +- .../Protocol1_20_2To1_20_3.java | 15 ++-- .../BlockItemPacketRewriter1_20_5.java | 6 +- .../v1_20to1_20_2/Protocol1_20To1_20_2.java | 4 +- .../v1_20to1_20_2/storage/LastTags.java | 17 ++--- .../viaversion/rewriter/EntityRewriter.java | 7 -- .../viaversion/rewriter/ItemRewriter.java | 28 +++----- .../viaversion/rewriter/RecipeRewriter.java | 3 +- .../viaversion/rewriter/TagRewriter.java | 68 ++++++++++--------- 10 files changed, 73 insertions(+), 83 deletions(-) diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java index 269d21fa0..bacf3e035 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java @@ -199,6 +199,10 @@ public class PacketWrapperImpl implements PacketWrapper { @Override public T passthroughAndMap(Type type, Type mappedType) throws InformativeException { + if (type == mappedType) { + return passthrough(mappedType); + } + final Object value = read(type); final T mappedValue = attemptTransform(mappedType, value); write(mappedType, mappedValue); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_16_4to1_17/Protocol1_16_4To1_17.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_16_4to1_17/Protocol1_16_4To1_17.java index 4d976e120..fe692871b 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_16_4to1_17/Protocol1_16_4To1_17.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_16_4to1_17/Protocol1_16_4To1_17.java @@ -68,7 +68,7 @@ public final class Protocol1_16_4To1_17 extends AbstractProtocol getTagRewriter() { return tagRewriter; } -} \ No newline at end of file +} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_2to1_20_3/Protocol1_20_2To1_20_3.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_2to1_20_3/Protocol1_20_2To1_20_3.java index 5a4ee580b..a9049f857 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_2to1_20_3/Protocol1_20_2To1_20_3.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_2to1_20_3/Protocol1_20_2To1_20_3.java @@ -29,18 +29,11 @@ import com.viaversion.viaversion.api.protocol.packet.provider.PacketTypesProvide import com.viaversion.viaversion.api.protocol.packet.provider.SimplePacketTypesProvider; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; -import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.types.misc.ParticleType; import com.viaversion.viaversion.api.type.types.version.Types1_20_3; import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.v1_19_3to1_19_4.rewriter.CommandRewriter1_19_4; -import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ClientboundConfigurationPackets1_20_2; -import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ClientboundPacket1_20_2; -import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ClientboundPackets1_20_2; -import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ServerboundConfigurationPackets1_20_2; -import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ServerboundPacket1_20_2; -import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ServerboundPackets1_20_2; import com.viaversion.viaversion.protocols.v1_20_2to1_20_3.packet.ClientboundConfigurationPackets1_20_3; import com.viaversion.viaversion.protocols.v1_20_2to1_20_3.packet.ClientboundPacket1_20_3; import com.viaversion.viaversion.protocols.v1_20_2to1_20_3.packet.ClientboundPackets1_20_3; @@ -48,6 +41,12 @@ import com.viaversion.viaversion.protocols.v1_20_2to1_20_3.packet.ServerboundPac import com.viaversion.viaversion.protocols.v1_20_2to1_20_3.packet.ServerboundPackets1_20_3; import com.viaversion.viaversion.protocols.v1_20_2to1_20_3.rewriter.BlockItemPacketRewriter1_20_3; import com.viaversion.viaversion.protocols.v1_20_2to1_20_3.rewriter.EntityPacketRewriter1_20_3; +import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ClientboundConfigurationPackets1_20_2; +import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ClientboundPacket1_20_2; +import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ClientboundPackets1_20_2; +import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ServerboundConfigurationPackets1_20_2; +import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ServerboundPacket1_20_2; +import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ServerboundPackets1_20_2; import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.TagRewriter; @@ -306,7 +305,7 @@ public final class Protocol1_20_2To1_20_3 extends AbstractProtocol { - tagRewriter.getGenericHandler().handle(wrapper); + tagRewriter.handleGeneric(wrapper); wrapper.resetReader(); wrapper.user().put(new LastTags(wrapper)); }); registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_2.UPDATE_TAGS.getId(), ClientboundConfigurationPackets1_20_2.UPDATE_TAGS.getId(), wrapper -> { - tagRewriter.getGenericHandler().handle(wrapper); + tagRewriter.handleGeneric(wrapper); wrapper.resetReader(); wrapper.user().put(new LastTags(wrapper)); }); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20to1_20_2/storage/LastTags.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20to1_20_2/storage/LastTags.java index 9c52bd8c5..3442a6f78 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20to1_20_2/storage/LastTags.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20to1_20_2/storage/LastTags.java @@ -19,6 +19,7 @@ package com.viaversion.viaversion.protocols.v1_20to1_20_2.storage; import com.viaversion.viaversion.api.connection.StorableObject; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.TagData; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.protocols.v1_20to1_20_2.Protocol1_20To1_20_2; @@ -34,15 +35,14 @@ public class LastTags implements StorableObject { public LastTags(final PacketWrapper wrapper) { final int length = wrapper.passthrough(Types.VAR_INT); for (int i = 0; i < length; i++) { - final List tags = new ArrayList<>(); + final List tags = new ArrayList<>(); final String registryKey = wrapper.passthrough(Types.STRING); final int tagsSize = wrapper.passthrough(Types.VAR_INT); for (int j = 0; j < tagsSize; j++) { final String key = wrapper.passthrough(Types.STRING); final int[] ids = wrapper.passthrough(Types.VAR_INT_ARRAY_PRIMITIVE); - tags.add(new Tag(key, ids)); + tags.add(new TagData(key, ids)); } - this.registryTags.add(new RegistryTags(registryKey, tags)); } } @@ -57,17 +57,14 @@ public class LastTags implements StorableObject { for (final RegistryTags registryTag : registryTags) { packet.write(Types.STRING, registryTag.registryKey); packet.write(Types.VAR_INT, registryTag.tags.size()); - for (final Tag tag : registryTag.tags) { - packet.write(Types.STRING, tag.key); - packet.write(Types.VAR_INT_ARRAY_PRIMITIVE, Arrays.copyOf(tag.ids, tag.ids.length)); + for (final TagData tag : registryTag.tags) { + packet.write(Types.STRING, tag.identifier()); + packet.write(Types.VAR_INT_ARRAY_PRIMITIVE, Arrays.copyOf(tag.entries(), tag.entries().length)); } } packet.send(Protocol1_20To1_20_2.class); } - private record RegistryTags(String registryKey, List tags) { - } - - private record Tag(String key, int[] ids) { + private record RegistryTags(String registryKey, List tags) { } } diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java index 0e1cc430b..eb18e8f80 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java @@ -38,7 +38,6 @@ import com.viaversion.viaversion.api.minecraft.entitydata.EntityDataType; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.rewriter.ItemRewriter; @@ -602,12 +601,6 @@ public abstract class EntityRewriter from, Type to) { - final Particle particle = wrapper.read(from); - rewriteParticle(wrapper.user(), particle); - wrapper.write(to, particle); - } - private void logException(Exception e, @Nullable EntityType type, List entityDataList, EntityData entityData) { if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) { protocol.getLogger().severe("An error occurred in entity data handler " + this.getClass().getSimpleName() diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/ItemRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/ItemRewriter.java index f0ff678f5..d9a66f176 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/ItemRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/ItemRewriter.java @@ -95,17 +95,11 @@ public class ItemRewriter { - Item[] items = wrapper.read(itemArrayType); - wrapper.write(mappedItemArrayType, items); - for (int i = 0; i < items.length; i++) { - items[i] = handleItemToClient(wrapper.user(), items[i]); - } - }); + protocol.registerClientbound(packetType, wrapper -> { + wrapper.passthrough(Types.UNSIGNED_BYTE); // Container id + Item[] items = wrapper.passthroughAndMap(itemArrayType, mappedItemArrayType); + for (int i = 0; i < items.length; i++) { + items[i] = handleItemToClient(wrapper.user(), items[i]); } }); } @@ -117,8 +111,7 @@ public class ItemRewriter { - Item[] items = wrapper.read(itemArrayType); - wrapper.write(mappedItemArrayType, items); + Item[] items = wrapper.passthroughAndMap(itemArrayType, mappedItemArrayType); for (int i = 0; i < items.length; i++) { items[i] = handleItemToClient(wrapper.user(), items[i]); } @@ -522,9 +515,8 @@ public class ItemRewriter { - final Particle particle = wrapper.read(particleType); + final Particle particle = wrapper.passthroughAndMap(particleType, mappedParticleType); rewriteParticle(wrapper.user(), particle); - wrapper.write(mappedParticleType, particle); }); } }); @@ -548,10 +540,8 @@ public class ItemRewriter { } protected void handleIngredient(final PacketWrapper wrapper) { - final Item[] items = wrapper.read(itemArrayType()); - wrapper.write(mappedItemArrayType(), items); + final Item[] items = wrapper.passthroughAndMap(itemArrayType(), mappedItemArrayType()); for (int i = 0; i < items.length; i++) { Item item = items[i]; items[i] = rewrite(wrapper.user(), item); 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 5325f82f0..164795732 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/TagRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/TagRewriter.java @@ -125,13 +125,13 @@ public class TagRewriter implements com.viavers } public void registerGeneric(C packetType) { - protocol.registerClientbound(packetType, getGenericHandler()); + protocol.registerClientbound(packetType, this::handleGeneric); } public PacketHandler getHandler(@Nullable RegistryType readUntilType) { return wrapper -> { for (RegistryType type : RegistryType.getValues()) { - handle(wrapper, getRewriter(type), getNewTags(type), toRename.get(type), toRemove.get(type)); + handle(wrapper, type); // Stop iterating if (type == readUntilType) { @@ -141,40 +141,46 @@ public class TagRewriter implements com.viavers }; } - public PacketHandler getGenericHandler() { - return wrapper -> { - final int length = wrapper.passthrough(Types.VAR_INT); - int editedLength = length; - for (int i = 0; i < length; i++) { - String registryKey = wrapper.read(Types.STRING); - if (toRemoveRegistries.contains(Key.stripMinecraftNamespace(registryKey))) { - wrapper.set(Types.VAR_INT, 0, --editedLength); - int tagsSize = wrapper.read(Types.VAR_INT); - for (int j = 0; j < tagsSize; j++) { - wrapper.read(Types.STRING); - wrapper.read(Types.VAR_INT_ARRAY_PRIMITIVE); - } - continue; - } - - wrapper.write(Types.STRING, registryKey); - registryKey = Key.stripMinecraftNamespace(registryKey); - - RegistryType type = RegistryType.getByKey(registryKey); - if (type != null) { - handle(wrapper, getRewriter(type), getNewTags(type), toRename.get(type), toRemove.get(type)); - } else { - handle(wrapper, null, null, null, null); + public void handleGeneric(final PacketWrapper wrapper) { + final int length = wrapper.passthrough(Types.VAR_INT); + int editedLength = length; + for (int i = 0; i < length; i++) { + final String registryKey = wrapper.read(Types.STRING); + if (toRemoveRegistries.contains(Key.stripMinecraftNamespace(registryKey))) { + wrapper.set(Types.VAR_INT, 0, --editedLength); + final int tagsSize = wrapper.read(Types.VAR_INT); + for (int j = 0; j < tagsSize; j++) { + wrapper.read(Types.STRING); + wrapper.read(Types.VAR_INT_ARRAY_PRIMITIVE); } + continue; } - }; + + wrapper.write(Types.STRING, registryKey); + handle(wrapper, Key.stripMinecraftNamespace(registryKey)); + } } - public void handle(PacketWrapper wrapper, @Nullable IdRewriteFunction rewriteFunction, @Nullable List newTags) { - handle(wrapper, rewriteFunction, newTags, null, null); + public void handle(final PacketWrapper wrapper, final String registryKey) { + final RegistryType type = RegistryType.getByKey(registryKey); + if (type != null) { + handle(wrapper, type); + } else { + handle(wrapper, null, null, null, null); + } } - public void handle(PacketWrapper wrapper, @Nullable IdRewriteFunction rewriteFunction, @Nullable List newTags, @Nullable Map tagsToRename, @Nullable Set tagsToRemove) { + public void handle(PacketWrapper wrapper, RegistryType registryType) { + handle(wrapper, getRewriter(registryType), getNewTags(registryType), toRename.get(registryType), toRemove.get(registryType)); + } + + protected void handle( + PacketWrapper wrapper, + @Nullable IdRewriteFunction rewriteFunction, + @Nullable List newTags, + @Nullable Map tagsToRename, + @Nullable Set tagsToRemove + ) { final int tagsSize = wrapper.read(Types.VAR_INT); final List tags = new ArrayList<>(newTags != null ? tagsSize + newTags.size() : tagsSize); final Set currentTags = new HashSet<>(tagsSize); @@ -260,4 +266,4 @@ public class TagRewriter implements com.viavers case FLUID, GAME_EVENT -> null; }; } -} \ No newline at end of file +}