From 7b51fb678cee294a8ca4e3da4cc8e5ef5fbb981e Mon Sep 17 00:00:00 2001 From: garbagemule Date: Sun, 2 Feb 2014 00:49:59 +0100 Subject: [PATCH] Add command for picking classes in the lobby. --- resources/plugin.yml | 5 +- src/com/garbagemule/MobArena/Msg.java | 1 + .../MobArena/commands/CommandHandler.java | 1 + .../commands/user/PickClassCommand.java | 124 ++++++++++++++++++ 4 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 src/com/garbagemule/MobArena/commands/user/PickClassCommand.java diff --git a/resources/plugin.yml b/resources/plugin.yml index 19758d2..ceca2d2 100644 --- a/resources/plugin.yml +++ b/resources/plugin.yml @@ -1,7 +1,7 @@ name: MobArena author: garbagemule main: com.garbagemule.MobArena.MobArena -version: 0.96.2.7 +version: 0.96.2.8 softdepend: [Multiverse-Core,Towny,Heroes,MagicSpells,Vault] commands: ma: @@ -37,6 +37,9 @@ permissions: mobarena.use.notready: description: Check who is not ready yet default: true + mobarena.use.class: + description: Pick a class by name + default: true mobarena.admin: description: Gives access to all admin commands diff --git a/src/com/garbagemule/MobArena/Msg.java b/src/com/garbagemule/MobArena/Msg.java index 7be8629..97aa928 100644 --- a/src/com/garbagemule/MobArena/Msg.java +++ b/src/com/garbagemule/MobArena/Msg.java @@ -59,6 +59,7 @@ public enum Msg { LOBBY_CLASS_PERMISSION("You don't have permission to use this class!"), LOBBY_CLASS_PRICE("This class costs &c%&r (paid on arena start)."), LOBBY_CLASS_TOO_EXPENSIVE("You can't afford that class (&c%&r)"), + LOBBY_NO_SUCH_CLASS("There is no class named &c%&r."), WARP_TO_ARENA("Warping to the arena not allowed!"), WARP_FROM_ARENA("Warping from the arena not allowed!"), WAVE_DEFAULT("Wave &b#%&r!"), diff --git a/src/com/garbagemule/MobArena/commands/CommandHandler.java b/src/com/garbagemule/MobArena/commands/CommandHandler.java index aa7c1c7..f3c1792 100644 --- a/src/com/garbagemule/MobArena/commands/CommandHandler.java +++ b/src/com/garbagemule/MobArena/commands/CommandHandler.java @@ -178,6 +178,7 @@ public class CommandHandler implements CommandExecutor register(ArenaListCommand.class); register(PlayerListCommand.class); register(NotReadyCommand.class); + register(PickClassCommand.class); // mobarena.admin register(EnableCommand.class); diff --git a/src/com/garbagemule/MobArena/commands/user/PickClassCommand.java b/src/com/garbagemule/MobArena/commands/user/PickClassCommand.java new file mode 100644 index 0000000..b794f14 --- /dev/null +++ b/src/com/garbagemule/MobArena/commands/user/PickClassCommand.java @@ -0,0 +1,124 @@ +package com.garbagemule.MobArena.commands.user; + +import com.garbagemule.MobArena.ArenaClass; +import com.garbagemule.MobArena.ClassLimitManager; +import com.garbagemule.MobArena.Messenger; +import com.garbagemule.MobArena.Msg; +import com.garbagemule.MobArena.commands.Command; +import com.garbagemule.MobArena.commands.CommandInfo; +import com.garbagemule.MobArena.commands.Commands; +import com.garbagemule.MobArena.framework.Arena; +import com.garbagemule.MobArena.framework.ArenaMaster; +import com.garbagemule.MobArena.util.TextUtils; +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.inventory.InventoryHolder; +import org.bukkit.inventory.ItemStack; + +@CommandInfo( + name = "class", + pattern = "(pick)?class", + usage = "/ma class ", + desc = "pick a class", + permission = "mobarena.use.class" +) +public class PickClassCommand implements Command +{ + @Override + public boolean execute(ArenaMaster am, CommandSender sender, String... args) { + if (!Commands.isPlayer(sender)) { + Messenger.tell(sender, Msg.MISC_NOT_FROM_CONSOLE); + return true; + } + + // Require a class name + if (args.length != 1) return false; + + // Cast the sender + Player p = (Player) sender; + + // Make sure the player is in an arena + Arena arena = am.getArenaWithPlayer(p); + if (arena == null) return true; + + // Make sure the player is in the lobby + if (!arena.inLobby(p)) { + Messenger.tell(p, Msg.MISC_NO_ACCESS); + return true; + } + + // Grab the ArenaClass, if it exists + String lowercase = args[0].toLowerCase(); + ArenaClass ac = am.getClasses().get(lowercase); + if (ac == null) { + Messenger.tell(p, Msg.LOBBY_NO_SUCH_CLASS, lowercase); + return true; + } + + // Check for permission. + if (!am.getPlugin().has(p, "mobarena.classes." + lowercase) && !lowercase.equals("random")) { + Messenger.tell(p, Msg.LOBBY_CLASS_PERMISSION); + return true; + } + + // Grab the old ArenaClass, if any, same => ignore + ArenaClass oldAC = arena.getArenaPlayer(p).getArenaClass(); + if (ac.equals(oldAC)) return true; + + // If the new class is full, inform the player. + ClassLimitManager clm = arena.getClassLimitManager(); + if (!clm.canPlayerJoinClass(ac)) { + Messenger.tell(p, Msg.LOBBY_CLASS_FULL); + return true; + } + + // Check price, balance, and inform + double price = ac.getPrice(); + if (price > 0D) { + if (!am.getPlugin().hasEnough(p, price)) { + Messenger.tell(p, Msg.LOBBY_CLASS_TOO_EXPENSIVE, am.getPlugin().economyFormat(price)); + return true; + } + } + + // Otherwise, leave the old class, and pick the new! + clm.playerLeftClass(oldAC, p); + clm.playerPickedClass(ac, p); + + if (!lowercase.equalsIgnoreCase("random")) { + if (arena.getSettings().getBoolean("use-class-chests", false)) { + Location loc = ac.getClassChest(); + if (loc != null) { + Block blockChest = loc.getBlock(); + InventoryHolder holder = (InventoryHolder) blockChest.getState(); + ItemStack[] contents = holder.getInventory().getContents(); + // Guard against double-chests for now + if (contents.length > 36) { + ItemStack[] newContents = new ItemStack[36]; + System.arraycopy(contents, 0, newContents, 0, 36); + contents = newContents; + } + arena.assignClassGiveInv(p, lowercase, contents); + p.getInventory().setContents(contents); + Messenger.tell(p, Msg.LOBBY_CLASS_PICKED, TextUtils.camelCase(lowercase)); + if (price > 0D) { + Messenger.tell(p, Msg.LOBBY_CLASS_PRICE, am.getPlugin().economyFormat(price)); + } + return true; + } + // No linked chest? Fall through to config-file + } + arena.assignClass(p, lowercase); + Messenger.tell(p, Msg.LOBBY_CLASS_PICKED, TextUtils.camelCase(lowercase)); + if (price > 0D) { + Messenger.tell(p, Msg.LOBBY_CLASS_PRICE, am.getPlugin().economyFormat(price)); + } + } else { + arena.addRandomPlayer(p); + Messenger.tell(p, Msg.LOBBY_CLASS_RANDOM); + } + return true; + } +}