From 4899a2c14d2db965e36f0214ee7e2b763c4accaa Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Wed, 23 Oct 2024 12:18:00 +0200 Subject: [PATCH] Add /god command --- .../java/cc/reconnected/server/RccServer.java | 2 + .../server/commands/GodCommand.java | 75 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/main/java/cc/reconnected/server/commands/GodCommand.java diff --git a/src/main/java/cc/reconnected/server/RccServer.java b/src/main/java/cc/reconnected/server/RccServer.java index 7492bd5..4439c5e 100644 --- a/src/main/java/cc/reconnected/server/RccServer.java +++ b/src/main/java/cc/reconnected/server/RccServer.java @@ -85,6 +85,8 @@ public class RccServer implements ModInitializer { CommandRegistrationCallback.EVENT.register(TellCommand::register); CommandRegistrationCallback.EVENT.register(ReplyCommand::register); FlyCommand.register(dispatcher, registryAccess, environment); + GodCommand.register(dispatcher, registryAccess, environment); + }); ServerLifecycleEvents.SERVER_STARTED.register(server -> { luckPerms = LuckPermsProvider.get(); diff --git a/src/main/java/cc/reconnected/server/commands/GodCommand.java b/src/main/java/cc/reconnected/server/commands/GodCommand.java new file mode 100644 index 0000000..c0d793c --- /dev/null +++ b/src/main/java/cc/reconnected/server/commands/GodCommand.java @@ -0,0 +1,75 @@ +package cc.reconnected.server.commands; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.StringArgumentType; +import me.lucko.fabric.api.permissions.v0.Permissions; +import net.minecraft.command.CommandRegistryAccess; +import net.minecraft.command.CommandSource; +import net.minecraft.server.command.CommandManager; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Style; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; + +import static net.minecraft.server.command.CommandManager.argument; +import static net.minecraft.server.command.CommandManager.literal; + +public class GodCommand { + public static void register(CommandDispatcher dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) { + var rootCommand = literal("god") + .requires(Permissions.require("rcc.command.god", 3)) + .executes(context -> { + if (!context.getSource().isExecutedByPlayer()) { + context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false); + return 1; + } + var player = context.getSource().getPlayer(); + context.getSource().sendFeedback(() -> toggleGod(player), true); + + return 1; + }) + .then(argument("player", StringArgumentType.word()) + .suggests((context, builder) -> { + var playerManager = context.getSource().getServer().getPlayerManager(); + return CommandSource.suggestMatching( + playerManager.getPlayerNames(), + builder); + }) + .executes(context -> { + var playerName = StringArgumentType.getString(context, "player"); + var player = context.getSource().getServer().getPlayerManager().getPlayer(playerName); + if(player == null) { + context.getSource().sendFeedback(() -> Text.literal("Player not found").setStyle(Style.EMPTY.withColor(Formatting.RED)), false); + return 1; + } + + context.getSource().sendFeedback(() -> toggleGod(player), true); + + return 1; + })); + + dispatcher.register(rootCommand); + } + + private static Text toggleGod(ServerPlayerEntity player) { + var abilities = player.getAbilities(); + + abilities.invulnerable = !abilities.invulnerable; + //player.setInvulnerable(!player.isInvulnerable()); + player.sendAbilitiesUpdate(); + + return Text.literal( + abilities.invulnerable ? + "God mode enabled" : + "God mode disabled" + ) + .append(" for ") + .append(player.getDisplayName()) + .setStyle(Style.EMPTY.withColor( + abilities.invulnerable ? + Formatting.GREEN : + Formatting.RED + )); + } +}