Skip to content

Commit c7f3d9f

Browse files
committed
chore: simplify custom argument errors
1 parent ba99e1a commit c7f3d9f

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/content/docs/paper/dev/api/command-api/basics/arguments-and-literals.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,4 @@ can read more about these [here](/paper/dev/command-api/arguments/minecraft).
173173

174174
### Custom arguments
175175
Sometimes you want to define your own, custom arguments. For that you can implement the `CustomArgumentType<T, N>` interface.
176+
You can read more about these [here](/paper/dev/command-api/basics/custom-arguments).

src/content/docs/paper/dev/api/command-api/basics/custom-arguments.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Commands.argument("player", ArgumentTypes.player())
2525
final Player player = ctx.getArgument("player", PlayerSelectorArgumentResolver.class).resolve(ctx.getSource()).getFirst();
2626
if (!player.isOp()) {
2727
final Message message = MessageComponentSerializer.message().serialize(text(player.getName() + " is not a server operator!"));
28-
throw new CommandSyntaxException(new SimpleCommandExceptionType(message), message);
28+
throw new SimpleCommandExceptionType(message).create();
2929
}
3030

3131
ctx.getSource().getSender().sendRichMessage("Player <player> is an operator!",
@@ -44,6 +44,14 @@ The solution to this problem are custom arguments. Before going into detail abou
4444
@NullMarked
4545
public final class OppedPlayerArgument implements CustomArgumentType<Player, PlayerSelectorArgumentResolver> {
4646

47+
private static final SimpleCommandExceptionType ERROR_BAD_SOURCE = new SimpleCommandExceptionType(
48+
MessageComponentSerializer.message().serialize(Component.text("The source needs to be a CommandSourceStack!"))
49+
);
50+
51+
private static final DynamicCommandExceptionType ERROR_NOT_OPERATOR = new DynamicCommandExceptionType(name -> {
52+
return MessageComponentSerializer.message().serialize(Component.text(name + " is not a server operator!"));
53+
});
54+
4755
@Override
4856
public Player parse(StringReader reader) {
4957
throw new UnsupportedOperationException("This method will never be called.");
@@ -52,14 +60,12 @@ public final class OppedPlayerArgument implements CustomArgumentType<Player, Pla
5260
@Override
5361
public <S> Player parse(StringReader reader, S source) throws CommandSyntaxException {
5462
if (!(source instanceof CommandSourceStack stack)) {
55-
final Message message = MessageComponentSerializer.message().serialize(Component.text("The source needs to be a CommandSourceStack!"));
56-
throw new CommandSyntaxException(new SimpleCommandExceptionType(message), message);
63+
throw ERROR_BAD_SOURCE.create();
5764
}
5865

5966
final Player player = getNativeType().parse(reader).resolve(stack).getFirst();
6067
if (!player.isOp()) {
61-
final Message message = MessageComponentSerializer.message().serialize(Component.text(player.getName() + " is not a server operator!"));
62-
throw new CommandSyntaxException(new SimpleCommandExceptionType(message), message);
68+
throw ERROR_NOT_OPERATOR.create(player.getName());
6369
}
6470

6571
return player;
@@ -226,14 +232,16 @@ package io.papermc.commands.icecream;
226232
@NullMarked
227233
public class IceCreamArgument implements CustomArgumentType.Converted<IceCreamFlavor, String> {
228234

235+
private static final DynamicCommandExceptionType ERROR_INVALID_FLAVOR = new DynamicCommandExceptionType(flavor -> {
236+
return MessageComponentSerializer.message().serialize(Component.text(flavor + " is not a valid flavor!"));
237+
});
238+
229239
@Override
230240
public IceCreamFlavor convert(String nativeType) throws CommandSyntaxException {
231241
try {
232242
return IceCreamFlavor.valueOf(nativeType.toUpperCase(Locale.ROOT));
233-
}
234-
catch (IllegalArgumentException e) {
235-
final Message message = MessageComponentSerializer.message().serialize(Component.text(nativeType + " is not a valid flavor!"));
236-
throw new CommandSyntaxException(new SimpleCommandExceptionType(message), message);
243+
} catch (IllegalArgumentException ignored) {
244+
throw ERROR_INVALID_FLAVOR.create(nativeType);
237245
}
238246
}
239247

0 commit comments

Comments
 (0)