From 2ac98ba448f302870f37a72e48862da53ea6a8bf Mon Sep 17 00:00:00 2001 From: Aikar Date: Wed, 13 Dec 2017 22:33:01 -0500 Subject: [PATCH] Updated JavaDocs --- .../co/aikar/commands/CommandManager.html | 114 +-- .../co/aikar/commands/CommandManager.html | 672 +++++++++--------- 2 files changed, 395 insertions(+), 391 deletions(-) diff --git a/docs/acf-core/co/aikar/commands/CommandManager.html b/docs/acf-core/co/aikar/commands/CommandManager.html index 09d81d00..fa4e61d8 100644 --- a/docs/acf-core/co/aikar/commands/CommandManager.html +++ b/docs/acf-core/co/aikar/commands/CommandManager.html @@ -108,7 +108,7 @@ var activeTableTab = "activeTableTab";


  • -
    public abstract class CommandManager<I,AI extends CommandIssuer,FT,F extends MessageFormatter<FT>>
    +
    public abstract class CommandManager<I,AI extends CommandIssuer,FT,F extends MessageFormatter<FT>>
     extends Object
  • @@ -493,7 +493,7 @@ extends
  • rootCommands

    -
    protected Map<String,co.aikar.commands.RootCommand> rootCommands
    +
    protected Map<String,co.aikar.commands.RootCommand> rootCommands
  • @@ -502,7 +502,7 @@ extends
  • replacements

    -
    protected final CommandReplacements replacements
    +
    protected final CommandReplacements replacements
  • @@ -511,7 +511,7 @@ extends
  • defaultExceptionHandler

    -
    protected ExceptionHandler defaultExceptionHandler
    +
    protected ExceptionHandler defaultExceptionHandler
  • @@ -520,7 +520,7 @@ extends
  • usePerIssuerLocale

    -
    protected boolean usePerIssuerLocale
    +
    protected boolean usePerIssuerLocale
  • @@ -529,7 +529,7 @@ extends
  • localeChangedCallbacks

    -
    protected List<IssuerLocaleChangedCallback<AI extends CommandIssuer>> localeChangedCallbacks
    +
    protected List<IssuerLocaleChangedCallback<AI extends CommandIssuer>> localeChangedCallbacks
  • @@ -538,7 +538,7 @@ extends
  • supportedLanguages

    -
    protected Set<Locale> supportedLanguages
    +
    protected Set<Locale> supportedLanguages
  • @@ -547,7 +547,7 @@ extends
  • formatters

    -
    protected Map<MessageType,F extends MessageFormatter<FT>> formatters
    +
    protected Map<MessageType,F extends MessageFormatter<FT>> formatters
  • @@ -556,7 +556,7 @@ extends
  • defaultFormatter

    -
    protected F extends MessageFormatter<FT> defaultFormatter
    +
    protected F extends MessageFormatter<FT> defaultFormatter
  • @@ -565,7 +565,7 @@ extends
  • defaultHelpPerPage

    -
    protected int defaultHelpPerPage
    +
    protected int defaultHelpPerPage
  • @@ -582,7 +582,7 @@ extends
  • CommandManager

    -
    public CommandManager()
    +
    public CommandManager()
  • @@ -599,7 +599,7 @@ extends
  • getCurrentCommandOperationContext

    -
    public static CommandOperationContext getCurrentCommandOperationContext()
    +
    public static CommandOperationContext getCurrentCommandOperationContext()
  • @@ -608,7 +608,7 @@ extends
  • getCurrentCommandIssuer

    -
    public static CommandIssuer getCurrentCommandIssuer()
    +
    public static CommandIssuer getCurrentCommandIssuer()
  • @@ -617,7 +617,7 @@ extends
  • getCurrentCommandManager

    -
    public static CommandManager getCurrentCommandManager()
    +
    public static CommandManager getCurrentCommandManager()
  • @@ -628,7 +628,7 @@ extends
  • setFormat

    -
    public F setFormat(MessageType type,
    +
    public F setFormat(MessageType type,
                        F formatter)
  • @@ -638,7 +638,7 @@ extends
  • getFormat

    -
    public F getFormat(MessageType type)
    +
    public F getFormat(MessageType type)
  • @@ -649,7 +649,7 @@ extends
  • setFormat

    -
    public void setFormat(MessageType type,
    +
    public void setFormat(MessageType type,
                           FT... colors)
  • @@ -661,7 +661,7 @@ extends
  • setFormat

    -
    public void setFormat(MessageType type,
    +
    public void setFormat(MessageType type,
                           int i,
                           FT color)
  • @@ -672,7 +672,7 @@ extends
  • getDefaultFormatter

    -
    public F getDefaultFormatter()
    +
    public F getDefaultFormatter()
  • @@ -683,7 +683,7 @@ extends
  • setDefaultFormatter

    -
    public void setDefaultFormatter(F defaultFormatter)
    +
    public void setDefaultFormatter(F defaultFormatter)
  • @@ -692,7 +692,7 @@ extends
  • getCommandContexts

    -
    public abstract CommandContexts<?> getCommandContexts()
    +
    public abstract CommandContexts<?> getCommandContexts()
    Gets the command contexts manager
    Returns:
    @@ -706,7 +706,7 @@ extends
  • getCommandCompletions

    -
    public abstract CommandCompletions<?> getCommandCompletions()
    +
    public abstract CommandCompletions<?> getCommandCompletions()
    Gets the command completions manager
    Returns:
    @@ -721,7 +721,7 @@ extends

    generateCommandHelp

    @Deprecated
    -public CommandHelp generateCommandHelp(@NotNull
    +public CommandHelp generateCommandHelp(@NotNull
                                                        @NotNull String command)
    Deprecated. Unstable API
  • @@ -733,7 +733,7 @@ public 

    generateCommandHelp

    @Deprecated
    -public CommandHelp generateCommandHelp(CommandIssuer issuer,
    +public CommandHelp generateCommandHelp(CommandIssuer issuer,
                                                        @NotNull
                                                        @NotNull String command)
    Deprecated. Unstable API
    @@ -746,7 +746,7 @@ public 

    generateCommandHelp

    @Deprecated
    -public CommandHelp generateCommandHelp()
    +public CommandHelp generateCommandHelp()
    Deprecated. Unstable API
  • @@ -757,7 +757,7 @@ public 

    generateCommandHelp

    @Deprecated
    -public CommandHelp generateCommandHelp(CommandIssuer issuer,
    +public CommandHelp generateCommandHelp(CommandIssuer issuer,
                                                        co.aikar.commands.RootCommand rootCommand)
    Deprecated. Unstable API
    @@ -769,7 +769,7 @@ public 

    getDefaultHelpPerPage

    @Deprecated
    -public int getDefaultHelpPerPage()
    +public int getDefaultHelpPerPage()
    Deprecated. Unstable API
    @@ -780,7 +780,7 @@ public int 

    setDefaultHelpPerPage

    @Deprecated
    -public void setDefaultHelpPerPage(int defaultHelpPerPage)
    +public void setDefaultHelpPerPage(int defaultHelpPerPage)
    Deprecated. Unstable API
    @@ -790,7 +790,7 @@ public void 
  • registerCommand

    -
    public abstract void registerCommand(BaseCommand command)
    +
    public abstract void registerCommand(BaseCommand command)
    Registers a command with ACF
    Parameters:
    @@ -804,7 +804,7 @@ public void 
  • hasRegisteredCommands

    -
    public abstract boolean hasRegisteredCommands()
    +
    public abstract boolean hasRegisteredCommands()
  • @@ -813,7 +813,7 @@ public void 
  • isCommandIssuer

    -
    public abstract boolean isCommandIssuer(Class<?> type)
    +
    public abstract boolean isCommandIssuer(Class<?> type)
  • @@ -822,7 +822,7 @@ public void 
  • getCommandIssuer

    -
    public abstract AI getCommandIssuer(Object issuer)
    +
    public abstract AI getCommandIssuer(Object issuer)
  • @@ -831,7 +831,7 @@ public void 
  • createRootCommand

    -
    public abstract co.aikar.commands.RootCommand createRootCommand(String cmd)
    +
    public abstract co.aikar.commands.RootCommand createRootCommand(String cmd)
  • @@ -840,7 +840,7 @@ public void 
  • getLocales

    -
    public abstract Locales getLocales()
    +
    public abstract Locales getLocales()
    Returns a Locales Manager to add and modify language tables for your commands.
    Returns:
    @@ -853,7 +853,7 @@ public void 
  • usingPerIssuerLocale

    -
    public boolean usingPerIssuerLocale()
    +
    public boolean usingPerIssuerLocale()
  • @@ -862,7 +862,7 @@ public void 
  • usePerIssuerLocale

    -
    public boolean usePerIssuerLocale(boolean setting)
    +
    public boolean usePerIssuerLocale(boolean setting)
  • @@ -871,7 +871,7 @@ public void 
  • createCommandContext

    -
    public abstract <R extends CommandExecutionContext> R createCommandContext(RegisteredCommand command,
    +
    public abstract <R extends CommandExecutionContext> R createCommandContext(RegisteredCommand command,
                                                                                Parameter parameter,
                                                                                CommandIssuer sender,
                                                                                List<String> args,
    @@ -885,7 +885,7 @@ public void 
     
  • createCompletionContext

    -
    public abstract CommandCompletionContext createCompletionContext(RegisteredCommand command,
    +
    public abstract CommandCompletionContext createCompletionContext(RegisteredCommand command,
                                                                      CommandIssuer sender,
                                                                      String input,
                                                                      String config,
    @@ -898,7 +898,7 @@ public void 
     
  • log

    -
    public abstract void log(co.aikar.commands.LogLevel level,
    +
    public abstract void log(co.aikar.commands.LogLevel level,
                              String message,
                              Throwable throwable)
  • @@ -909,7 +909,7 @@ public void 
  • log

    -
    public void log(co.aikar.commands.LogLevel level,
    +
    public void log(co.aikar.commands.LogLevel level,
                     String message)
  • @@ -919,7 +919,7 @@ public void 
  • getCommandReplacements

    -
    public CommandReplacements getCommandReplacements()
    +
    public CommandReplacements getCommandReplacements()
    Lets you add custom string replacements that can be applied to annotation values, to reduce duplication/repetition of common values such as permission nodes and command prefixes. @@ -938,7 +938,7 @@ public void 
  • hasPermission

    -
    public boolean hasPermission(CommandIssuer issuer,
    +
    public boolean hasPermission(CommandIssuer issuer,
                                  String permission)
  • @@ -948,7 +948,7 @@ public void 
  • getRootCommand

    -
    public co.aikar.commands.RootCommand getRootCommand(@NotNull
    +
    public co.aikar.commands.RootCommand getRootCommand(@NotNull
                                                         @NotNull String cmd)
  • @@ -958,7 +958,7 @@ public void 
  • obtainRootCommand

    -
    public co.aikar.commands.RootCommand obtainRootCommand(@NotNull
    +
    public co.aikar.commands.RootCommand obtainRootCommand(@NotNull
                                                            @NotNull String cmd)
  • @@ -968,7 +968,7 @@ public void 
  • createRegisteredCommand

    -
    public RegisteredCommand createRegisteredCommand(BaseCommand command,
    +
    public RegisteredCommand createRegisteredCommand(BaseCommand command,
                                                      String cmdName,
                                                      Method method,
                                                      String prefSubCommand)
    @@ -980,7 +980,7 @@ public void 
  • setDefaultExceptionHandler

    -
    public void setDefaultExceptionHandler(ExceptionHandler exceptionHandler)
    +
    public void setDefaultExceptionHandler(ExceptionHandler exceptionHandler)
    Sets the default ExceptionHandler that is called when an exception occurs while executing a command, if the command doesn't have it's own exception handler registered.
    Parameters:
    @@ -994,7 +994,7 @@ public void 
  • getDefaultExceptionHandler

    -
    public ExceptionHandler getDefaultExceptionHandler()
    +
    public ExceptionHandler getDefaultExceptionHandler()
    Gets the current default exception handler, might be null.
    Returns:
    @@ -1008,7 +1008,7 @@ public void 
  • handleUncaughtException

    -
    protected boolean handleUncaughtException(BaseCommand scope,
    +
    protected boolean handleUncaughtException(BaseCommand scope,
                                               RegisteredCommand registeredCommand,
                                               CommandIssuer sender,
                                               List<String> args,
    @@ -1023,7 +1023,7 @@ public void 
     
  • sendMessage

    -
    public void sendMessage(I issuerArg,
    +
    public void sendMessage(I issuerArg,
                             MessageType type,
                             co.aikar.locales.MessageKeyProvider key,
                             String... replacements)
    @@ -1035,7 +1035,7 @@ public void 
  • sendMessage

    -
    public void sendMessage(CommandIssuer issuer,
    +
    public void sendMessage(CommandIssuer issuer,
                             MessageType type,
                             co.aikar.locales.MessageKeyProvider key,
                             String... replacements)
    @@ -1047,7 +1047,7 @@ public void 
  • formatMessage

    -
    public String formatMessage(CommandIssuer issuer,
    +
    public String formatMessage(CommandIssuer issuer,
                                 MessageType type,
                                 co.aikar.locales.MessageKeyProvider key,
                                 String... replacements)
    @@ -1059,7 +1059,7 @@ public void 
  • onLocaleChange

    -
    public void onLocaleChange(IssuerLocaleChangedCallback<AI> onChange)
    +
    public void onLocaleChange(IssuerLocaleChangedCallback<AI> onChange)
  • @@ -1070,7 +1070,7 @@ public void 
  • notifyLocaleChange

    -
    public void notifyLocaleChange(AI issuer,
    +
    public void notifyLocaleChange(AI issuer,
                                    Locale oldLocale,
                                    Locale newLocale)
  • @@ -1081,7 +1081,7 @@ public void 
  • getIssuerLocale

    -
    public Locale getIssuerLocale(CommandIssuer issuer)
    +
    public Locale getIssuerLocale(CommandIssuer issuer)
  • @@ -1090,7 +1090,7 @@ public void 
  • getSupportedLanguages

    -
    public Set<LocalegetSupportedLanguages()
    +
    public Set<LocalegetSupportedLanguages()
    Gets a list of all currently supported languages for this manager. These locales will be automatically loaded from
    @@ -1104,7 +1104,7 @@ public void 
  • addSupportedLanguage

    -
    public void addSupportedLanguage(Locale locale)
    +
    public void addSupportedLanguage(Locale locale)
    Adds a new locale to the list of automatic Locales to load Message Bundles for. All bundles loaded under the previous supported languages will now automatically load for this new locale too.
    @@ -1120,7 +1120,7 @@ public void 

    enableUnstableAPI

    @Deprecated
    -public void enableUnstableAPI(String api)
    +public void enableUnstableAPI(String api)
  • Deprecated. Use this with caution! If you enable and use Unstable API's, your next compile using ACF may require you to update your implementation to those unstable API's
  • diff --git a/docs/acf-core/src-html/co/aikar/commands/CommandManager.html b/docs/acf-core/src-html/co/aikar/commands/CommandManager.html index 98ff5327..1603f76a 100644 --- a/docs/acf-core/src-html/co/aikar/commands/CommandManager.html +++ b/docs/acf-core/src-html/co/aikar/commands/CommandManager.html @@ -36,346 +36,350 @@ 028import com.google.common.collect.Sets; 029import org.jetbrains.annotations.NotNull; 030 -031import java.lang.reflect.Method; -032import java.lang.reflect.Parameter; -033import java.util.HashMap; -034import java.util.IdentityHashMap; -035import java.util.List; -036import java.util.Locale; -037import java.util.Map; -038import java.util.Set; -039import java.util.Stack; -040 -041@SuppressWarnings("WeakerAccess") -042public abstract class CommandManager <I, AI extends CommandIssuer, FT, F extends MessageFormatter<FT>> { -043 -044 /** -045 * This is a stack incase a command calls a command -046 */ -047 static ThreadLocal<Stack<CommandOperationContext>> commandOperationContext = ThreadLocal.withInitial(() -> { -048 return new Stack<CommandOperationContext>() { -049 @Override -050 public synchronized CommandOperationContext peek() { -051 return super.size() == 0 ? null : super.peek(); -052 } -053 }; -054 }); -055 protected Map<String, RootCommand> rootCommands = new HashMap<>(); -056 protected final CommandReplacements replacements = new CommandReplacements(this); -057 protected ExceptionHandler defaultExceptionHandler = null; -058 -059 protected boolean usePerIssuerLocale = false; -060 protected List<IssuerLocaleChangedCallback<AI>> localeChangedCallbacks = Lists.newArrayList(); -061 protected Set<Locale> supportedLanguages = Sets.newHashSet(Locales.ENGLISH, Locales.GERMAN, Locales.SPANISH, Locales.CZECH); -062 protected Map<MessageType, F> formatters = new IdentityHashMap<>(); -063 protected F defaultFormatter; -064 protected int defaultHelpPerPage = 10; -065 -066 private Set<String> unstableAPIs = Sets.newHashSet(); -067 -068 public static CommandOperationContext getCurrentCommandOperationContext() { -069 return commandOperationContext.get().peek(); -070 } -071 -072 public static CommandIssuer getCurrentCommandIssuer() { -073 CommandOperationContext context = commandOperationContext.get().peek(); -074 return context != null ? context.getCommandIssuer() : null; -075 } -076 -077 public static CommandManager getCurrentCommandManager() { -078 CommandOperationContext context = commandOperationContext.get().peek(); -079 return context != null ? context.getCommandManager() : null; -080 } -081 -082 public F setFormat(MessageType type, F formatter) { -083 return formatters.put(type, formatter); -084 } -085 -086 public F getFormat(MessageType type) { -087 return formatters.getOrDefault(type, defaultFormatter); -088 } -089 -090 public void setFormat(MessageType type, FT... colors) { -091 F format = getFormat(type); -092 for (int i = 0; i < colors.length; i++) { -093 format.setColor(i, colors[i]); -094 } -095 } -096 -097 public void setFormat(MessageType type, int i, FT color) { -098 F format = getFormat(type); -099 format.setColor(i, color); -100 } -101 -102 public F getDefaultFormatter() { -103 return defaultFormatter; -104 } -105 -106 public void setDefaultFormatter(F defaultFormatter) { -107 this.defaultFormatter = defaultFormatter; -108 } -109 -110 /** -111 * Gets the command contexts manager -112 * @return Command Contexts -113 */ -114 public abstract CommandContexts<?> getCommandContexts(); -115 -116 /** -117 * Gets the command completions manager -118 * @return Command Completions -119 */ -120 public abstract CommandCompletions<?> getCommandCompletions(); -121 -122 /** @deprecated Unstable API */ @Deprecated @UnstableAPI -123 public CommandHelp generateCommandHelp(@NotNull String command) { -124 verifyUnstableAPI("help"); -125 CommandOperationContext context = getCurrentCommandOperationContext(); -126 if (context == null) { -127 throw new IllegalStateException("This method can only be called as part of a command execution."); -128 } -129 return generateCommandHelp(context.getCommandIssuer(), command); -130 } -131 -132 /** @deprecated Unstable API */ @Deprecated @UnstableAPI -133 public CommandHelp generateCommandHelp(CommandIssuer issuer, @NotNull String command) { -134 verifyUnstableAPI("help"); -135 return generateCommandHelp(issuer, obtainRootCommand(command)); -136 } -137 -138 /** @deprecated Unstable API */ @Deprecated @UnstableAPI -139 public CommandHelp generateCommandHelp() { -140 verifyUnstableAPI("help"); -141 CommandOperationContext context = getCurrentCommandOperationContext(); -142 if (context == null) { -143 throw new IllegalStateException("This method can only be called as part of a command execution."); -144 } -145 String commandLabel = context.getCommandLabel(); -146 return generateCommandHelp(context.getCommandIssuer(), this.obtainRootCommand(commandLabel)); -147 } -148 -149 /** @deprecated Unstable API */ @Deprecated @UnstableAPI -150 public CommandHelp generateCommandHelp(CommandIssuer issuer, RootCommand rootCommand) { -151 verifyUnstableAPI("help"); -152 return new CommandHelp(this, rootCommand, issuer); -153 } -154 -155 /** @deprecated Unstable API */ @Deprecated @UnstableAPI -156 public int getDefaultHelpPerPage() { -157 verifyUnstableAPI("help"); -158 return defaultHelpPerPage; -159 } -160 -161 /** @deprecated Unstable API */ @Deprecated @UnstableAPI -162 public void setDefaultHelpPerPage(int defaultHelpPerPage) { -163 verifyUnstableAPI("help"); -164 this.defaultHelpPerPage = defaultHelpPerPage; -165 } -166 -167 /** -168 * Registers a command with ACF -169 * -170 * @param command The command to register -171 * @return boolean -172 */ -173 public abstract void registerCommand(BaseCommand command); -174 public abstract boolean hasRegisteredCommands(); -175 public abstract boolean isCommandIssuer(Class<?> type); -176 -177 // TODO: Change this to I if we make a breaking change -178 public abstract AI getCommandIssuer(Object issuer); -179 -180 public abstract RootCommand createRootCommand(String cmd); -181 -182 /** -183 * Returns a Locales Manager to add and modify language tables for your commands. -184 * @return -185 */ -186 public abstract Locales getLocales(); -187 -188 public boolean usingPerIssuerLocale() { -189 return usePerIssuerLocale; -190 } -191 -192 public boolean usePerIssuerLocale(boolean setting) { -193 boolean old = usePerIssuerLocale; -194 usePerIssuerLocale = setting; -195 return old; -196 } -197 -198 public abstract <R extends CommandExecutionContext> R createCommandContext(RegisteredCommand command, Parameter parameter, CommandIssuer sender, List<String> args, int i, Map<String, Object> passedArgs); -199 -200 public abstract CommandCompletionContext createCompletionContext(RegisteredCommand command, CommandIssuer sender, String input, String config, String[] args); -201 -202 public abstract void log(final LogLevel level, final String message, final Throwable throwable); -203 -204 public void log(final LogLevel level, final String message) { -205 log(level, message, null); -206 } -207 -208 /** -209 * Lets you add custom string replacements that can be applied to annotation values, -210 * to reduce duplication/repetition of common values such as permission nodes and command prefixes. -211 * -212 * Any replacement registered starts with a % -213 * -214 * So for ex @CommandPermission("%staff") -215 * @return Replacements Manager -216 */ -217 public CommandReplacements getCommandReplacements() { -218 return replacements; -219 } -220 -221 public boolean hasPermission(CommandIssuer issuer, String permission) { -222 if (permission == null || permission.isEmpty()) { -223 return true; -224 } -225 for (String perm : ACFPatterns.COMMA.split(permission)) { -226 if (!perm.isEmpty() && !issuer.hasPermission(perm)) { -227 return false; -228 } -229 } -230 return true; -231 } -232 -233 BaseCommand getBaseCommand(String commandLabel, @NotNull String[] args) { -234 RootCommand rootCommand = obtainRootCommand(commandLabel); -235 if (rootCommand == null) { -236 return null; -237 } -238 return rootCommand.getBaseCommand(args); -239 } -240 -241 public synchronized RootCommand getRootCommand(@NotNull String cmd) { -242 return rootCommands.get(ACFPatterns.SPACE.split(cmd.toLowerCase(), 2)[0]); -243 } -244 -245 public synchronized RootCommand obtainRootCommand(@NotNull String cmd) { -246 return rootCommands.computeIfAbsent(ACFPatterns.SPACE.split(cmd.toLowerCase(), 2)[0], this::createRootCommand); -247 } -248 -249 public RegisteredCommand createRegisteredCommand(BaseCommand command, String cmdName, Method method, String prefSubCommand) { -250 return new RegisteredCommand(command, cmdName, method, prefSubCommand); -251 } -252 -253 /** -254 * Sets the default {@link ExceptionHandler} that is called when an exception occurs while executing a command, if the command doesn't have it's own exception handler registered. -255 * -256 * @param exceptionHandler the handler that should handle uncaught exceptions -257 */ -258 public void setDefaultExceptionHandler(ExceptionHandler exceptionHandler) { -259 defaultExceptionHandler = exceptionHandler; -260 } -261 -262 /** -263 * Gets the current default exception handler, might be null. -264 * -265 * @return the default exception handler -266 */ -267 public ExceptionHandler getDefaultExceptionHandler() { -268 return defaultExceptionHandler; -269 } -270 -271 protected boolean handleUncaughtException(BaseCommand scope, RegisteredCommand registeredCommand, CommandIssuer sender, List<String> args, Throwable t) { -272 boolean result = false; -273 if (scope.getExceptionHandler() != null) { -274 result = scope.getExceptionHandler().execute(scope, registeredCommand, sender, args, t); -275 } else if (defaultExceptionHandler != null) { -276 result = defaultExceptionHandler.execute(scope, registeredCommand, sender, args, t); -277 } -278 return result; -279 } -280 -281 public void sendMessage(I issuerArg, MessageType type, MessageKeyProvider key, String... replacements) { -282 sendMessage(getCommandIssuer(issuerArg), type, key, replacements); +031import java.lang.reflect.InvocationTargetException; +032import java.lang.reflect.Method; +033import java.lang.reflect.Parameter; +034import java.util.HashMap; +035import java.util.IdentityHashMap; +036import java.util.List; +037import java.util.Locale; +038import java.util.Map; +039import java.util.Set; +040import java.util.Stack; +041 +042@SuppressWarnings("WeakerAccess") +043public abstract class CommandManager <I, AI extends CommandIssuer, FT, F extends MessageFormatter<FT>> { +044 +045 /** +046 * This is a stack incase a command calls a command +047 */ +048 static ThreadLocal<Stack<CommandOperationContext>> commandOperationContext = ThreadLocal.withInitial(() -> { +049 return new Stack<CommandOperationContext>() { +050 @Override +051 public synchronized CommandOperationContext peek() { +052 return super.size() == 0 ? null : super.peek(); +053 } +054 }; +055 }); +056 protected Map<String, RootCommand> rootCommands = new HashMap<>(); +057 protected final CommandReplacements replacements = new CommandReplacements(this); +058 protected ExceptionHandler defaultExceptionHandler = null; +059 +060 protected boolean usePerIssuerLocale = false; +061 protected List<IssuerLocaleChangedCallback<AI>> localeChangedCallbacks = Lists.newArrayList(); +062 protected Set<Locale> supportedLanguages = Sets.newHashSet(Locales.ENGLISH, Locales.GERMAN, Locales.SPANISH, Locales.CZECH); +063 protected Map<MessageType, F> formatters = new IdentityHashMap<>(); +064 protected F defaultFormatter; +065 protected int defaultHelpPerPage = 10; +066 +067 private Set<String> unstableAPIs = Sets.newHashSet(); +068 +069 public static CommandOperationContext getCurrentCommandOperationContext() { +070 return commandOperationContext.get().peek(); +071 } +072 +073 public static CommandIssuer getCurrentCommandIssuer() { +074 CommandOperationContext context = commandOperationContext.get().peek(); +075 return context != null ? context.getCommandIssuer() : null; +076 } +077 +078 public static CommandManager getCurrentCommandManager() { +079 CommandOperationContext context = commandOperationContext.get().peek(); +080 return context != null ? context.getCommandManager() : null; +081 } +082 +083 public F setFormat(MessageType type, F formatter) { +084 return formatters.put(type, formatter); +085 } +086 +087 public F getFormat(MessageType type) { +088 return formatters.getOrDefault(type, defaultFormatter); +089 } +090 +091 public void setFormat(MessageType type, FT... colors) { +092 F format = getFormat(type); +093 for (int i = 0; i < colors.length; i++) { +094 format.setColor(i, colors[i]); +095 } +096 } +097 +098 public void setFormat(MessageType type, int i, FT color) { +099 F format = getFormat(type); +100 format.setColor(i, color); +101 } +102 +103 public F getDefaultFormatter() { +104 return defaultFormatter; +105 } +106 +107 public void setDefaultFormatter(F defaultFormatter) { +108 this.defaultFormatter = defaultFormatter; +109 } +110 +111 /** +112 * Gets the command contexts manager +113 * @return Command Contexts +114 */ +115 public abstract CommandContexts<?> getCommandContexts(); +116 +117 /** +118 * Gets the command completions manager +119 * @return Command Completions +120 */ +121 public abstract CommandCompletions<?> getCommandCompletions(); +122 +123 /** @deprecated Unstable API */ @Deprecated @UnstableAPI +124 public CommandHelp generateCommandHelp(@NotNull String command) { +125 verifyUnstableAPI("help"); +126 CommandOperationContext context = getCurrentCommandOperationContext(); +127 if (context == null) { +128 throw new IllegalStateException("This method can only be called as part of a command execution."); +129 } +130 return generateCommandHelp(context.getCommandIssuer(), command); +131 } +132 +133 /** @deprecated Unstable API */ @Deprecated @UnstableAPI +134 public CommandHelp generateCommandHelp(CommandIssuer issuer, @NotNull String command) { +135 verifyUnstableAPI("help"); +136 return generateCommandHelp(issuer, obtainRootCommand(command)); +137 } +138 +139 /** @deprecated Unstable API */ @Deprecated @UnstableAPI +140 public CommandHelp generateCommandHelp() { +141 verifyUnstableAPI("help"); +142 CommandOperationContext context = getCurrentCommandOperationContext(); +143 if (context == null) { +144 throw new IllegalStateException("This method can only be called as part of a command execution."); +145 } +146 String commandLabel = context.getCommandLabel(); +147 return generateCommandHelp(context.getCommandIssuer(), this.obtainRootCommand(commandLabel)); +148 } +149 +150 /** @deprecated Unstable API */ @Deprecated @UnstableAPI +151 public CommandHelp generateCommandHelp(CommandIssuer issuer, RootCommand rootCommand) { +152 verifyUnstableAPI("help"); +153 return new CommandHelp(this, rootCommand, issuer); +154 } +155 +156 /** @deprecated Unstable API */ @Deprecated @UnstableAPI +157 public int getDefaultHelpPerPage() { +158 verifyUnstableAPI("help"); +159 return defaultHelpPerPage; +160 } +161 +162 /** @deprecated Unstable API */ @Deprecated @UnstableAPI +163 public void setDefaultHelpPerPage(int defaultHelpPerPage) { +164 verifyUnstableAPI("help"); +165 this.defaultHelpPerPage = defaultHelpPerPage; +166 } +167 +168 /** +169 * Registers a command with ACF +170 * +171 * @param command The command to register +172 * @return boolean +173 */ +174 public abstract void registerCommand(BaseCommand command); +175 public abstract boolean hasRegisteredCommands(); +176 public abstract boolean isCommandIssuer(Class<?> type); +177 +178 // TODO: Change this to I if we make a breaking change +179 public abstract AI getCommandIssuer(Object issuer); +180 +181 public abstract RootCommand createRootCommand(String cmd); +182 +183 /** +184 * Returns a Locales Manager to add and modify language tables for your commands. +185 * @return +186 */ +187 public abstract Locales getLocales(); +188 +189 public boolean usingPerIssuerLocale() { +190 return usePerIssuerLocale; +191 } +192 +193 public boolean usePerIssuerLocale(boolean setting) { +194 boolean old = usePerIssuerLocale; +195 usePerIssuerLocale = setting; +196 return old; +197 } +198 +199 public abstract <R extends CommandExecutionContext> R createCommandContext(RegisteredCommand command, Parameter parameter, CommandIssuer sender, List<String> args, int i, Map<String, Object> passedArgs); +200 +201 public abstract CommandCompletionContext createCompletionContext(RegisteredCommand command, CommandIssuer sender, String input, String config, String[] args); +202 +203 public abstract void log(final LogLevel level, final String message, final Throwable throwable); +204 +205 public void log(final LogLevel level, final String message) { +206 log(level, message, null); +207 } +208 +209 /** +210 * Lets you add custom string replacements that can be applied to annotation values, +211 * to reduce duplication/repetition of common values such as permission nodes and command prefixes. +212 * +213 * Any replacement registered starts with a % +214 * +215 * So for ex @CommandPermission("%staff") +216 * @return Replacements Manager +217 */ +218 public CommandReplacements getCommandReplacements() { +219 return replacements; +220 } +221 +222 public boolean hasPermission(CommandIssuer issuer, String permission) { +223 if (permission == null || permission.isEmpty()) { +224 return true; +225 } +226 for (String perm : ACFPatterns.COMMA.split(permission)) { +227 if (!perm.isEmpty() && !issuer.hasPermission(perm)) { +228 return false; +229 } +230 } +231 return true; +232 } +233 +234 BaseCommand getBaseCommand(String commandLabel, @NotNull String[] args) { +235 RootCommand rootCommand = obtainRootCommand(commandLabel); +236 if (rootCommand == null) { +237 return null; +238 } +239 return rootCommand.getBaseCommand(args); +240 } +241 +242 public synchronized RootCommand getRootCommand(@NotNull String cmd) { +243 return rootCommands.get(ACFPatterns.SPACE.split(cmd.toLowerCase(), 2)[0]); +244 } +245 +246 public synchronized RootCommand obtainRootCommand(@NotNull String cmd) { +247 return rootCommands.computeIfAbsent(ACFPatterns.SPACE.split(cmd.toLowerCase(), 2)[0], this::createRootCommand); +248 } +249 +250 public RegisteredCommand createRegisteredCommand(BaseCommand command, String cmdName, Method method, String prefSubCommand) { +251 return new RegisteredCommand(command, cmdName, method, prefSubCommand); +252 } +253 +254 /** +255 * Sets the default {@link ExceptionHandler} that is called when an exception occurs while executing a command, if the command doesn't have it's own exception handler registered. +256 * +257 * @param exceptionHandler the handler that should handle uncaught exceptions +258 */ +259 public void setDefaultExceptionHandler(ExceptionHandler exceptionHandler) { +260 defaultExceptionHandler = exceptionHandler; +261 } +262 +263 /** +264 * Gets the current default exception handler, might be null. +265 * +266 * @return the default exception handler +267 */ +268 public ExceptionHandler getDefaultExceptionHandler() { +269 return defaultExceptionHandler; +270 } +271 +272 protected boolean handleUncaughtException(BaseCommand scope, RegisteredCommand registeredCommand, CommandIssuer sender, List<String> args, Throwable t) { +273 if (t instanceof InvocationTargetException && t.getCause() != null) { +274 t = t.getCause(); +275 } +276 boolean result = false; +277 if (scope.getExceptionHandler() != null) { +278 result = scope.getExceptionHandler().execute(scope, registeredCommand, sender, args, t); +279 } else if (defaultExceptionHandler != null) { +280 result = defaultExceptionHandler.execute(scope, registeredCommand, sender, args, t); +281 } +282 return result; 283 } 284 -285 public void sendMessage(CommandIssuer issuer, MessageType type, MessageKeyProvider key, String... replacements) { -286 String message = formatMessage(issuer, type, key, replacements); -287 -288 for (String msg : ACFPatterns.NEWLINE.split(message)) { -289 issuer.sendMessageInternal(ACFUtil.rtrim(msg)); -290 } -291 } -292 -293 public String formatMessage(CommandIssuer issuer, MessageType type, MessageKeyProvider key, String... replacements) { -294 String message = getLocales().getMessage(issuer, key.getMessageKey()); -295 if (replacements.length > 0) { -296 message = ACFUtil.replaceStrings(message, replacements); -297 } -298 -299 message = getCommandReplacements().replace(message); -300 -301 MessageFormatter formatter = formatters.getOrDefault(type, defaultFormatter); -302 if (formatter != null) { -303 message = formatter.format(message); -304 } -305 return message; -306 } -307 -308 public void onLocaleChange(IssuerLocaleChangedCallback<AI> onChange) { -309 localeChangedCallbacks.add(onChange); +285 public void sendMessage(I issuerArg, MessageType type, MessageKeyProvider key, String... replacements) { +286 sendMessage(getCommandIssuer(issuerArg), type, key, replacements); +287 } +288 +289 public void sendMessage(CommandIssuer issuer, MessageType type, MessageKeyProvider key, String... replacements) { +290 String message = formatMessage(issuer, type, key, replacements); +291 +292 for (String msg : ACFPatterns.NEWLINE.split(message)) { +293 issuer.sendMessageInternal(ACFUtil.rtrim(msg)); +294 } +295 } +296 +297 public String formatMessage(CommandIssuer issuer, MessageType type, MessageKeyProvider key, String... replacements) { +298 String message = getLocales().getMessage(issuer, key.getMessageKey()); +299 if (replacements.length > 0) { +300 message = ACFUtil.replaceStrings(message, replacements); +301 } +302 +303 message = getCommandReplacements().replace(message); +304 +305 MessageFormatter formatter = formatters.getOrDefault(type, defaultFormatter); +306 if (formatter != null) { +307 message = formatter.format(message); +308 } +309 return message; 310 } 311 -312 public void notifyLocaleChange(AI issuer, Locale oldLocale, Locale newLocale) { -313 localeChangedCallbacks.forEach(cb -> { -314 try { -315 cb.onIssuerLocaleChange(issuer, oldLocale, newLocale); -316 } catch (Exception e) { -317 this.log(LogLevel.ERROR, "Error in notifyLocaleChange", e); -318 } -319 }); -320 } -321 -322 public Locale getIssuerLocale(CommandIssuer issuer) { -323 return getLocales().getDefaultLocale(); +312 public void onLocaleChange(IssuerLocaleChangedCallback<AI> onChange) { +313 localeChangedCallbacks.add(onChange); +314 } +315 +316 public void notifyLocaleChange(AI issuer, Locale oldLocale, Locale newLocale) { +317 localeChangedCallbacks.forEach(cb -> { +318 try { +319 cb.onIssuerLocaleChange(issuer, oldLocale, newLocale); +320 } catch (Exception e) { +321 this.log(LogLevel.ERROR, "Error in notifyLocaleChange", e); +322 } +323 }); 324 } 325 -326 CommandOperationContext createCommandOperationContext(BaseCommand command, CommandIssuer issuer, String commandLabel, String[] args, boolean isAsync) { -327 return new CommandOperationContext( -328 this, -329 issuer, -330 command, -331 commandLabel, -332 args, -333 isAsync -334 ); -335 } -336 -337 /** -338 * Gets a list of all currently supported languages for this manager. -339 * These locales will be automatically loaded from -340 * @return -341 */ -342 public Set<Locale> getSupportedLanguages() { -343 return supportedLanguages; -344 } -345 -346 /** -347 * Adds a new locale to the list of automatic Locales to load Message Bundles for. -348 * All bundles loaded under the previous supported languages will now automatically load for this new locale too. -349 * -350 * @param locale -351 */ -352 public void addSupportedLanguage(Locale locale) { -353 supportedLanguages.add(locale); -354 getLocales().loadMissingBundles(); -355 } -356 -357 /** -358 * @deprecated Use this with caution! If you enable and use Unstable API's, your next compile using ACF -359 * may require you to update your implementation to those unstable API's -360 */ -361 @Deprecated -362 public void enableUnstableAPI(String api) { -363 unstableAPIs.add(api); -364 } -365 void verifyUnstableAPI(String api) { -366 if (!unstableAPIs.contains(api)) { -367 throw new IllegalStateException("Using an unstable API that has not been enabled ( " + api + "). See https://acfunstable.emc.gs"); -368 } -369 } -370} +326 public Locale getIssuerLocale(CommandIssuer issuer) { +327 return getLocales().getDefaultLocale(); +328 } +329 +330 CommandOperationContext createCommandOperationContext(BaseCommand command, CommandIssuer issuer, String commandLabel, String[] args, boolean isAsync) { +331 return new CommandOperationContext( +332 this, +333 issuer, +334 command, +335 commandLabel, +336 args, +337 isAsync +338 ); +339 } +340 +341 /** +342 * Gets a list of all currently supported languages for this manager. +343 * These locales will be automatically loaded from +344 * @return +345 */ +346 public Set<Locale> getSupportedLanguages() { +347 return supportedLanguages; +348 } +349 +350 /** +351 * Adds a new locale to the list of automatic Locales to load Message Bundles for. +352 * All bundles loaded under the previous supported languages will now automatically load for this new locale too. +353 * +354 * @param locale +355 */ +356 public void addSupportedLanguage(Locale locale) { +357 supportedLanguages.add(locale); +358 getLocales().loadMissingBundles(); +359 } +360 +361 /** +362 * @deprecated Use this with caution! If you enable and use Unstable API's, your next compile using ACF +363 * may require you to update your implementation to those unstable API's +364 */ +365 @Deprecated +366 public void enableUnstableAPI(String api) { +367 unstableAPIs.add(api); +368 } +369 void verifyUnstableAPI(String api) { +370 if (!unstableAPIs.contains(api)) { +371 throw new IllegalStateException("Using an unstable API that has not been enabled ( " + api + "). See https://acfunstable.emc.gs"); +372 } +373 } +374}