Lazy command initialization

This commit is contained in:
boy0001 2015-07-28 01:41:06 +10:00
parent 870495ac1a
commit faf6cbea0f
5 changed files with 18 additions and 11 deletions

View File

@ -613,9 +613,9 @@ import com.intellectualcrafters.plot.uuid.UUIDWrapper;
*/ */
public void registerCommand(final SubCommand c) { public void registerCommand(final SubCommand c) {
if (c.getCommand() != null) { if (c.getCommand() != null) {
MainCommand.instance.addCommand(c); MainCommand.getInstance().addCommand(c);
} else { } else {
MainCommand.instance.createCommand(c); MainCommand.getInstance().createCommand(c);
} }
} }

View File

@ -49,7 +49,7 @@ import com.plotsquared.bukkit.generator.HybridGen;
import com.plotsquared.bukkit.util.SetupUtils; import com.plotsquared.bukkit.util.SetupUtils;
@CommandDeclaration( @CommandDeclaration(
command = "cluser", command = "cluster",
aliases = {"clusters"}, aliases = {"clusters"},
category = CommandCategory.ACTIONS, category = CommandCategory.ACTIONS,
requiredType = RequiredType.PLAYER, requiredType = RequiredType.PLAYER,

View File

@ -46,11 +46,18 @@ import com.intellectualsites.commands.CommandManager;
*/ */
public class MainCommand extends CommandManager<PlotPlayer> { public class MainCommand extends CommandManager<PlotPlayer> {
public static MainCommand instance = new MainCommand(); private static MainCommand instance;
public static MainCommand getInstance() {
if (instance == null) {
instance = new MainCommand();
}
return instance;
}
private MainCommand() { private MainCommand() {
super(null, new ArrayList<Command<PlotPlayer>>()); super(null, new ArrayList<Command<PlotPlayer>>());
List<SubCommand> toAdd = Arrays.asList( List<SubCommand> toAdd = new ArrayList<>(Arrays.asList(
new Buy(), new Save(), new Load(), new Buy(), new Save(), new Load(),
new Template(), new Download(), new Template(), new Download(),
new Update(), new Template(), new Update(), new Template(),
@ -74,7 +81,7 @@ public class MainCommand extends CommandManager<PlotPlayer> {
new Trust(), new DebugExec(), new FlagCmd(), new Trust(), new DebugExec(), new FlagCmd(),
new Target(), new DebugFixFlags(), new Move(), new Target(), new DebugFixFlags(), new Move(),
new Condense(), new Condense(), new Copy(), new Condense(), new Condense(), new Copy(),
new Chat()); new Chat()));
if (Settings.ENABLE_CLUSTERS) { if (Settings.ENABLE_CLUSTERS) {
toAdd.add(new Cluster()); toAdd.add(new Cluster());
} }
@ -91,7 +98,7 @@ public class MainCommand extends CommandManager<PlotPlayer> {
} }
public static ArrayList<Command<PlotPlayer>> getCommands(final CommandCategory category, final PlotPlayer player) { public static ArrayList<Command<PlotPlayer>> getCommands(final CommandCategory category, final PlotPlayer player) {
ArrayList<Command<PlotPlayer>> cmds = instance.getCommands(); ArrayList<Command<PlotPlayer>> cmds = getInstance().getCommands();
for (Iterator<Command<PlotPlayer>> iter = cmds.iterator(); iter.hasNext();){ for (Iterator<Command<PlotPlayer>> iter = cmds.iterator(); iter.hasNext();){
Command<PlotPlayer> cmd = iter.next(); Command<PlotPlayer> cmd = iter.next();
if ((category != null && (cmd.getCategory().equals(category))) || !player.hasPermission(cmd.getPermission())) { if ((category != null && (cmd.getCategory().equals(category))) || !player.hasPermission(cmd.getPermission())) {
@ -235,7 +242,7 @@ public class MainCommand extends CommandManager<PlotPlayer> {
builder.append(" "); builder.append(" ");
} }
} }
instance.handle(player, builder.toString()); getInstance().handle(player, builder.toString());
return true; return true;
} }

View File

@ -349,7 +349,7 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain {
} else { } else {
getServer().getPluginManager().registerEvents(new WEListener(), this); getServer().getPluginManager().registerEvents(new WEListener(), this);
WorldEdit.getInstance().getEventBus().register(new WESubscriber()); WorldEdit.getInstance().getEventBus().register(new WESubscriber());
MainCommand.instance.createCommand(new WE_Anywhere()); MainCommand.getInstance().createCommand(new WE_Anywhere());
} }
} }
} }

View File

@ -53,11 +53,11 @@ public class BukkitCommand implements CommandExecutor, TabCompleter {
return null; return null;
} }
final Set<String> tabOptions = new HashSet<>(); final Set<String> tabOptions = new HashSet<>();
ArrayList<Command<PlotPlayer>> commands = MainCommand.instance.getCommands(); ArrayList<Command<PlotPlayer>> commands = MainCommand.getInstance().getCommands();
String best = new StringComparison(strings[0], commands).getBestMatch(); String best = new StringComparison(strings[0], commands).getBestMatch();
tabOptions.add(best); tabOptions.add(best);
final String arg = strings[0].toLowerCase(); final String arg = strings[0].toLowerCase();
for (final Command<PlotPlayer> cmd : MainCommand.instance.getCommands()) { for (final Command<PlotPlayer> cmd : MainCommand.getInstance().getCommands()) {
String label = cmd.getCommand(); String label = cmd.getCommand();
if (!label.equalsIgnoreCase(best)) { if (!label.equalsIgnoreCase(best)) {
if (label.startsWith(arg)) { if (label.startsWith(arg)) {