SetWorldSpawnCommand.java

net.minecraft.server.commands.SetWorldSpawnCommand

信息

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

    TODO

字段/常量

内部类/嵌套类型

构造器

方法

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

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

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

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

private static int setSpawn(CommandSourceStack source, BlockPos pos, Coordinates rotation) @ L33

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

参数:

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

说明:

TODO

代码

public class SetWorldSpawnCommand {
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
            Commands.literal("setworldspawn")
                .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
                .executes(c -> setSpawn(c.getSource(), BlockPos.containing(c.getSource().getPosition()), WorldCoordinates.ZERO_ROTATION))
                .then(
                    Commands.argument("pos", BlockPosArgument.blockPos())
                        .executes(c -> setSpawn(c.getSource(), BlockPosArgument.getSpawnablePos(c, "pos"), WorldCoordinates.ZERO_ROTATION))
                        .then(
                            Commands.argument("rotation", RotationArgument.rotation())
                                .executes(c -> setSpawn(c.getSource(), BlockPosArgument.getSpawnablePos(c, "pos"), RotationArgument.getRotation(c, "rotation")))
                        )
                )
        );
    }
 
    private static int setSpawn(CommandSourceStack source, BlockPos pos, Coordinates rotation) {
        ServerLevel level = source.getLevel();
        Vec2 rotationVector = rotation.getRotation(source);
        float yaw = rotationVector.y;
        float pitch = rotationVector.x;
        LevelData.RespawnData respawnData = LevelData.RespawnData.of(level.dimension(), pos, yaw, pitch);
        level.setRespawnData(respawnData);
        source.sendSuccess(
            () -> Component.translatable(
                "commands.setworldspawn.success",
                pos.getX(),
                pos.getY(),
                pos.getZ(),
                respawnData.yaw(),
                respawnData.pitch(),
                level.dimension().identifier().toString()
            ),
            true
        );
        return 1;
    }
}

引用的其他类

  • CommandSourceStack

    • 引用位置: 参数
  • Commands

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

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

    • 引用位置: 参数
  • RotationArgument

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

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

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

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