diff --git a/src/com/massivecraft/massivecore/command/MassiveCommand.java b/src/com/massivecraft/massivecore/command/MassiveCommand.java index 3f06b015..6c2b3ac3 100644 --- a/src/com/massivecraft/massivecore/command/MassiveCommand.java +++ b/src/com/massivecraft/massivecore/command/MassiveCommand.java @@ -33,7 +33,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.Iterator; import java.util.List; import java.util.Set; @@ -187,7 +186,10 @@ public class MassiveCommand implements Active, PluginIdentifiableCommand // The visibility of this command in help command. protected Visibility visibility = Visibility.VISIBLE; - + + // The priority of this command when aliases are ambiguous. + protected long priority = 0; + // === SETUP === // Determines whether the smart setup process will be used, works for most of commands @@ -396,12 +398,14 @@ public class MassiveCommand implements Active, PluginIdentifiableCommand // // token - the full alias or an alias prefix. // levenshtein - should we use levenshtein instead of starts with? - // prioritizeExact - return single element set on full match. + // onlyRelevantToSender - if non null only returns commands relevant to specific sender + // prioritize - only return commands with the highest priority + // // An empty set means no child was found. // A single element set means we found an unambiguous match. // A larger set means the token was ambiguous. - private Set getChildren(String token, boolean levenshtein, CommandSender onlyRelevantToSender) + private Set getChildren(String token, boolean levenshtein, CommandSender onlyRelevantToSender, boolean prioritize) { // Create Ret Set ret = new MassiveSet<>(); @@ -435,24 +439,50 @@ public class MassiveCommand implements Active, PluginIdentifiableCommand } // Only Relevant - if (onlyRelevantToSender != null) - { - for (Iterator iterator = ret.iterator(); iterator.hasNext(); ) - { - MassiveCommand command = iterator.next(); - if (command.isRelevant(onlyRelevantToSender)) continue; - iterator.remove(); - } - } + if (onlyRelevantToSender != null) ret = getRelevantCommands(ret, onlyRelevantToSender); + + // Priority + if (prioritize) ret = getPrioritizedCommands(ret); // Return Ret return ret; } + private static Set getRelevantCommands(Iterable commands, CommandSender sender) + { + Set ret = new MassiveSet<>(); + for (MassiveCommand command : commands) + { + if (!command.isRelevant(sender)) continue; + ret.add(command); + } + return ret; + } + + private static Set getPrioritizedCommands(Iterable commands) + { + Set ret = new MassiveSet<>(); + long highestPriority = Long.MIN_VALUE; + + for (MassiveCommand command : commands) + { + long priority = command.getPriority(); + if (priority < highestPriority) continue; + + if (priority > highestPriority) + { + ret.clear(); + highestPriority = priority; + } + ret.add(command); + } + return ret; + } + // A simplified version returning null on ambiguity and nothing found. public MassiveCommand getChild(String token) { - Set children = this.getChildren(token, false, null); + Set children = this.getChildren(token, false, null, true); if (children.isEmpty()) return null; if (children.size() > 1) return null; @@ -878,7 +908,10 @@ public class MassiveCommand implements Active, PluginIdentifiableCommand if (this.getVisibility() == Visibility.INVISIBLE) return false; return this.isRequirementsMet(sender, false); } - + + public long getPriority() { return priority; } + public void setPriority(long priority) { this.priority = priority; } + // -------------------------------------------- // // SETUP // -------------------------------------------- // @@ -1088,7 +1121,7 @@ public class MassiveCommand implements Active, PluginIdentifiableCommand // Get matches String token = args.get(0); - Set matches = this.getChildren(token, false, null); + Set matches = this.getChildren(token, false, null, true); // Score! if (matches.size() == 1) @@ -1106,12 +1139,12 @@ public class MassiveCommand implements Active, PluginIdentifiableCommand if (matches.isEmpty()) { base = Lang.COMMAND_CHILD_NONE; - suggestions = this.getChildren(token, true, sender); + suggestions = this.getChildren(token, true, sender, false); } else { base = Lang.COMMAND_CHILD_AMBIGUOUS; - suggestions = this.getChildren(token, false, sender); + suggestions = this.getChildren(token, false, sender, false); } // Message: "The sub command X couldn't be found." diff --git a/src/com/massivecraft/massivecore/command/MassiveCommandDeprecated.java b/src/com/massivecraft/massivecore/command/MassiveCommandDeprecated.java index 16d0ed0a..dacd7fb7 100644 --- a/src/com/massivecraft/massivecore/command/MassiveCommandDeprecated.java +++ b/src/com/massivecraft/massivecore/command/MassiveCommandDeprecated.java @@ -25,6 +25,9 @@ public class MassiveCommandDeprecated extends MassiveCommand // Visibility this.setVisibility(Visibility.INVISIBLE); + + // Priority + this.setPriority(-1); } // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/command/MassiveCommandVersion.java b/src/com/massivecraft/massivecore/command/MassiveCommandVersion.java index c16a2ad0..4edef89c 100644 --- a/src/com/massivecraft/massivecore/command/MassiveCommandVersion.java +++ b/src/com/massivecraft/massivecore/command/MassiveCommandVersion.java @@ -32,6 +32,9 @@ public class MassiveCommandVersion extends MassiveCommand this.setAliases("version"); this.setDesc("display plugin version"); + + // Priority + this.setPriority(-1); } // -------------------------------------------- //