Don't require BukkitCommandManager to use BukkitCommandSender

This commit is contained in:
Alexander Söderberg 2020-09-15 14:07:00 +02:00
parent d144c3ea8c
commit d78d64329b
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
6 changed files with 84 additions and 37 deletions

View file

@ -32,7 +32,8 @@ import org.bukkit.plugin.Plugin;
import javax.annotation.Nonnull;
import java.util.List;
final class BukkitCommand<C extends BukkitCommandSender> extends org.bukkit.command.Command implements PluginIdentifiableCommand {
final class BukkitCommand<C extends com.intellectualsites.commands.sender.CommandSender>
extends org.bukkit.command.Command implements PluginIdentifiableCommand {
private final CommandComponent<C, ?> command;
private final BukkitCommandManager<C> bukkitCommandManager;
@ -54,7 +55,8 @@ final class BukkitCommand<C extends BukkitCommandSender> extends org.bukkit.comm
for (final String string : strings) {
builder.append(" ").append(string);
}
this.bukkitCommandManager.executeCommand((C) BukkitCommandSender.of(commandSender), builder.toString())
this.bukkitCommandManager.executeCommand(this.bukkitCommandManager.getCommandSenderMapper().apply(commandSender),
builder.toString())
.whenComplete(((commandResult, throwable) -> {
if (throwable != null) {
commandSender.sendMessage(ChatColor.RED + throwable.getCause().getMessage());
@ -79,7 +81,8 @@ final class BukkitCommand<C extends BukkitCommandSender> extends org.bukkit.comm
for (final String string : args) {
builder.append(" ").append(string);
}
return this.bukkitCommandManager.suggest((C) BukkitCommandSender.of(sender), builder.toString());
return this.bukkitCommandManager.suggest(this.bukkitCommandManager.getCommandSenderMapper().apply(sender),
builder.toString());
}
@Override

View file

@ -27,6 +27,7 @@ import com.google.common.reflect.TypeToken;
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
import com.intellectualsites.commands.parsers.WorldComponent;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import javax.annotation.Nonnull;
@ -35,25 +36,32 @@ import java.util.function.Function;
/**
* Command manager for the Bukkit platform, using {@link BukkitCommandSender} as the
* command sender type
*
* @param <C> Command sender type
*/
public class BukkitCommandManager<C extends BukkitCommandSender> extends CommandManager<C, BukkitCommandMeta> {
public class BukkitCommandManager<C extends com.intellectualsites.commands.sender.CommandSender>
extends CommandManager<C, BukkitCommandMeta> {
private final Plugin owningPlugin;
private final Function<CommandSender, C> commandSenderMapper;
/**
* Construct a new Bukkit command manager
*
* @param owningPlugin Plugin that is constructing the manager
* @param commandExecutionCoordinator Coordinator provider
* @param commandSenderMapper Function that maps {@link CommandSender} to the command sender type
* @throws Exception If the construction of the manager fails
*/
public BukkitCommandManager(@Nonnull final Plugin owningPlugin,
@Nonnull final Function<CommandTree<C, BukkitCommandMeta>,
CommandExecutionCoordinator<C, BukkitCommandMeta>> commandExecutionCoordinator)
CommandExecutionCoordinator<C, BukkitCommandMeta>> commandExecutionCoordinator,
@Nonnull final Function<CommandSender, C> commandSenderMapper)
throws Exception {
super(commandExecutionCoordinator, new BukkitPluginRegistrationHandler());
((BukkitPluginRegistrationHandler) this.getCommandRegistrationHandler()).initialize(this);
this.owningPlugin = owningPlugin;
this.commandSenderMapper = commandSenderMapper;
/* Register Bukkit parsers */
this.getParserRegistry().registerParserSupplier(TypeToken.of(World.class), params -> new WorldComponent.WorldParser<>());
@ -80,4 +88,9 @@ public class BukkitCommandManager<C extends BukkitCommandSender> extends Command
return BukkitCommandMetaBuilder.builder().withDescription("").build();
}
@Nonnull
final Function<CommandSender, C> getCommandSenderMapper() {
return this.commandSenderMapper;
}
}

View file

@ -23,11 +23,11 @@
//
package com.intellectualsites.commands.parsers;
import com.intellectualsites.commands.BukkitCommandSender;
import com.intellectualsites.commands.components.CommandComponent;
import com.intellectualsites.commands.components.parser.ComponentParseResult;
import com.intellectualsites.commands.components.parser.ComponentParser;
import com.intellectualsites.commands.context.CommandContext;
import com.intellectualsites.commands.sender.CommandSender;
import org.bukkit.Bukkit;
import org.bukkit.World;
@ -41,7 +41,7 @@ import java.util.stream.Collectors;
*
* @param <C> Command sender type
*/
public class WorldComponent<C extends BukkitCommandSender> extends CommandComponent<C, World> {
public class WorldComponent<C extends CommandSender> extends CommandComponent<C, World> {
protected WorldComponent(final boolean required,
@Nonnull final String name,
@ -57,7 +57,7 @@ public class WorldComponent<C extends BukkitCommandSender> extends CommandCompon
* @return Created builder
*/
@Nonnull
public static <C extends BukkitCommandSender> CommandComponent.Builder<C, World> newBuilder(@Nonnull final String name) {
public static <C extends CommandSender> CommandComponent.Builder<C, World> newBuilder(@Nonnull final String name) {
return new WorldComponent.Builder<>(name);
}
@ -69,7 +69,7 @@ public class WorldComponent<C extends BukkitCommandSender> extends CommandCompon
* @return Created component
*/
@Nonnull
public static <C extends BukkitCommandSender> CommandComponent<C, World> required(@Nonnull final String name) {
public static <C extends CommandSender> CommandComponent<C, World> required(@Nonnull final String name) {
return WorldComponent.<C>newBuilder(name).asRequired().build();
}
@ -81,7 +81,7 @@ public class WorldComponent<C extends BukkitCommandSender> extends CommandCompon
* @return Created component
*/
@Nonnull
public static <C extends BukkitCommandSender> CommandComponent<C, World> optional(@Nonnull final String name) {
public static <C extends CommandSender> CommandComponent<C, World> optional(@Nonnull final String name) {
return WorldComponent.<C>newBuilder(name).asOptional().build();
}
@ -94,13 +94,13 @@ public class WorldComponent<C extends BukkitCommandSender> extends CommandCompon
* @return Created component
*/
@Nonnull
public static <C extends BukkitCommandSender> CommandComponent<C, World> optional(@Nonnull final String name,
@Nonnull final String defaultValue) {
public static <C extends CommandSender> CommandComponent<C, World> optional(@Nonnull final String name,
@Nonnull final String defaultValue) {
return WorldComponent.<C>newBuilder(name).asOptionalWithDefault(defaultValue).build();
}
public static final class Builder<C extends BukkitCommandSender> extends CommandComponent.Builder<C, World> {
public static final class Builder<C extends CommandSender> extends CommandComponent.Builder<C, World> {
protected Builder(@Nonnull final String name) {
super(World.class, name);
@ -114,7 +114,7 @@ public class WorldComponent<C extends BukkitCommandSender> extends CommandCompon
}
public static final class WorldParser<C extends BukkitCommandSender> implements ComponentParser<C, World> {
public static final class WorldParser<C extends CommandSender> implements ComponentParser<C, World> {
@Nonnull
@Override