SetSpawnCommand.java

net.minecraft.server.commands.SetSpawnCommand

信息

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

    TODO

字段/常量

内部类/嵌套类型

构造器

方法

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

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

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

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

private static int setSpawn(CommandSourceStack source, Collection<ServerPlayer> targets, BlockPos pos, Coordinates rotation) @ L71

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

参数:

  • source: CommandSourceStack
  • targets: Collection
  • pos: BlockPos
  • rotation: Coordinates

说明:

TODO

代码

public class SetSpawnCommand {
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
            Commands.literal("spawnpoint")
                .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
                .executes(
                    c -> setSpawn(
                        c.getSource(),
                        Collections.singleton(c.getSource().getPlayerOrException()),
                        BlockPos.containing(c.getSource().getPosition()),
                        WorldCoordinates.ZERO_ROTATION
                    )
                )
                .then(
                    Commands.argument("targets", EntityArgument.players())
                        .executes(
                            c -> setSpawn(
                                c.getSource(),
                                EntityArgument.getPlayers(c, "targets"),
                                BlockPos.containing(c.getSource().getPosition()),
                                WorldCoordinates.ZERO_ROTATION
                            )
                        )
                        .then(
                            Commands.argument("pos", BlockPosArgument.blockPos())
                                .executes(
                                    c -> setSpawn(
                                        c.getSource(),
                                        EntityArgument.getPlayers(c, "targets"),
                                        BlockPosArgument.getSpawnablePos(c, "pos"),
                                        WorldCoordinates.ZERO_ROTATION
                                    )
                                )
                                .then(
                                    Commands.argument("rotation", RotationArgument.rotation())
                                        .executes(
                                            c -> setSpawn(
                                                c.getSource(),
                                                EntityArgument.getPlayers(c, "targets"),
                                                BlockPosArgument.getSpawnablePos(c, "pos"),
                                                RotationArgument.getRotation(c, "rotation")
                                            )
                                        )
                                )
                        )
                )
        );
    }
 
    private static int setSpawn(CommandSourceStack source, Collection<ServerPlayer> targets, BlockPos pos, Coordinates rotation) {
        ResourceKey<Level> dimension = source.getLevel().dimension();
        Vec2 rotationVector = rotation.getRotation(source);
        float yaw = Mth.wrapDegrees(rotationVector.y);
        float pitch = Mth.clamp(rotationVector.x, -90.0F, 90.0F);
 
        for (ServerPlayer target : targets) {
            target.setRespawnPosition(new ServerPlayer.RespawnConfig(LevelData.RespawnData.of(dimension, pos, yaw, pitch), true), false);
        }
 
        String dimensionName = dimension.identifier().toString();
        if (targets.size() == 1) {
            source.sendSuccess(
                () -> Component.translatable(
                    "commands.spawnpoint.success.single",
                    pos.getX(),
                    pos.getY(),
                    pos.getZ(),
                    yaw,
                    pitch,
                    dimensionName,
                    targets.iterator().next().getDisplayName()
                ),
                true
            );
        } else {
            source.sendSuccess(
                () -> Component.translatable(
                    "commands.spawnpoint.success.multiple", pos.getX(), pos.getY(), pos.getZ(), yaw, pitch, dimensionName, targets.size()
                ),
                true
            );
        }
 
        return targets.size();
    }
}

引用的其他类

  • CommandSourceStack

    • 引用位置: 参数
  • Commands

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

    • 引用位置: 方法调用
    • 关联成员: EntityArgument.getPlayers(), EntityArgument.players()
  • BlockPosArgument

    • 引用位置: 方法调用
    • 关联成员: BlockPosArgument.blockPos(), BlockPosArgument.getSpawnablePos()
  • Coordinates

    • 引用位置: 参数
  • RotationArgument

    • 引用位置: 方法调用
    • 关联成员: RotationArgument.getRotation(), RotationArgument.rotation()
  • BlockPos

    • 引用位置: 参数/方法调用
    • 关联成员: BlockPos.containing()
  • Component

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

    • 引用位置: 参数/方法调用/构造调用
    • 关联成员: RespawnConfig(), ServerPlayer.RespawnConfig()
  • Mth

    • 引用位置: 方法调用
    • 关联成员: Mth.clamp(), Mth.wrapDegrees()
  • LevelData

    • 引用位置: 方法调用
    • 关联成员: LevelData.RespawnData.of()