diff --git a/Bukkit/src/main/java/com/intellectualcrafters/plot/api/PlotAPI.java b/Bukkit/src/main/java/com/intellectualcrafters/plot/api/PlotAPI.java index cf3884149..d6c6b3b91 100644 --- a/Bukkit/src/main/java/com/intellectualcrafters/plot/api/PlotAPI.java +++ b/Bukkit/src/main/java/com/intellectualcrafters/plot/api/PlotAPI.java @@ -23,7 +23,6 @@ package com.intellectualcrafters.plot.api; import com.intellectualcrafters.configuration.file.YamlConfiguration; import com.intellectualcrafters.plot.PS; -import com.intellectualcrafters.plot.commands.MainCommand; import com.intellectualcrafters.plot.commands.SubCommand; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.flag.AbstractFlag; @@ -40,17 +39,16 @@ import com.intellectualcrafters.plot.util.SetQueue; import com.intellectualcrafters.plot.util.UUIDHandler; import com.intellectualcrafters.plot.uuid.UUIDWrapper; import com.plotsquared.bukkit.util.BukkitUtil; -import org.bukkit.Location; -import org.bukkit.OfflinePlayer; -import org.bukkit.World; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; - import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Set; import java.util.UUID; +import org.bukkit.Location; +import org.bukkit.OfflinePlayer; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; /** * PlotSquared API
@@ -641,17 +639,13 @@ public class PlotAPI { /** * Register a subcommand - * + * @deprecated Command registration is done on object creation * @param c SubCommand, that we want to register - * * @see com.intellectualcrafters.plot.commands.SubCommand */ + @Deprecated public void registerCommand(SubCommand c) { - if (c.getCommand() != null) { - MainCommand.getInstance().addCommand(c); - } else { - MainCommand.getInstance().createCommand(c); - } + PS.debug("SubCommands are now registered on creation"); } /** diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java index ee2fbec7d..28af29e0c 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -3,7 +3,6 @@ package com.plotsquared.bukkit; import com.intellectualcrafters.configuration.ConfigurationSection; import com.intellectualcrafters.plot.IPlotMain; import com.intellectualcrafters.plot.PS; -import com.intellectualcrafters.plot.commands.MainCommand; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.ConfigurationNode; import com.intellectualcrafters.plot.config.Settings; @@ -74,6 +73,12 @@ import com.plotsquared.bukkit.uuid.LowerOfflineUUIDWrapper; import com.plotsquared.bukkit.uuid.OfflineUUIDWrapper; import com.plotsquared.bukkit.uuid.SQLUUIDHandler; import com.sk89q.worldedit.bukkit.WorldEditPlugin; +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; @@ -88,13 +93,6 @@ import org.bukkit.metadata.MetadataValue; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; -import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; -import java.util.UUID; - public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { public static BukkitMain THIS; @@ -173,7 +171,7 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { plotCommand.setExecutor(bcmd); plotCommand.setAliases(Arrays.asList("p", "ps", "plotme", "plot")); plotCommand.setTabCompleter(bcmd); - MainCommand.getInstance().addCommand(new DebugUUID()); + new DebugUUID(); } @Override @@ -215,7 +213,6 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { case COMPLEX_PART: case FISHING_HOOK: case ENDER_SIGNAL: - case LINGERING_POTION: case AREA_EFFECT_CLOUD: case EXPERIENCE_ORB: case LEASH_HITCH: diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/commands/DebugUUID.java b/Bukkit/src/main/java/com/plotsquared/bukkit/commands/DebugUUID.java index 446a60aab..5e2c5d696 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/commands/DebugUUID.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/commands/DebugUUID.java @@ -65,7 +65,7 @@ import java.util.UUID; public class DebugUUID extends SubCommand { public DebugUUID() { - this.requiredArguments = new Argument[]{Argument.String}; + super(Argument.String); } @Override diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitCommand.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitCommand.java index 9d7f52d2b..d7a81d57c 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitCommand.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitCommand.java @@ -3,36 +3,31 @@ package com.plotsquared.bukkit.util; import com.intellectualcrafters.plot.commands.MainCommand; import com.intellectualcrafters.plot.object.ConsolePlayer; import com.intellectualcrafters.plot.object.PlotPlayer; -import com.intellectualcrafters.plot.util.Permissions; -import com.intellectualcrafters.plot.util.StringComparison; import com.plotsquared.bukkit.commands.DebugUUID; -import com.plotsquared.general.commands.Command; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; import org.bukkit.Bukkit; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - public class BukkitCommand implements CommandExecutor, TabCompleter { public BukkitCommand() { - MainCommand.getInstance().addCommand(new DebugUUID()); + new DebugUUID(); } @Override public boolean onCommand(final CommandSender commandSender, org.bukkit.command.Command command, final String commandLabel, String[] args) { if (commandSender instanceof Player) { - return MainCommand.onCommand(BukkitUtil.getPlayer((Player) commandSender), commandLabel, args); + return MainCommand.onCommand(BukkitUtil.getPlayer((Player) commandSender), args); } if (commandSender == null || commandSender.getClass() == Bukkit.getConsoleSender().getClass()) { - return MainCommand.onCommand(ConsolePlayer.getConsole(), commandLabel, args); + return MainCommand.onCommand(ConsolePlayer.getConsole(), args); } @SuppressWarnings("deprecation") ConsolePlayer sender = new ConsolePlayer() { @@ -55,49 +50,28 @@ public class BukkitCommand implements CommandExecutor, TabCompleter { } }; sender.teleport(ConsolePlayer.getConsole().getLocationFull()); - boolean result = MainCommand.onCommand(sender, commandLabel, args); + boolean result = MainCommand.onCommand(sender, args); ConsolePlayer.getConsole().teleport(sender.getLocationFull()); return result; } @Override - public List onTabComplete(CommandSender commandSender, org.bukkit.command.Command command, String s, - String[] strings) { + public List onTabComplete(CommandSender commandSender, org.bukkit.command.Command command, String s, String[] args) { if (!(commandSender instanceof Player)) { return null; } PlotPlayer player = BukkitUtil.getPlayer((Player) commandSender); - if (strings.length < 1) { - if (strings.length == 0 || "plots".startsWith(s)) { - return Collections.singletonList("plots"); - } + if (args.length == 0) { + return Collections.singletonList("plots"); } - if (strings.length > 1) { + Collection objects = MainCommand.getInstance().tab(player, args, s.endsWith(" ")); + if (objects == null) { return null; } - Set tabOptions = new HashSet<>(); - String arg = strings[0].toLowerCase(); - ArrayList labels = new ArrayList<>(); - for (Command cmd : MainCommand.getInstance().getCommands()) { - String label = cmd.getCommand(); - HashSet aliases = new HashSet<>(cmd.getAliases()); - aliases.add(label); - for (String alias : aliases) { - labels.add(alias); - if (alias.startsWith(arg)) { - if (Permissions.hasPermission(player, cmd.getPermission())) { - tabOptions.add(label); - } else { - break; - } - } - } + List result = new ArrayList<>(); + for (Object o : objects) { + result.add(o.toString()); } - String best = new StringComparison<>(arg, labels).getBestMatch(); - tabOptions.add(best); - if (!tabOptions.isEmpty()) { - return new ArrayList<>(tabOptions); - } - return null; + return result; } } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/PS.java b/Core/src/main/java/com/intellectualcrafters/plot/PS.java index 2055ffbbd..89b3bbe24 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/PS.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/PS.java @@ -3,7 +3,6 @@ package com.intellectualcrafters.plot; import com.intellectualcrafters.configuration.ConfigurationSection; import com.intellectualcrafters.configuration.MemorySection; import com.intellectualcrafters.configuration.file.YamlConfiguration; -import com.intellectualcrafters.plot.commands.MainCommand; import com.intellectualcrafters.plot.commands.WE_Anywhere; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Configuration; @@ -54,7 +53,6 @@ import com.intellectualcrafters.plot.util.WorldUtil; import com.intellectualcrafters.plot.util.area.QuadMap; import com.plotsquared.listener.WESubscriber; import com.sk89q.worldedit.WorldEdit; - import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -106,10 +104,12 @@ public class PS { // public: public File styleFile; public File configFile; + public File commandsFile; public File translationFile; public YamlConfiguration style; public YamlConfiguration config; public YamlConfiguration storage; + public YamlConfiguration commands; public IPlotMain IMP = null; public TaskManager TASK; public WorldEdit worldedit; @@ -176,7 +176,7 @@ public class PS { if (this.IMP.initWorldEdit()) { this.worldedit = WorldEdit.getInstance(); WorldEdit.getInstance().getEventBus().register(new WESubscriber()); - MainCommand.getInstance().createCommand(new WE_Anywhere()); + new WE_Anywhere(); } // Events @@ -649,26 +649,24 @@ public class PS { plot.setArea(plotarea); } } - if (Settings.ENABLE_CLUSTERS) { - Set clusters = this.clusters_tmp.remove(plotarea.toString()); - if (clusters == null) { - if (plotarea.TYPE == 2) { - clusters = this.clusters_tmp.get(plotarea.worldname); - if (clusters != null) { - Iterator iter = clusters.iterator(); - while (iter.hasNext()) { - PlotCluster next = iter.next(); - if (next.intersects(plotarea.getMin(), plotarea.getMax())) { - next.setArea(plotarea); - iter.remove(); - } + Set clusters = this.clusters_tmp.remove(plotarea.toString()); + if (clusters == null) { + if (plotarea.TYPE == 2) { + clusters = this.clusters_tmp.get(plotarea.worldname); + if (clusters != null) { + Iterator iter = clusters.iterator(); + while (iter.hasNext()) { + PlotCluster next = iter.next(); + if (next.intersects(plotarea.getMin(), plotarea.getMax())) { + next.setArea(plotarea); + iter.remove(); } } } - } else { - for (PlotCluster cluster : clusters) { - cluster.setArea(plotarea); - } + } + } else { + for (PlotCluster cluster : clusters) { + cluster.setArea(plotarea); } } Set localAreas = getPlotAreas(plotarea.worldname); @@ -736,10 +734,8 @@ public class PS { public Set getClusters(String world) { HashSet set = new HashSet<>(); - if (Settings.ENABLE_CLUSTERS) { - for (PlotArea area : getPlotAreas(world)) { - set.addAll(area.getClusters()); - } + for (PlotArea area : getPlotAreas(world)) { + set.addAll(area.getClusters()); } return set; @@ -991,7 +987,7 @@ public class PS { Collections.sort(list, new Comparator() { @Override public int compare(Plot a, Plot b) { - return (int) Math.signum(ExpireManager.IMP.getTimestamp(a.owner) - ExpireManager.IMP.getTimestamp(b.owner)); + return Long.compare(ExpireManager.IMP.getTimestamp(a.owner), ExpireManager.IMP.getTimestamp(b.owner)); } }); return list; @@ -1785,9 +1781,7 @@ public class PS { } DBFunc.dbManager = new SQLManager(this.database, Settings.DB.PREFIX, false); this.plots_tmp = DBFunc.getPlots(); - if (Settings.ENABLE_CLUSTERS) { - this.clusters_tmp = DBFunc.getClusters(); - } + this.clusters_tmp = DBFunc.getClusters(); } catch (ClassNotFoundException | SQLException e) { log(C.PREFIX + "&cFailed to open DATABASE connection. The plugin will disable itself."); if (Settings.DB.USE_MONGO) { @@ -1967,9 +1961,6 @@ public class PS { options.put("protection.redstone.disable-offline", Settings.REDSTONE_DISABLER); options.put("protection.redstone.disable-unoccupied", Settings.REDSTONE_DISABLER_UNOCCUPIED); - // Clusters - options.put("clusters.enabled", Settings.ENABLE_CLUSTERS); - // PlotMe options.put("plotme-alias", Settings.USE_PLOTME_ALIAS); options.put("plotme-convert.enabled", Settings.CONVERT_PLOTME); @@ -2100,9 +2091,6 @@ public class PS { Settings.REDSTONE_DISABLER = this.config.getBoolean("protection.redstone.disable-offline"); Settings.REDSTONE_DISABLER_UNOCCUPIED = this.config.getBoolean("protection.redstone.disable-unoccupied"); - // Clusters - Settings.ENABLE_CLUSTERS = this.config.getBoolean("clusters.enabled"); - // PlotMe Settings.USE_PLOTME_ALIAS = this.config.getBoolean("plotme-alias"); Settings.CONVERT_PLOTME = this.config.getBoolean("plotme-convert.enabled"); @@ -2265,10 +2253,23 @@ public class PS { log("Failed to save storage.yml"); } try { - this.style.save(this.styleFile); - this.config.save(this.configFile); - this.storage.save(this.storageFile); - } catch (IOException e) { + commandsFile = new File(IMP.getDirectory() + File.separator + "config" + File.separator + "commands.yml"); + if (!commandsFile.exists()) { + if (!commandsFile.createNewFile()) { + log("Could not the storage settings file, please create \"commands.yml\" manually."); + } + } + commands = YamlConfiguration.loadConfiguration(commandsFile); + setupStorage(); + } catch (IOException err_trans) { + log("Failed to save commands.yml"); + } + try { + style.save(styleFile); + config.save(configFile); + storage.save(storageFile); + commands.save(commandsFile); + } catch (final IOException e) { log("Configuration file saving failed"); e.printStackTrace(); } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Add.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Add.java index ca959394b..fd7e2baef 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Add.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Add.java @@ -45,7 +45,7 @@ import java.util.UUID; public class Add extends SubCommand { public Add() { - this.requiredArguments = new Argument[]{Argument.PlayerName}; + super(Argument.PlayerName); } @Override diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/CommandCategory.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/CommandCategory.java index cdaa6aa55..6c958c42e 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/CommandCategory.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/CommandCategory.java @@ -8,13 +8,13 @@ package com.intellectualcrafters.plot.commands; */ public enum CommandCategory { /** - * Claiming Commands + * Claiming CommandConfig * * Such as: /plot claim */ CLAIMING("Claiming"), /** - * Teleportation Commands + * Teleportation CommandConfig * * Such as: /plot visit */ @@ -36,13 +36,13 @@ public enum CommandCategory { */ APPEARANCE("Cosmetic"), /** - * Information Commands + * Information CommandConfig * * Such as: /plot info */ INFO("Info"), /** - * Debug Commands + * Debug CommandConfig * * Such as: /plot debug */ diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java index 3674dd8a6..7ba064d51 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java @@ -37,6 +37,8 @@ import com.intellectualcrafters.plot.object.PlotBlock; import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.RunnableVal; +import com.intellectualcrafters.plot.object.RunnableVal2; +import com.intellectualcrafters.plot.object.RunnableVal3; import com.intellectualcrafters.plot.util.AbstractTitle; import com.intellectualcrafters.plot.util.ChunkManager; import com.intellectualcrafters.plot.util.EconHandler; @@ -74,7 +76,7 @@ import javax.script.SimpleScriptContext; @CommandDeclaration(command = "debugexec", permission = "plots.admin", description = "Mutli-purpose debug command", - aliases = "exec", + aliases = {"exec", "$"}, category = CommandCategory.DEBUG) public class DebugExec extends SubCommand { @@ -310,22 +312,19 @@ public class DebugExec extends SubCommand { .readLines(MainUtil.getFile(new File(PS.get().IMP.getDirectory() + File.separator + "scripts"), args[1]), StandardCharsets.UTF_8), System.getProperty("line.separator")); - Command subcommand = new Command(args[1].split("\\.")[0]) { + new Command(MainCommand.getInstance(), true, args[1].split("\\.")[0], null, RequiredType.NONE, CommandCategory.DEBUG) { @Override - public boolean onCommand(PlotPlayer plr, String[] args) { + public void execute(PlotPlayer player, String[] args, RunnableVal3 confirm, RunnableVal2 whenDone) { try { - DebugExec.this.scope.put("PlotPlayer", plr); + DebugExec.this.scope.put("PlotPlayer", player); DebugExec.this.scope.put("args", args); DebugExec.this.engine.eval(cmd, DebugExec.this.scope); - return true; } catch (ScriptException e) { e.printStackTrace(); MainUtil.sendMessage(player, C.COMMAND_WENT_WRONG); - return false; } } }; - MainCommand.getInstance().addCommand(subcommand); return true; } catch (IOException e) { e.printStackTrace(); @@ -358,7 +357,7 @@ public class DebugExec extends SubCommand { return false; } long start = System.currentTimeMillis(); - Command cmd = MainCommand.getInstance().getCommand(args[3]); + Command cmd = MainCommand.getInstance().getCommand(args[3]); String[] params = Arrays.copyOfRange(args, 4, args.length); if ("true".equals(args[1])) { Location loc = player.getMeta("location"); @@ -366,7 +365,7 @@ public class DebugExec extends SubCommand { for (Plot current : PS.get().getBasePlots()) { player.setMeta("location", current.getBottomAbs()); player.setMeta("lastplot", current); - cmd.onCommand(player, params); + cmd.execute(player, params, null, null); } if (loc == null) { player.deleteMeta("location"); diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/DebugFixFlags.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/DebugFixFlags.java index 117860494..8407127be 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/DebugFixFlags.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/DebugFixFlags.java @@ -47,7 +47,7 @@ import java.util.Map.Entry; public class DebugFixFlags extends SubCommand { public DebugFixFlags() { - this.requiredArguments = new Argument[]{Argument.String}; + super(Argument.String); } @Override diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Deny.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Deny.java index 99c242416..1fa1426cc 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Deny.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Deny.java @@ -45,7 +45,7 @@ import java.util.UUID; public class Deny extends SubCommand { public Deny() { - this.requiredArguments = new Argument[]{Argument.PlayerName}; + super(Argument.PlayerName); } @Override diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/GenerateDocs.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/GenerateDocs.java index 8cbbfd291..47342f66d 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/GenerateDocs.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/GenerateDocs.java @@ -1,10 +1,8 @@ package com.intellectualcrafters.plot.commands; import com.intellectualcrafters.plot.config.C; -import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.StringMan; import com.plotsquared.general.commands.Command; - import java.io.File; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -12,16 +10,14 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; -import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; public class GenerateDocs { public static void main(String[] args) { - MainCommand.getInstance().addCommand(new WE_Anywhere()); - MainCommand.getInstance().addCommand(new Cluster()); - ArrayList> commands = MainCommand.getInstance().getCommands(); + new WE_Anywhere(); + List commands = MainCommand.getInstance().getCommands(); log("### Want to document some commands?"); log(" - This page is automatically generated"); log(" - Fork the project and add a javadoc comment to one of the command classes"); @@ -30,22 +26,21 @@ public class GenerateDocs { log("# Contents"); for (CommandCategory category : CommandCategory.values()) { log("###### " + category.name()); - for (Command command : MainCommand.getCommands(category, null)) { - log(" - [/plot " + command.getCommand() + "](https://github.com/IntellectualSites/PlotSquared/wiki/Commands#" + command.getCommand() - + ") "); + for (Command command : MainCommand.getInstance().getCommands(category, null)) { + log(" - [/plot " + command.getId() + "](https://github.com/IntellectualSites/PlotSquared/wiki/Commands#" + command.getId() + ") "); } log(""); } log("# Commands"); - for (Command command : commands) { + for (Command command : commands) { printCommand(command); } } - public static void printCommand(Command command) { + public static void printCommand(Command command) { try { String clazz = command.getClass().getSimpleName(); - String name = command.getCommand(); + String name = command.getId(); // Header String source = @@ -88,7 +83,7 @@ public class GenerateDocs { log("`" + command.getRequiredType().name() + "`"); } - Set aliases = command.getAliases(); + List aliases = command.getAliases(); if (!aliases.isEmpty()) { log("#### Aliases"); log("`" + StringMan.getString(command.getAliases()) + "`"); diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Help.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Help.java index 5406712f2..16b1c2fa7 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Help.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Help.java @@ -1,16 +1,98 @@ package com.intellectualcrafters.plot.commands; +import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.object.RunnableVal2; +import com.intellectualcrafters.plot.object.RunnableVal3; +import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.util.MathMan; +import com.intellectualcrafters.plot.util.StringMan; +import com.intellectualcrafters.plot.util.helpmenu.HelpMenu; +import com.plotsquared.general.commands.Command; import com.plotsquared.general.commands.CommandDeclaration; @CommandDeclaration(command = "help", description = "Get this help menu", - aliases = {"he"}, - category = CommandCategory.INFO) -public class Help extends SubCommand { + aliases = {"he", "h", "?"}, + category = CommandCategory.INFO, + usage="help [category|#]", + permission="plots.use") +public class Help extends Command { + public Help(Command parent) { + super(parent, true); + } @Override - public boolean onCommand(PlotPlayer plr, String[] args) { + public boolean canExecute(PlotPlayer player) { return true; } + + @Override + public void execute(PlotPlayer player, String[] args, RunnableVal3 confirm, RunnableVal2 whenDone) { + switch (args.length) { + case 0: { + displayHelp(player, null, 0); + return; + } + case 1: { + if (MathMan.isInteger(args[0])) { + try { + displayHelp(player, null, Integer.parseInt(args[0])); + } catch (NumberFormatException e) { + displayHelp(player, null, 1); + } + } else { + displayHelp(player, args[0], 1); + } + return; + } + case 2: { + if (MathMan.isInteger(args[1])) { + try { + displayHelp(player, args[1], Integer.parseInt(args[1])); + } catch (NumberFormatException e) { + displayHelp(player, args[1], 1); + } + } + return; + } + default: { + C.COMMAND_SYNTAX.send(player, getUsage()); + } + } + } + + public void displayHelp(PlotPlayer player, String cat, int page) { + CommandCategory catEnum = null; + if (cat != null) { + if (StringMan.isEqualIgnoreCase(cat, "all")) { + catEnum = null; + } else { + for (CommandCategory c : CommandCategory.values()) { + if (StringMan.isEqualIgnoreCaseToAny(cat, c.name(), c.toString())) { + catEnum = c; + cat = c.name(); + break; + } + } + if (catEnum == null) { + cat = null; + } + } + } + if (cat == null && page == 0) { + StringBuilder builder = new StringBuilder(); + builder.append(C.HELP_HEADER.s()); + for (CommandCategory c : CommandCategory.values()) { + builder.append( + "\n" + StringMan.replaceAll(C.HELP_INFO_ITEM.s(), "%category%", c.toString().toLowerCase(), "%category_desc%", c.toString())); + } + builder.append("\n").append(C.HELP_INFO_ITEM.s().replaceAll("%category%", "all").replaceAll("%category_desc%", "Display all commands")); + builder.append("\n" + C.HELP_FOOTER.s()); + MainUtil.sendMessage(player, builder.toString(), false); + return; + } + page--; + new HelpMenu(player).setCategory(catEnum).getCommands().generateMaxPages().generatePage(page, getParent().toString()).render(); + } } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Home.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Home.java deleted file mode 100644 index d9a5197c0..000000000 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Home.java +++ /dev/null @@ -1,38 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////////////////////////// -// PlotSquared - A plot manager and world generator for the Bukkit API / -// Copyright (c) 2014 IntellectualSites/IntellectualCrafters / -// / -// This program is free software; you can redistribute it and/or modify / -// it under the terms of the GNU General Public License as published by / -// the Free Software Foundation; either version 3 of the License, or / -// (at your option) any later version. / -// / -// This program is distributed in the hope that it will be useful, / -// but WITHOUT ANY WARRANTY; without even the implied warranty of / -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the / -// GNU General Public License for more details. / -// / -// You should have received a copy of the GNU General Public License / -// along with this program; if not, write to the Free Software Foundation, / -// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA / -// / -// You can contact us via: support@intellectualsites.com / -//////////////////////////////////////////////////////////////////////////////////////////////////// -package com.intellectualcrafters.plot.commands; - -import com.intellectualcrafters.plot.object.PlotPlayer; -import com.plotsquared.general.commands.CommandDeclaration; - -@CommandDeclaration(command = "home", - aliases = {"h"}, - description = "Go to your plot", - usage = "/plot home [id|alias]", - category = CommandCategory.TELEPORT, - requiredType = RequiredType.NONE) -public class Home extends SubCommand { - - @Override - public boolean onCommand(PlotPlayer plr, String[] args) { - return MainCommand.getInstance().getCommand("visit").onCommand(plr, args); - } -} diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/list.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/ListCmd.java similarity index 99% rename from Core/src/main/java/com/intellectualcrafters/plot/commands/list.java rename to Core/src/main/java/com/intellectualcrafters/plot/commands/ListCmd.java index 006bc560b..8b6643e11 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/list.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/ListCmd.java @@ -56,7 +56,7 @@ import java.util.UUID; permission = "plots.list", category = CommandCategory.INFO, usage = "/plot list > [#]") -public class List extends SubCommand { +public class ListCmd extends SubCommand { private String[] getArgumentList(PlotPlayer player) { java.util.List args = new ArrayList<>(); diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java index 29ff2e7df..01369af4f 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java @@ -20,287 +20,221 @@ //////////////////////////////////////////////////////////////////////////////////////////////////// package com.intellectualcrafters.plot.commands; +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.PlotArea; import com.intellectualcrafters.plot.object.PlotPlayer; -import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.object.RunnableVal2; +import com.intellectualcrafters.plot.object.RunnableVal3; +import com.intellectualcrafters.plot.util.CmdConfirm; +import com.intellectualcrafters.plot.util.EconHandler; import com.intellectualcrafters.plot.util.MathMan; import com.intellectualcrafters.plot.util.Permissions; -import com.intellectualcrafters.plot.util.StringComparison; -import com.intellectualcrafters.plot.util.StringMan; -import com.intellectualcrafters.plot.util.helpmenu.HelpMenu; -import com.plotsquared.general.commands.Argument; import com.plotsquared.general.commands.Command; -import com.plotsquared.general.commands.CommandHandlingOutput; -import com.plotsquared.general.commands.CommandManager; - -import java.util.ArrayList; +import com.plotsquared.general.commands.CommandDeclaration; import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; /** * PlotSquared command class. */ -public class MainCommand extends CommandManager { - +@CommandDeclaration( + command = "plot", + aliases = {"plots", "p", "plotsquared", "plot2", "p2", "ps", "2", "plotme", "plotz", "ap"}) +public class MainCommand extends Command { + private static MainCommand instance; + private Help help; private MainCommand() { - super(null, new ArrayList>()); + super(null, true); instance = this; - createCommand(new Buy()); - createCommand(new Save()); - createCommand(new Load()); - createCommand(new Confirm()); - createCommand(new Template()); - createCommand(new Download()); - createCommand(new Update()); - createCommand(new Template()); - createCommand(new Setup()); - createCommand(new Area()); - createCommand(new DebugSaveTest()); - createCommand(new DebugLoadTest()); - createCommand(new CreateRoadSchematic()); - createCommand(new DebugAllowUnsafe()); - createCommand(new RegenAllRoads()); - createCommand(new Claim()); - createCommand(new Auto()); - createCommand(new Visit()); - createCommand(new Home()); - createCommand(new Set()); - createCommand(new Toggle()); - createCommand(new Clear()); - createCommand(new Delete()); - createCommand(new Trust()); - createCommand(new Add()); - createCommand(new Deny()); - createCommand(new Untrust()); - createCommand(new Remove()); - createCommand(new Undeny()); - createCommand(new Info()); - createCommand(new com.intellectualcrafters.plot.commands.List()); - createCommand(new Help()); - createCommand(new Debug()); - createCommand(new SchematicCmd()); - createCommand(new Plugin()); - createCommand(new Purge()); - createCommand(new Reload()); - createCommand(new Merge()); - createCommand(new DebugPaste()); - createCommand(new Unlink()); - createCommand(new Kick()); - createCommand(new Rate()); - createCommand(new DebugClaimTest()); - createCommand(new Inbox()); - createCommand(new Comment()); - createCommand(new Database()); - createCommand(new Swap()); - createCommand(new Music()); - createCommand(new DebugRoadRegen()); - createCommand(new Trust()); - createCommand(new DebugExec()); - createCommand(new FlagCmd()); - createCommand(new Target()); - createCommand(new DebugFixFlags()); - createCommand(new Move()); - createCommand(new Condense()); - createCommand(new Condense()); - createCommand(new Copy()); - createCommand(new Chat()); - createCommand(new Trim()); - createCommand(new Done()); - createCommand(new Continue()); - createCommand(new BO3()); - createCommand(new Middle()); - createCommand(new Grant()); - // set commands - createCommand(new Owner()); - createCommand(new Desc()); - createCommand(new Biome()); - createCommand(new Alias()); - createCommand(new SetHome()); - if (Settings.ENABLE_CLUSTERS) { - MainCommand.getInstance().addCommand(new Cluster()); - } } public static MainCommand getInstance() { if (instance == null) { instance = new MainCommand(); + new Buy(); + new Save(); + new Load(); + new Confirm(); + new Template(); + new Download(); + new Update(); + new Template(); + new Setup(); + new Area(); + new DebugSaveTest(); + new DebugLoadTest(); + new CreateRoadSchematic(); + new DebugAllowUnsafe(); + new RegenAllRoads(); + new Claim(); + new Auto(); + new Visit(); + new Set(); + new Toggle(); + new Clear(); + new Delete(); + new Trust(); + new Add(); + new Deny(); + new Untrust(); + new Remove(); + new Undeny(); + new Info(); + new ListCmd(); + instance.help = new Help(instance); + new Debug(); + new SchematicCmd(); + new PluginCmd(); + new Purge(); + new Reload(); + new Merge(); + new DebugPaste(); + new Unlink(); + new Kick(); + new Rate(); + new DebugClaimTest(); + new Inbox(); + new Comment(); + new Database(); + new Swap(); + new Music(); + new DebugRoadRegen(); + new Trust(); + new DebugExec(); + new FlagCmd(); + new Target(); + new DebugFixFlags(); + new Move(); + new Condense(); + new Condense(); + new Copy(); + new Chat(); + new Trim(); + new Done(); + new Continue(); + new BO3(); + new Middle(); + new Grant(); + // set commands + new Owner(); + new Desc(); + new Biome(); + new Alias(); + new SetHome(); + new Cluster(); } return instance; } - public static boolean no_permission(PlotPlayer player, String permission) { - MainUtil.sendMessage(player, C.NO_PERMISSION, permission); - return false; + @Deprecated + /** + * @Deprecated legacy + */ + public void addCommand(Command command) { + PS.debug("Command registration is now done during instantiation"); } - public static List> getCommandAndAliases(CommandCategory category, PlotPlayer player) { - List> commands = new ArrayList<>(); - for (Command command : getInstance().getCommands()) { - if ((category != null) && !command.getCategory().equals(category)) { - continue; + public static boolean onCommand(final PlotPlayer player, String... args) { + if (args.length >= 1 && args[0].contains(":")) { + String[] split2 = args[0].split(":"); + if (split2.length == 2) { + // Ref: c:v, this will push value to the last spot in the array + // ex. /p h:2 SomeUsername + // > /p h SomeUsername 2 + String[] tmp = new String[args.length + 1]; + tmp[0] = split2[0]; + tmp[args.length] = split2[1]; + if (args.length > 2) { + System.arraycopy(args, 1, tmp, 1, args.length - 1); + } + args = tmp; } - if ((player != null) && !Permissions.hasPermission(player, command.getPermission())) { - continue; - } - commands.add(command); } - return commands; - } - - public static List> getCommands(CommandCategory category, PlotPlayer player) { - List> commands = new ArrayList<>(); - for (Command command : new HashSet<>(getInstance().getCommands())) { - if ((category != null) && !command.getCategory().equals(category)) { - continue; - } - if ((player != null) && !Permissions.hasPermission(player, command.getPermission())) { - continue; - } - commands.add(command); + { + try { + if (args.length == 0 || MathMan.isInteger(args[0]) || CommandCategory.valueOf(args[0].toUpperCase()) != null) { + // This will default certain syntax to the help command + // e.g. /plot, /plot 1, /plot claiming + getInstance().help.execute(player, args, null, null); + return true; + } + } catch (IllegalArgumentException e) {} } - return commands; - } - - public static void displayHelp(PlotPlayer player, String cat, int page, String label) { - CommandCategory catEnum = null; - if (cat != null) { - if (StringMan.isEqualIgnoreCase(cat, "all")) { - catEnum = null; - } else { - for (CommandCategory c : CommandCategory.values()) { - if (StringMan.isEqualIgnoreCaseToAny(cat, c.name(), c.toString())) { - catEnum = c; - cat = c.name(); - break; + getInstance().execute(player, args, new RunnableVal3() { + @Override + public void run(final Command cmd, final Runnable success, final Runnable failure) { + if (cmd.hasConfirmation(player) ) { + CmdConfirm.addPending(player, "/plot area create pos2 (Creates world)", new Runnable() { + @Override + public void run() { + if (EconHandler.manager != null) { + PlotArea area = player.getApplicablePlotArea(); + if (area != null) { + Double price = area.PRICES.get(cmd.getId()); + if (price != null && EconHandler.manager.getMoney(player) < price) { + failure.run(); + return; + } + } + } + success.run(); + } + }); + return; + } + if (EconHandler.manager != null) { + PlotArea area = player.getApplicablePlotArea(); + if (area != null) { + Double price = area.PRICES.get(cmd.getId()); + if (price != null && EconHandler.manager.getMoney(player) < price) { + failure.run(); + return; + } } } - if (catEnum == null) { - cat = null; - } + success.run(); } - } - if (cat == null && page == 0) { - StringBuilder builder = new StringBuilder(); - builder.append(C.HELP_HEADER.s()); - for (CommandCategory c : CommandCategory.values()) { - builder.append( - "\n" + StringMan.replaceAll(C.HELP_INFO_ITEM.s(), "%category%", c.toString().toLowerCase(), "%category_desc%", c.toString())); + }, new RunnableVal2() { + @Override + public void run(Command cmd, CommandResult result) { + // Post command stuff!? } - builder.append("\n").append(C.HELP_INFO_ITEM.s().replaceAll("%category%", "all").replaceAll("%category_desc%", "Display all commands")); - builder.append("\n" + C.HELP_FOOTER.s()); - MainUtil.sendMessage(player, builder.toString(), false); - return; - } - page--; - new HelpMenu(player).setCategory(catEnum).getCommands().generateMaxPages().generatePage(page, label).render(); + }); + // Always true + return true; } - public static boolean onCommand(PlotPlayer player, String cmd, String... args) { + @Override + public void execute(PlotPlayer player, String[] args, RunnableVal3 confirm, RunnableVal2 whenDone) { // Clear perm caching // player.deleteMeta("perm"); - //////////////////////// - int help_index = -1; + // Optional command scope // String category = null; Location loc = null; Plot plot = null; boolean tp = false; - switch (args.length) { - case 0: { - help_index = 0; - break; - } - case 1: { - if (MathMan.isInteger(args[0])) { - try { - help_index = Integer.parseInt(args[args.length - 1]); - } catch (NumberFormatException e) { - } - break; - } - } - default: { - switch (args[0].toLowerCase()) { - case "he": - case "help": - case "?": { - switch (args.length) { - case 1: { - help_index = 0; - break; - } - case 2: { - if (MathMan.isInteger(args[1])) { - category = null; - try { - help_index = Integer.parseInt(args[1]); - } catch (NumberFormatException e) { - help_index = 1; - } - } else { - help_index = 1; - category = args[1]; - } - break; - } - case 3: { - category = args[1]; - if (MathMan.isInteger(args[2])) { - try { - help_index = Integer.parseInt(args[2]); - } catch (NumberFormatException e) { - help_index = 1; - } - } - break; - } - default: { - C.COMMAND_SYNTAX.send(player, "/" + cmd + "? [#||category [#]]"); - return true; - } - } - break; - } - default: { - if (args.length >= 2) { - PlotArea area = player.getApplicablePlotArea(); - Plot newPlot = Plot.fromString(area, args[0]); - if (newPlot == null) { - break; - } - if (!ConsolePlayer.isConsole(player) && (!newPlot.getArea().equals(area) || newPlot.isDenied(player.getUUID())) - && !Permissions.hasPermission(player, C.PERMISSION_ADMIN)) { - break; - } - // Save meta - loc = player.getMeta("location"); - plot = player.getMeta("lastplot"); - tp = true; - // Set loc - player.setMeta("location", newPlot.getBottomAbs()); - player.setMeta("lastplot", newPlot); - // Trim command - args = Arrays.copyOfRange(args, 1, args.length); - } - } - } + if (args.length >= 2) { + PlotArea area = player.getApplicablePlotArea(); + Plot newPlot = Plot.fromString(area, args[0]); + if (newPlot != null && (ConsolePlayer.isConsole(player) || newPlot.getArea().equals(area) || Permissions.hasPermission(player, C.PERMISSION_ADMIN)) && !newPlot.isDenied(player.getUUID())) { + // Save meta + loc = player.getMeta("location"); + plot = player.getMeta("lastplot"); + tp = true; + // Set loc + player.setMeta("location", newPlot.getBottomAbs()); + player.setMeta("lastplot", newPlot); + // Trim command + args = Arrays.copyOfRange(args, 1, args.length); } } - if (help_index != -1) { - displayHelp(player, category, help_index, cmd); - return true; - } - String fullCmd = StringMan.join(args, " "); - getInstance().handle(player, cmd + " " + fullCmd); - // Restore location + super.execute(player, args, confirm, whenDone); + // Reset command scope // if (tp) { if (loc == null) { player.deleteMeta("location"); @@ -313,148 +247,10 @@ public class MainCommand extends CommandManager { player.setMeta("lastplot", plot); } } - return true; - } - - public int getMatch(String[] args, Command cmd) { - int count = 0; - String perm = cmd.getPermission(); - HashSet desc = new HashSet<>(); - for (String alias : cmd.getAliases()) { - if (alias.startsWith(args[0])) { - count += 5; - } - } - Collections.addAll(desc, cmd.getDescription().split(" ")); - for (String arg : args) { - if (perm.startsWith(arg)) { - count++; - } - if (desc.contains(arg)) { - count++; - } - } - String[] usage = cmd.getUsage().split(" "); - for (int i = 0; i < Math.min(4, usage.length); i++) { - int require; - if (usage[i].startsWith("<")) { - require = 1; - } else { - require = 0; - } - String[] split = usage[i].split("\\|| |\\>|\\<|\\[|\\]|\\{|\\}|\\_|\\/"); - for (String aSplit : split) { - for (String arg : args) { - if (StringMan.isEqualIgnoreCase(arg, aSplit)) { - count += 5 - i + require; - } - } - } - } - count += StringMan.intersection(desc, args); - return count; } @Override - public int handle(PlotPlayer plr, String input) { - String[] parts = input.split(" "); - String[] args; - String label; - if (parts.length == 1) { - label = null; - args = new String[0]; - } else { - label = parts[1]; - args = new String[parts.length - 2]; - System.arraycopy(parts, 2, args, 0, args.length); - } - Command cmd; - if (label != null) { - if (label.contains(":")) { - // Ref: c:v, this will push value to the last spot in the array - // ex. /p h:2 SomeUsername - // > /p h SomeUsername 2 - String[] temp = label.split(":"); - if (temp.length == 2) { - label = temp[0]; - String[] tempArgs = new String[args.length + 1]; - System.arraycopy(args, 0, tempArgs, 0, args.length); - tempArgs[tempArgs.length - 1] = temp[1]; - args = tempArgs; - } - } - cmd = getInstance().commands.get(label.toLowerCase()); - } else { - cmd = null; - } - if (cmd == null) { - MainUtil.sendMessage(plr, C.NOT_VALID_SUBCOMMAND); - { - List> cmds = getCommands(null, plr); - if ((label == null) || cmds.isEmpty()) { - MainUtil.sendMessage(plr, C.DID_YOU_MEAN, "/plot help"); - } else { - HashSet setargs = new HashSet<>(args.length + 1); - for (String arg : args) { - setargs.add(arg.toLowerCase()); - } - setargs.add(label); - String[] allargs = setargs.toArray(new String[setargs.size()]); - int best = 0; - for (Command current : cmds) { - int match = getMatch(allargs, current); - if (match > best) { - cmd = current; - } - } - if (cmd == null) { - cmd = new StringComparison<>(label, getCommandAndAliases(null, plr)).getMatchObject(); - } - MainUtil.sendMessage(plr, C.DID_YOU_MEAN, cmd.getUsage().replaceAll("\\{label\\}", parts[0])); - } - } - return CommandHandlingOutput.NOT_FOUND; - } - if (!cmd.getRequiredType().allows(plr)) { - if (ConsolePlayer.isConsole(plr)) { - MainUtil.sendMessage(plr, C.IS_CONSOLE); - } else { - MainUtil.sendMessage(plr, C.NOT_CONSOLE); - } - return CommandHandlingOutput.CALLER_OF_WRONG_TYPE; - } - if (!Permissions.hasPermission(plr, cmd.getPermission())) { - MainUtil.sendMessage(plr, C.NO_PERMISSION, cmd.getPermission()); - return CommandHandlingOutput.NOT_PERMITTED; - } - Argument[] requiredArguments = cmd.getRequiredArguments(); - if ((requiredArguments != null) && (requiredArguments.length > 0)) { - boolean success = true; - if (args.length < requiredArguments.length) { - success = false; - } else { - for (int i = 0; i < requiredArguments.length; i++) { - if (requiredArguments[i].parse(args[i]) == null) { - success = false; - break; - } - } - } - if (!success) { - C.COMMAND_SYNTAX.send(plr, cmd.getUsage()); - return CommandHandlingOutput.WRONG_USAGE; - } - } - try { - boolean result = cmd.onCommand(plr, args); - if (!result) { - cmd.getUsage(); - return CommandHandlingOutput.WRONG_USAGE; - } - } catch (Throwable t) { - t.printStackTrace(); - return CommandHandlingOutput.ERROR; - } - return CommandHandlingOutput.SUCCESS; + public boolean canExecute(PlotPlayer player) { + return true; } } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/plugin.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/PluginCmd.java similarity index 98% rename from Core/src/main/java/com/intellectualcrafters/plot/commands/plugin.java rename to Core/src/main/java/com/intellectualcrafters/plot/commands/PluginCmd.java index 2ac8bc7e4..ad840ce38 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/plugin.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/PluginCmd.java @@ -30,7 +30,7 @@ import com.plotsquared.general.commands.CommandDeclaration; description = "Show plugin information", aliases = "version", category = CommandCategory.INFO) -public class Plugin extends SubCommand { +public class PluginCmd extends SubCommand { @Override public boolean onCommand(PlotPlayer plr, String[] args) { diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Rate.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Rate.java index 35758cd84..eb1c78ad0 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Rate.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Rate.java @@ -134,9 +134,9 @@ public class Rate extends SubCommand { } setTitle(Settings.RATING_CATEGORIES.get(index.getValue())); if (Permissions.hasPermission(this.player, "plots.comment")) { - Command command = MainCommand.getInstance().getCommand("comment"); + Command command = MainCommand.getInstance().getCommand("comment"); if (command != null) { - MainUtil.sendMessage(this.player, C.COMMENT_THIS, command.getUsage().replaceAll("{label}", "plot")); + MainUtil.sendMessage(this.player, C.COMMENT_THIS, command.getUsage()); } } return false; diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Remove.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Remove.java index 8f1d09003..dfb39a87c 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Remove.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Remove.java @@ -45,7 +45,7 @@ import java.util.UUID; public class Remove extends SubCommand { public Remove() { - this.requiredArguments = new Argument[]{Argument.PlayerName}; + super(Argument.PlayerName); } @Override diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Set.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Set.java index 63c2d57e4..ce7611968 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Set.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Set.java @@ -61,7 +61,7 @@ public class Set extends SubCommand { this.component = new SetCommand() { @Override - public String getCommand() { + public String getId() { return "set.component"; } @@ -163,9 +163,10 @@ public class Set extends SubCommand { if (args.length == 0) { return noArgs(plr); } - Command cmd = MainCommand.getInstance().getCommand("set" + args[0]); + Command cmd = MainCommand.getInstance().getCommand("set" + args[0]); if (cmd != null) { - return cmd.onCommand(plr, Arrays.copyOfRange(args, 1, args.length)); + cmd.execute(plr, Arrays.copyOfRange(args, 1, args.length), null, null); + return true; } // Additional checks Plot plot = plr.getCurrentPlot(); @@ -193,7 +194,7 @@ public class Set extends SubCommand { a.append(" ").append(args[x]); } } - MainCommand.onCommand(plr, "plot", ("flag set " + args[0] + a.toString()).split(" ")); + MainCommand.onCommand(plr, ("flag set " + args[0] + a.toString()).split(" ")); return true; } } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/SetCommand.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/SetCommand.java index 0734ccdb2..ffdb93f6d 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/SetCommand.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/SetCommand.java @@ -18,15 +18,15 @@ public abstract class SetCommand extends SubCommand { return !sendMessage(plr, C.NOT_IN_PLOT); } if (!plot.hasOwner()) { - if (!Permissions.hasPermission(plr, "plots.admin.command." + getCommand())) { - MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.admin.command." + getCommand()); + if (!Permissions.hasPermission(plr, "plots.admin.command." + getId())) { + MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.admin.command." + getId()); MainUtil.sendMessage(plr, C.PLOT_NOT_CLAIMED); return false; } } if (!plot.isOwner(plr.getUUID())) { - if (!Permissions.hasPermission(plr, "plots.admin.command." + getCommand())) { - MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.admin.command." + getCommand()); + if (!Permissions.hasPermission(plr, "plots.admin.command." + getId())) { + MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.admin.command." + getId()); MainUtil.sendMessage(plr, C.NO_PLOT_PERMS); return false; } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/SubCommand.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/SubCommand.java index 811451b54..69fc41a0d 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/SubCommand.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/SubCommand.java @@ -21,80 +21,37 @@ package com.intellectualcrafters.plot.commands; import com.intellectualcrafters.plot.config.C; -import com.intellectualcrafters.plot.object.PlotMessage; import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.object.RunnableVal2; import com.intellectualcrafters.plot.object.RunnableVal3; -import com.intellectualcrafters.plot.util.MainUtil; - -import java.util.List; +import com.plotsquared.general.commands.Argument; +import com.plotsquared.general.commands.Command; /** - * SubCommand class. + * SubCommand class + * @see com.plotsquared.general.commands.Command(com.plotsquared.general.commands.Command, boolean) + * @Deprecated In favor of normal Command class */ -public abstract class SubCommand extends com.plotsquared.general.commands.Command { +public abstract class SubCommand extends Command { + public SubCommand() { + super(MainCommand.getInstance(), true); + } - /** - * The category. - */ - public CommandCategory category; + public SubCommand(Argument... arguments) { + this(); + setRequiredArguments(arguments); + } - /** - * Send a message. - * - * @param plr Player who will receive the message - * @param c Caption - * @param args Arguments (%s's) - * - * @see MainUtil#sendMessage(PlotPlayer, C, String...) - */ - public boolean sendMessage(PlotPlayer plr, C c, String... args) { - c.send(plr, args); + @Override + public void execute(PlotPlayer player, String[] args, RunnableVal3 confirm, RunnableVal2 whenDone) { + onCommand(player, args); + } + + + public abstract boolean onCommand(PlotPlayer plr, String[] args); + + public boolean sendMessage(PlotPlayer player, C message, Object... args) { + message.send(player, args); return true; } - - public void paginate(PlotPlayer player, List c, int size, int page, RunnableVal3 add, String baseCommand, - String header) { - // Calculate pages & index - if (page < 0) { - page = 0; - } - int totalPages = (int) Math.ceil(c.size() / size); - if (page > totalPages) { - page = totalPages; - } - int max = page * size + size; - if (max > c.size()) { - max = c.size(); - } - // Send the header - header = header.replaceAll("%cur", page + 1 + "").replaceAll("%max", totalPages + 1 + "").replaceAll("%amount%", c.size() + "") - .replaceAll("%word%", "all"); - MainUtil.sendMessage(player, header); - // Send the page content - List subList = c.subList(page * size, max); - int i = page * size; - for (T obj : subList) { - i++; - PlotMessage msg = new PlotMessage(); - add.run(i, obj, msg); - msg.send(player); - } - // Send the footer - if (page < totalPages && page > 0) { // Back | Next - new PlotMessage().text("<-").color("$1").command(baseCommand + " " + page).text(" | ").color("$3").text("->").color("$1") - .command(baseCommand + " " + (page + 2)) - .text(C.CLICKABLE.s()).color("$2").send(player); - return; - } - if (page == 0 && totalPages != 0) { // Next - new PlotMessage().text("<-").color("$3").text(" | ").color("$3").text("->").color("$1").command(baseCommand + " " + (page + 2)) - .text(C.CLICKABLE.s()).color("$2").send(player); - return; - } - if (page == totalPages && totalPages != 0) { // Back - new PlotMessage().text("<-").color("$1").command(baseCommand + " " + page).text(" | ").color("$3").text("->").color("$3") - .text(C.CLICKABLE.s()).color("$2").send(player); - - } - } } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Toggle.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Toggle.java index 3725bd8ae..a8cca8817 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Toggle.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Toggle.java @@ -20,20 +20,14 @@ //////////////////////////////////////////////////////////////////////////////////////////////////// package com.intellectualcrafters.plot.commands; -import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.object.RunnableVal2; +import com.intellectualcrafters.plot.object.RunnableVal3; import com.intellectualcrafters.plot.util.MainUtil; -import com.intellectualcrafters.plot.util.Permissions; -import com.intellectualcrafters.plot.util.StringMan; import com.plotsquared.general.commands.Command; import com.plotsquared.general.commands.CommandDeclaration; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map.Entry; - @CommandDeclaration( command = "toggle", aliases = {"attribute"}, @@ -42,97 +36,59 @@ import java.util.Map.Entry; usage = "/plot toggle ", requiredType = RequiredType.NONE, category = CommandCategory.SETTINGS) -public class Toggle extends SubCommand { - - private final HashMap> toggles; - +public class Toggle extends Command { public Toggle() { - this.toggles = new HashMap<>(); - this.toggles.put("titles", - new Command("titles", "/plot toggle titles", "Toggle titles for yourself", C.PERMISSION_PLOT_TOGGLE_TITLES.s()) { - - @Override - public boolean onCommand(PlotPlayer player, String[] args) { - if (toggle(player, "disabletitles")) { - MainUtil.sendMessage(player, C.TOGGLE_ENABLED, getCommand()); - } else { - MainUtil.sendMessage(player, C.TOGGLE_DISABLED, getCommand()); - } - return true; - } - }); - this.toggles.put("chatspy", new Command("chatspy", "/plot toggle chatspy", "Toggle chat spying", C.PERMISSION_COMMANDS_CHAT.s()) { - - @Override - public boolean onCommand(PlotPlayer player, String[] args) { - if (toggle(player, "chatspy")) { - MainUtil.sendMessage(player, C.TOGGLE_DISABLED, getCommand()); - } else { - MainUtil.sendMessage(player, C.TOGGLE_ENABLED, getCommand()); - } - return true; - } - }); - this.toggles.put("chat", - new Command("chat", "/plot toggle chat", "Toggle plot chat for yourself", C.PERMISSION_PLOT_TOGGLE_CHAT.s()) { - - @Override - public boolean onCommand(PlotPlayer player, String[] args) { - if (toggle(player, "chat")) { - MainUtil.sendMessage(player, C.PLOT_CHAT_OFF); - } else { - MainUtil.sendMessage(player, C.PLOT_CHAT_ON); - } - return true; - } - }); - if (PS.get() != null && PS.get().worldedit != null) { - this.toggles.put("worldedit", - new Command("worldedit", "/plot toggle worldedit", "Toggle worldedit bypass", C.PERMISSION_WORLDEDIT_BYPASS.s()) { - - @Override - public boolean onCommand(PlotPlayer player, String[] args) { - if (toggle(player, "worldedit")) { - MainUtil.sendMessage(player, C.WORLDEDIT_RESTRICTED); - } else { - MainUtil.sendMessage(player, C.WORLDEDIT_UNMASKED); - } - return true; - } - }); - } - + super(MainCommand.getInstance(), true); } - public void noArgs(PlotPlayer plr) { - MainUtil.sendMessage(plr, C.COMMAND_SYNTAX, "/plot toggle "); - ArrayList options = new ArrayList<>(); - for (Entry> entry : this.toggles.entrySet()) { - if (Permissions.hasPermission(plr, entry.getValue().getPermission())) { - options.add(entry.getKey()); - } - } - if (!options.isEmpty()) { - MainUtil.sendMessage(plr, C.SUBCOMMAND_SET_OPTIONS_HEADER.s() + StringMan.join(options, ",")); + @CommandDeclaration( + command = "chatspy", + aliases = {"spy"}, + permission = "plots.admin.command.chat", + description = "Toggle admin chat spying") + public void chatspy(Command command, final PlotPlayer player, String[] args, RunnableVal3 confirm, RunnableVal2 whenDone) { + if (toggle(player, "chatspy")) { + MainUtil.sendMessage(player, C.TOGGLE_DISABLED, command.toString()); + } else { + MainUtil.sendMessage(player, C.TOGGLE_ENABLED, command.toString()); } } - @Override - public boolean onCommand(PlotPlayer player, String[] args) { - if (args.length == 0) { - noArgs(player); - return false; + @CommandDeclaration( + command = "worldedit", + aliases = {"we", "wea"}, + permission = "plots.worldedit.bypass", + description = "Toggle worldedit area restrictions") + public void worldedit(Command command, final PlotPlayer player, String[] args, RunnableVal3 confirm, RunnableVal2 whenDone) { + if (toggle(player, "worldedit")) { + MainUtil.sendMessage(player, C.TOGGLE_DISABLED, command.toString()); + } else { + MainUtil.sendMessage(player, C.TOGGLE_ENABLED, command.toString()); } - Command cmd = this.toggles.get(args[0].toLowerCase()); - if (cmd == null) { - noArgs(player); - return false; + } + + @CommandDeclaration( + command = "chat", + permission = "plots.toggle.chat", + description = "Toggle plot chat") + public void chat(Command command, final PlotPlayer player, String[] args, RunnableVal3 confirm, RunnableVal2 whenDone) { + if (toggle(player, "chat")) { + MainUtil.sendMessage(player, C.TOGGLE_DISABLED, command.toString()); + } else { + MainUtil.sendMessage(player, C.TOGGLE_ENABLED, command.toString()); } - if (!Permissions.hasPermission(player, cmd.getPermission())) { - C.NO_PERMISSION.send(player, cmd.getPermission()); - return false; + } + + @CommandDeclaration( + command = "titles", + permission = "plots.toggle.titles", + description = "Toggle plot title messages") + public void titles(Command command, final PlotPlayer player, String[] args, RunnableVal3 confirm, RunnableVal2 whenDone) { + if (toggle(player, "titles")) { + MainUtil.sendMessage(player, C.TOGGLE_DISABLED, command.toString()); + } else { + MainUtil.sendMessage(player, C.TOGGLE_ENABLED, command.toString()); } - return cmd.onCommand(player, Arrays.copyOfRange(args, 1, args.length)); } public boolean toggle(PlotPlayer player, String key) { diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Trust.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Trust.java index c3e5b2e1f..f16ff0223 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Trust.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Trust.java @@ -44,7 +44,7 @@ import java.util.UUID; public class Trust extends SubCommand { public Trust() { - this.requiredArguments = new Argument[]{Argument.PlayerName}; + super(Argument.PlayerName); } @Override diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Undeny.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Undeny.java index c50cc729c..f781ebd91 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Undeny.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Undeny.java @@ -43,7 +43,7 @@ import java.util.UUID; public class Undeny extends SubCommand { public Undeny() { - this.requiredArguments = new Argument[]{Argument.PlayerName}; + super(Argument.PlayerName); } @Override diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Untrust.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Untrust.java index 33a9e98d9..4add20bb9 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Untrust.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Untrust.java @@ -46,7 +46,7 @@ import java.util.UUID; public class Untrust extends SubCommand { public Untrust() { - this.requiredArguments = new Argument[]{Argument.PlayerName}; + super(Argument.PlayerName); } @Override diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Visit.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Visit.java index 19e9b6cf0..d3f344ae5 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Visit.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Visit.java @@ -43,13 +43,13 @@ import java.util.UUID; permission = "plots.visit", description = "Visit someones plot", usage = "/plot visit [player|alias|world|id] [#]", - aliases = {"v", "tp", "teleport", "goto"}, + aliases = {"v", "tp", "teleport", "goto", "home"}, requiredType = RequiredType.NONE, category = CommandCategory.TELEPORT) public class Visit extends SubCommand { public Visit() { - this.requiredArguments = new Argument[]{Argument.String}; + super(Argument.String); } public List getPlots(UUID uuid) { diff --git a/Core/src/main/java/com/intellectualcrafters/plot/config/C.java b/Core/src/main/java/com/intellectualcrafters/plot/config/C.java index f395ee591..16e2ffc23 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/config/C.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/config/C.java @@ -369,14 +369,14 @@ public enum C { UNLINK_IMPOSSIBLE("$2You can only unlink a mega-plot", "Merge"), UNLINK_SUCCESS("$2Successfully unlinked plots.", "Merge"), /* - * Commands + * CommandConfig */ - NOT_VALID_SUBCOMMAND("$2That is not a valid subcommand", "Commands"), - DID_YOU_MEAN("$2Did you mean: $1%s", "Commands"), - NAME_LITTLE("$2%s0 name is too short, $1%s1$2<$1%s3", "Commands"), - NO_COMMANDS("$2I'm sorry, but you're not permitted to use any subcommands.", "Commands"), - SUBCOMMAND_SET_OPTIONS_HEADER("$2Possible Values: ", "Commands"), - COMMAND_SYNTAX("$1Usage: $2%s", "Commands"), + NOT_VALID_SUBCOMMAND("$2That is not a valid subcommand", "CommandConfig"), + DID_YOU_MEAN("$2Did you mean: $1%s", "CommandConfig"), + NAME_LITTLE("$2%s0 name is too short, $1%s1$2<$1%s3", "CommandConfig"), + NO_COMMANDS("$2I'm sorry, but you're not permitted to use any subcommands.", "CommandConfig"), + SUBCOMMAND_SET_OPTIONS_HEADER("$2Possible Values: ", "CommandConfig"), + COMMAND_SYNTAX("$1Usage: $2%s", "CommandConfig"), /* * Player not found */ @@ -573,7 +573,7 @@ public enum C { FLAG_NOT_ADDED("$2The flag could not be added", "Flag"), FLAG_REMOVED("$4Successfully removed flag", "Flag"), FLAG_ADDED("$4Successfully added flag", "Flag"), - FLAG_TUTORIAL_USAGE("$1Have an admin set the flag: $2%s", "Commands"), + FLAG_TUTORIAL_USAGE("$1Have an admin set the flag: $2%s", "CommandConfig"), /* * Trusted */ @@ -633,16 +633,14 @@ public enum C { */ private final String def; /** - * Should the string be prefixed. - */ - private final boolean prefix; /** * What locale category should this translation fall under. */ private final String category; /** * Translated. */ - private String s; /** + private String s; + /** * Should the string be prefixed. */ private final boolean prefix; diff --git a/Core/src/main/java/com/intellectualcrafters/plot/config/Settings.java b/Core/src/main/java/com/intellectualcrafters/plot/config/Settings.java index e9ba60fd2..57dac19c9 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/config/Settings.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/config/Settings.java @@ -60,10 +60,6 @@ public class Settings { public static boolean REQUIRE_DONE = false; public static boolean DONE_COUNTS_TOWARDS_LIMIT = false; public static boolean DONE_RESTRICTS_BUILDING = false; - /** - * Clusters - */ - public static boolean ENABLE_CLUSTERS = false; /** * PlotMe settings */ diff --git a/Core/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java b/Core/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java index f348a18cb..8a9f2ba56 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -1044,13 +1044,7 @@ public class SQLManager implements AbstractDB { */ @Override public void createTables() throws SQLException { - String[] tables; - if (Settings.ENABLE_CLUSTERS) { - tables = new String[] { "plot", "plot_denied", "plot_helpers", "plot_comments", "plot_trusted", "plot_rating", "plot_settings", "cluster", "player_meta" }; - } else { - tables = new String[]{"plot", "plot_denied", "plot_helpers", "plot_comments", "plot_trusted", "plot_rating", "plot_settings", - "player_meta"}; - } + String[] tables = new String[] { "plot", "plot_denied", "plot_helpers", "plot_comments", "plot_trusted", "plot_rating", "plot_settings", "cluster", "player_meta" }; DatabaseMetaData meta = this.connection.getMetaData(); int create = 0; for (String s : tables) { diff --git a/Core/src/main/java/com/intellectualcrafters/plot/generator/AugmentedUtils.java b/Core/src/main/java/com/intellectualcrafters/plot/generator/AugmentedUtils.java index dd24f23e7..a29296e32 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/generator/AugmentedUtils.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/generator/AugmentedUtils.java @@ -115,7 +115,7 @@ public class AugmentedUtils { for (int z = bzz; z <= tzz; z++) { int rx = x + bx; int rz = z + bz; - boolean can = manager.getPlotIdAbs(area, rx, 0, rz) == null; + boolean can = manager.getPlotId(area, rx, 0, rz) == null; if (can) { for (int y = 1; y < 128; y++) { result.setBlock(x, y, z, air); diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java b/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java index 00f80372b..a52003ac0 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java @@ -20,6 +20,25 @@ //////////////////////////////////////////////////////////////////////////////////////////////////// package com.intellectualcrafters.plot.object; +import java.awt.Rectangle; +import java.awt.geom.Area; +import java.awt.geom.PathIterator; +import java.io.File; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map.Entry; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; + import com.google.common.collect.BiMap; import com.intellectualcrafters.jnbt.CompoundTag; import com.intellectualcrafters.plot.PS; @@ -41,25 +60,6 @@ import com.intellectualcrafters.plot.util.UUIDHandler; import com.intellectualcrafters.plot.util.WorldUtil; import com.plotsquared.listener.PlotListener; -import java.awt.Rectangle; -import java.awt.geom.Area; -import java.awt.geom.PathIterator; -import java.io.File; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map.Entry; -import java.util.Set; -import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicInteger; - /** * The plot class
* [IMPORTANT] @@ -1960,12 +1960,10 @@ public class Plot { * @return */ public boolean canClaim(PlotPlayer player) { - if (Settings.ENABLE_CLUSTERS) { - PlotCluster cluster = this.getCluster(); - if (cluster != null) { - if (!cluster.isAdded(player.getUUID()) && !Permissions.hasPermission(player, "plots.admin.command.claim")) { - return false; - } + PlotCluster cluster = this.getCluster(); + if (cluster != null) { + if (!cluster.isAdded(player.getUUID()) && !Permissions.hasPermission(player, "plots.admin.command.claim")) { + return false; } } return this.guessOwner() == null && !isMerged(); @@ -2650,14 +2648,14 @@ public class Plot { greaterPlot.setMerged(0, true); lesserPlot.mergeData(greaterPlot); if (removeRoads) { - if (lesserPlot.getMerged(5)) { - lesserPlot.removeRoadSouthEast(); - } lesserPlot.removeRoadSouth(); Plot other = this.getRelative(3); if (other.getMerged(2) && other.getMerged(1)) { - other.removeRoadEast(); greaterPlot.mergePlot(other, removeRoads); + other.removeRoadEast(); + } + if (lesserPlot.getMerged(5)) { + lesserPlot.removeRoadSouthEast(); } } } @@ -2674,15 +2672,15 @@ public class Plot { greaterPlot.setMerged(3, true); lesserPlot.mergeData(greaterPlot); if (removeRoads) { - lesserPlot.removeRoadEast(); + final Plot other = lesserPlot.getRelative(0); + if (other.getMerged(2) && other.getMerged(1)) { + greaterPlot.mergePlot(other, removeRoads); + other.removeRoadSouthEast(); + } if (lesserPlot.getMerged(5)) { lesserPlot.removeRoadSouthEast(); } - Plot other = lesserPlot.getRelative(0); - if (other.getMerged(2) && other.getMerged(1)) { - other.removeRoadSouthEast(); - greaterPlot.mergePlot(other, removeRoads); - } + lesserPlot.removeRoadEast(); } } } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java index 88a1be682..b1fab38e8 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java @@ -554,9 +554,6 @@ public abstract class PlotArea { } public PlotCluster getCluster(Location location) { - if (!Settings.ENABLE_CLUSTERS) { - return null; - } Plot plot = getPlot(location); if (plot == null) { return null; @@ -565,7 +562,7 @@ public abstract class PlotArea { } public PlotCluster getFirstIntersectingCluster(PlotId pos1, PlotId pos2) { - if (!Settings.ENABLE_CLUSTERS || this.clusters == null) { + if (this.clusters == null) { return null; } for (PlotCluster cluster : this.clusters.getAll()) { @@ -577,9 +574,6 @@ public abstract class PlotArea { } public PlotCluster getCluster(PlotId id) { - if (!Settings.ENABLE_CLUSTERS) { - return null; - } return this.clusters != null ? this.clusters.get(id.x, id.y) : null; } @@ -862,16 +856,13 @@ public abstract class PlotArea { } public void removeCluster(PlotCluster plotCluster) { - if (!Settings.ENABLE_CLUSTERS || this.clusters == null) { + if (this.clusters == null) { throw new IllegalAccessError("Clusters not enabled!"); } this.clusters.remove(plotCluster); } public void addCluster(PlotCluster plotCluster) { - if (!Settings.ENABLE_CLUSTERS) { - throw new IllegalAccessError("Clusters not enabled!"); - } if (this.clusters == null) { this.clusters = new QuadMap(Integer.MAX_VALUE, 0, 0, 64) { @Override diff --git a/Core/src/main/java/com/intellectualcrafters/plot/util/PlotChunk.java b/Core/src/main/java/com/intellectualcrafters/plot/util/PlotChunk.java index 5663ad2e0..a8497a5ce 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/util/PlotChunk.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/util/PlotChunk.java @@ -1,7 +1,13 @@ package com.intellectualcrafters.plot.util; +import com.intellectualcrafters.plot.PS; +import com.intellectualcrafters.plot.object.Location; +import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotArea; import com.intellectualcrafters.plot.object.PlotBlock; +import com.intellectualcrafters.plot.object.PlotManager; import com.intellectualcrafters.plot.object.PseudoRandom; +import com.intellectualcrafters.plot.object.RunnableVal3; import com.intellectualcrafters.plot.util.SetQueue.ChunkWrapper; /** @@ -129,6 +135,39 @@ public abstract class PlotChunk implements Cloneable { } } + /** + * Run a task for each x,z value corresponding to the plot at that location
+ * - Plot: The plot at the x,z (may be null)
+ * - Location: The location in the chunk (y = 0)
+ * - PlotChunk: Reference to this chunk object
+ * @param task + */ + public void mapByType2D(RunnableVal3 task) { + int bx = getX() << 4; + int bz = getZ() << 4; + String world = getChunkWrapper().world; + PlotArea area = PS.get().getPlotArea(world, null); + Location loc = new Location(world, bx, 0, bz); + if (area != null) { + PlotManager manager = area.getPlotManager(); + for (int x = 0; x < 16; x++) { + loc.setX(bx + x); + for (int z = 0; z < 16; z++) { + loc.setZ(bz + z); + task.run(area.getPlotAbs(loc), x, z); + } + } + } else { + for (int x = 0; x < 16; x++) { + loc.setX(bx + x); + for (int z = 0; z < 16; z++) { + loc.setZ(bz + z); + task.run(loc.getPlotAbs(), x, z); + } + } + } + } + /** * Fill a cuboid in this chunk with a block * @param x1 diff --git a/Core/src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpMenu.java b/Core/src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpMenu.java index 7bb68eb0c..c894f5c9d 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpMenu.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpMenu.java @@ -15,7 +15,7 @@ public class HelpMenu { private HelpPage _page = new HelpPage(CommandCategory.INFO, 0, 0); private int _maxPage; private CommandCategory _commandCategory; - private List> _commands; + private List _commands; public HelpMenu(final PlotPlayer player) { _player = player; @@ -27,7 +27,7 @@ public class HelpMenu { } public HelpMenu getCommands() { - _commands = MainCommand.getCommands(_commandCategory, _player); + _commands = MainCommand.getInstance().getCommands(_commandCategory, _player); return this; } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpPage.java b/Core/src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpPage.java index 677347461..d6875c8d4 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpPage.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpPage.java @@ -4,6 +4,7 @@ import com.intellectualcrafters.plot.commands.CommandCategory; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.util.StringMan; import java.util.ArrayList; import java.util.List; @@ -21,12 +22,8 @@ public class HelpPage { if (helpObjects.size() < 1) { MainUtil.sendMessage(player, C.NOT_VALID_NUMBER, "(0)"); } else { - MainUtil.sendMessage(player, C.HELP_HEADER, false); - MainUtil.sendMessage(player, _header, false); - for (final HelpObject object : helpObjects) { - MainUtil.sendMessage(player, object.toString(), false); - } - MainUtil.sendMessage(player, C.HELP_FOOTER, false); + String message = C.HELP_HEADER.s() + "\n" + _header + "\n" + StringMan.join(helpObjects, "\n") + "\n" + C.HELP_FOOTER.s(); + MainUtil.sendMessage(player, message, false); } } diff --git a/Core/src/main/java/com/plotsquared/general/commands/Command.java b/Core/src/main/java/com/plotsquared/general/commands/Command.java index cbea66327..17614675d 100644 --- a/Core/src/main/java/com/plotsquared/general/commands/Command.java +++ b/Core/src/main/java/com/plotsquared/general/commands/Command.java @@ -1,156 +1,449 @@ package com.plotsquared.general.commands; +import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.commands.CommandCategory; import com.intellectualcrafters.plot.commands.RequiredType; - +import com.intellectualcrafters.plot.config.C; +import com.intellectualcrafters.plot.object.PlotMessage; +import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.object.RunnableVal2; +import com.intellectualcrafters.plot.object.RunnableVal3; +import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.util.Permissions; +import com.intellectualcrafters.plot.util.StringComparison; +import com.intellectualcrafters.plot.util.StringMan; +import java.io.IOException; import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; import java.util.Set; -public abstract class Command extends CommandManager { +public abstract class Command { - protected Argument[] requiredArguments; - private RequiredType requiredType = RequiredType.NONE; - private String command; - private String usage = ""; - private String description = ""; - private String permission = ""; - private Set aliases = new HashSet<>(); + // May be none + private ArrayList allCommands = new ArrayList<>(); + private ArrayList dynamicCommands = new ArrayList<>(); + private HashMap staticCommands = new HashMap<>(); + + // Parent command (may be null) + private Command parent; + + // The command ID + private String id; + private List aliases; + private RequiredType required; + private String usage; + private String description; + private boolean isStatic; + private String perm; + private boolean confirmation; private CommandCategory category; - private int hash; + private Argument[] arguments; - public Command() { - super(null, new ArrayList()); + public Command getParent() { + return parent; } - public Command(String command) { - super(null, new ArrayList()); - this.command = command; + public String getId() { + return id; } - public Command(String command, String usage) { - super(null, new ArrayList()); - this.command = command; - this.usage = usage; + public List getCommands(PlotPlayer player) { + List commands = new ArrayList<>(); + for (Command cmd : allCommands) { + if (cmd.canExecute(player)) { + commands.add(cmd); + } + } + return commands; } - public Command(String command, String usage, String description) { - super(null, new ArrayList()); - this.command = command; - this.usage = usage; - this.description = description; + public List getCommands(CommandCategory cat, PlotPlayer player) { + List cmds = getCommands(player); + if (cat != null) { + Iterator iter = cmds.iterator(); + while (iter.hasNext()) { + if (iter.next().category != cat) { + iter.remove(); + } + } + } + return cmds; } - public Command(String command, String usage, String description, String permission) { - super(null, new ArrayList()); - this.command = command; - this.usage = usage; - this.description = description; - this.permission = permission; + public List getCommands() { + return allCommands; } - public Command(String command, String[] aliases, String usage) { - super(null, new ArrayList()); - this.command = command; - this.aliases = new HashSet<>(Arrays.asList(aliases)); - this.usage = usage; + public boolean hasConfirmation(PlotPlayer player) { + return confirmation && !player.hasPermission(getPermission() + ".confirm.bypass"); } - public Command(String command, String[] aliases) { - super(null, new ArrayList()); - this.command = command; - this.aliases = new HashSet<>(Arrays.asList(aliases)); + public List getAliases() { + return aliases; } - public Command(String command, String usage, String description, String permission, String[] aliases, RequiredType requiredType) { - super(null, new ArrayList()); - this.command = command; - this.usage = usage; - this.description = description; - this.permission = permission; - this.aliases = new HashSet<>(Arrays.asList(aliases)); - this.requiredType = requiredType; + public String getDescription() { + return description; } public RequiredType getRequiredType() { - return this.requiredType; + return required; } - public void create() { - Annotation annotation = getClass().getAnnotation(CommandDeclaration.class); - if (annotation == null) { - throw new RuntimeException("Command does not have a CommandDeclaration"); + public Argument[] getRequiredArguments() { + return arguments; + } + + public void setRequiredArguments(Argument[] arguments) { + this.arguments = arguments; + } + + public Command(Command parent, boolean isStatic, String id, String perm, RequiredType required, CommandCategory cat) { + this.parent = parent; + this.isStatic = isStatic; + this.id = id; + this.perm = perm; + this.required = required; + this.category = cat; + this.aliases = Arrays.asList(id); + } + + public Command(Command parent, boolean isStatic) { + this.parent = parent; + this.isStatic = isStatic; + final Annotation cdAnnotation = getClass().getAnnotation(CommandDeclaration.class); + if (cdAnnotation != null) { + final CommandDeclaration declaration = (CommandDeclaration) cdAnnotation; + init(declaration); } - CommandDeclaration declaration = (CommandDeclaration) annotation; - this.command = declaration.command(); - this.usage = declaration.usage(); - this.description = declaration.description(); - this.usage = declaration.usage(); - this.permission = declaration.permission(); - this.aliases = new HashSet<>(Arrays.asList(declaration.aliases())); - this.requiredType = declaration.requiredType(); + for (final Method method : getClass().getDeclaredMethods()) { + if (method.isAnnotationPresent(CommandDeclaration.class)) { + Class[] types = method.getParameterTypes(); + // final PlotPlayer player, String[] args, RunnableVal3 confirm, RunnableVal2 whenDone + if (types.length == 5 && types[0] == Command.class && types[1] == PlotPlayer.class && types[2] == String[].class && types[3] == RunnableVal3.class && types[4] == RunnableVal2.class) { + Command tmp = new Command(this, true) { + @Override + public void execute(PlotPlayer player, String[] args, RunnableVal3 confirm, RunnableVal2 whenDone) { + try { + method.invoke(Command.this, this, player, args, confirm, whenDone); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + }; + tmp.init(method.getAnnotation(CommandDeclaration.class)); + } + } + } + } + + public void init(CommandDeclaration declaration) { + this.id = declaration.command(); + this.perm = declaration.permission(); + this.required = declaration.requiredType(); this.category = declaration.category(); + HashMap options = new HashMap<>(); + List aliasOptions = new ArrayList<>(); + aliasOptions.add(id); + aliasOptions.addAll(Arrays.asList(declaration.aliases())); + options.put("aliases", aliasOptions); + options.put("description", declaration.description()); + options.put("usage", declaration.usage()); + options.put("confirmation", declaration.confirmation()); + boolean set = false; + for (Map.Entry entry : options.entrySet()) { + String key = id + "." + entry.getKey(); + if (!PS.get().commands.contains(key)) { + PS.get().commands.set(key, entry.getValue()); + set = true; + } + } + if (set) { + try { + PS.get().commands.save(PS.get().commandsFile); + } catch (IOException e) { + e.printStackTrace(); + + } + } + aliases = PS.get().commands.getStringList(id + ".aliases"); + description = PS.get().commands.getString(id + ".description"); + usage = PS.get().commands.getString(id + ".usage"); + confirmation = PS.get().commands.getBoolean(id + ".confirmation"); + if (parent != null) { + parent.register(this); + } + } + + public void register(Command command) { + if (command.isStatic) { + for (String alias : command.aliases) { + staticCommands.put(alias.toLowerCase(), command); + } + } else { + dynamicCommands.add(command); + } + allCommands.add(command); + } + + public enum CommandResult { + FAILURE, + SUCCESS + } + + public String getPermission() { + if (perm != null && perm.length() != 0) { + return perm; + } + if (parent == null) { + return "plots.use"; + } + if (parent.parent == null) { + return "plots." + id; + } + return parent.getPermission() + "." + id; + } + + public void paginate(PlotPlayer player, List c, int size, int page, RunnableVal3 add, String baseCommand, String header) { + // Calculate pages & index + if (page < 0) { + page = 0; + } + final int totalPages = (int) Math.ceil(c.size() / size); + if (page > totalPages) { + page = totalPages; + } + int max = page * size + size; + if (max > c.size()) { + max = c.size(); + } + // Send the header + header = header.replaceAll("%cur", page + 1 + "").replaceAll("%max", totalPages + 1 + "").replaceAll("%amount%", c.size() + "").replaceAll("%word%", "all"); + MainUtil.sendMessage(player, header); + // Send the page content + final List subList = c.subList(page * size, max); + int i = page * size; + for (final T obj : subList) { + i++; + PlotMessage msg = new PlotMessage(); + add.run(i, obj, msg); + msg.send(player); + } + // Send the footer + if (page < totalPages && page > 0) { // Back | Next + new PlotMessage().text("<-").color("$1").command(baseCommand + " " + page).text(" | ").color("$3").text("->").color("$1") + .command(baseCommand + " " + (page + 2)) + .text(C.CLICKABLE.s()).color("$2").send(player); + return; + } + if (page == 0 && totalPages != 0) { // Next + new PlotMessage().text("<-").color("$3").text(" | ").color("$3").text("->").color("$1").command(baseCommand + " " + (page + 2)).text(C.CLICKABLE.s()).color("$2").send(player); + return; + } + if (page == totalPages && totalPages != 0) { // Back + new PlotMessage().text("<-").color("$1").command(baseCommand + " " + page).text(" | ").color("$3").text("->").color("$3") + .text(C.CLICKABLE.s()).color("$2").send(player); + } + } + + /** + * + * @param player Caller + * @param args Arguments + * @param confirm Instance, Success, Failure + * @return + */ + public void execute(final PlotPlayer player, String[] args, RunnableVal3 confirm, RunnableVal2 whenDone) { + if (args.length == 0 || args[0] == null) { + C.COMMAND_SYNTAX.send(player, getUsage()); + return; + } + if (allCommands == null || allCommands.size() == 0) { + player.sendMessage("Not Implemented: https://github.com/IntellectualSites/PlotSquared/issues/new"); + return; + } + Command cmd = getCommand(args[0]); + if (cmd == null) { + if (parent != null) { + C.COMMAND_SYNTAX.send(player, getUsage()); + return; + } + // Command recommendation + MainUtil.sendMessage(player, C.NOT_VALID_SUBCOMMAND); + { + List cmds = getCommands(player); + if (cmds.isEmpty()) { + MainUtil.sendMessage(player, C.DID_YOU_MEAN, "/plot help"); + return; + } + HashSet setargs = new HashSet<>(args.length); + for (String arg : args) { + setargs.add(arg.toLowerCase()); + } + String[] allargs = setargs.toArray(new String[setargs.size()]); + int best = 0; + for (Command current : cmds) { + int match = getMatch(allargs, current); + if (match > best) { + cmd = current; + } + } + if (cmd == null) { + cmd = new StringComparison<>(args[0], allCommands).getMatchObject(); + } + MainUtil.sendMessage(player, C.DID_YOU_MEAN, cmd.getUsage()); + } + return; + } + if (!cmd.canExecute(player)) { + Argument[] reqArgs = cmd.getRequiredArguments(); + if ((reqArgs != null) && (reqArgs.length > 0)) { + boolean failed = args.length > reqArgs.length; + String[] baseSplit = getCommandString().split(" "); + String[] fullSplit = getUsage().split(" "); + String base = getCommandString(); + for (int i = 0; i < reqArgs.length; i++) { + fullSplit[i + baseSplit.length] = reqArgs[i].getExample().toString(); + failed = failed || reqArgs[i].parse(args[i]) == null; + } + if (failed) { + C.COMMAND_SYNTAX.send(player, StringMan.join(fullSplit, " ")); + return; + } + } + C.NO_PERMISSION.send(player, cmd.getPermission()); + return; + } + cmd.execute(player, Arrays.copyOfRange(args, 1, args.length), confirm, whenDone); + } + + public int getMatch(String[] args, Command cmd) { + int count = 0; + String perm = cmd.getPermission(); + HashSet desc = new HashSet<>(); + for (String alias : cmd.getAliases()) { + if (alias.startsWith(args[0])) { + count += 5; + } + } + Collections.addAll(desc, cmd.getDescription().split(" ")); + for (String arg : args) { + if (perm.startsWith(arg)) { + count++; + } + if (desc.contains(arg)) { + count++; + } + } + String[] usage = cmd.getUsage().split(" "); + for (int i = 0; i < Math.min(4, usage.length); i++) { + int require; + if (usage[i].startsWith("<")) { + require = 1; + } else { + require = 0; + } + String[] split = usage[i].split("\\|| |\\>|\\<|\\[|\\]|\\{|\\}|\\_|\\/"); + for (String aSplit : split) { + for (String arg : args) { + if (StringMan.isEqualIgnoreCase(arg, aSplit)) { + count += 5 - i + require; + } + } + } + } + count += StringMan.intersection(desc, args); + return count; + } + + public Command getCommand(String arg) { + Command cmd = staticCommands.get(arg.toLowerCase()); + if (cmd == null) { + for (Command command : dynamicCommands) { + if (command.matches(arg)) { + return command; + } + } + } + return cmd; + } + + public boolean canExecute(PlotPlayer player) { + return required.allows(player) && Permissions.hasPermission(player, getPermission()); + } + + public boolean matches(String arg) { + arg = arg.toLowerCase(); + return StringMan.isEqual(arg, id) || aliases.contains(arg); + } + + public String getCommandString() { + String base; + if (parent == null) { + return "/" + toString(); + } else { + return parent.getCommandString() + " " + toString(); + } + } + + public String getUsage() { + if (usage != null && usage.length() != 0) { + if (usage.startsWith("/")) { + return usage; + } + return getCommandString() + " " + usage; + } + if (allCommands.size() == 0) { + return getCommandString(); + } + StringBuilder args = new StringBuilder("["); + String prefix = ""; + for (Command cmd : allCommands) { + args.append(prefix).append(cmd.isStatic ? cmd.toString() : "<" + cmd + ">"); + prefix = "|"; + } + return getCommandString() + " " + args + "]"; + } + + public Collection tab(PlotPlayer player, String[] args, boolean space) { + switch (args.length) { + case 0: + return allCommands; + case 1: + String arg = args[0].toLowerCase(); + if (space) { + Command cmd = getCommand(arg); + return (cmd != null && cmd.canExecute(player)) ? (cmd.tab(player, Arrays.copyOfRange(args, 1, args.length), space)) : null; + } else { + Set commands = new HashSet(); + for (Map.Entry entry : staticCommands.entrySet()) { + if (entry.getKey().startsWith(arg) && entry.getValue().canExecute(player)) { + commands.add(entry.getValue()); + } + } + return commands; + } + default: + Command cmd = getCommand(args[0]); + return cmd != null ? cmd.tab(player, Arrays.copyOfRange(args, 1, args.length), space) : null; + } } @Override public String toString() { - return this.command; - } - - public abstract boolean onCommand(E plr, String[] arguments); - - public int handle(E plr, String[] args) { - if (args.length == 0) { - return super.handle(plr, ""); - } - StringBuilder builder = new StringBuilder(); - for (String s : args) { - builder.append(s).append(" "); - } - String s = builder.substring(0, builder.length() - 1); - return super.handle(plr, s); - } - - public String getCommand() { - return this.command; - } - - public String getUsage() { - if (this.usage.isEmpty()) { - return "/{label} " + this.command; - } - return this.usage; - } - - public String getPermission() { - if (this.permission == null || this.permission.isEmpty()) { - this.permission = "plots." + this.command.toLowerCase(); - } - return this.permission; - } - - public String getDescription() { - return this.description; - } - - public Set getAliases() { - return this.aliases; - } - - public Argument[] getRequiredArguments() { - if (this.requiredArguments == null) { - return new Argument[0]; - } - return this.requiredArguments; - } - - public CommandCategory getCategory() { - if (this.category == null) { - return CommandCategory.DEBUG; - } - return this.category; + return aliases.size() > 0 ? aliases.get(0) : id; } @Override @@ -161,18 +454,15 @@ public abstract class Command extends CommandManager { if (getClass() != obj.getClass()) { return false; } - Command other = (Command) obj; + final Command other = (Command) obj; if (this.hashCode() != other.hashCode()) { return false; } - return this.command.equals(other.command); + return this.id.equals(other.id); } @Override public int hashCode() { - if (this.hash == 0) { - this.hash = getCommand().hashCode(); - } - return this.hash; + return id.hashCode(); } } diff --git a/Core/src/main/java/com/plotsquared/general/commands/CommandDeclaration.java b/Core/src/main/java/com/plotsquared/general/commands/CommandDeclaration.java index 120b574b0..d8d427e67 100644 --- a/Core/src/main/java/com/plotsquared/general/commands/CommandDeclaration.java +++ b/Core/src/main/java/com/plotsquared/general/commands/CommandDeclaration.java @@ -8,7 +8,7 @@ import java.lang.annotation.Target; import com.intellectualcrafters.plot.commands.CommandCategory; import com.intellectualcrafters.plot.commands.RequiredType; -@Target(ElementType.TYPE) +@Target({ElementType.TYPE,ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface CommandDeclaration { @@ -24,5 +24,7 @@ public @interface CommandDeclaration { RequiredType requiredType() default RequiredType.NONE; - CommandCategory category(); + CommandCategory category() default CommandCategory.INFO; + + boolean confirmation() default false; } diff --git a/Core/src/main/java/com/plotsquared/general/commands/CommandManager.java b/Core/src/main/java/com/plotsquared/general/commands/CommandManager.java deleted file mode 100644 index e6bbc323d..000000000 --- a/Core/src/main/java/com/plotsquared/general/commands/CommandManager.java +++ /dev/null @@ -1,149 +0,0 @@ -package com.plotsquared.general.commands; - -import com.intellectualcrafters.plot.config.C; -import com.intellectualcrafters.plot.util.Permissions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashSet; -import java.util.List; -import java.util.concurrent.ConcurrentHashMap; - -public class CommandManager { - - public final ConcurrentHashMap> commands; - private final Character initialCharacter; - - public CommandManager(Character initialCharacter, List> commands) { - this.commands = new ConcurrentHashMap<>(); - for (Command command : commands) { - addCommand(command); - } - this.initialCharacter = initialCharacter; - } - - public final void addCommand(Command command) { - if (command.getCommand() == null) { - command.create(); - } - this.commands.put(command.getCommand().toLowerCase(), command); - for (String alias : command.getAliases()) { - this.commands.put(alias.toLowerCase(), command); - } - } - - public final Command getCommand(String command) { - return this.commands.get(command.toLowerCase()); - } - - public final boolean createCommand(Command command) { - try { - command.create(); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - if (command.getCommand() != null) { - addCommand(command); - return true; - } - return false; - } - - public final ArrayList> getCommands() { - HashSet> set = new HashSet<>(this.commands.values()); - ArrayList> result = new ArrayList<>(set); - Collections.sort(result, new Comparator>() { - @Override - public int compare(Command a, Command b) { - if (a == b) { - return 0; - } - if (a == null) { - return -1; - } - if (b == null) { - return 1; - } - return a.getCommand().compareTo(b.getCommand()); - } - }); - return result; - } - - public final ArrayList getCommandLabels(ArrayList> cmds) { - ArrayList labels = new ArrayList<>(cmds.size()); - for (Command cmd : cmds) { - labels.add(cmd.getCommand()); - } - return labels; - } - - public int handle(T plr, String input) { - if (this.initialCharacter != null && !input.startsWith(this.initialCharacter + "")) { - return CommandHandlingOutput.NOT_COMMAND; - } - if (this.initialCharacter == null) { - input = input; - } else { - input = input.substring(1); - } - String[] parts = input.split(" "); - String[] args; - String command = parts[0].toLowerCase(); - if (parts.length == 1) { - args = new String[0]; - } else { - args = new String[parts.length - 1]; - System.arraycopy(parts, 1, args, 0, args.length); - } - Command cmd = this.commands.get(command); - if (cmd == null) { - return CommandHandlingOutput.NOT_FOUND; - } - if (!cmd.getRequiredType().allows(plr)) { - return CommandHandlingOutput.CALLER_OF_WRONG_TYPE; - } - if (!Permissions.hasPermission(plr, cmd.getPermission())) { - return CommandHandlingOutput.NOT_PERMITTED; - } - Argument[] requiredArguments = cmd.getRequiredArguments(); - if ((requiredArguments != null) && (requiredArguments.length > 0)) { - boolean success = true; - if (args.length < requiredArguments.length) { - success = false; - } else { - for (int i = 0; i < requiredArguments.length; i++) { - if (requiredArguments[i].parse(args[i]) == null) { - success = false; - break; - } - } - } - if (!success) { - cmd.getUsage().replaceAll("\\{label\\}", parts[0]); - C.COMMAND_SYNTAX.send(plr, cmd.getUsage()); - return CommandHandlingOutput.WRONG_USAGE; - } - } - try { - boolean a = cmd.onCommand(plr, args); - if (!a) { - String usage = cmd.getUsage(); - if ((usage != null) && !usage.isEmpty()) { - plr.sendMessage(usage); - } - return CommandHandlingOutput.WRONG_USAGE; - } - } catch (Throwable t) { - t.printStackTrace(); - return CommandHandlingOutput.ERROR; - } - return CommandHandlingOutput.SUCCESS; - } - - public final char getInitialCharacter() { - return this.initialCharacter; - } -}