Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-11-08 17:20:24 +01:00
?????????????????????????????????????????????????????
Also add removeTags and renameTag method to TagRewriter
Dieser Commit ist enthalten in:
Ursprung
dd189411e6
Commit
c0fc8195ce
@ -22,6 +22,9 @@
|
|||||||
*/
|
*/
|
||||||
package com.viaversion.viaversion.api.type.types.minecraft;
|
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.minecraft.GlobalPosition;
|
||||||
import com.viaversion.viaversion.api.type.Type;
|
import com.viaversion.viaversion.api.type.Type;
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
@ -34,15 +37,25 @@ public class OptionalGlobalPositionType extends Type<GlobalPosition> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public GlobalPosition read(ByteBuf buffer) throws Exception {
|
public GlobalPosition read(ByteBuf buffer) throws Exception {
|
||||||
boolean present = buffer.readBoolean();
|
if (buffer.readBoolean()) {
|
||||||
return present ? Type.OPTIONAL_GLOBAL_POSITION.read(buffer) : null;
|
// ♨︎_♨︎
|
||||||
|
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
|
@Override
|
||||||
public void write(ByteBuf buffer, GlobalPosition object) throws Exception {
|
public void write(ByteBuf buffer, GlobalPosition object) throws Exception {
|
||||||
buffer.writeBoolean(object != null);
|
buffer.writeBoolean(object != null);
|
||||||
if (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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,13 +33,18 @@ import org.checkerframework.checker.nullness.qual.Nullable;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class TagRewriter {
|
public class TagRewriter {
|
||||||
private static final int[] EMPTY_ARRAY = {};
|
private static final int[] EMPTY_ARRAY = {};
|
||||||
private final Protocol protocol;
|
private final Protocol protocol;
|
||||||
private final Map<RegistryType, List<TagData>> newTags = new EnumMap<>(RegistryType.class);
|
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) {
|
public TagRewriter(Protocol protocol) {
|
||||||
this.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).
|
* Adds an empty tag (since the client crashes if a checked tag is not registered).
|
||||||
*
|
*
|
||||||
@ -148,7 +161,7 @@ public class TagRewriter {
|
|||||||
public PacketHandler getHandler(@Nullable RegistryType readUntilType) {
|
public PacketHandler getHandler(@Nullable RegistryType readUntilType) {
|
||||||
return wrapper -> {
|
return wrapper -> {
|
||||||
for (RegistryType type : RegistryType.getValues()) {
|
for (RegistryType type : RegistryType.getValues()) {
|
||||||
handle(wrapper, getRewriter(type), getNewTags(type));
|
handle(wrapper, getRewriter(type), getNewTags(type), toRename.get(type));
|
||||||
|
|
||||||
// Stop iterating
|
// Stop iterating
|
||||||
if (type == readUntilType) {
|
if (type == readUntilType) {
|
||||||
@ -160,29 +173,53 @@ public class TagRewriter {
|
|||||||
|
|
||||||
public PacketHandler getGenericHandler() {
|
public PacketHandler getGenericHandler() {
|
||||||
return wrapper -> {
|
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++) {
|
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:")) {
|
if (registryKey.startsWith("minecraft:")) {
|
||||||
registryKey = registryKey.substring(10);
|
registryKey = registryKey.substring(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
RegistryType type = RegistryType.getByKey(registryKey);
|
RegistryType type = RegistryType.getByKey(registryKey);
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
handle(wrapper, getRewriter(type), getNewTags(type));
|
handle(wrapper, getRewriter(type), getNewTags(type), toRename.get(type));
|
||||||
} else {
|
} else {
|
||||||
handle(wrapper, null, null);
|
handle(wrapper, null, null, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handle(PacketWrapper wrapper, @Nullable IdRewriteFunction rewriteFunction, @Nullable List<TagData> newTags) throws Exception {
|
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);
|
int tagsSize = wrapper.read(Type.VAR_INT);
|
||||||
wrapper.write(Type.VAR_INT, newTags != null ? tagsSize + newTags.size() : tagsSize); // add new tags count
|
wrapper.write(Type.VAR_INT, newTags != null ? tagsSize + newTags.size() : tagsSize); // add new tags count
|
||||||
|
|
||||||
for (int i = 0; i < tagsSize; i++) {
|
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);
|
int[] ids = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE);
|
||||||
if (rewriteFunction != null) {
|
if (rewriteFunction != null) {
|
||||||
// Map ids and filter out new blocks
|
// Map ids and filter out new blocks
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren