SwingCommand.java

net.minecraft.server.commands.SwingCommand

信息

  • 全限定名:net.minecraft.server.commands.SwingCommand
  • 类型:public class
  • 包:net.minecraft.server.commands
  • 源码路径:src/main/java/net/minecraft/server/commands/SwingCommand.java
  • 起始行号:L16
  • 职责:

    TODO

字段/常量

  • ERROR_NO_LIVING_ENTITY
    • 类型: SimpleCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L17
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

下面的方法块按源码顺序生成。

public static void register(CommandDispatcher<CommandSourceStack> dispatcher) @ L21

  • 方法名:register
  • 源码定位:L21
  • 返回类型:void
  • 修饰符:public static

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

private static int swing(CommandSourceStack source, Collection<?extends Entity> targets, InteractionHand hand) @ L40

  • 方法名:swing
  • 源码定位:L40
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • targets: Collection<?extends Entity>
  • hand: InteractionHand

说明:

TODO

代码

public class SwingCommand {
    private static final SimpleCommandExceptionType ERROR_NO_LIVING_ENTITY = new SimpleCommandExceptionType(
        Component.translatable("commands.swing.failed.notliving")
    );
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
            Commands.literal("swing")
                .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
                .executes(c -> swing(c.getSource(), List.of(c.getSource().getEntityOrException()), InteractionHand.MAIN_HAND))
                .then(
                    Commands.argument("targets", EntityArgument.entities())
                        .executes(c -> swing(c.getSource(), EntityArgument.getEntities(c, "targets"), InteractionHand.MAIN_HAND))
                        .then(
                            Commands.literal("mainhand")
                                .executes(c -> swing(c.getSource(), EntityArgument.getEntities(c, "targets"), InteractionHand.MAIN_HAND))
                        )
                        .then(
                            Commands.literal("offhand").executes(c -> swing(c.getSource(), EntityArgument.getEntities(c, "targets"), InteractionHand.OFF_HAND))
                        )
                )
        );
    }
 
    private static int swing(CommandSourceStack source, Collection<? extends Entity> targets, InteractionHand hand) throws CommandSyntaxException {
        int livingEntitiesCount = 0;
 
        for (Entity entity : targets) {
            if (entity instanceof LivingEntity livingEntity) {
                livingEntity.swing(hand, true);
                livingEntitiesCount++;
            }
        }
 
        if (livingEntitiesCount == 0) {
            throw ERROR_NO_LIVING_ENTITY.create();
        } else {
            if (livingEntitiesCount == 1) {
                source.sendSuccess(() -> Component.translatable("commands.swing.success.single", targets.iterator().next().getDisplayName()), true);
            } else {
                int count = livingEntitiesCount;
                source.sendSuccess(() -> Component.translatable("commands.swing.success.multiple", count), true);
            }
 
            return livingEntitiesCount;
        }
    }
}

引用的其他类

  • CommandSourceStack

    • 引用位置: 参数
  • Commands

    • 引用位置: 方法调用
    • 关联成员: Commands.argument(), Commands.hasPermission(), Commands.literal()
  • EntityArgument

    • 引用位置: 方法调用
    • 关联成员: EntityArgument.entities(), EntityArgument.getEntities()
  • Component

    • 引用位置: 方法调用
    • 关联成员: Component.translatable()
  • InteractionHand

    • 引用位置: 参数
  • Entity

    • 引用位置: 参数