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

@ -76,11 +76,14 @@ public class BungeeCommandManager<C> extends CommandManager<C> {
this.commandSenderMapper = commandSenderMapper;
this.backwardsCommandSenderMapper = backwardsCommandSenderMapper;
/* Register Bungee Preprocessor */
this.registerCommandPreProcessor(new BungeeCommandPreprocessor<>(this));
/* Register Bungee Parsers */
this.getParserRegistry().registerParserSupplier(TypeToken.get(ProxiedPlayer.class), parserParameters ->
new PlayerArgument.PlayerParser<>(owningPlugin.getProxy()));
new PlayerArgument.PlayerParser<>());
this.getParserRegistry().registerParserSupplier(TypeToken.get(ServerInfo.class), parserParameters ->
new ServerArgument.ServerParser<>(owningPlugin.getProxy()));
new ServerArgument.ServerParser<>());
/* Register default captions */
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> {
private PlayerArgument(
final @NonNull ProxyServer proxyServer,
final boolean required,
final @NonNull String name,
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionProvider,
@ -62,7 +61,7 @@ public final class PlayerArgument<C> extends CommandArgument<C, ProxiedPlayer> {
super(
required,
name,
new PlayerParser<>(proxyServer),
new PlayerParser<>(),
"",
TypeToken.get(ProxiedPlayer.class),
suggestionProvider,
@ -74,21 +73,16 @@ public final class PlayerArgument<C> extends CommandArgument<C, ProxiedPlayer> {
* Create a new argument builder
*
* @param name Argument name
* @param proxyServer Proxy server instance
* @param <C> Command sender type
* @return Constructed builder
**/
public static <C> CommandArgument.@NonNull Builder<C, ProxiedPlayer> newBuilder(
final @NonNull String name,
final @NonNull ProxyServer proxyServer
final @NonNull String name
) {
return new Builder<C>(
name,
proxyServer
name
).withParser(
new PlayerParser<>(
proxyServer
)
new PlayerParser<>()
);
}
@ -96,49 +90,40 @@ public final class PlayerArgument<C> extends CommandArgument<C, ProxiedPlayer> {
* Create a new required player argument
*
* @param name Argument name
* @param proxyServer Proxy server instance
* @param <C> Command sender type
* @return Created argument
*/
public static <C> CommandArgument<C, ProxiedPlayer> of(
final @NonNull String name,
final @NonNull ProxyServer proxyServer
final @NonNull String name
) {
return PlayerArgument.<C>newBuilder(name, proxyServer).asRequired().build();
return PlayerArgument.<C>newBuilder(name).asRequired().build();
}
/**
* Create a new optional player argument
*
* @param name Argument name
* @param proxyServer Proxy server instance
* @param <C> Command sender type
* @return Created argument
*/
public static <C> CommandArgument<C, ProxiedPlayer> optional(
final @NonNull String name,
final @NonNull ProxyServer proxyServer
final @NonNull String name
) {
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> {
private final ProxyServer proxyServer;
private Builder(
final @NonNull String name,
final @NonNull ProxyServer proxyServer
final @NonNull String name
) {
super(TypeToken.get(ProxiedPlayer.class), name);
this.proxyServer = proxyServer;
}
@Override
public @NonNull CommandArgument<@NonNull C, @NonNull ProxiedPlayer> build() {
return new PlayerArgument<>(
this.proxyServer,
this.isRequired(),
this.getName(),
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> {
private final ProxyServer proxyServer;
/**
* Create a new player parser
*
* @param proxyServer Proxy server instance
*/
public PlayerParser(
final @NonNull ProxyServer proxyServer
) {
this.proxyServer = proxyServer;
}
@Override
public @NonNull ArgumentParseResult<@NonNull ProxiedPlayer> parse(
@NonNull final CommandContext<@NonNull C> commandContext,
@ -176,7 +148,7 @@ public final class PlayerArgument<C> extends CommandArgument<C, ProxiedPlayer> {
commandContext
));
}
final ProxiedPlayer player = this.proxyServer.getPlayer(input);
final ProxiedPlayer player = commandContext.<ProxyServer>get("ProxyServer").getPlayer(input);
if (player == null) {
return ArgumentParseResult.failure(
new PlayerParseException(
@ -194,7 +166,11 @@ public final class PlayerArgument<C> extends CommandArgument<C, ProxiedPlayer> {
final @NonNull CommandContext<C> commandContext,
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

View file

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