minecraft-src/net/minecraft/server/commands/SetWorldSpawnCommand.java
2025-07-04 01:41:11 +03:00

45 lines
1.8 KiB
Java

package net.minecraft.server.commands;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.AngleArgument;
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.Level;
public class SetWorldSpawnCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(
Commands.literal("setworldspawn")
.requires(commandSourceStack -> commandSourceStack.hasPermission(2))
.executes(commandContext -> setSpawn(commandContext.getSource(), BlockPos.containing(commandContext.getSource().getPosition()), 0.0F))
.then(
Commands.argument("pos", BlockPosArgument.blockPos())
.executes(commandContext -> setSpawn(commandContext.getSource(), BlockPosArgument.getSpawnablePos(commandContext, "pos"), 0.0F))
.then(
Commands.argument("angle", AngleArgument.angle())
.executes(
commandContext -> setSpawn(
commandContext.getSource(), BlockPosArgument.getSpawnablePos(commandContext, "pos"), AngleArgument.getAngle(commandContext, "angle")
)
)
)
)
);
}
private static int setSpawn(CommandSourceStack source, BlockPos pos, float angle) {
ServerLevel serverLevel = source.getLevel();
if (serverLevel.dimension() != Level.OVERWORLD) {
source.sendFailure(Component.translatable("commands.setworldspawn.failure.not_overworld"));
return 0;
} else {
serverLevel.setDefaultSpawnPos(pos, angle);
source.sendSuccess(() -> Component.translatable("commands.setworldspawn.success", pos.getX(), pos.getY(), pos.getZ(), angle), true);
return 1;
}
}
}