我目前正在尝试为1.15.2开发一个简单的Minecraft mod。我要创建的命令是使您的消息匿名化的命令,例如在伪造选举期间。它基本上接收您编写的消息,然后将其匿名发送给服务器中的每个人。
这是处理行为的命令类的代码:
public class CommandVote {
public static void register(CommandDispatcher<CommandSource> dispatcher) {
LiteralArgumentBuilder<CommandSource> builder = Commands.literal("vote").requires(source -> source.hasPermissionLevel(0));
builder.executes(context -> vote(context))
.then(Commands.argument("message", MessageArgument.message()))
.executes(context -> voteArgs(context));
dispatcher.register(builder);
}
private static int vote(CommandContext<CommandSource> context) throws CommandSyntaxException {
ServerPlayerEntity player = context.getSource().asPlayer();
player.sendMessage(new StringTextComponent("Por favor, introduzca su opcion en la votacion"));
return Command.SINGLE_SUCCESS;
}
private static int voteArgs(CommandContext<CommandSource> context) throws CommandSyntaxException {
ServerPlayerEntity player = context.getSource().asPlayer();
player.server.getPlayerList().sendMessage(MessageArgument.getMessage(context, "message"));
return Command.SINGLE_SUCCESS;
}
}
该命令注册,并显示在游戏中。问题出在我尝试执行它时,例如:“ / vote hello”。每当将参数添加到命令中时,Minecraft都会告诉我该命令不存在或“未知命令”
我不太了解我在这里搞砸了。朝着正确方向提出的任何建议都非常感谢。
提前谢谢您
谢谢您的提问!作为顶部函数的主体,这可能会更好地工作:
LiteralArgumentBuilder<CommandSource> builder = Commands.literal("vote")
.then(Commands.argument("number", IntegerArgumentType.integer())
.then(Commands.argument("message", MessageArgument.message())
.executes(context -> {
System.out.println("Level 3 " + context.getInput());
return Command.SINGLE_SUCCESS;
}))
.executes(context -> {
System.out.println("Level 2 " + context.getInput());
return Command.SINGLE_SUCCESS;
}))
.executes(context -> {
System.out.println("Level 1 " + context.getInput());
return Command.SINGLE_SUCCESS;
});
dispatcher.register(builder);