diff --git a/gameserver/src/main/java/brainwine/gameserver/command/CommandManager.java b/gameserver/src/main/java/brainwine/gameserver/command/CommandManager.java index 2853ef1..6636738 100644 --- a/gameserver/src/main/java/brainwine/gameserver/command/CommandManager.java +++ b/gameserver/src/main/java/brainwine/gameserver/command/CommandManager.java @@ -11,6 +11,7 @@ import org.apache.logging.log4j.Logger; import brainwine.gameserver.command.commands.AdminCommand; import brainwine.gameserver.command.commands.BroadcastCommand; +import brainwine.gameserver.command.commands.GiveCommand; import brainwine.gameserver.command.commands.HelpCommand; import brainwine.gameserver.command.commands.KickCommand; import brainwine.gameserver.command.commands.PlayerIdCommand; @@ -53,6 +54,7 @@ public class CommandManager { registerCommand(new ZoneIdCommand()); registerCommand(new AdminCommand()); registerCommand(new HelpCommand()); + registerCommand(new GiveCommand()); } public static void executeCommand(CommandExecutor executor, String commandLine) { diff --git a/gameserver/src/main/java/brainwine/gameserver/command/commands/GiveCommand.java b/gameserver/src/main/java/brainwine/gameserver/command/commands/GiveCommand.java new file mode 100644 index 0000000..fde0659 --- /dev/null +++ b/gameserver/src/main/java/brainwine/gameserver/command/commands/GiveCommand.java @@ -0,0 +1,80 @@ +package brainwine.gameserver.command.commands; + +import brainwine.gameserver.GameServer; +import brainwine.gameserver.command.Command; +import brainwine.gameserver.command.CommandExecutor; +import brainwine.gameserver.entity.player.Player; +import brainwine.gameserver.item.Item; +import brainwine.gameserver.item.ItemRegistry; + +public class GiveCommand extends Command { + + @Override + public void execute(CommandExecutor executor, String[] args) { + if(args.length < 2) { + executor.sendMessage(String.format("Usage: %s", getUsage())); + return; + } + + Player target = GameServer.getInstance().getPlayerManager().getPlayer(args[0]); + + if(target == null) { + executor.sendMessage("That player does not exist."); + return; + } + + Item item = Item.AIR; + + try { + item = ItemRegistry.getItem(Integer.parseInt(args[1])); + } catch(NumberFormatException e) { + item = ItemRegistry.getItem(args[1]); + } + + if(item.isAir()) { + executor.sendMessage("This item does not exist."); + return; + } + + int quantity = 1; + + if(args.length > 2) { + try { + quantity = Integer.parseInt(args[2]); + } catch(NumberFormatException e) { + executor.sendMessage("Quantity must be a valid number."); + return; + } + } + + if(quantity > 0) { + target.getInventory().addItem(item, quantity); + target.alert(String.format("You received %s %s from an administrator.", quantity, item.getName())); + executor.sendMessage(String.format("Gave %s %s to %s", quantity, item.getName(), target.getName())); + } else { + target.getInventory().removeItem(item, -quantity); + target.alert(String.format("%s %s was taken from your inventory.", -quantity, item.getName())); + executor.sendMessage(String.format("Took %s %s from %s", quantity, item.getName(), target.getName())); + } + } + + @Override + public String getName() { + return "give"; + } + + @Override + public String getDescription() { + return "Gives the specified amount of the specified item to the specified player."; + } + + @Override + public String getUsage() { + return "/give [quantity]"; + } + + @Override + public boolean requiresAdmin() { + return true; + } +}