1
0
Fork 0
minecraft-tweaks-2a03/src/main/java/party/_2a03/mc/command/SpawnCommand.java

52 lines
2.1 KiB
Java

package party._2a03.mc.command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.util.registry.RegistryKey;
import party._2a03.mc.util.Config;
import party._2a03.mc.util.PlayerPosition;
public class SpawnCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
LiteralArgumentBuilder<ServerCommandSource> literalargumentbuilder = CommandManager.literal("spawn").executes(ctx -> {
ServerCommandSource source = ctx.getSource();
ServerPlayerEntity sender = source.getPlayer();
PlayerPosition position = new PlayerPosition(Config.getData("spawn"));
if (position.registrykey == null) {
if (source.hasPermissionLevel(2)) {
source.sendFeedback(new LiteralText("Spawn not found, do /spawn set"), false);
} else {
source.sendFeedback(new LiteralText("Spawn not found, ask an admin to set it"), false);
}
return -1;
}
sender.teleport(sender.getServer().getWorld(position.registrykey), position.x, position.y, position.z, position.yaw, position.pitch);
source.sendFeedback(new LiteralText("Teleported to the spawn point"), true);
return 1;
});
literalargumentbuilder.then(CommandManager.literal("set").requires(ctx -> {
return ctx.hasPermissionLevel(2);
}).executes(ctx -> {
ServerCommandSource source = ctx.getSource();
ServerPlayerEntity sender = source.getPlayer();
double x = sender.getX();
double y = sender.getY();
double z = sender.getZ();
float yaw = sender.getYaw();
float pitch = sender.getPitch();
RegistryKey registrykey = sender.getWorld().getRegistryKey();
PlayerPosition location = new PlayerPosition(x, y, z, yaw, pitch, registrykey);
Config.setData("spawn", location.getJSON());
source.sendFeedback(new LiteralText("Spawn has been set"), true);
return 1;
}));
dispatcher.register(literalargumentbuilder);
}
}