User Tools

Site Tools


zh_cn:tutorial:command_examples

命令示例

这些例子使用 CC Attribution-Noncommercial-Share Alike 4.0 International 版权协议,即本 wiki 其他文章使用的版权协议。

发送一个消息

  1. public final class BroadCastCommand {
  2. public static void register(CommandDispatcher<ServerCommandSource> dispatcher){
  3. dispatcher.register(literal("broadcast")
  4. .requires(source -> source.hasPermissionLevel(2)) // 只有管理员能够执行命令。命令不会对非操作员或低于 1 级权限的玩家显示在 tab-完成中,也不会让他们执行。
  5. .then(argument("color", ColorArgumentType.color())
  6. .then(argument("message", greedyString())
  7. .executes(ctx -> broadcast(ctx.getSource(), getColor(ctx, "color"), getString(ctx, "message")))))); // 你可以在这里处理参数,并处理成命令。
  8. }
  9.  
  10. public static int broadcast(ServerCommandSource source, Formatting formatting, String message) {
  11. final Text text = Text.literal(message).formatted(formatting);
  12.  
  13. source.getMinecraftServer().getPlayerManager().broadcastChatMessage(text, MessageType.CHAT, source.getPlayer().getUuid());
  14. return Command.SINGLE_SUCCESS; // 成功
  15. }
  16. }

在你的 initializer 中:

  1. public class ExampleMod implements ModInitializer{
  2. @Override
  3. public void onInitialize() {
  4. ...
  5.  
  6. CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> register(dispatcher));
  7. }
  8. }

下面的例子会省略命令注册的过程。

/giveMeDiamond

首先,这个代码会注册 giveMeDiamond 作为纯文本,以及一个“executes”块以将方法返回的内容告诉分发器。

  1. public static LiteralCommandNode register(CommandDispatcher<ServerCommandSource> dispatcher) { // 你也可以返回一个,以用于可能的重定向
  2. return dispatcher.register(literal("giveMeDiamond")
  3. .executes(ctx -> giveDiamond(ctx)));
  4. }

由于我们只会给予玩家,我们检查 CommandSources 是否为玩家。但是我们可以使用 getPlayer 并同时做这两个,并在源不是玩家时抛出错误。

  1. public static int giveDiamond(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
  2. final ServerCommandSource source = ctx.getSource();
  3.  
  4. final PlayerEntity self = source.getPlayer(); // 如果不是玩家,命令结束

现在我们将其添加到玩家的物品栏中,并检查物品栏是否已满

  1. if(!player.inventory.insertStack(new ItemStack(Items.DIAMOND))){
  2. throw new SimpleCommandExceptionType(Text.translatable("inventory.isfull")).create();
  3. }
  4.  
  5. return 1;
  6. }

Antioch

…lobbest thou thy Holy Hand Grenade of Antioch towards thy foe. who being naughty in My sight, shall snuff it.

如同一个玩笑,此命令在指定的位置或者在执行者指针的的位置召唤一个激活的 TNT。

首先向 CommandDispatcher 创建一个项,接收一个纯文本 antioch,以及一个可选的参数,表示实体召唤的位置。

  1. public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
  2. dispatcher.register(literal("antioch")
  3. .then(required("location", BlockPosArgumentType.blockPos()
  4. .executes(ctx -> antioch(ctx.getSource(), BlockPosArgument.getBlockPos(ctx, "location")))))
  5. .executes(ctx -> antioch(ctx.getSource(), null)));
  6. }

然后是创建,以及 joke 后的消息。

  1. public static int antioch(ServerCommandSource source, BlockPos blockPos) throws CommandSyntaxException {
  2. if(blockPos == null) {
  3. // 对于没有输入参数的情况,我们计算当前玩家指针的位置,如果最近的位置太远或者在世界外面,则抛出错误。
  4. // 这里仅用作一个例子,实际上并不存在。
  5. blockPos = LocationUtil.calculateCursorOrThrow(source, source.getRotation());
  6. }
  7.  
  8. final TntEntity tnt = new TntEntity(source.getWorld(), blockPos.getX(), blockPos.getY(), blockPos.getZ(), null);
  9. tnt.setFuse(3);
  10.  
  11. source.getServer().getPlayerManager().broadcastChatMessage(Text.literal("...lobbest thou thy Holy Hand Grenade of Antioch towards thy foe", MessageType.CHAT, UUID.randomUUID()).formatted(Formatting.RED), false);
  12. source.getServer().getPlayerManager().broadcastChatMessage(Text.literal("who being naughty in My sight, shall snuff it.", MessageType.CHAT, UUID.randomUUID()).formatted(Formatting.RED), false);
  13. source.getWorld().spawnEntity(tnt);
  14. return 1;
  15. }
zh_cn/tutorial/command_examples.txt · Last modified: 2023/04/20 11:23 by solidblock