Skip to content

Commit 9558415

Browse files
committed
Keep signed command arguments after custom parse failures
1 parent 6e88e46 commit 9558415

3 files changed

Lines changed: 80 additions & 2 deletions

File tree

‎paper-server/patches/sources/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,7 @@
14481448
ParseResults<CommandSourceStack> parsed = this.parseCommand(command);
14491449
if (this.server.enforceSecureProfile() && SignableCommand.hasSignableArguments(parsed)) {
14501450
LOGGER.error(
1451-
@@ -1617,26 +_,55 @@
1451+
@@ -1617,26 +_,61 @@
14521452
Optional<LastSeenMessages> unpackedLastSeen = this.unpackAndApplyLastSeen(packet.lastSeenMessages());
14531453
if (!unpackedLastSeen.isEmpty()) {
14541454
this.tryHandleChat(packet.command(), true, () -> {
@@ -1477,11 +1477,18 @@
14771477
+ commandString = event.getMessage().substring(1);
14781478
+
14791479
ParseResults<CommandSourceStack> command = this.parseCommand(packet.command());
1480+
+ // Paper start - Parse client-visible argument types when a custom parser rejects a signed command
1481+
+ ParseResults<CommandSourceStack> signingCommand = command;
1482+
+ if (!command.getExceptions().isEmpty() && !packet.argumentSignatures().entries().isEmpty()) {
1483+
+ signingCommand = io.papermc.paper.command.brigadier.argument.WrappedArgumentCommandNode.parseNativeArguments(() -> this.parseCommand(packet.command()));
1484+
+ }
1485+
+ // Paper end - Parse client-visible argument types when a custom parser rejects a signed command
14801486

14811487
Map<String, PlayerChatMessage> signedArguments;
14821488
try {
1489+
- signedArguments = this.collectSignedArguments(packet, SignableCommand.of(command), lastSeenMessages);
14831490
+ // Paper - Always parse the original command to add to the chat chain
1484-
signedArguments = this.collectSignedArguments(packet, SignableCommand.of(command), lastSeenMessages);
1491+
+ signedArguments = this.collectSignedArguments(packet, SignableCommand.of(signingCommand), lastSeenMessages);
14851492
} catch (SignedMessageChain.DecodeException e) {
14861493
this.handleMessageDecodeFailure(e);
14871494
return;

‎paper-server/src/main/java/io/papermc/paper/command/brigadier/argument/WrappedArgumentCommandNode.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,31 @@
1414
import net.minecraft.commands.synchronization.ArgumentTypeInfos;
1515

1616
import java.util.function.Predicate;
17+
import java.util.function.Supplier;
1718

1819
/*
1920
Basically this converts the argument to a different type when parsing.
2021
*/
2122
public class WrappedArgumentCommandNode<NMS, API> extends ArgumentCommandNode<CommandSourceStack, NMS> {
2223

24+
// Signature checks must parse the same argument types the client received.
25+
private static final ThreadLocal<Boolean> PARSE_NATIVE_ARGUMENTS = ThreadLocal.withInitial(() -> false);
2326
private final ArgumentType<API> pureArgumentType;
2427

28+
public static <T> T parseNativeArguments(final Supplier<T> action) {
29+
boolean previous = PARSE_NATIVE_ARGUMENTS.get();
30+
PARSE_NATIVE_ARGUMENTS.set(true);
31+
try {
32+
return action.get();
33+
} finally {
34+
if (previous) {
35+
PARSE_NATIVE_ARGUMENTS.set(true);
36+
} else {
37+
PARSE_NATIVE_ARGUMENTS.remove();
38+
}
39+
}
40+
}
41+
2542
public WrappedArgumentCommandNode(
2643
final String name,
2744
final ArgumentType<API> pureArgumentType,
@@ -45,6 +62,11 @@ public WrappedArgumentCommandNode(
4562
// See ArgumentCommandNode#parse
4663
@Override
4764
public void parse(final StringReader reader, final CommandContextBuilder<CommandSourceStack> contextBuilder) throws CommandSyntaxException {
65+
if (PARSE_NATIVE_ARGUMENTS.get()) {
66+
super.parse(reader, contextBuilder);
67+
return;
68+
}
69+
4870
final int start = reader.getCursor();
4971
final API result = this.pureArgumentType.parse(reader, contextBuilder.getSource()); // Use the api argument parser
5072
final ParsedArgument<CommandSourceStack, API> parsed = new ParsedArgument<>(start, reader.getCursor(), result); // Return an API parsed argument instead.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.papermc.paper.command.brigadier.argument;
2+
3+
import com.mojang.brigadier.CommandDispatcher;
4+
import com.mojang.brigadier.LiteralMessage;
5+
import com.mojang.brigadier.StringReader;
6+
import com.mojang.brigadier.arguments.ArgumentType;
7+
import com.mojang.brigadier.arguments.StringArgumentType;
8+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
9+
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
10+
import com.mojang.brigadier.tree.ArgumentCommandNode;
11+
import io.papermc.paper.command.brigadier.CommandSourceStack;
12+
import net.minecraft.commands.arguments.SignedArgument;
13+
import net.minecraft.network.chat.SignableCommand;
14+
import org.bukkit.support.environment.Normal;
15+
import org.junit.jupiter.api.Test;
16+
import org.mockito.Mockito;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
20+
@Normal
21+
class WrappedArgumentCommandNodeTest {
22+
23+
@Test
24+
void nativeParseKeepsSignedArgumentAfterCustomParseFailure() {
25+
CommandDispatcher<CommandSourceStack> dispatcher = new CommandDispatcher<>();
26+
ArgumentType<String> custom = reader -> {
27+
StringArgumentType.word().parse(reader);
28+
throw new SimpleCommandExceptionType(new LiteralMessage("unknown player")).create();
29+
};
30+
WrappedArgumentCommandNode<String, String> wrapped = new WrappedArgumentCommandNode<>(
31+
"player", custom, StringArgumentType.word(), null, source -> true, null, null, false, null
32+
);
33+
SignedArgument<String> signed = new SignedArgument<>() {
34+
@Override
35+
public String parse(final StringReader reader) throws CommandSyntaxException {
36+
return StringArgumentType.word().parse(reader);
37+
}
38+
};
39+
wrapped.addChild(new ArgumentCommandNode<>("message", signed, context -> 1, source -> true, null, null, false, null));
40+
dispatcher.getRoot().addChild(wrapped);
41+
CommandSourceStack source = Mockito.mock(CommandSourceStack.class);
42+
43+
assertEquals(0, SignableCommand.of(dispatcher.parse("missing hello", source)).arguments().size());
44+
assertEquals("message", SignableCommand.of(WrappedArgumentCommandNode.parseNativeArguments(
45+
() -> dispatcher.parse("missing hello", source)
46+
)).arguments().getFirst().name());
47+
assertEquals(0, SignableCommand.of(dispatcher.parse("missing hello", source)).arguments().size());
48+
}
49+
}

0 commit comments

Comments
 (0)