Remove removed registries from command arguments

This commit is contained in:
Nassim Jahnke committed 2026-03-25 16:21:07 +01:00
1 parent 8ee0b66eed
commit 815ec24af1
7 files changed
+53 -15

No files matched your search

@@ -328,6 +328,13 @@ public interface PacketWrapper {
*/
void resetReader();
/**
* Rewind the reader by the given number of arguments, so they can be read again.
*
* @param values number of values to add to the reader dequeue again
*/
void rewindReader(int values);
/**
* Sends this packet to the server.
* <b>Unlike {@link #sendToServer(Class)}, this method does not handle the pipeline with packet id and data changes.</b>
@@ -27,5 +27,8 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public interface RegistryDataRewriter {
@Nullable KeyMappings getMappings(String registryKey);
@Nullable
KeyMappings getMappings(String registryKey);
boolean shouldRemoveRegistry(String registryKey);
}
@@ -478,6 +478,19 @@ public class PacketWrapperImpl implements PacketWrapper {
this.packetValues.clear();
}
@Override
public void rewindReader(final int values) {
if (allActionsRead) {
return;
}
final int size = packetValues.size();
Preconditions.checkArgument(values <= size, "Tried resetting more values than there are readable values");
for (int i = size - 1; i >= size - values; i--) {
this.readableObjects.addFirst(this.packetValues.remove(i));
}
}
@Override
public void sendToServerRaw() throws InformativeException {
sendToServerRaw(true);
@@ -130,17 +130,6 @@ public final class Protocol1_19_1To1_19_3 extends AbstractProtocol<ClientboundPa
default -> super.handleArgument(wrapper, argumentType);
}
}
@Override
public String handleArgumentType(final String argumentType) {
return switch (argumentType) {
case "minecraft:resource" -> "minecraft:resource_key";
case "minecraft:resource_or_tag" -> "minecraft:resource_or_tag_key";
case "minecraft:entity_summon", "minecraft:item_enchantment", "minecraft:mob_effect" ->
"minecraft:resource";
default -> argumentType;
};
}
};
commandRewriter.registerDeclareCommands1_19(ClientboundPackets1_19_1.COMMANDS);
@@ -23,14 +23,18 @@ 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.type.Types;
import com.viaversion.viaversion.util.Key;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Abstract rewriter for the declare commands packet to handle argument type name and content changes.
*/
public class CommandRewriter<C extends ClientboundPacketType> {
private static final Set<String> REGISTRY_ARGUMENT_TYPES = Set.of("resource", "resource_key", "resource_or_tag", "resource_or_tag_key", "resource_selector");
protected final Protocol<C, ?, ?, ?> protocol;
protected final Map<String, CommandArgumentConsumer> parserHandlers = new HashMap<>();
@@ -130,10 +134,27 @@ public class CommandRewriter<C extends ClientboundPacketType> {
String newArgumentType = handleArgumentType(argumentType);
Preconditions.checkNotNull(newArgumentType, "No mapping for argument type %s", argumentType);
wrapper.write(Types.VAR_INT, mappedArgumentTypeId(newArgumentType));
// Always call the handler using the previous name
handleArgument(wrapper, argumentType);
boolean removedRegistryArgument = false;
if (protocol.getRegistryDataRewriter() != null && REGISTRY_ARGUMENT_TYPES.contains(Key.stripMinecraftNamespace(newArgumentType))) {
final String registry = wrapper.read(Types.STRING);
if (protocol.getRegistryDataRewriter().shouldRemoveRegistry(registry)) {
// Map removed registry arguments to a plain quotable string
removedRegistryArgument = true;
wrapper.write(Types.VAR_INT, mappedArgumentTypeId("brigadier:string"));
wrapper.write(Types.VAR_INT, 1);
} else {
// Keep reading
wrapper.write(Types.STRING, registry);
wrapper.rewindReader(1);
}
}
if (!removedRegistryArgument) {
wrapper.write(Types.VAR_INT, mappedArgumentTypeId(newArgumentType));
// Call the handler using the previous name
handleArgument(wrapper, argumentType);
}
if ((flags & 0x10) != 0) {
wrapper.passthrough(Types.STRING); // Suggestion type
@@ -370,4 +370,9 @@ public class RegistryDataRewriter implements com.viaversion.viaversion.api.rewri
public Map<String, KeyMappings> registryKeyMappings() {
return registryKeyMappings;
}
@Override
public boolean shouldRemoveRegistry(final String registryKey) {
return this.toRemove.contains(Key.stripMinecraftNamespace(registryKey));
}
}