From 614e5d3d5b4fdad982a2b78a8e0cb778ed75067d Mon Sep 17 00:00:00 2001 From: Poslovitch Date: Mon, 31 Jul 2017 10:58:28 +0200 Subject: [PATCH] Commands are now working (still need some work) --- .../api/commands/AbstractCommand.java | 37 +++++++++---------- .../bskyblock/commands/AdminCommand.java | 2 +- .../bskyblock/commands/IslandCommand.java | 11 +++--- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/main/java/us/tastybento/bskyblock/api/commands/AbstractCommand.java b/src/main/java/us/tastybento/bskyblock/api/commands/AbstractCommand.java index 25279ef82..ea8c51f6a 100644 --- a/src/main/java/us/tastybento/bskyblock/api/commands/AbstractCommand.java +++ b/src/main/java/us/tastybento/bskyblock/api/commands/AbstractCommand.java @@ -1,50 +1,43 @@ package us.tastybento.bskyblock.api.commands; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.UUID; - import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; - import us.tastybento.bskyblock.BSkyBlock; +import java.util.*; + /** * * @author Poslovitch */ public abstract class AbstractCommand implements CommandExecutor, TabCompleter { + private BSkyBlock plugin; + private Map argumentsMap; private Map aliasesMap; + public String label; public boolean isPlayer; public boolean inTeam; - public UUID playerUUID; public UUID teamLeaderUUID; public Set teamMembers; public Player player; - - private final String name; + public UUID playerUUID; private final boolean help; - private BSkyBlock plugin; - private static final int MAX_PER_PAGE = 7; - protected AbstractCommand(String name, boolean help, BSkyBlock plugin) { - this.name = name; - this.help = help; + protected AbstractCommand(BSkyBlock plugin, String label, boolean help) { this.plugin = plugin; - // These are not initialized elsewhere, so need to be done so. Initialize with size 1 to save memory - this.aliasesMap = new HashMap(1); this.argumentsMap = new HashMap(1); + this.aliasesMap = new HashMap(1); + this.label = label; + this.help = help; + // Register the help argument if needed if (help) { @@ -70,7 +63,11 @@ public abstract class AbstractCommand implements CommandExecutor, TabCompleter { public abstract void execute(CommandSender sender, String[] args); public void addArgument(String[] names, ArgumentHandler handler) { - + // TODO add some security checks to avoid duplicates + argumentsMap.put(names[0], handler); + for (int i = 1 ; i < names.length ; i++) { + aliasesMap.put(names[0], names[i]); + } } public ArgumentHandler getHandler(String argument) { @@ -117,7 +114,6 @@ public abstract class AbstractCommand implements CommandExecutor, TabCompleter { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - this.label = label; if (this.canUse(sender)) { // Check if the command sender is a player or not if (sender instanceof Player) { @@ -131,6 +127,7 @@ public abstract class AbstractCommand implements CommandExecutor, TabCompleter { teamLeaderUUID = plugin.getIslands().getTeamLeader(playerUUID); teamMembers = plugin.getIslands().getMembers(teamLeaderUUID); } + if(args.length >= 1) { ArgumentHandler handler = getHandler(args[0]); // Store the handler to save some calculations if (handler != null && handler.canUse(sender)) { diff --git a/src/main/java/us/tastybento/bskyblock/commands/AdminCommand.java b/src/main/java/us/tastybento/bskyblock/commands/AdminCommand.java index 037a58a34..777ffce3c 100755 --- a/src/main/java/us/tastybento/bskyblock/commands/AdminCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/AdminCommand.java @@ -12,7 +12,7 @@ public class AdminCommand extends AbstractCommand { BSkyBlock plugin; public AdminCommand(BSkyBlock plugin) { - super(Settings.ADMINCOMMAND, true, plugin); + super(plugin, Settings.ADMINCOMMAND, true); plugin.getCommand(Settings.ADMINCOMMAND).setExecutor(this); plugin.getCommand(Settings.ADMINCOMMAND).setTabCompleter(this); this.plugin = plugin; diff --git a/src/main/java/us/tastybento/bskyblock/commands/IslandCommand.java b/src/main/java/us/tastybento/bskyblock/commands/IslandCommand.java index 5b9d619cb..ce360ec44 100755 --- a/src/main/java/us/tastybento/bskyblock/commands/IslandCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/IslandCommand.java @@ -1,16 +1,11 @@ package us.tastybento.bskyblock.commands; -import java.util.HashMap; -import java.util.List; -import java.util.UUID; - import org.apache.commons.lang.math.NumberUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.permissions.PermissionAttachmentInfo; - import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.api.commands.AbstractCommand; import us.tastybento.bskyblock.config.Settings; @@ -19,6 +14,10 @@ import us.tastybento.bskyblock.schematics.Schematic; import us.tastybento.bskyblock.util.Util; import us.tastybento.bskyblock.util.VaultHelper; +import java.util.HashMap; +import java.util.List; +import java.util.UUID; + /** * "/island" command * @@ -35,7 +34,7 @@ public class IslandCommand extends AbstractCommand { public IslandCommand(BSkyBlock plugin) { - super(Settings.ISLANDCOMMAND, true, plugin); + super(plugin, Settings.ISLANDCOMMAND, true); plugin.getCommand(Settings.ISLANDCOMMAND).setExecutor(this); plugin.getCommand(Settings.ISLANDCOMMAND).setTabCompleter(this); this.plugin = plugin;