SpectateCommand.java

net.minecraft.server.commands.SpectateCommand

信息

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

    TODO

字段/常量

  • ERROR_SELF

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

      TODO

  • ERROR_NOT_SPECTATOR

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

      TODO

  • ERROR_CANNOT_SPECTATE

    • 类型: DynamicCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L20
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

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

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

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

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

private static int spectate(CommandSourceStack source, Entity target, ServerPlayer player) @ L40

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

参数:

  • source: CommandSourceStack
  • target: Entity
  • player: ServerPlayer

说明:

TODO

代码

public class SpectateCommand {
    private static final SimpleCommandExceptionType ERROR_SELF = new SimpleCommandExceptionType(Component.translatable("commands.spectate.self"));
    private static final DynamicCommandExceptionType ERROR_NOT_SPECTATOR = new DynamicCommandExceptionType(
        s -> Component.translatableEscape("commands.spectate.not_spectator", s)
    );
    private static final DynamicCommandExceptionType ERROR_CANNOT_SPECTATE = new DynamicCommandExceptionType(
        s -> Component.translatableEscape("commands.spectate.cannot_spectate", s)
    );
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
            Commands.literal("spectate")
                .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
                .executes(c -> spectate(c.getSource(), null, c.getSource().getPlayerOrException()))
                .then(
                    Commands.argument("target", EntityArgument.entity())
                        .executes(c -> spectate(c.getSource(), EntityArgument.getEntity(c, "target"), c.getSource().getPlayerOrException()))
                        .then(
                            Commands.argument("player", EntityArgument.player())
                                .executes(c -> spectate(c.getSource(), EntityArgument.getEntity(c, "target"), EntityArgument.getPlayer(c, "player")))
                        )
                )
        );
    }
 
    private static int spectate(CommandSourceStack source, @Nullable Entity target, ServerPlayer player) throws CommandSyntaxException {
        if (player == target) {
            throw ERROR_SELF.create();
        } else if (!player.isSpectator()) {
            throw ERROR_NOT_SPECTATOR.create(player.getDisplayName());
        } else if (target != null && target.getType().clientTrackingRange() == 0) {
            throw ERROR_CANNOT_SPECTATE.create(target.getDisplayName());
        } else {
            player.setCamera(target);
            if (target != null) {
                source.sendSuccess(() -> Component.translatable("commands.spectate.success.started", target.getDisplayName()), false);
            } else {
                source.sendSuccess(() -> Component.translatable("commands.spectate.success.stopped"), false);
            }
 
            return 1;
        }
    }
}

引用的其他类

  • CommandSourceStack

    • 引用位置: 参数
  • Commands

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

    • 引用位置: 方法调用
    • 关联成员: EntityArgument.entity(), EntityArgument.getEntity(), EntityArgument.getPlayer(), EntityArgument.player()
  • Component

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

    • 引用位置: 参数
  • Entity

    • 引用位置: 参数