From a6f9e649ffa3c9a9ab50acf8016f38ef2dc6cdbd Mon Sep 17 00:00:00 2001 From: kuroppoi <68156848+kuroppoi@users.noreply.github.com> Date: Fri, 22 Jul 2022 11:48:41 +0200 Subject: [PATCH] Added a command to manipulate health --- .../gameserver/command/CommandManager.java | 2 + .../command/commands/HealthCommand.java | 75 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 gameserver/src/main/java/brainwine/gameserver/command/commands/HealthCommand.java diff --git a/gameserver/src/main/java/brainwine/gameserver/command/CommandManager.java b/gameserver/src/main/java/brainwine/gameserver/command/CommandManager.java index 1f3eaa1..4e390fd 100644 --- a/gameserver/src/main/java/brainwine/gameserver/command/CommandManager.java +++ b/gameserver/src/main/java/brainwine/gameserver/command/CommandManager.java @@ -17,6 +17,7 @@ import brainwine.gameserver.command.commands.EntityCommand; import brainwine.gameserver.command.commands.ExportCommand; import brainwine.gameserver.command.commands.GenerateZoneCommand; import brainwine.gameserver.command.commands.GiveCommand; +import brainwine.gameserver.command.commands.HealthCommand; import brainwine.gameserver.command.commands.HelpCommand; import brainwine.gameserver.command.commands.ImportCommand; import brainwine.gameserver.command.commands.KickCommand; @@ -71,6 +72,7 @@ public class CommandManager { registerCommand(new PositionCommand()); registerCommand(new RickrollCommand()); registerCommand(new EntityCommand()); + registerCommand(new HealthCommand()); } public static void executeCommand(CommandExecutor executor, String commandLine) { diff --git a/gameserver/src/main/java/brainwine/gameserver/command/commands/HealthCommand.java b/gameserver/src/main/java/brainwine/gameserver/command/commands/HealthCommand.java new file mode 100644 index 0000000..ecdd013 --- /dev/null +++ b/gameserver/src/main/java/brainwine/gameserver/command/commands/HealthCommand.java @@ -0,0 +1,75 @@ +package brainwine.gameserver.command.commands; + +import static brainwine.gameserver.entity.player.NotificationType.ALERT; + +import brainwine.gameserver.GameServer; +import brainwine.gameserver.command.Command; +import brainwine.gameserver.command.CommandExecutor; +import brainwine.gameserver.entity.player.Player; + +public class HealthCommand extends Command { + + @Override + public void execute(CommandExecutor executor, String[] args) { + Player target = null; + + if(args.length < 2) { + if(args.length == 0 || !(executor instanceof Player)) { + executor.notify(String.format("Usage: %s", getUsage(executor)), ALERT); + return; + } + target = (Player)executor; + } else { + target = GameServer.getInstance().getPlayerManager().getPlayer(args[1]); + } + + if(target == null) { + executor.notify("This player does not exist.", ALERT); + return; + } else if(!target.isOnline()) { + executor.notify("This player is offline.", ALERT); + return; + } + + float health = 0; + + try { + health = Float.parseFloat(args[0]); + } catch(NumberFormatException e) { + executor.notify("Health must be a valid number.", ALERT); + return; + } + + target.setHealth(health); + target.alert(String.format("Your health has been set to %s", target.getHealth())); + + if(target != executor) { + executor.notify(String.format("Set %s's health to %s", target.getName(), target.getHealth()), ALERT); + } + } + + @Override + public String getName() { + return "health"; + } + + @Override + public String[] getAliases() { + return new String[] { "hp" }; + } + + @Override + public String getDescription() { + return "Sets a player's health."; + } + + @Override + public String getUsage(CommandExecutor executor) { + return String.format("/health %s", executor instanceof Player ? "[player]" : ""); + } + + @Override + public boolean canExecute(CommandExecutor executor) { + return executor.isAdmin(); + } +}