Add BungeeCommandPreprocessor

This commit is contained in:
jmp 2020-10-20 16:38:00 -07:00 committed by Alexander Söderberg
parent f73b713658
commit 7cb1661f42
6 changed files with 89 additions and 82 deletions

View file

@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added ServerArgument to cloud-bungee - Added ServerArgument to cloud-bungee
- Added ExampleBungeePlugin - Added ExampleBungeePlugin
- Added CaptionKeys to cloud-bungee - Added CaptionKeys to cloud-bungee
- Added BungeeCommandPreprocessor to cloud-bungee
## [1.0.2] - 2020-10-18 ## [1.0.2] - 2020-10-18

View file

@ -76,11 +76,14 @@ public class BungeeCommandManager<C> extends CommandManager<C> {
this.commandSenderMapper = commandSenderMapper; this.commandSenderMapper = commandSenderMapper;
this.backwardsCommandSenderMapper = backwardsCommandSenderMapper; this.backwardsCommandSenderMapper = backwardsCommandSenderMapper;
/* Register Bungee Preprocessor */
this.registerCommandPreProcessor(new BungeeCommandPreprocessor<>(this));
/* Register Bungee Parsers */ /* Register Bungee Parsers */
this.getParserRegistry().registerParserSupplier(TypeToken.get(ProxiedPlayer.class), parserParameters -> this.getParserRegistry().registerParserSupplier(TypeToken.get(ProxiedPlayer.class), parserParameters ->
new PlayerArgument.PlayerParser<>(owningPlugin.getProxy())); new PlayerArgument.PlayerParser<>());
this.getParserRegistry().registerParserSupplier(TypeToken.get(ServerInfo.class), parserParameters -> this.getParserRegistry().registerParserSupplier(TypeToken.get(ServerInfo.class), parserParameters ->
new ServerArgument.ServerParser<>(owningPlugin.getProxy())); new ServerArgument.ServerParser<>());
/* Register default captions */ /* Register default captions */
if (this.getCaptionRegistry() instanceof FactoryDelegatingCaptionRegistry) { if (this.getCaptionRegistry() instanceof FactoryDelegatingCaptionRegistry) {

View file

@ -0,0 +1,55 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.bungee;
import cloud.commandframework.execution.preprocessor.CommandPreprocessingContext;
import cloud.commandframework.execution.preprocessor.CommandPreprocessor;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
* Command preprocessor which decorates incoming {@link cloud.commandframework.context.CommandContext}
* with Bungee specific objects
*
* @param <C>
* @since 1.1.0
*/
final class BungeeCommandPreprocessor<C> implements CommandPreprocessor<C> {
private final BungeeCommandManager<C> mgr;
/**
* The Bungee Command Preprocessor for storing Bungee-specific contexts in the command contexts
*
* @param mgr The BungeeCommandManager
*/
BungeeCommandPreprocessor(final @NonNull BungeeCommandManager<C> mgr) {
this.mgr = mgr;
}
@Override
public void accept(final @NonNull CommandPreprocessingContext<C> context) {
context.getCommandContext().store("ProxyServer", mgr.getOwningPlugin().getProxy());
}
}

View file

@ -52,7 +52,6 @@ import java.util.stream.Collectors;
public final class PlayerArgument<C> extends CommandArgument<C, ProxiedPlayer> { public final class PlayerArgument<C> extends CommandArgument<C, ProxiedPlayer> {
private PlayerArgument( private PlayerArgument(
final @NonNull ProxyServer proxyServer,
final boolean required, final boolean required,
final @NonNull String name, final @NonNull String name,
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionProvider, final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionProvider,
@ -62,7 +61,7 @@ public final class PlayerArgument<C> extends CommandArgument<C, ProxiedPlayer> {
super( super(
required, required,
name, name,
new PlayerParser<>(proxyServer), new PlayerParser<>(),
"", "",
TypeToken.get(ProxiedPlayer.class), TypeToken.get(ProxiedPlayer.class),
suggestionProvider, suggestionProvider,
@ -74,21 +73,16 @@ public final class PlayerArgument<C> extends CommandArgument<C, ProxiedPlayer> {
* Create a new argument builder * Create a new argument builder
* *
* @param name Argument name * @param name Argument name
* @param proxyServer Proxy server instance
* @param <C> Command sender type * @param <C> Command sender type
* @return Constructed builder * @return Constructed builder
**/ **/
public static <C> CommandArgument.@NonNull Builder<C, ProxiedPlayer> newBuilder( public static <C> CommandArgument.@NonNull Builder<C, ProxiedPlayer> newBuilder(
final @NonNull String name, final @NonNull String name
final @NonNull ProxyServer proxyServer
) { ) {
return new Builder<C>( return new Builder<C>(
name, name
proxyServer
).withParser( ).withParser(
new PlayerParser<>( new PlayerParser<>()
proxyServer
)
); );
} }
@ -96,49 +90,40 @@ public final class PlayerArgument<C> extends CommandArgument<C, ProxiedPlayer> {
* Create a new required player argument * Create a new required player argument
* *
* @param name Argument name * @param name Argument name
* @param proxyServer Proxy server instance
* @param <C> Command sender type * @param <C> Command sender type
* @return Created argument * @return Created argument
*/ */
public static <C> CommandArgument<C, ProxiedPlayer> of( public static <C> CommandArgument<C, ProxiedPlayer> of(
final @NonNull String name, final @NonNull String name
final @NonNull ProxyServer proxyServer
) { ) {
return PlayerArgument.<C>newBuilder(name, proxyServer).asRequired().build(); return PlayerArgument.<C>newBuilder(name).asRequired().build();
} }
/** /**
* Create a new optional player argument * Create a new optional player argument
* *
* @param name Argument name * @param name Argument name
* @param proxyServer Proxy server instance
* @param <C> Command sender type * @param <C> Command sender type
* @return Created argument * @return Created argument
*/ */
public static <C> CommandArgument<C, ProxiedPlayer> optional( public static <C> CommandArgument<C, ProxiedPlayer> optional(
final @NonNull String name, final @NonNull String name
final @NonNull ProxyServer proxyServer
) { ) {
return PlayerArgument.<C>newBuilder(name, proxyServer).asOptional().build(); return PlayerArgument.<C>newBuilder(name).asOptional().build();
} }
public static final class Builder<C> extends CommandArgument.Builder<C, ProxiedPlayer> { public static final class Builder<C> extends CommandArgument.Builder<C, ProxiedPlayer> {
private final ProxyServer proxyServer;
private Builder( private Builder(
final @NonNull String name, final @NonNull String name
final @NonNull ProxyServer proxyServer
) { ) {
super(TypeToken.get(ProxiedPlayer.class), name); super(TypeToken.get(ProxiedPlayer.class), name);
this.proxyServer = proxyServer;
} }
@Override @Override
public @NonNull CommandArgument<@NonNull C, @NonNull ProxiedPlayer> build() { public @NonNull CommandArgument<@NonNull C, @NonNull ProxiedPlayer> build() {
return new PlayerArgument<>( return new PlayerArgument<>(
this.proxyServer,
this.isRequired(), this.isRequired(),
this.getName(), this.getName(),
this.getSuggestionsProvider(), this.getSuggestionsProvider(),
@ -151,19 +136,6 @@ public final class PlayerArgument<C> extends CommandArgument<C, ProxiedPlayer> {
public static final class PlayerParser<C> implements ArgumentParser<C, ProxiedPlayer> { public static final class PlayerParser<C> implements ArgumentParser<C, ProxiedPlayer> {
private final ProxyServer proxyServer;
/**
* Create a new player parser
*
* @param proxyServer Proxy server instance
*/
public PlayerParser(
final @NonNull ProxyServer proxyServer
) {
this.proxyServer = proxyServer;
}
@Override @Override
public @NonNull ArgumentParseResult<@NonNull ProxiedPlayer> parse( public @NonNull ArgumentParseResult<@NonNull ProxiedPlayer> parse(
@NonNull final CommandContext<@NonNull C> commandContext, @NonNull final CommandContext<@NonNull C> commandContext,
@ -176,7 +148,7 @@ public final class PlayerArgument<C> extends CommandArgument<C, ProxiedPlayer> {
commandContext commandContext
)); ));
} }
final ProxiedPlayer player = this.proxyServer.getPlayer(input); final ProxiedPlayer player = commandContext.<ProxyServer>get("ProxyServer").getPlayer(input);
if (player == null) { if (player == null) {
return ArgumentParseResult.failure( return ArgumentParseResult.failure(
new PlayerParseException( new PlayerParseException(
@ -194,7 +166,11 @@ public final class PlayerArgument<C> extends CommandArgument<C, ProxiedPlayer> {
final @NonNull CommandContext<C> commandContext, final @NonNull CommandContext<C> commandContext,
final @NonNull String input final @NonNull String input
) { ) {
return this.proxyServer.getPlayers().stream().map(ProxiedPlayer::getDisplayName).collect(Collectors.toList()); return commandContext.<ProxyServer>get("ProxyServer")
.getPlayers()
.stream()
.map(ProxiedPlayer::getDisplayName)
.collect(Collectors.toList());
} }
@Override @Override

View file

@ -52,7 +52,6 @@ import java.util.function.BiFunction;
public final class ServerArgument<C> extends CommandArgument<C, ServerInfo> { public final class ServerArgument<C> extends CommandArgument<C, ServerInfo> {
private ServerArgument( private ServerArgument(
final @NonNull ProxyServer proxyServer,
final boolean required, final boolean required,
final @NonNull String name, final @NonNull String name,
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider, final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider,
@ -62,7 +61,7 @@ public final class ServerArgument<C> extends CommandArgument<C, ServerInfo> {
super( super(
required, required,
name, name,
new ServerParser<>(proxyServer), new ServerParser<>(),
"", "",
TypeToken.get(ServerInfo.class), TypeToken.get(ServerInfo.class),
suggestionsProvider, suggestionsProvider,
@ -74,21 +73,16 @@ public final class ServerArgument<C> extends CommandArgument<C, ServerInfo> {
* Create a new argument builder * Create a new argument builder
* *
* @param name Argument name * @param name Argument name
* @param proxyServer Proxy server instance
* @param <C> Command sender type * @param <C> Command sender type
* @return Constructed builder * @return Constructed builder
*/ */
public static <C> CommandArgument.@NonNull Builder<C, ServerInfo> newBuilder( public static <C> CommandArgument.@NonNull Builder<C, ServerInfo> newBuilder(
final @NonNull String name, final @NonNull String name
final @NonNull ProxyServer proxyServer
) { ) {
return new Builder<C>( return new Builder<C>(
name, name
proxyServer
).withParser( ).withParser(
new ServerParser<>( new ServerParser<>()
proxyServer
)
); );
} }
@ -96,48 +90,39 @@ public final class ServerArgument<C> extends CommandArgument<C, ServerInfo> {
* Create a new required server argument * Create a new required server argument
* *
* @param name Argument name * @param name Argument name
* @param proxyServer Proxy server instance
* @param <C> Command sender type * @param <C> Command sender type
* @return Created argument * @return Created argument
*/ */
public static <C> @NonNull CommandArgument<C, ServerInfo> of( public static <C> @NonNull CommandArgument<C, ServerInfo> of(
final @NonNull String name, final @NonNull String name
final @NonNull ProxyServer proxyServer
) { ) {
return ServerArgument.<C>newBuilder(name, proxyServer).asRequired().build(); return ServerArgument.<C>newBuilder(name).asRequired().build();
} }
/** /**
* Create a new optional server argument * Create a new optional server argument
* *
* @param name Argument name * @param name Argument name
* @param proxyServer Proxy server instance
* @param <C> Command sender type * @param <C> Command sender type
* @return Created argument * @return Created argument
*/ */
public static <C> @NonNull CommandArgument<C, ServerInfo> optional( public static <C> @NonNull CommandArgument<C, ServerInfo> optional(
final @NonNull String name, final @NonNull String name
final @NonNull ProxyServer proxyServer
) { ) {
return ServerArgument.<C>newBuilder(name, proxyServer).asOptional().build(); return ServerArgument.<C>newBuilder(name).asOptional().build();
} }
public static final class Builder<C> extends CommandArgument.Builder<C, ServerInfo> { public static final class Builder<C> extends CommandArgument.Builder<C, ServerInfo> {
private final ProxyServer proxyServer;
private Builder( private Builder(
final @NonNull String name, final @NonNull String name
final @NonNull ProxyServer proxyServer
) { ) {
super(TypeToken.get(ServerInfo.class), name); super(TypeToken.get(ServerInfo.class), name);
this.proxyServer = proxyServer;
} }
@Override @Override
public @NonNull CommandArgument<@NonNull C, @NonNull ServerInfo> build() { public @NonNull CommandArgument<@NonNull C, @NonNull ServerInfo> build() {
return new ServerArgument<>( return new ServerArgument<>(
this.proxyServer,
this.isRequired(), this.isRequired(),
this.getName(), this.getName(),
this.getSuggestionsProvider(), this.getSuggestionsProvider(),
@ -149,19 +134,6 @@ public final class ServerArgument<C> extends CommandArgument<C, ServerInfo> {
public static final class ServerParser<C> implements ArgumentParser<C, ServerInfo> { public static final class ServerParser<C> implements ArgumentParser<C, ServerInfo> {
private final ProxyServer proxyServer;
/**
* Create a new server parser
*
* @param proxyServer Proxy server instance
*/
public ServerParser(
final @NonNull ProxyServer proxyServer
) {
this.proxyServer = proxyServer;
}
@Override @Override
public @NonNull ArgumentParseResult<@NonNull ServerInfo> parse( public @NonNull ArgumentParseResult<@NonNull ServerInfo> parse(
@NonNull final CommandContext<@NonNull C> commandContext, @NonNull final CommandContext<@NonNull C> commandContext,
@ -174,7 +146,7 @@ public final class ServerArgument<C> extends CommandArgument<C, ServerInfo> {
commandContext commandContext
)); ));
} }
final ServerInfo server = this.proxyServer.getServerInfo(input); final ServerInfo server = commandContext.<ProxyServer>get("ProxyServer").getServerInfo(input);
if (server == null) { if (server == null) {
return ArgumentParseResult.failure( return ArgumentParseResult.failure(
new ServerParseException( new ServerParseException(
@ -192,7 +164,7 @@ public final class ServerArgument<C> extends CommandArgument<C, ServerInfo> {
final @NonNull CommandContext<C> commandContext, final @NonNull CommandContext<C> commandContext,
final @NonNull String input final @NonNull String input
) { ) {
return new ArrayList<>(this.proxyServer.getServers().keySet()); return new ArrayList<>(commandContext.<ProxyServer>get("ProxyServer").getServers().keySet());
} }
} }

View file

@ -111,8 +111,8 @@ public final class ExamplePlugin extends Plugin {
.meta("description", "Confirm a pending command") .meta("description", "Confirm a pending command")
.handler(this.confirmationManager.createConfirmationExecutionHandler())); .handler(this.confirmationManager.createConfirmationExecutionHandler()));
final CommandArgument<CommandSender, ProxiedPlayer> playerArgument = PlayerArgument.of("player", this.getProxy()); final CommandArgument<CommandSender, ProxiedPlayer> playerArgument = PlayerArgument.of("player");
final CommandArgument<CommandSender, ServerInfo> serverArgument = ServerArgument.of("server", this.getProxy()); final CommandArgument<CommandSender, ServerInfo> serverArgument = ServerArgument.of("server");
// //
// Create a player command // Create a player command