From ed7a72047015d532824f71a20f0fbf32ada273f0 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Mon, 27 Jul 2015 16:00:20 +1000 Subject: [PATCH] Fix errors --- .../plot/commands/Cluster.java | 16 ++- .../plot/commands/DebugSetup.java | 124 ------------------ .../plot/commands/MainCommand.java | 24 ++-- .../plot/commands/Setup.java | 24 ++-- .../commands/CommandManager.java | 22 +++- .../bukkit/commands/BukkitCommand.java | 28 ++-- 6 files changed, 71 insertions(+), 167 deletions(-) delete mode 100644 src/main/java/com/intellectualcrafters/plot/commands/DebugSetup.java diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Cluster.java b/src/main/java/com/intellectualcrafters/plot/commands/Cluster.java index f8893f727..79cb25fc8 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Cluster.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Cluster.java @@ -24,6 +24,8 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.UUID; +import org.bukkit.generator.ChunkGenerator; + import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.database.DBFunc; @@ -31,6 +33,7 @@ import com.intellectualcrafters.plot.generator.PlotGenerator; import com.intellectualsites.commands.CommandDeclaration; import com.intellectualsites.commands.CommandCaller; import com.plotsquared.bukkit.generator.AugmentedPopulator; +import com.plotsquared.bukkit.generator.BukkitPlotGenerator; import com.plotsquared.bukkit.generator.HybridGen; import com.intellectualcrafters.plot.object.BlockLoc; import com.intellectualcrafters.plot.object.Location; @@ -43,6 +46,8 @@ import com.intellectualcrafters.plot.object.PlotWorld; import com.intellectualcrafters.plot.util.ClusterManager; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.Permissions; +import com.intellectualcrafters.plot.util.StringMan; +import com.plotsquared.bukkit.util.SetupUtils; import com.plotsquared.bukkit.util.UUIDHandler; @CommandDeclaration( @@ -155,11 +160,18 @@ public class Cluster extends SubCommand { } else { final String gen_string = PS.get().config.getString("worlds." + world + "." + "generator.plugin"); - PlotGenerator generator; + BukkitPlotGenerator generator; if (gen_string == null) { generator = new HybridGen(world); } else { - generator = (PlotGenerator) PS.get().IMP.getGenerator(world, gen_string); + ChunkGenerator chunkgen = (ChunkGenerator) PS.get().IMP.getGenerator(world, gen_string).generator; + if (chunkgen instanceof BukkitPlotGenerator) { + generator = (BukkitPlotGenerator) chunkgen; + } + else { + MainUtil.sendMessage(plr, C.SETUP_INVALID_GENERATOR, StringMan.join(SetupUtils.generators.keySet(), ",")); + return false; + } } new AugmentedPopulator(world, generator, cluster, plotworld.TERRAIN == 2, plotworld.TERRAIN != 2); } diff --git a/src/main/java/com/intellectualcrafters/plot/commands/DebugSetup.java b/src/main/java/com/intellectualcrafters/plot/commands/DebugSetup.java deleted file mode 100644 index 7d0d9e1c1..000000000 --- a/src/main/java/com/intellectualcrafters/plot/commands/DebugSetup.java +++ /dev/null @@ -1,124 +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 java.util.Map.Entry; - -import com.intellectualcrafters.plot.commands.callers.PlotPlayerCaller; -import com.intellectualcrafters.plot.generator.PlotGenerator; -import com.intellectualsites.commands.CommandDeclaration; -import com.intellectualsites.commands.CommandCaller; -import org.bukkit.generator.ChunkGenerator; - -import com.intellectualcrafters.plot.config.C; -import com.intellectualcrafters.plot.config.ConfigurationNode; -import com.plotsquared.bukkit.generator.HybridGen; - -import com.intellectualcrafters.plot.object.PlotPlayer; -import com.intellectualcrafters.plot.object.SetupObject; -import com.intellectualcrafters.plot.util.MainUtil; -import com.plotsquared.bukkit.util.SetupUtils; - -@CommandDeclaration( - command = "setup", - permission = "plots.admin.command.setup", - description = "Plotworld setup command", - usage = "/plot setup", - aliases = {"create"}, - category = CommandCategory.ACTIONS -) -public class DebugSetup extends SubCommand { - - public void displayGenerators(PlotPlayer plr) { - StringBuffer message = new StringBuffer(); - message.append("&6What generator do you want?"); - for (Entry entry : SetupUtils.generators.entrySet()) { - if (entry.getKey().equals("PlotSquared")) { - message.append("\n&8 - &2" + entry.getKey() + " (Default Generator)"); - } - else if (entry.getValue() instanceof HybridGen) { - message.append("\n&8 - &7" + entry.getKey() + " (Hybrid Generator)"); - } - else if (entry.getValue() instanceof PlotGenerator) { - message.append("\n&8 - &7" + entry.getKey() + " (Plot Generator)"); - } - else { - message.append("\n&8 - &7" + entry.getKey() + " (Unknown structure)"); - } - } - MainUtil.sendMessage(plr, message.toString()); - } - - @Override - public boolean onCommand(final CommandCaller caller, final String ... args) { - final PlotPlayer plr = caller instanceof PlotPlayerCaller ? (PlotPlayer) caller.getSuperCaller() : null; - // going through setup - String name; - if (plr == null) { - name = "*"; - } - else { - name = plr.getName(); - } - if (!SetupUtils.setupMap.containsKey(name)) { - final SetupObject object = new SetupObject(); - SetupUtils.setupMap.put(name, object); - SetupUtils.manager.updateGenerators(); - sendMessage(plr, C.SETUP_INIT); - displayGenerators(plr); - return false; - } - if (args.length == 1) { - if (args[0].equalsIgnoreCase("cancel")) { - SetupUtils.setupMap.remove(name); - MainUtil.sendMessage(plr, "&aCancelled setup"); - return false; - } - if (args[0].equalsIgnoreCase("back")) { - final SetupObject object = SetupUtils.setupMap.get(name); - if (object.setup_index > 0) { - object.setup_index--; - final ConfigurationNode node = object.step[object.setup_index]; - sendMessage(plr, C.SETUP_STEP, object.setup_index + 1 + "", node.getDescription(), node.getType().getType(), node.getDefaultValue() + ""); - return false; - } else if (object.current > 0) { - object.current--; - } - } - } - final SetupObject object = SetupUtils.setupMap.get(name); - final int index = object.current; - switch (index) { - case 0: { // choose plot manager // skip if 1 option - } - case 1: { // choose type (default, augmented, cluster) - } - case 2: { // Choose generator (vanilla, non plot generator) // skip if one option - } - case 3: { // world setup // skip if one option - } - case 4: { // world name - } - } - return false; - } - -} diff --git a/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java b/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java index 47430d03f..633f54712 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java @@ -33,6 +33,7 @@ import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.StringComparison; +import com.intellectualcrafters.plot.util.StringMan; import com.intellectualsites.commands.Argument; import com.intellectualsites.commands.Command; import com.intellectualsites.commands.CommandHandlingOutput; @@ -93,9 +94,9 @@ public class MainCommand extends CommandManager { public static List getCommands(final CommandCategory category, final PlotPlayer player) { final List cmds = new ArrayList<>(); - for (final Command c : instance.commands) { - if (!c.getRequiredType().equals(PlotPlayer.class)) { - if ((c.getCategory().equals(category)) && player.hasPermission(c.getPermission())) { + for (final Command c : instance.commands.values()) { + if (c.getRequiredType().isInstance(PlotPlayer.class)) { + if ((category == null || (c.getCategory().equals(category))) && player.hasPermission(c.getPermission())) { cmds.add(c); } } @@ -105,11 +106,7 @@ public class MainCommand extends CommandManager { public static List helpMenu(final PlotPlayer player, final CommandCategory category, int page) { List commands; - if (category != null) { - commands = getCommands(category, player); - } else { - commands = instance.commands; - } + commands = getCommands(category, player); // final int totalPages = ((int) Math.ceil(12 * (commands.size()) / // 100)); final int perPage = 5; @@ -130,8 +127,8 @@ public class MainCommand extends CommandManager { for (int x = start; x < max; x++) { cmd = commands.get(x); String s = C.HELP_ITEM.s(); - if (cmd.getAliases().length > 0) { - s = s.replace("%alias%", cmd.getAliases()[0]); + if (cmd.getAliases().size() > 0) { + s = s.replace("%alias%", StringMan.join(cmd.getAliases(), "|")); } else { s = s.replace("%alias%", ""); @@ -255,12 +252,7 @@ public class MainCommand extends CommandManager { System.arraycopy(parts, 1, args, 0, args.length); } Command cmd = null; - for (Command c1 : this.commands) { - if (c1.getCommand().equalsIgnoreCase(command) || c1.getAliases().contains(command)) { - cmd = c1; - break; - } - } + cmd = this.commands.get(command); if (cmd == null) { caller.message(C.NOT_VALID_SUBCOMMAND); { diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Setup.java b/src/main/java/com/intellectualcrafters/plot/commands/Setup.java index 5e59e2ace..e6422bf76 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Setup.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Setup.java @@ -26,17 +26,17 @@ import java.util.List; import java.util.Map.Entry; import com.intellectualcrafters.plot.commands.callers.PlotPlayerCaller; -import com.intellectualcrafters.plot.generator.PlotGenerator; import com.intellectualsites.commands.CommandDeclaration; import com.intellectualsites.commands.CommandCaller; + import org.apache.commons.lang.StringUtils; import org.bukkit.generator.ChunkGenerator; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.ConfigurationNode; import com.intellectualcrafters.plot.config.Settings; +import com.plotsquared.bukkit.generator.BukkitPlotGenerator; import com.plotsquared.bukkit.generator.HybridGen; - import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.SetupObject; import com.intellectualcrafters.plot.util.BlockManager; @@ -63,7 +63,7 @@ public class Setup extends SubCommand { else if (entry.getValue() instanceof HybridGen) { message.append("\n&8 - &7" + entry.getKey() + " (Hybrid Generator)"); } - else if (entry.getValue() instanceof PlotGenerator) { + else if (entry.getValue() instanceof BukkitPlotGenerator) { message.append("\n&8 - &7" + entry.getKey() + " (Plot Generator)"); } else { @@ -131,7 +131,7 @@ public class Setup extends SubCommand { List allTypes = Arrays.asList(new String[] { "default", "augmented", "partial"}); List allDesc = Arrays.asList(new String[] { "Standard plot generation", "Plot generation with vanilla terrain", "Vanilla with clusters of plots"}); ArrayList types = new ArrayList<>(); - if (SetupUtils.generators.get(object.setupGenerator) instanceof PlotGenerator) { + if (SetupUtils.generators.get(object.setupGenerator) instanceof BukkitPlotGenerator) { types.add("default"); } types.add("augmented"); @@ -157,8 +157,8 @@ public class Setup extends SubCommand { object.current++; if (object.step == null) { object.plotManager = object.setupGenerator; - object.step = ((PlotGenerator) SetupUtils.generators.get(object.plotManager)).getNewPlotWorld(null).getSettingNodes(); - ((PlotGenerator) SetupUtils.generators.get(object.plotManager)).processSetup(object); + object.step = ((BukkitPlotGenerator) SetupUtils.generators.get(object.plotManager)).getNewPlotWorld(null).getSettingNodes(); + ((BukkitPlotGenerator) SetupUtils.generators.get(object.plotManager)).processSetup(object); } if (object.step.length == 0) { object.current = 4; @@ -169,17 +169,17 @@ public class Setup extends SubCommand { final ConfigurationNode step = object.step[object.setup_index]; sendMessage(plr, C.SETUP_STEP, object.setup_index + 1 + "", step.getDescription(), step.getType().getType(), step.getDefaultValue() + ""); } else { - if (gen instanceof PlotGenerator) { + if (gen instanceof BukkitPlotGenerator) { object.plotManager = object.setupGenerator; object.setupGenerator = null; - object.step = ((PlotGenerator) SetupUtils.generators.get(object.plotManager)).getNewPlotWorld(null).getSettingNodes(); - ((PlotGenerator) SetupUtils.generators.get(object.plotManager)).processSetup(object); + object.step = ((BukkitPlotGenerator) SetupUtils.generators.get(object.plotManager)).getNewPlotWorld(null).getSettingNodes(); + ((BukkitPlotGenerator) SetupUtils.generators.get(object.plotManager)).processSetup(object); } else { object.plotManager = "PlotSquared"; - MainUtil.sendMessage(plr, "&c[WARNING] The specified generator does not identify as PlotGenerator"); + MainUtil.sendMessage(plr, "&c[WARNING] The specified generator does not identify as BukkitPlotGenerator"); MainUtil.sendMessage(plr, "&7 - You may need to manually configure the other plugin"); - object.step = ((PlotGenerator) SetupUtils.generators.get(object.plotManager)).getNewPlotWorld(null).getSettingNodes(); + object.step = ((BukkitPlotGenerator) SetupUtils.generators.get(object.plotManager)).getNewPlotWorld(null).getSettingNodes(); } MainUtil.sendMessage(plr, "&6What terrain would you like in plots?" + "\n&8 - &2NONE&8 - &7No terrain at all" + "\n&8 - &7ORE&8 - &7Just some ore veins and trees" + "\n&8 - &7ROAD&8 - &7Terrain seperated by roads" + "\n&8 - &7ALL&8 - &7Entirely vanilla generation"); } @@ -195,7 +195,7 @@ public class Setup extends SubCommand { object.terrain = terrain.indexOf(args[0].toLowerCase()); object.current++; if (object.step == null) { - object.step = ((PlotGenerator) SetupUtils.generators.get(object.plotManager)).getNewPlotWorld(null).getSettingNodes(); + object.step = ((BukkitPlotGenerator) SetupUtils.generators.get(object.plotManager)).getNewPlotWorld(null).getSettingNodes(); } final ConfigurationNode step = object.step[object.setup_index]; sendMessage(plr, C.SETUP_STEP, object.setup_index + 1 + "", step.getDescription(), step.getType().getType(), step.getDefaultValue() + ""); diff --git a/src/main/java/com/intellectualsites/commands/CommandManager.java b/src/main/java/com/intellectualsites/commands/CommandManager.java index ebda36336..5f99ca23d 100644 --- a/src/main/java/com/intellectualsites/commands/CommandManager.java +++ b/src/main/java/com/intellectualsites/commands/CommandManager.java @@ -2,6 +2,8 @@ package com.intellectualsites.commands; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.concurrent.ConcurrentHashMap; @@ -46,8 +48,24 @@ public class CommandManager { return false; } - final public Collection getCommands() { - return this.commands.values(); + final public ArrayList getCommands() { + ArrayList result = new ArrayList<>(this.commands.values()); + 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 int handle(CommandCaller caller, String input) { diff --git a/src/main/java/com/plotsquared/bukkit/commands/BukkitCommand.java b/src/main/java/com/plotsquared/bukkit/commands/BukkitCommand.java index 332746097..79605440b 100644 --- a/src/main/java/com/plotsquared/bukkit/commands/BukkitCommand.java +++ b/src/main/java/com/plotsquared/bukkit/commands/BukkitCommand.java @@ -3,10 +3,13 @@ package com.plotsquared.bukkit.commands; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; import com.intellectualcrafters.plot.commands.MainCommand; import com.intellectualsites.commands.Command; + import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; @@ -48,27 +51,30 @@ public class BukkitCommand implements CommandExecutor, TabCompleter { if (!command.getLabel().equalsIgnoreCase("plots")) { return null; } - final List tabOptions = new ArrayList<>(); - final String[] commands = new String[MainCommand.instance.getCommands().size()]; - for (int x = 0; x < MainCommand.instance.getCommands().size(); x++) { - commands[x] = MainCommand.instance.getCommands().get(x).getCommand(); - } + final Set tabOptions = new HashSet<>(); + ArrayList commands = MainCommand.instance.getCommands(); String best = new StringComparison(strings[0], commands).getBestMatch(); tabOptions.add(best); final String arg = strings[0].toLowerCase(); for (final Command cmd : MainCommand.instance.getCommands()) { - if (!cmd.getCommand().equalsIgnoreCase(best)) { - if (player.hasPermission(cmd.getPermission())) { - if (cmd.getCommand().startsWith(arg)) { + String label = cmd.getCommand(); + if (!label.equalsIgnoreCase(best)) { + if (label.startsWith(arg)) { + if (player.hasPermission(cmd.getPermission())) { tabOptions.add(cmd.getCommand()); - } else if (cmd.getAliases().length > 0 && cmd.getAliases()[0].startsWith(arg)) { - tabOptions.add(cmd.getAliases()[0]); + } else if (cmd.getAliases().size() > 0) { + for (String alias : cmd.getAliases()) { + if (alias.startsWith(arg)) { + tabOptions.add(label); + break; + } + } } } } } if (tabOptions.size() > 0) { - return tabOptions; + return new ArrayList<>(tabOptions); } return null; }