|
| 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