?????????????????????????????????????????????????????

Also add removeTags and renameTag method to TagRewriter
This commit is contained in:
Nassim Jahnke 2022-04-06 22:50:59 +02:00
parent dd189411e6
commit c0fc8195ce
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
2 changed files with 60 additions and 10 deletions

View File

@ -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<GlobalPosition> {
@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);
}
}
}

View File

@ -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<RegistryType, List<TagData>> newTags = new EnumMap<>(RegistryType.class);
private final Map<RegistryType, Map<String, String>> toRename = new EnumMap<>(RegistryType.class);
private final Set<String> 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<TagData> newTags) throws Exception {
handle(wrapper, rewriteFunction, newTags, null);
}
public void handle(PacketWrapper wrapper, @Nullable IdRewriteFunction rewriteFunction, @Nullable List<TagData> newTags, @Nullable Map<String, String> 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