From c0fc8195ce6ccff57c7385e81983b7f225ecf720 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Wed, 6 Apr 2022 22:50:59 +0200 Subject: [PATCH] ????????????????????????????????????????????????????? Also add removeTags and renameTag method to TagRewriter --- .../minecraft/OptionalGlobalPositionType.java | 19 +++++-- .../viaversion/rewriter/TagRewriter.java | 51 ++++++++++++++++--- 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptionalGlobalPositionType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptionalGlobalPositionType.java index aa4c423b4..1bb3dc653 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptionalGlobalPositionType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptionalGlobalPositionType.java @@ -22,6 +22,9 @@ */ package com.viaversion.viaversion.api.type.types.minecraft; +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.github.steveice10.opennbt.tag.builtin.IntArrayTag; +import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.viaversion.viaversion.api.minecraft.GlobalPosition; import com.viaversion.viaversion.api.type.Type; import io.netty.buffer.ByteBuf; @@ -34,15 +37,25 @@ public class OptionalGlobalPositionType extends Type { @Override public GlobalPosition read(ByteBuf buffer) throws Exception { - boolean present = buffer.readBoolean(); - return present ? Type.OPTIONAL_GLOBAL_POSITION.read(buffer) : null; + if (buffer.readBoolean()) { + // ♨︎_♨︎ + final CompoundTag compound = Type.NBT.read(buffer); + final String dimension = (String) compound.get("dimension").getValue(); + final IntArrayTag positionFields = compound.get("pos"); + return new GlobalPosition(dimension, positionFields.getValue(0), positionFields.getValue(1), positionFields.getValue(2)); + } + return null; } @Override public void write(ByteBuf buffer, GlobalPosition object) throws Exception { buffer.writeBoolean(object != null); if (object != null) { - Type.OPTIONAL_GLOBAL_POSITION.write(buffer, object); + final CompoundTag compound = new CompoundTag(); + compound.put("dimension", new StringTag(object.dimension())); + final int[] positionFields = {object.x(), object.y(), object.z()}; + compound.put("pos", new IntArrayTag(positionFields)); + Type.NBT.write(buffer, compound); } } } 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 7bb06d0cb..8e5676485 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/TagRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/TagRewriter.java @@ -33,13 +33,18 @@ import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; import java.util.EnumMap; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; public class TagRewriter { private static final int[] EMPTY_ARRAY = {}; private final Protocol protocol; private final Map> newTags = new EnumMap<>(RegistryType.class); + private final Map> toRename = new EnumMap<>(RegistryType.class); + private final Set toRemove = new HashSet<>(); public TagRewriter(Protocol protocol) { this.protocol = protocol; @@ -57,6 +62,14 @@ public class TagRewriter { } } + public void removeTags(final String registryKey) { + toRemove.add(registryKey); + } + + 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). * @@ -77,7 +90,7 @@ public class TagRewriter { /** * Adds an entity tag type to be filled with the given entity type ids. * - * @param tagId registry tag type + * @param tagId registry tag type * @param entities mapped entity types */ public void addEntityTag(String tagId, EntityType... entities) { @@ -148,7 +161,7 @@ public class TagRewriter { public PacketHandler getHandler(@Nullable RegistryType readUntilType) { return wrapper -> { for (RegistryType type : RegistryType.getValues()) { - handle(wrapper, getRewriter(type), getNewTags(type)); + handle(wrapper, getRewriter(type), getNewTags(type), toRename.get(type)); // Stop iterating if (type == readUntilType) { @@ -160,29 +173,53 @@ public class TagRewriter { public PacketHandler getGenericHandler() { return wrapper -> { - int length = wrapper.passthrough(Type.VAR_INT); + final int length = wrapper.passthrough(Type.VAR_INT); + int editedLength = length; for (int i = 0; i < length; i++) { - String registryKey = wrapper.passthrough(Type.STRING); + String registryKey = wrapper.read(Type.STRING); + if (toRemove.contains(registryKey)) { + wrapper.set(Type.VAR_INT, 0, --editedLength); + int tagsSize = wrapper.read(Type.VAR_INT); + for (int j = 0; j < tagsSize; j++) { + wrapper.read(Type.STRING); + wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE); + } + continue; + } + + wrapper.write(Type.STRING, registryKey); if (registryKey.startsWith("minecraft:")) { registryKey = registryKey.substring(10); } RegistryType type = RegistryType.getByKey(registryKey); if (type != null) { - handle(wrapper, getRewriter(type), getNewTags(type)); + handle(wrapper, getRewriter(type), getNewTags(type), toRename.get(type)); } else { - handle(wrapper, null, null); + handle(wrapper, null, null, null); } } }; } public void handle(PacketWrapper wrapper, @Nullable IdRewriteFunction rewriteFunction, @Nullable List newTags) throws Exception { + handle(wrapper, rewriteFunction, newTags, null); + } + + public void handle(PacketWrapper wrapper, @Nullable IdRewriteFunction rewriteFunction, @Nullable List newTags, @Nullable Map tagsToRename) throws Exception { int tagsSize = wrapper.read(Type.VAR_INT); wrapper.write(Type.VAR_INT, newTags != null ? tagsSize + newTags.size() : tagsSize); // add new tags count for (int i = 0; i < tagsSize; i++) { - wrapper.passthrough(Type.STRING); + String key = wrapper.read(Type.STRING); + if (tagsToRename != null) { + String renamedKey = tagsToRename.get(key); + if (renamedKey != null) { + key = renamedKey; + } + } + wrapper.write(Type.STRING, key); + int[] ids = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE); if (rewriteFunction != null) { // Map ids and filter out new blocks