Use generics on manager to understand impl types

Slight risk of API breakage but risk is minimal enough that I'm not bumping versions.

0.5.0 Migration guide suggested (Platform)CommandManager, which will set the generics for you.
Only if you stored the ref as the Abstract CommandManager would you now run into generic issues.

Also, cleaned up the sendMessage API's to not need Object now since we know the Generic type.

This is technically an API break, but considering Locales was only released last night and
the likelyhood someone overrode that method, is unlikey.

If you did, just fix the method signature! Sorry <3
This commit is contained in:
Aikar
2017-07-18 21:43:32 -04:00
parent 495e33e47e
commit 418bbd6fd9
6 changed files with 13 additions and 26 deletions
@@ -49,7 +49,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
@SuppressWarnings("WeakerAccess")
public class BukkitCommandManager extends CommandManager {
public class BukkitCommandManager extends CommandManager<CommandSender, BukkitMessageFormatter> {
@SuppressWarnings("WeakerAccess")
protected final Plugin plugin;
@@ -38,7 +38,7 @@ import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
public class BungeeCommandManager extends CommandManager {
public class BungeeCommandManager extends CommandManager<CommandSender, BungeeMessageFormatter> {
protected final Plugin plugin;
protected Map<String, BungeeRootCommand> registeredCommands = new HashMap<>();
@@ -58,7 +58,7 @@ public abstract class BaseCommand {
private String execSubcommand;
@SuppressWarnings("WeakerAccess")
private String[] origArgs;
CommandManager manager = null;
CommandManager<?, ?> manager = null;
BaseCommand parentCommand;
Map<String, RootCommand> registeredCommands = new HashMap<>();
String description;
@@ -23,7 +23,6 @@
package co.aikar.commands;
import co.aikar.locales.MessageKey;
import co.aikar.locales.MessageKeyProvider;
import com.google.common.collect.Sets;
@@ -32,7 +31,7 @@ import java.lang.reflect.Parameter;
import java.util.*;
@SuppressWarnings("WeakerAccess")
public abstract class CommandManager {
public abstract class CommandManager <I, F extends MessageFormatter<?>> {
/**
* This is a stack incase a command calls a command
@@ -42,20 +41,8 @@ public abstract class CommandManager {
protected CommandReplacements replacements = new CommandReplacements(this);
protected ExceptionHandler defaultExceptionHandler = null;
protected Set<Locale> supportedLanguages = Sets.newHashSet(Locale.ENGLISH);
protected Map<MessageType, MessageFormatter> formatters = new IdentityHashMap<>();
protected MessageFormatter defaultFormatter;
{
MessageFormatter plain = new MessageFormatter<Object>() {
@Override
String format(Object color, String message) {
return message;
}
};
defaultFormatter = plain;
formatters.put(MessageType.INFO, plain);
formatters.put(MessageType.SYNTAX, plain);
formatters.put(MessageType.ERROR, plain);
}
protected Map<MessageType, F> formatters = new IdentityHashMap<>();
protected F defaultFormatter;
public static CommandOperationContext getCurrentCommandOperationContext() {
return commandOperationContext.get().peek();
@@ -93,6 +80,7 @@ public abstract class CommandManager {
public abstract boolean hasRegisteredCommands();
public abstract boolean isCommandIssuer(Class<?> type);
// TODO: Change this to I if we make a breaking change
public abstract CommandIssuer getCommandIssuer(Object issuer);
public abstract RootCommand createRootCommand(String cmd);
@@ -166,13 +154,12 @@ public abstract class CommandManager {
return result;
}
public void sendMessage(Object issuerArg, MessageType type, MessageKeyProvider key, String... replacements) {
sendMessage(issuerArg, type, key.getMessageKey(), replacements);
public void sendMessage(I issuerArg, MessageType type, MessageKeyProvider key, String... replacements) {
sendMessage(getCommandIssuer(issuerArg), type, key, replacements);
}
public void sendMessage(Object issuerArg, MessageType type, MessageKey key, String... replacements) {
CommandIssuer issuer = issuerArg instanceof CommandIssuer ? (CommandIssuer) issuerArg : getCommandIssuer(issuerArg);
String message = getLocales().getMessage(issuer, key);
public void sendMessage(CommandIssuer issuer, MessageType type, MessageKeyProvider key, String... replacements) {
String message = getLocales().getMessage(issuer, key.getMessageKey());
if (replacements.length > 0) {
message = ACFUtil.replaceStrings(message, replacements);
}
@@ -35,7 +35,7 @@ import java.util.Map;
@SuppressWarnings("WeakerAccess")
public class Locales {
private final CommandManager manager;
private final CommandManager<?, ?> manager;
private final LocaleManager<CommandIssuer> localeManager;
private final SetMultimap<String, Locale> loadedBundles = HashMultimap.create();
@@ -40,7 +40,7 @@ import java.util.List;
import java.util.Map;
@SuppressWarnings("WeakerAccess")
public class SpongeCommandManager extends CommandManager {
public class SpongeCommandManager extends CommandManager<CommandSource, SpongeMessageFormatter> {
protected final PluginContainer plugin;
protected Map<String, SpongeRootCommand> registeredCommands = new HashMap<>();