diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Auto.java b/src/main/java/com/intellectualcrafters/plot/commands/Auto.java index 5be21ef0f..3e9b8259e 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Auto.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Auto.java @@ -33,6 +33,7 @@ import com.intellectualcrafters.plot.util.ClusterManager; import com.intellectualcrafters.plot.util.EconHandler; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.Permissions; +import com.intellectualsites.commands.CommandCaller; import com.intellectualsites.commands.CommandDeclaration; import com.intellectualcrafters.plot.object.PlotPlayer; diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Database.java b/src/main/java/com/intellectualcrafters/plot/commands/Database.java index 91437258e..1295c3fa7 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Database.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Database.java @@ -113,9 +113,7 @@ public class Database extends SubCommand { return sendMessage(plr, "Could not create the required tables and/or load the database") && sendMessage(plr, "Please see the stacktrace for more information"); } UUID requester = null; - if (plr != null) { - requester = UUIDHandler.getUUID(plr); - } + requester = UUIDHandler.getUUID(plr); insertPlots(manager, requester, n); break; case "sqlite": diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Info.java b/src/main/java/com/intellectualcrafters/plot/commands/Info.java index 37967934e..fd1f698a1 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Info.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Info.java @@ -105,10 +105,10 @@ public class Info extends SubCommand { case "members": case "owner": case "rating": - plot = MainUtil.getPlotFromString(player, null, player == null); + plot = MainUtil.getPlotFromString(player, null, false); break; default: - plot = MainUtil.getPlotFromString(player, arg, player == null); + plot = MainUtil.getPlotFromString(player, arg, false); if (args.length == 2) { arg = args[1]; } @@ -119,15 +119,12 @@ public class Info extends SubCommand { } } else { - plot = MainUtil.getPlotFromString(player, null, player == null); + plot = MainUtil.getPlotFromString(player, null, false); } if (plot == null && arg != null) { - plot = MainUtil.getPlotFromString(player, null, player == null); + plot = MainUtil.getPlotFromString(player, null, false); } if (plot == null) { - if (player == null) { - return false; - } MainUtil.sendMessage(player, C.NOT_IN_PLOT); return false; } @@ -214,7 +211,7 @@ public class Info extends SubCommand { final String description = descriptionFlag == null ? C.NONE.s() : descriptionFlag.getValueString(); final String flags = StringMan.replaceFromMap("$2" + (StringUtils.join(FlagManager.getPlotFlags(plot.world, plot.getSettings(), true).values(), "").length() > 0 ? StringUtils.join(FlagManager.getPlotFlags(plot.world, plot.getSettings(), true).values(), "$1, $2") : C.NONE.s()), C.replacements); - final boolean build = (player == null) || plot.isAdded(player.getUUID()); + final boolean build = plot.isAdded(player.getUUID()); String owner = plot.owner == null ? "unowned" : getPlayerList(plot.getOwners()); diff --git a/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java b/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java index f0d19f30b..82a0be3b6 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java @@ -24,13 +24,11 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; -import java.util.UUID; import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.object.ConsolePlayer; -import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.MathMan; @@ -40,19 +38,18 @@ import com.intellectualsites.commands.Argument; import com.intellectualsites.commands.Command; import com.intellectualsites.commands.CommandHandlingOutput; import com.intellectualsites.commands.CommandManager; -import com.intellectualcrafters.plot.object.PlotPlayer; /** * PlotSquared command class * * @author Citymonstret */ -public class MainCommand extends CommandManager { +public class MainCommand extends CommandManager { public static MainCommand instance = new MainCommand(); private MainCommand() { - super(null, new ArrayList()); + super(null, new ArrayList>()); List toAdd = Arrays.asList( new Buy(), new Save(), new Load(), new Template(), new Download(), @@ -93,10 +90,10 @@ public class MainCommand extends CommandManager { return false; } - public static ArrayList getCommands(final CommandCategory category, final PlotPlayer player) { - ArrayList cmds = instance.getCommands(); - for (Iterator iter = cmds.iterator(); iter.hasNext();){ - Command cmd = iter.next(); + public static ArrayList> getCommands(final CommandCategory category, final PlotPlayer player) { + ArrayList> cmds = instance.getCommands(); + for (Iterator> iter = cmds.iterator(); iter.hasNext();){ + Command cmd = iter.next(); if ((category != null && (cmd.getCategory().equals(category))) || !player.hasPermission(cmd.getPermission())) { iter.remove(); } @@ -105,7 +102,7 @@ public class MainCommand extends CommandManager { } public static List helpMenu(final PlotPlayer player, final CommandCategory category, int page) { - List commands; + List> commands; commands = getCommands(category, player); // final int totalPages = ((int) Math.ceil(12 * (commands.size()) / // 100)); @@ -122,7 +119,7 @@ public class MainCommand extends CommandManager { help.add(C.HELP_HEADER.s()); // HELP_CATEGORY("&cCategory: &6%category%&c, Page: %current%&c/&6%max%&c, Displaying: &6%dis%&c/&6%total%"), help.add(C.HELP_CATEGORY.s().replace("%category%", category == null ? "All" : category.toString()).replace("%current%", "" + (page + 1)).replace("%max%", "" + (totalPages)).replace("%dis%", "" + perPage).replace("%total%", "" + commands.size())); - Command cmd; + Command cmd; final int start = page * perPage; for (int x = start; x < max; x++) { cmd = commands.get(x); @@ -230,7 +227,6 @@ public class MainCommand extends CommandManager { displayHelp(player, category, help_index); return true; } - PlotPlayer caller; StringBuilder builder = new StringBuilder(cmd).append(" "); Iterator iterator = Arrays.asList(args).iterator(); while (iterator.hasNext()) { @@ -240,31 +236,6 @@ public class MainCommand extends CommandManager { } } instance.handle(player, builder.toString()); - // for (final SubCommand command : subCommands) { - // if (command.cmd.equalsIgnoreCase(args[0]) || command.alias.contains(args[0].toLowerCase())) { - // final String[] arguments = new String[args.length - 1]; - // System.arraycopy(args, 1, arguments, 0, args.length - 1); - // if (command.permission.hasPermissipon(player)) { - // if ((player != null) || !command.isPlayer) { - // return command.execute(player, arguments); - // } else { - // return !MainUtil.sendMessage(null, C.IS_CONSOLE); - // } - // } else { - // return no_permission(player, command.permission.permission.toLowerCase()); - // } - // } - // } - // MainUtil.sendMessage(player, C.NOT_VALID_SUBCOMMAND); - // final String[] commands = new String[subCommands.size()]; - // for (int x = 0; x < subCommands.size(); x++) { - // commands[x] = subCommands.get(x).cmd; - // } - // /* Let's try to get a proper usage string */ - // final String command = new StringComparison(args[0], commands).getBestMatch(); - // return MainUtil.sendMessage(player, C.DID_YOU_MEAN, "/plot " + command); - // PlayerFunctions.sendMessage(player, C.DID_YOU_MEAN, new - // StringComparsion(args[0], commands).getBestMatch()); return true; } @@ -272,30 +243,27 @@ public class MainCommand extends CommandManager { public int handle(PlotPlayer plr, String input) { String[] parts = input.split(" "); String[] args; - String command = parts[0].toLowerCase(); + String label; if (parts.length == 1) { + label = null; args = new String[0]; } else { - args = new String[parts.length - 1]; - System.arraycopy(parts, 1, args, 0, args.length); + label = parts[1]; + args = new String[parts.length - 2]; + System.arraycopy(parts, 2, args, 0, args.length); } - Command cmd = null; - System.out.print(command); - System.out.print(StringMan.join(commands.entrySet(), ", ")); - cmd = this.commands.get(command); + Command cmd = null; + cmd = this.commands.get(label); if (cmd == null) { MainUtil.sendMessage(plr, C.NOT_VALID_SUBCOMMAND); { - final String[] commands = new String[this.commands.size()]; - for (int i = 0; i < commands.length; i++) { - commands[i] = this.commands.get(i).getCommand(); - } - final String bestMatch = new StringComparison(args[0], commands).getBestMatch(); - MainUtil.sendMessage(plr, C.DID_YOU_MEAN, "/plot " + bestMatch); + ArrayList> cmds = getCommands(); + cmd = new StringComparison>(args[0], cmds).getMatchObject(); + MainUtil.sendMessage(plr, C.DID_YOU_MEAN, cmd.getUsage()); } return CommandHandlingOutput.NOT_FOUND; } - if (cmd.getRequiredType().allows(plr)) { + if (!cmd.getRequiredType().allows(plr)) { if (ConsolePlayer.isConsole(plr)) { MainUtil.sendMessage(plr, C.IS_CONSOLE); } else { @@ -307,7 +275,7 @@ public class MainCommand extends CommandManager { MainUtil.sendMessage(plr, C.NO_PERMISSION, cmd.getPermission()); return CommandHandlingOutput.NOT_PERMITTED; } - Argument[] requiredArguments = cmd.getRequiredArguments(); + Argument[] requiredArguments = cmd.getRequiredArguments(); if (requiredArguments != null && requiredArguments.length > 0) { boolean success = true; if (args.length < requiredArguments.length) { diff --git a/src/main/java/com/intellectualcrafters/plot/commands/RequiredType.java b/src/main/java/com/intellectualcrafters/plot/commands/RequiredType.java index 3a0987180..c6a86bb23 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/RequiredType.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/RequiredType.java @@ -1,23 +1,18 @@ package com.intellectualcrafters.plot.commands; -import com.intellectualcrafters.plot.object.ConsolePlayer; -import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualsites.commands.CommandCaller; public enum RequiredType { CONSOLE, PLAYER, NONE; - public boolean allows(PlotPlayer player) { + public boolean allows(CommandCaller player) { switch (this) { case NONE: return true; - case PLAYER: - return !ConsolePlayer.isConsole(player); - case CONSOLE: - return ConsolePlayer.isConsole(player); default: - return false; + return this == player.getSuperCaller(); } } } diff --git a/src/main/java/com/intellectualcrafters/plot/commands/SchematicCmd.java b/src/main/java/com/intellectualcrafters/plot/commands/SchematicCmd.java index 37ec55906..12d7efc36 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/SchematicCmd.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/SchematicCmd.java @@ -27,9 +27,9 @@ import java.util.HashMap; import java.util.UUID; import com.intellectualcrafters.plot.PS; - import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Settings; +import com.intellectualcrafters.plot.object.ConsolePlayer; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotId; @@ -142,10 +142,6 @@ public class SchematicCmd extends SubCommand { break; } case "test": { - if (plr == null) { - PS.log(C.IS_CONSOLE.s()); - return false; - } if (!Permissions.hasPermission(plr, "plots.schematic.test")) { MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.schematic.test"); return false; @@ -178,7 +174,7 @@ public class SchematicCmd extends SubCommand { } case "saveall": case "exportall": { - if (plr != null) { + if (!ConsolePlayer.isConsole(plr)) { MainUtil.sendMessage(plr, C.NOT_CONSOLE); return false; } @@ -220,43 +216,21 @@ public class SchematicCmd extends SubCommand { } final String world; final Plot p2; - if (plr != null) { - final Location loc = plr.getLocation(); - final Plot plot = MainUtil.getPlot(loc); - if (plot == null) { - return !sendMessage(plr, C.NOT_IN_PLOT); - } - if (!plot.hasOwner()) { - MainUtil.sendMessage(plr, C.PLOT_UNOWNED); - return false; - } - if (!plot.isOwner(plr.getUUID()) && !Permissions.hasPermission(plr, "plots.admin.command.schematic.save")) { - MainUtil.sendMessage(plr, C.NO_PLOT_PERMS); - return false; - } - p2 = plot; - world = loc.getWorld(); - } else { - if (args.length == 3) { - try { - world = args[1]; - final String[] split = args[2].split(";"); - final PlotId i = new PlotId(Integer.parseInt(split[0]), Integer.parseInt(split[1])); - if ((PS.get().getPlots(world) == null) || (PS.get().getPlots(world).get(i) == null)) { - MainUtil.sendMessage(null, "&cInvalid world or id. Use &7/plots sch save "); - return false; - } - p2 = PS.get().getPlots(world).get(i); - } catch (final Exception e) { - MainUtil.sendMessage(null, "&cInvalid world or id. Use &7/plots sch save "); - return false; - } - } else { - MainUtil.sendMessage(null, "&cInvalid world or id. Use &7/plots sch save "); - return false; - } + final Location loc = plr.getLocation(); + final Plot plot = MainUtil.getPlot(loc); + if (plot == null) { + return !sendMessage(plr, C.NOT_IN_PLOT); } - + if (!plot.hasOwner()) { + MainUtil.sendMessage(plr, C.PLOT_UNOWNED); + return false; + } + if (!plot.isOwner(plr.getUUID()) && !Permissions.hasPermission(plr, "plots.admin.command.schematic.save")) { + MainUtil.sendMessage(plr, C.NO_PLOT_PERMS); + return false; + } + p2 = plot; + world = loc.getWorld(); Collection plots = new ArrayList(); plots.add(p2); boolean result = SchematicHandler.manager.exportAll(plots, null, null, new Runnable() { diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Setup.java b/src/main/java/com/intellectualcrafters/plot/commands/Setup.java index f1618ed70..77f4e16d1 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Setup.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Setup.java @@ -26,6 +26,8 @@ import java.util.List; import java.util.Map.Entry; + +import com.intellectualsites.commands.CommandCaller; import com.intellectualsites.commands.CommandDeclaration; import com.intellectualcrafters.plot.object.PlotPlayer; @@ -76,13 +78,7 @@ public class Setup extends SubCommand { @Override public boolean onCommand(final PlotPlayer plr, final String[] args) { // going through setup - String name; - if (plr == null) { - name = "*"; - } - else { - name = plr.getName(); - } + String name = plr.getName(); if (!SetupUtils.setupMap.containsKey(name)) { final SetupObject object = new SetupObject(); SetupUtils.setupMap.put(name, object); @@ -247,9 +243,7 @@ public class Setup extends SubCommand { world = object.setupManager.setupWorld(object); } try { - if (plr != null) { - plr.teleport(BlockManager.manager.getSpawn(world)); - } + plr.teleport(BlockManager.manager.getSpawn(world)); } catch (final Exception e) { plr.sendMessage("&cAn error occured. See console for more information"); e.printStackTrace(); @@ -260,5 +254,4 @@ public class Setup extends SubCommand { } return false; } - } diff --git a/src/main/java/com/intellectualcrafters/plot/commands/SubCommand.java b/src/main/java/com/intellectualcrafters/plot/commands/SubCommand.java index f445218e2..1a02ee08e 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/SubCommand.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/SubCommand.java @@ -30,7 +30,7 @@ import com.intellectualcrafters.plot.util.MainUtil; * @author Citymonstret */ @SuppressWarnings({ "deprecation", "unused" }) -public abstract class SubCommand extends com.intellectualsites.commands.Command { +public abstract class SubCommand extends com.intellectualsites.commands.Command { /** * The category @@ -46,9 +46,8 @@ public abstract class SubCommand extends com.intellectualsites.commands.Command * * @see com.intellectualcrafters.plot.util.MainUtil#sendMessage(PlotPlayer, C, String...) */ - public boolean sendMessage(final PlotPlayer plr, final C c, final String... args) { - MainUtil.sendMessage(plr, c, args); + public boolean sendMessage(PlotPlayer plr, final C c, final String... args) { + c.send(plr, args); return true; } - } diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Template.java b/src/main/java/com/intellectualcrafters/plot/commands/Template.java index 8840c909b..6248eac52 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Template.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Template.java @@ -180,9 +180,7 @@ public class Template extends SubCommand { setup.world = world; SetupUtils.manager.setupWorld(setup); MainUtil.sendMessage(plr, "Done!"); - if (plr != null) { - plr.teleport(BlockManager.manager.getSpawn(world)); - } + plr.teleport(BlockManager.manager.getSpawn(world)); return true; } case "export": { diff --git a/src/main/java/com/intellectualcrafters/plot/commands/list.java b/src/main/java/com/intellectualcrafters/plot/commands/list.java index 51b637b84..ac7115d52 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/list.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/list.java @@ -74,42 +74,35 @@ public class list extends SubCommand { private String[] getArgumentList(PlotPlayer player) { List args = new ArrayList<>(); - if (player == null) { - args.addAll(Arrays.asList("world", "all", "unowned", "unknown", "top", "")); - if (EconHandler.manager != null) { - args.add("forsale"); - } - } else { - if (EconHandler.manager != null && player.hasPermission("plots.list.forsale")) { - args.add("forsale"); - } - if (player.hasPermission("plots.list.mine")) { - args.add("mine"); - } - if (player.hasPermission("plots.list.shared")) { - args.add("shared"); - } - if (player.hasPermission("plots.list.world")) { - args.add("world"); - } - if (player.hasPermission("plots.list.top")) { - args.add("top"); - } - if (player.hasPermission("plots.list..all")) { - args.add("all"); - } - if (player.hasPermission("plots.list.unowned")) { - args.add("unowned"); - } - if (player.hasPermission("plots.list.unknown")) { - args.add("unknown"); - } - if (player.hasPermission("plots.list.player")) { - args.add(""); - } - if (player.hasPermission("plots.list.world")) { - args.add(""); - } + if (EconHandler.manager != null && player.hasPermission("plots.list.forsale")) { + args.add("forsale"); + } + if (player.hasPermission("plots.list.mine")) { + args.add("mine"); + } + if (player.hasPermission("plots.list.shared")) { + args.add("shared"); + } + if (player.hasPermission("plots.list.world")) { + args.add("world"); + } + if (player.hasPermission("plots.list.top")) { + args.add("top"); + } + if (player.hasPermission("plots.list..all")) { + args.add("all"); + } + if (player.hasPermission("plots.list.unowned")) { + args.add("unowned"); + } + if (player.hasPermission("plots.list.unknown")) { + args.add("unknown"); + } + if (player.hasPermission("plots.list.player")) { + args.add(""); + } + if (player.hasPermission("plots.list.world")) { + args.add(""); } return args.toArray(new String[args.size()]); } @@ -139,26 +132,11 @@ public class list extends SubCommand { List plots = null; - String world; - if (plr != null) { - world = plr.getLocation().getWorld(); - } - else { - Set worlds = PS.get().getPlotWorlds(); - if (worlds.size() == 0) { - world = "world"; - } - else { - world = worlds.iterator().next(); - } - } + String world = plr.getLocation().getWorld(); String arg = args[0].toLowerCase(); boolean sort = true; switch (arg) { case "mine": { - if (plr == null) { - break; - } if (!Permissions.hasPermission(plr, "plots.list.mine")) { MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.list.mine"); return false; @@ -167,9 +145,6 @@ public class list extends SubCommand { break; } case "shared": { - if (plr == null) { - break; - } if (!Permissions.hasPermission(plr, "plots.list.shared")) { MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.list.shared"); return false; diff --git a/src/main/java/com/intellectualcrafters/plot/config/C.java b/src/main/java/com/intellectualcrafters/plot/config/C.java index ba3434b29..8848ae51e 100644 --- a/src/main/java/com/intellectualcrafters/plot/config/C.java +++ b/src/main/java/com/intellectualcrafters/plot/config/C.java @@ -36,6 +36,7 @@ import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.StringMan; +import com.intellectualsites.commands.CommandCaller; /** * Captions class. @@ -692,12 +693,12 @@ public enum C { return cat; } - public void send(PlotPlayer plr, String... args) { + public void send(CommandCaller plr, String... args) { if (plr == null) { MainUtil.sendConsoleMessage(this, args); } else { - MainUtil.sendMessage(plr, this, args); + plr.sendMessage(this, args); } } } diff --git a/src/main/java/com/intellectualcrafters/plot/object/ConsolePlayer.java b/src/main/java/com/intellectualcrafters/plot/object/ConsolePlayer.java index a627647cb..a24702846 100644 --- a/src/main/java/com/intellectualcrafters/plot/object/ConsolePlayer.java +++ b/src/main/java/com/intellectualcrafters/plot/object/ConsolePlayer.java @@ -5,7 +5,10 @@ import java.util.Set; import java.util.UUID; import com.intellectualcrafters.plot.PS; +import com.intellectualcrafters.plot.commands.RequiredType; +import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.database.DBFunc; +import com.intellectualcrafters.plot.util.MainUtil; import com.plotsquared.bukkit.util.bukkit.BukkitUtil; public class ConsolePlayer implements PlotPlayer { @@ -67,6 +70,11 @@ public class ConsolePlayer implements PlotPlayer { public void sendMessage(String message) { PS.log(message); } + + @Override + public void sendMessage(C c, String... args) { + MainUtil.sendMessage(this, c, args); + } @Override public void teleport(Location loc) { @@ -122,5 +130,10 @@ public class ConsolePlayer implements PlotPlayer { public void deleteMeta(String key) { this.meta.remove(key); } + + @Override + public RequiredType getSuperCaller() { + return RequiredType.CONSOLE; + } } diff --git a/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java b/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java index 37ec9c3fb..752fe0c63 100644 --- a/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java +++ b/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java @@ -2,12 +2,14 @@ package com.intellectualcrafters.plot.object; import java.util.UUID; +import com.intellectualsites.commands.CommandCaller; + /** * Created 2015-02-20 for PlotSquared * * @author Citymonstret */ -public interface PlotPlayer { +public interface PlotPlayer extends CommandCaller { public long getPreviousLogin(); diff --git a/src/main/java/com/intellectualsites/commands/Command.java b/src/main/java/com/intellectualsites/commands/Command.java index 25f9224d3..8508a9ac1 100644 --- a/src/main/java/com/intellectualsites/commands/Command.java +++ b/src/main/java/com/intellectualsites/commands/Command.java @@ -8,16 +8,14 @@ import java.util.Set; import com.intellectualcrafters.plot.commands.CommandCategory; import com.intellectualcrafters.plot.commands.RequiredType; -import com.intellectualcrafters.plot.object.Plot; -import com.intellectualcrafters.plot.object.PlotPlayer; -public abstract class Command extends CommandManager { +public abstract class Command extends CommandManager { private RequiredType requiredType = RequiredType.NONE; private String command, usage = "", description = "", permission = ""; private Set aliases = new HashSet<>(); private CommandCategory category; - protected Argument[] requiredArguments; + protected Argument[] requiredArguments; public Command() { super(null, new ArrayList()); @@ -97,9 +95,9 @@ public abstract class Command extends CommandManager { return this.command; } - public abstract boolean onCommand(PlotPlayer plr, String[] arguments); + public abstract boolean onCommand(E plr, String[] arguments); - final public int handle(PlotPlayer plr, String[] args) { + final public int handle(E plr, String[] args) { if (args.length == 0) { return super.handle(plr, ""); } @@ -131,7 +129,7 @@ public abstract class Command extends CommandManager { return this.aliases; } - final public Argument[] getRequiredArguments() { + final public Argument[] getRequiredArguments() { return this.requiredArguments; } @@ -150,7 +148,7 @@ public abstract class Command extends CommandManager { if (getClass() != obj.getClass()) { return false; } - final Command other = (Command) obj; + final Command other = (Command) obj; if (this.hashCode() != other.hashCode()) { return false; } diff --git a/src/main/java/com/intellectualsites/commands/CommandCaller.java b/src/main/java/com/intellectualsites/commands/CommandCaller.java new file mode 100644 index 000000000..f150d9f8b --- /dev/null +++ b/src/main/java/com/intellectualsites/commands/CommandCaller.java @@ -0,0 +1,14 @@ +package com.intellectualsites.commands; + +import com.intellectualcrafters.plot.commands.RequiredType; +import com.intellectualcrafters.plot.config.C; + +public interface CommandCaller { + void sendMessage(final String message); + + void sendMessage(C c, String ... args); + + boolean hasPermission(final String perm); + + RequiredType getSuperCaller(); +} diff --git a/src/main/java/com/intellectualsites/commands/CommandManager.java b/src/main/java/com/intellectualsites/commands/CommandManager.java index 1380a568b..e94b0e482 100644 --- a/src/main/java/com/intellectualsites/commands/CommandManager.java +++ b/src/main/java/com/intellectualsites/commands/CommandManager.java @@ -5,40 +5,41 @@ import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.commands.RequiredType; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.object.ConsolePlayer; -import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.MainUtil; @SuppressWarnings("unused") -public class CommandManager { +public class CommandManager { - protected final ConcurrentHashMap commands; + protected final ConcurrentHashMap> commands; protected final Character initialCharacter; public CommandManager() { - this('/', new ArrayList()); + this('/', new ArrayList>()); } - public CommandManager(Character initialCharacter, List commands) { + public CommandManager(Character initialCharacter, List> commands) { this.commands = new ConcurrentHashMap<>(); - for (Command command : commands) { + for (Command command : commands) { addCommand(command); } this.initialCharacter = initialCharacter; } - final public void addCommand(final Command command) { + final public void addCommand(final Command command) { this.commands.put(command.getCommand().toLowerCase(), command); for (String alias : command.getAliases()) { this.commands.put(alias.toLowerCase(), command); } } - final public boolean createCommand(final Command command) { + final public boolean createCommand(final Command command) { try { command.create(); } catch(final Exception e) { @@ -52,11 +53,11 @@ public class CommandManager { return false; } - final public ArrayList getCommands() { - ArrayList result = new ArrayList<>(this.commands.values()); - Collections.sort(result, new Comparator() { + final public ArrayList> getCommands() { + ArrayList> result = new ArrayList<>(this.commands.values()); + Collections.sort(result, new Comparator>() { @Override - public int compare(Command a, Command b) { + public int compare(Command a, Command b) { if (a == b) { return 0; } @@ -71,8 +72,16 @@ public class CommandManager { }); return result; } + + final public ArrayList getCommandLabels(ArrayList> cmds) { + ArrayList labels = new ArrayList<>(cmds.size()); + for (Command cmd : cmds) { + labels.add(cmd.getCommand()); + } + return labels; + } - public int handle(PlotPlayer plr, String input) { + public int handle(T plr, String input) { if (initialCharacter != null && !input.startsWith(initialCharacter + "")) { return CommandHandlingOutput.NOT_COMMAND; } @@ -86,7 +95,7 @@ public class CommandManager { args = new String[parts.length - 1]; System.arraycopy(parts, 1, args, 0, args.length); } - Command cmd = null; + Command cmd = null; cmd = commands.get(command); if (cmd == null) { return CommandHandlingOutput.NOT_FOUND; @@ -97,7 +106,7 @@ public class CommandManager { if (!plr.hasPermission(cmd.getPermission())) { return CommandHandlingOutput.NOT_PERMITTED; } - Argument[] requiredArguments = cmd.getRequiredArguments(); + Argument[] requiredArguments = cmd.getRequiredArguments(); if (requiredArguments != null && requiredArguments.length > 0) { boolean success = true; if (args.length < requiredArguments.length) { @@ -121,7 +130,7 @@ public class CommandManager { if (!a) { String usage = cmd.getUsage(); if (usage != null && !usage.isEmpty()) { - MainUtil.sendMessage(plr, usage); + plr.sendMessage(usage); } return CommandHandlingOutput.WRONG_USAGE; } diff --git a/src/main/java/com/plotsquared/bukkit/commands/BukkitCommand.java b/src/main/java/com/plotsquared/bukkit/commands/BukkitCommand.java index ecdb09185..a484c6514 100644 --- a/src/main/java/com/plotsquared/bukkit/commands/BukkitCommand.java +++ b/src/main/java/com/plotsquared/bukkit/commands/BukkitCommand.java @@ -53,11 +53,11 @@ public class BukkitCommand implements CommandExecutor, TabCompleter { return null; } final Set tabOptions = new HashSet<>(); - ArrayList commands = MainCommand.instance.getCommands(); + ArrayList> commands = MainCommand.instance.getCommands(); String best = new StringComparison(strings[0], commands).getBestMatch(); tabOptions.add(best); final String arg = strings[0].toLowerCase(); - for (final Command cmd : MainCommand.instance.getCommands()) { + for (final Command cmd : MainCommand.instance.getCommands()) { String label = cmd.getCommand(); if (!label.equalsIgnoreCase(best)) { if (label.startsWith(arg)) { diff --git a/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java b/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java index 67236a987..07611dbab 100644 --- a/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java +++ b/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java @@ -12,8 +12,11 @@ import org.bukkit.entity.Player; import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; +import com.intellectualcrafters.plot.commands.RequiredType; +import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.util.EconHandler; +import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.UUIDHandler; import com.plotsquared.bukkit.util.bukkit.BukkitUtil; @@ -82,6 +85,11 @@ public class BukkitPlayer implements PlotPlayer { this.player.sendMessage(message); } + @Override + public void sendMessage(C c, String... args) { + MainUtil.sendMessage(this, c, args); + } + @Override public void teleport(final Location loc) { this.player.teleport(new org.bukkit.Location(BukkitUtil.getWorld(loc.getWorld()), loc.getX() + 0.5, loc.getY(), loc.getZ() + 0.5, loc.getYaw(), loc.getPitch())); @@ -191,4 +199,9 @@ public class BukkitPlayer implements PlotPlayer { public void saveData() { player.saveData(); } + + @Override + public RequiredType getSuperCaller() { + return RequiredType.PLAYER; + } }