Question argument

Chara_SS

New member
Aug 1, 2024
2
0
1
Hi everyone.
I am trying to register a command with paper-plugin's command api in folia, and got cast Exception.
public void createCommand(String name) {
cmd = literal(name)
.then(
literal("install")
.then(
argument("player", ArgumentTypes.player())
.executes(context -> installPlayer(context.getArgument("player", Player.class)))
)
.executes(
context -> {
var sender = context.getSource().getSender();
if (sender instanceof Player player) {
return installPlayer(player);
}
sender.sendMessage(Component.text("只有玩家才能用这个命令"));
return -1;
}
)
)
.executes(context -> {
context.getSource().getSender().sendMessage("Hello, MNL!");
return 1;
})
.build();
}

public static int installPlayer(@NotNull Player player) {
player.sendMessage(
Component.text("Hi! %s!, Your pos is %s, %s, %s".
formatted(
player.getName(),
player.getLocation().getBlockX(),
player.getLocation().getBlockY(),
player.getLocation().getBlockZ()
)));
return 1;
}
log:
[15:40:12 ERROR]: Command exception: /mnl install Dev
java.lang.IllegalArgumentException: Argument 'player' is defined as VanillaArgumentProviderImpl$$Lambda/0x000001e30847e540, not interface org.bukkit.entity.Entity
at com.mojang.brigadier.context.CommandContext.getArgument(CommandContext.java:109) ~[brigadier-1.2.9.jar:?]
at MNL-1.0-SNAPSHOT.jar/top.kelcey.mnl.command.MNLCommand.lambda$createCommand$0(MNLCommand.java:28) ~[MNL-1.0-SNAPSHOT.jar:?]
at com.mojang.brigadier.context.ContextChain.runExecutable(ContextChain.java:73) ~[brigadier-1.2.9.jar:?]
at net.minecraft.commands.execution.tasks.ExecuteCommand.execute(ExecuteCommand.java:31) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.commands.execution.tasks.ExecuteCommand.execute(ExecuteCommand.java:19) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.commands.execution.UnboundEntryAction.lambda$bind$0(UnboundEntryAction.java:8) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.commands.execution.CommandQueueEntry.execute(CommandQueueEntry.java:5) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.commands.execution.ExecutionContext.runCommandQueue(ExecutionContext.java:103) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.commands.Commands.executeCommandInContext(Commands.java:443) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.commands.Commands.performCommand(Commands.java:350) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.commands.Commands.performCommand(Commands.java:337) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.commands.Commands.performCommand(Commands.java:332) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.server.network.ServerGamePacketListenerImpl.performUnsignedChatCommand(ServerGamePacketListenerImpl.java:2147) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.server.network.ServerGamePacketListenerImpl.lambda$handleChatCommand$11(ServerGamePacketListenerImpl.java:2121) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.server.TickTask.run(TickTask.java:18) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.util.thread.BlockableEventLoop.doRunTask(BlockableEventLoop.java:151) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.util.thread.ReentrantBlockableEventLoop.doRunTask(ReentrantBlockableEventLoop.java:24) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:1537) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:201) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.util.thread.BlockableEventLoop.pollTask(BlockableEventLoop.java:125) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.server.MinecraftServer.pollTaskInternal(MinecraftServer.java:1514) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.server.MinecraftServer.pollTask(MinecraftServer.java:1507) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.util.thread.BlockableEventLoop.managedBlock(BlockableEventLoop.java:135) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.server.MinecraftServer.managedBlock(MinecraftServer.java:1466) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.server.MinecraftServer.waitUntilNextTick(MinecraftServer.java:1473) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1318) ~[paper-1.21.jar:1.21-123-0a1be9a]
at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:330) ~[paper-1.21.jar:1.21-123-0a1be9a]
at java.base/java.lang.Thread.run(Thread.java:1583) ~[?:?]
 

MinecraftJoe09

New member
Aug 12, 2024
1
1
3
You need to use PlayerSelectorArgumentResolver.class instead of Player.class. You can then get the player with context.getArgument("player", PlayerSelectorArgumentResolver.class).resolve(context.getSource()).getFirst().
 
  • Like
Reactions: Chara_SS

Machine Maker

Paper Developer
Staff member
Dec 17, 2021
148
6
22
18
California
In the future, look at the generic parameter for the ArgumentType you are using from the ArgumentTypes class. That will show you the type you have to use in the getArgument() method.