Clean up generics (and get rid of the type parameter for command meta data)

This commit is contained in:
Alexander Söderberg 2020-09-19 12:14:09 +02:00
parent 1a85251fc6
commit ccd0e8ae0e
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
50 changed files with 577 additions and 596 deletions

View file

@ -30,7 +30,6 @@ import com.intellectualsites.commands.exceptions.InvalidCommandSenderException;
import com.intellectualsites.commands.exceptions.InvalidSyntaxException;
import com.intellectualsites.commands.exceptions.NoPermissionException;
import com.intellectualsites.commands.exceptions.NoSuchCommandException;
import com.intellectualsites.commands.meta.SimpleCommandMeta;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.ComponentBuilder;
@ -42,16 +41,16 @@ import javax.annotation.Nonnull;
public final class BungeeCommand<C> extends Command implements TabExecutor {
private static final String MESSAGE_NO_PERMS =
"I'm sorry, but you do not have permission to perform this command. "
+ "Please contact the server administrators if you believe that this is in error.";
"I'm sorry, but you do not have permission to perform this command. "
+ "Please contact the server administrators if you believe that this is in error.";
private static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command. Type \"/help\" for help.";
private final BungeeCommandManager<C> bungeeCommandManager;
private final CommandArgument<C, ?> command;
private final com.intellectualsites.commands.Command<C, SimpleCommandMeta> cloudCommand;
private final com.intellectualsites.commands.Command<C> cloudCommand;
@SuppressWarnings("unchecked")
BungeeCommand(@Nonnull final com.intellectualsites.commands.Command<C, SimpleCommandMeta> cloudCommand,
BungeeCommand(@Nonnull final com.intellectualsites.commands.Command<C> cloudCommand,
@Nonnull final CommandArgument<C, ?> command,
@Nonnull final BungeeCommandManager<C> bungeeCommandManager) {
super(command.getName(),
@ -71,41 +70,41 @@ public final class BungeeCommand<C> extends Command implements TabExecutor {
}
this.bungeeCommandManager.executeCommand(this.bungeeCommandManager.getCommandSenderMapper().apply(commandSender),
builder.toString())
.whenComplete(((commandResult, throwable) -> {
if (throwable != null) {
if (throwable instanceof InvalidSyntaxException) {
commandSender.sendMessage(
new ComponentBuilder("Invalid Command Syntax. Correct command syntax is: ")
.color(ChatColor.RED)
.append("/")
.color(ChatColor.GRAY)
.append(((InvalidSyntaxException) throwable).getCorrectSyntax())
.color(ChatColor.GRAY)
.create()
);
} else if (throwable instanceof InvalidCommandSenderException) {
commandSender.sendMessage(new ComponentBuilder(throwable.getMessage())
.color(ChatColor.RED)
.create());
} else if (throwable instanceof NoPermissionException) {
commandSender.sendMessage(new ComponentBuilder(MESSAGE_NO_PERMS)
.color(ChatColor.WHITE)
.create());
} else if (throwable instanceof NoSuchCommandException) {
commandSender.sendMessage(new ComponentBuilder(MESSAGE_UNKNOWN_COMMAND)
.color(ChatColor.WHITE)
.create());
} else if (throwable instanceof ArgumentParseException) {
commandSender.sendMessage(new ComponentBuilder("Invalid Command Argument: ")
.color(ChatColor.GRAY)
.append(throwable.getCause().getMessage())
.create());
} else {
commandSender.sendMessage(new ComponentBuilder(throwable.getMessage()).create());
throwable.printStackTrace();
}
}
}));
.whenComplete(((commandResult, throwable) -> {
if (throwable != null) {
if (throwable instanceof InvalidSyntaxException) {
commandSender.sendMessage(
new ComponentBuilder("Invalid Command Syntax. Correct command syntax is: ")
.color(ChatColor.RED)
.append("/")
.color(ChatColor.GRAY)
.append(((InvalidSyntaxException) throwable).getCorrectSyntax())
.color(ChatColor.GRAY)
.create()
);
} else if (throwable instanceof InvalidCommandSenderException) {
commandSender.sendMessage(new ComponentBuilder(throwable.getMessage())
.color(ChatColor.RED)
.create());
} else if (throwable instanceof NoPermissionException) {
commandSender.sendMessage(new ComponentBuilder(MESSAGE_NO_PERMS)
.color(ChatColor.WHITE)
.create());
} else if (throwable instanceof NoSuchCommandException) {
commandSender.sendMessage(new ComponentBuilder(MESSAGE_UNKNOWN_COMMAND)
.color(ChatColor.WHITE)
.create());
} else if (throwable instanceof ArgumentParseException) {
commandSender.sendMessage(new ComponentBuilder("Invalid Command Argument: ")
.color(ChatColor.GRAY)
.append(throwable.getCause().getMessage())
.create());
} else {
commandSender.sendMessage(new ComponentBuilder(throwable.getMessage()).create());
throwable.printStackTrace();
}
}
}));
}
@Override

View file

@ -33,7 +33,7 @@ import net.md_5.bungee.api.plugin.Plugin;
import javax.annotation.Nonnull;
import java.util.function.Function;
public class BungeeCommandManager<C> extends CommandManager<C, SimpleCommandMeta> {
public class BungeeCommandManager<C> extends CommandManager<C> {
private final Plugin owningPlugin;
private final Function<CommandSender, C> commandSenderMapper;
@ -49,8 +49,8 @@ public class BungeeCommandManager<C> extends CommandManager<C, SimpleCommandMeta
* @throws Exception If the construction of the manager fails
*/
public BungeeCommandManager(@Nonnull final Plugin owningPlugin,
@Nonnull final Function<CommandTree<C, SimpleCommandMeta>,
CommandExecutionCoordinator<C, SimpleCommandMeta>> commandExecutionCoordinator,
@Nonnull final Function<CommandTree<C>,
CommandExecutionCoordinator<C>> commandExecutionCoordinator,
@Nonnull final Function<CommandSender, C> commandSenderMapper,
@Nonnull final Function<C, CommandSender> backwardsCommandSenderMapper)
throws Exception {
@ -63,7 +63,7 @@ public class BungeeCommandManager<C> extends CommandManager<C, SimpleCommandMeta
@Override
public final boolean hasPermission(@Nonnull final C sender,
@Nonnull final String permission) {
@Nonnull final String permission) {
return this.backwardsCommandSenderMapper.apply(sender).hasPermission(permission);
}

View file

@ -26,14 +26,12 @@ package com.intellectualsites.commands.bungee;
import com.intellectualsites.commands.Command;
import com.intellectualsites.commands.arguments.CommandArgument;
import com.intellectualsites.commands.internal.CommandRegistrationHandler;
import com.intellectualsites.commands.meta.SimpleCommandMeta;
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
final class BungeePluginRegistrationHandler<C> implements
CommandRegistrationHandler<SimpleCommandMeta> {
final class BungeePluginRegistrationHandler<C> implements CommandRegistrationHandler {
private final Map<CommandArgument<?, ?>, net.md_5.bungee.api.plugin.Command> registeredCommands = new HashMap<>();
@ -47,19 +45,19 @@ final class BungeePluginRegistrationHandler<C> implements
}
@Override
public boolean registerCommand(@Nonnull final Command<?, SimpleCommandMeta> command) {
public boolean registerCommand(@Nonnull final Command<?> command) {
/* We only care about the root command argument */
final CommandArgument<?, ?> commandArgument = command.getArguments().get(0);
if (this.registeredCommands.containsKey(commandArgument)) {
return false;
}
@SuppressWarnings("unchecked") final BungeeCommand<C> bungeeCommand = new BungeeCommand<>(
(Command<C, SimpleCommandMeta>) command,
(Command<C>) command,
(CommandArgument<C, ?>) commandArgument,
this.bungeeCommandManager);
this.registeredCommands.put(commandArgument, bungeeCommand);
this.bungeeCommandManager.getOwningPlugin().getProxy().getPluginManager()
.registerCommand(this.bungeeCommandManager.getOwningPlugin(), bungeeCommand);
.registerCommand(this.bungeeCommandManager.getOwningPlugin(), bungeeCommand);
return true;
}