minecraft-src/net/minecraft/server/commands/SetSpawnCommand.java
2025-07-04 03:45:38 +03:00

83 lines
3 KiB
Java

package net.minecraft.server.commands;
import com.mojang.brigadier.CommandDispatcher;
import java.util.Collection;
import java.util.Collections;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.AngleArgument;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.Level;
public class SetSpawnCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(
Commands.literal("spawnpoint")
.requires(commandSourceStack -> commandSourceStack.hasPermission(2))
.executes(
commandContext -> setSpawn(
commandContext.getSource(),
Collections.singleton(commandContext.getSource().getPlayerOrException()),
BlockPos.containing(commandContext.getSource().getPosition()),
0.0F
)
)
.then(
Commands.argument("targets", EntityArgument.players())
.executes(
commandContext -> setSpawn(
commandContext.getSource(), EntityArgument.getPlayers(commandContext, "targets"), BlockPos.containing(commandContext.getSource().getPosition()), 0.0F
)
)
.then(
Commands.argument("pos", BlockPosArgument.blockPos())
.executes(
commandContext -> setSpawn(
commandContext.getSource(), EntityArgument.getPlayers(commandContext, "targets"), BlockPosArgument.getSpawnablePos(commandContext, "pos"), 0.0F
)
)
.then(
Commands.argument("angle", AngleArgument.angle())
.executes(
commandContext -> setSpawn(
commandContext.getSource(),
EntityArgument.getPlayers(commandContext, "targets"),
BlockPosArgument.getSpawnablePos(commandContext, "pos"),
AngleArgument.getAngle(commandContext, "angle")
)
)
)
)
)
);
}
private static int setSpawn(CommandSourceStack source, Collection<ServerPlayer> targets, BlockPos pos, float angle) {
ResourceKey<Level> resourceKey = source.getLevel().dimension();
for (ServerPlayer serverPlayer : targets) {
serverPlayer.setRespawnPosition(new ServerPlayer.RespawnConfig(resourceKey, pos, angle, true), false);
}
String string = resourceKey.location().toString();
if (targets.size() == 1) {
source.sendSuccess(
() -> Component.translatable(
"commands.spawnpoint.success.single", pos.getX(), pos.getY(), pos.getZ(), angle, string, ((ServerPlayer)targets.iterator().next()).getDisplayName()
),
true
);
} else {
source.sendSuccess(
() -> Component.translatable("commands.spawnpoint.success.multiple", pos.getX(), pos.getY(), pos.getZ(), angle, string, targets.size()), true
);
}
return targets.size();
}
}