/** * Registers a command with the given name is possible. Also uses fallbackPrefix to create a * unique name. * * @param label the name of the command, without the '/'-prefix. * @param command the command to register * @param isAlias whether the command is an alias * @param fallbackPrefix a prefix which is prepended to the command for a unique address * @return true if command was registered, false otherwise. */ private synchronized boolean register( String label, Command command, boolean isAlias, String fallbackPrefix) { knownCommands.put(fallbackPrefix + ":" + label, command); if ((command instanceof VanillaCommand || isAlias) && knownCommands.containsKey(label)) { // Request is for an alias/fallback command and it conflicts with // a existing command or previous alias ignore it // Note: This will mean it gets removed from the commands list of active aliases return false; } boolean registered = true; // If the command exists but is an alias we overwrite it, otherwise we return Command conflict = knownCommands.get(label); if (conflict != null && conflict.getLabel().equals(label)) { return false; } if (!isAlias) { command.setLabel(label); } knownCommands.put(label, command); return registered; }
/** {@inheritDoc} */ public boolean register(String label, String fallbackPrefix, Command command) { label = label.toLowerCase().trim(); fallbackPrefix = fallbackPrefix.toLowerCase().trim(); boolean registered = register(label, command, false, fallbackPrefix); Iterator<String> iterator = command.getAliases().iterator(); while (iterator.hasNext()) { if (!register(iterator.next(), command, true, fallbackPrefix)) { iterator.remove(); } } // If we failed to register under the real name, we need to set the command label to the direct // address if (!registered) { command.setLabel(fallbackPrefix + ":" + label); } // Register to us so further updates of the commands label and aliases are postponed until its // reregistered command.register(this); return registered; }