Added an item give command

This commit is contained in:
kuroppoi 2021-04-24 20:38:06 +02:00
parent 05a6051f22
commit 0ccfb940cb
2 changed files with 82 additions and 0 deletions

View file

@ -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) {

View file

@ -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 <player> <item> [quantity]";
}
@Override
public boolean requiresAdmin() {
return true;
}
}