From a679c0c9064b7c7ca8e424edfe227c920c739919 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Wed, 30 Mar 2011 17:36:39 -0400 Subject: [PATCH] Fixed command aliases priorities. Command aliases are now given lower weight than the "official" name of the command; this allows you to define aliases without worrying about them overriding someone else's command. By: Celtic Minstrel --- .../org/bukkit/command/SimpleCommandMap.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/command/SimpleCommandMap.java b/paper-api/src/main/java/org/bukkit/command/SimpleCommandMap.java index a194ece9bb..77391c7593 100644 --- a/paper-api/src/main/java/org/bukkit/command/SimpleCommandMap.java +++ b/paper-api/src/main/java/org/bukkit/command/SimpleCommandMap.java @@ -3,8 +3,11 @@ package org.bukkit.command; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; + import org.bukkit.ChatColor; import org.bukkit.Server; @@ -14,6 +17,7 @@ import static org.bukkit.util.Java15Compat.Arrays_copyOfRange; public final class SimpleCommandMap implements CommandMap { private final Map knownCommands = new HashMap(); + private final Set aliases = new HashSet(); private final Server server; public SimpleCommandMap(final Server server) { @@ -41,12 +45,11 @@ public final class SimpleCommandMap implements CommandMap { } private void register(String fallbackPrefix, Command command) { - List names = new ArrayList(); + register(command.getName(), fallbackPrefix, command); + aliases.addAll(command.getAliases()); + aliases.remove(command.getName()); - names.add(command.getName()); - names.addAll(command.getAliases()); - - for (String name : names) { + for (String name : command.getAliases()) { register(name, fallbackPrefix, command); } } @@ -55,7 +58,7 @@ public final class SimpleCommandMap implements CommandMap { * {@inheritDoc} */ public boolean register(String name, String fallbackPrefix, Command command) { - boolean nameInUse = (getCommand(name) != null); + boolean nameInUse = nameInUse(name); if (nameInUse) { name = fallbackPrefix + ":" + name; @@ -65,6 +68,13 @@ public final class SimpleCommandMap implements CommandMap { return !nameInUse; } + private boolean nameInUse(String name) { + if (getCommand(name) != null) { + return !aliases.contains(name); + } + return false; + } + /** * {@inheritDoc} */