Replace a bit of code duplication and misc cleanup

This commit is contained in:
Aikar
2017-07-03 00:02:43 -05:00
parent d9652ba9eb
commit b128908a77
7 changed files with 58 additions and 65 deletions
@@ -133,13 +133,13 @@ public class BukkitCommandManager extends CommandManager {
final String plugin = this.plugin.getName().toLowerCase();
command.onRegister(this);
for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) {
String key = entry.getKey().toLowerCase();
BukkitRootCommand value = (BukkitRootCommand) entry.getValue();
if (!value.isRegistered) {
commandMap.register(key, plugin, value);
String commandName = entry.getKey().toLowerCase();
BukkitRootCommand bukkitCommand = (BukkitRootCommand) entry.getValue();
if (!bukkitCommand.isRegistered) {
commandMap.register(commandName, plugin, bukkitCommand);
}
value.isRegistered = true;
registeredCommands.put(key, value);
bukkitCommand.isRegistered = true;
registeredCommands.put(commandName, bukkitCommand);
}
}
@@ -49,6 +49,11 @@ public class BukkitRootCommand extends Command implements RootCommand {
this.name = name;
}
@Override
public String getCommandName() {
return name;
}
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
return tabComplete(new BukkitCommandIssuer(manager, sender), alias, args);
@@ -86,19 +91,7 @@ public class BukkitRootCommand extends Command implements RootCommand {
//this.setDescription(command.getDescription());
//this.setUsage(command.getUsage());
}
command.subCommands.keySet().forEach(key -> {
if (key.equals(BaseCommand.DEFAULT) || key.equals(BaseCommand.UNKNOWN)) {
return;
}
BaseCommand regged = this.subCommands.get(key);
if (regged != null) {
this.manager.log(LogLevel.ERROR, "ACF Error: " + command.getName() + " registered subcommand " + key + " for root command " + getName() + " - but it is already defined in " + regged.getName());
this.manager.log(LogLevel.ERROR, "2 subcommands of the same prefix may not be spread over 2 different classes. Ignoring this.");
return;
}
this.subCommands.put(key, command);
});
this.children.add(command);
addChildShared(this.children, this.subCommands, command);
}
@Override
@@ -78,13 +78,13 @@ public class BungeeCommandManager extends CommandManager {
public void registerCommand(BaseCommand command) {
command.onRegister(this);
for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) {
String key = entry.getKey().toLowerCase();
BungeeRootCommand value = (BungeeRootCommand) entry.getValue();
if (!value.isRegistered) {
this.plugin.getProxy().getPluginManager().registerCommand(this.plugin, value);
String commandName = entry.getKey().toLowerCase();
BungeeRootCommand bungeeCommand = (BungeeRootCommand) entry.getValue();
if (!bungeeCommand.isRegistered) {
this.plugin.getProxy().getPluginManager().registerCommand(this.plugin, bungeeCommand);
}
value.isRegistered = true;
registeredCommands.put(key, value);
bungeeCommand.isRegistered = true;
registeredCommands.put(commandName, bungeeCommand);
}
}
@@ -37,7 +37,6 @@ public class BungeeRootCommand extends Command implements RootCommand, TabExecut
private BaseCommand defCommand;
private Map<String, BaseCommand> subCommands = new HashMap<>();
private List<BaseCommand> children = new ArrayList<>();
private String permission;
boolean isRegistered = false;
BungeeRootCommand(BungeeCommandManager manager, String name) {
@@ -46,28 +45,18 @@ public class BungeeRootCommand extends Command implements RootCommand, TabExecut
this.name = name;
}
@Override
public String getCommandName() {
return name;
}
@Override
public void addChild(BaseCommand command) {
if (this.defCommand == null || !command.subCommands.get("__default").isEmpty()) {
this.defCommand = command;
this.permission = command.permission;
//this.setPermissionMessage(command.permissionMessage);
//this.setDescription(command.getDescription());
//this.setUsage(command.getUsage());
}
command.subCommands.keySet().forEach(key -> {
if (key.equals(BaseCommand.DEFAULT) || key.equals(BaseCommand.UNKNOWN)) {
return;
}
BaseCommand regged = this.subCommands.get(key);
if (regged != null) {
this.manager.log(LogLevel.ERROR, "ACF Error: " + command.getName() + " registered subcommand " + key + " for root command " + getName() + " - but it is already defined in " + regged.getName());
this.manager.log(LogLevel.ERROR, "2 subcommands of the same prefix may not be spread over 2 different classes. Ignoring this.");
return;
}
this.subCommands.put(key, command);
});
this.children.add(command);
this.addChildShared(this.children, this.subCommands, command);
}
@Override
@@ -23,7 +23,28 @@
package co.aikar.commands;
import java.util.List;
import java.util.Map;
interface RootCommand {
void addChild(BaseCommand command);
CommandManager getManager();
String getCommandName();
default void addChildShared(List<BaseCommand> children, Map<String, BaseCommand> subCommands, BaseCommand command) {
command.subCommands.keySet().forEach(key -> {
if (key.equals(BaseCommand.DEFAULT) || key.equals(BaseCommand.UNKNOWN)) {
return;
}
BaseCommand regged = subCommands.get(key);
if (regged != null) {
this.getManager().log(LogLevel.ERROR, "ACF Error: " + command.getName() + " registered subcommand " + key + " for root command " + getCommandName() + " - but it is already defined in " + regged.getName());
this.getManager().log(LogLevel.ERROR, "2 subcommands of the same prefix may not be spread over 2 different classes. Ignoring this.");
return;
}
subCommands.put(key, command);
});
children.add(command);
}
}
@@ -89,13 +89,13 @@ public class SpongeCommandManager extends CommandManager {
command.onRegister(this);
for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) {
String key = entry.getKey().toLowerCase();
SpongeRootCommand value = (SpongeRootCommand) entry.getValue();
if (!value.isRegistered) {
Sponge.getCommandManager().register(this.plugin, value, value.name);
String commandName = entry.getKey().toLowerCase();
SpongeRootCommand spongeCommand = (SpongeRootCommand) entry.getValue();
if (!spongeCommand.isRegistered) {
Sponge.getCommandManager().register(this.plugin, spongeCommand, commandName);
}
value.isRegistered = true;
registeredCommands.put(key, value);
spongeCommand.isRegistered = true;
registeredCommands.put(commandName, spongeCommand);
}
}
@@ -56,6 +56,11 @@ public class SpongeRootCommand implements CommandCallable, RootCommand {
this.name = name;
}
@Override
public String getCommandName() {
return name;
}
@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
if(this.execute(new SpongeCommandIssuer(manager, source), this.name, arguments.split(" "))) {
@@ -112,23 +117,8 @@ public class SpongeRootCommand implements CommandCallable, RootCommand {
public void addChild(BaseCommand command) {
if (this.defCommand == null || !command.subCommands.get("__default").isEmpty()) {
this.defCommand = command;
//this.setDescription(command.getDescription());
//this.setUsage(command.getUsage());
//this.setAliases(command.getAliases());
}
command.subCommands.keySet().forEach(key -> {
if (key.equals(BaseCommand.DEFAULT) || key.equals(BaseCommand.UNKNOWN)) {
return;
}
BaseCommand regged = this.subCommands.get(key);
if (regged != null) {
this.manager.log(LogLevel.ERROR, "ACF Error: " + command.getName() + " registered subcommand " + key + " for root command " + this.name + " - but it is already defined in " + regged.getName());
this.manager.log(LogLevel.ERROR, "2 subcommands of the same prefix may not be spread over 2 different classes. Ignoring this.");
return;
}
this.subCommands.put(key, command);
});
this.children.add(command);
addChildShared(this.children, this.subCommands, command);
}
@Override