feat: added debug commands /pos /tp /gm
This commit is contained in:
parent
c4c39c6f11
commit
49b0b838cd
@ -8,6 +8,7 @@ import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.event.player.AsyncPlayerConfigurationEvent;
|
||||
import net.minestom.server.event.player.PlayerDisconnectEvent;
|
||||
import net.minestom.server.event.player.PlayerSpawnEvent;
|
||||
import ru.dragonestia.msb3.api.command.DebugCommands;
|
||||
import ru.dragonestia.msb3.api.dialog.DialogRegistry;
|
||||
import ru.dragonestia.msb3.api.dialog.action.CloseDialogActionHandler;
|
||||
import ru.dragonestia.msb3.api.dialog.action.DialogDialogActionHandler;
|
||||
@ -93,12 +94,10 @@ public final class ServerBootstrap {
|
||||
initializer.onResourcePackCompiled(Resources.getResourcePack());
|
||||
|
||||
initPlayerContextManager();
|
||||
|
||||
initDefaultSkins();
|
||||
|
||||
initDefaultDialogActionsAndConditions();
|
||||
|
||||
NPCClickEvent.init();
|
||||
DebugCommands.init();
|
||||
}
|
||||
|
||||
private void initDefaultModules() {
|
||||
|
||||
@ -0,0 +1,106 @@
|
||||
package ru.dragonestia.msb3.api.command;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.event.ClickEvent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.command.builder.Command;
|
||||
import net.minestom.server.command.builder.arguments.ArgumentEnum;
|
||||
import net.minestom.server.command.builder.arguments.ArgumentType;
|
||||
import net.minestom.server.entity.GameMode;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.utils.location.RelativeVec;
|
||||
import ru.dragonestia.msb3.api.util.Env;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Log4j2
|
||||
@UtilityClass
|
||||
public class DebugCommands {
|
||||
|
||||
public void init() {
|
||||
if (!Env.bool("MSB3_DEBUG").orElse(false)) return;
|
||||
|
||||
registerPosCommand();
|
||||
registerTeleportCommand();
|
||||
registerGameModeCommand();
|
||||
|
||||
log.info("Registered debug commands");
|
||||
}
|
||||
|
||||
private void registerPosCommand() {
|
||||
var command = new Command("pos");
|
||||
var counter = new AtomicInteger(0);
|
||||
command.setDefaultExecutor((sender, ctx) -> {
|
||||
var player = (Player) sender;
|
||||
var pos = player.getPosition();
|
||||
var decimal = new DecimalFormat("#.00");
|
||||
var result = "new Pos(%s, %s, %s, %sf, %sf)".formatted(
|
||||
decimal.format(pos.x()).replace(",", "."),
|
||||
decimal.format(pos.y()).replace(",", "."),
|
||||
decimal.format(pos.z()).replace(",", "."),
|
||||
decimal.format(pos.yaw()).replace(",", "."),
|
||||
decimal.format(pos.pitch()).replace(",", ".")
|
||||
);
|
||||
|
||||
var color = (counter.getAndIncrement() & 1) == 1 ? NamedTextColor.YELLOW : NamedTextColor.GOLD;
|
||||
var component = Component.text(result, color, TextDecoration.UNDERLINED)
|
||||
.clickEvent(ClickEvent.clickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, result));
|
||||
player.sendMessage(Component.text(counter.get() + ") Pos: ").append(component));
|
||||
|
||||
log.info("Pos: {}", result);
|
||||
});
|
||||
MinecraftServer.getCommandManager().register(command);
|
||||
}
|
||||
|
||||
private void registerTeleportCommand() {
|
||||
var command = new Command("tp");
|
||||
command.setDefaultExecutor((source, context) -> source.sendMessage(Component.text("Использование: /tp x y z")));
|
||||
var posArg = ArgumentType.RelativeBlockPosition("pos");
|
||||
command.addSyntax((sender, ctx) -> {
|
||||
var player = (Player) sender;
|
||||
RelativeVec relativeVec = ctx.get("pos");
|
||||
var position = player.getPosition().withCoord(relativeVec.fromSender(player));
|
||||
player.teleport(position);
|
||||
player.sendMessage(Component.text("Выбили телепортирован на " + position));
|
||||
}, posArg);
|
||||
MinecraftServer.getCommandManager().register(command);
|
||||
}
|
||||
|
||||
private void registerGameModeCommand() {
|
||||
var command = new Command("gm", "gamemode");
|
||||
|
||||
ArgumentEnum<GameMode> gamemode = ArgumentType.Enum("gamemode", GameMode.class).setFormat(ArgumentEnum.Format.LOWER_CASED);
|
||||
gamemode.setCallback((sender, exception) -> {
|
||||
sender.sendMessage(Component.text("Invalid gamemode ", NamedTextColor.RED)
|
||||
.append(Component.text(exception.getInput(), NamedTextColor.WHITE))
|
||||
.append(Component.text("!")));
|
||||
});
|
||||
|
||||
|
||||
command.setDefaultExecutor((sender, context) -> {
|
||||
var commandName = context.getCommandName();
|
||||
sender.sendMessage(Component.text("Использование: /" + commandName + " <gamemode>", NamedTextColor.RED));
|
||||
});
|
||||
|
||||
command.addSyntax((sender, context) -> {
|
||||
if (!(sender instanceof Player p)) {
|
||||
sender.sendMessage(Component.text("Please run this command in-game.", NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
|
||||
var mode = context.get(gamemode);
|
||||
|
||||
p.setGameMode(mode);
|
||||
String gamemodeString = "gameMode." + mode.name().toLowerCase(Locale.ROOT);
|
||||
Component gamemodeComponent = Component.translatable(gamemodeString);
|
||||
sender.sendMessage(Component.translatable("commands.gamemode.success.self", gamemodeComponent));
|
||||
}, gamemode);
|
||||
MinecraftServer.getCommandManager().register(command);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user