diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ad3c1d9..222ddc78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added ServerArgument to cloud-bungee - Added ExampleBungeePlugin - Added CaptionKeys to cloud-bungee + - Added BungeeCommandPreprocessor to cloud-bungee ## [1.0.2] - 2020-10-18 diff --git a/cloud-minecraft/cloud-bungee/src/main/java/cloud/commandframework/bungee/BungeeCommandManager.java b/cloud-minecraft/cloud-bungee/src/main/java/cloud/commandframework/bungee/BungeeCommandManager.java index 43e21ff1..56907a1f 100644 --- a/cloud-minecraft/cloud-bungee/src/main/java/cloud/commandframework/bungee/BungeeCommandManager.java +++ b/cloud-minecraft/cloud-bungee/src/main/java/cloud/commandframework/bungee/BungeeCommandManager.java @@ -76,11 +76,14 @@ public class BungeeCommandManager extends CommandManager { 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) { diff --git a/cloud-minecraft/cloud-bungee/src/main/java/cloud/commandframework/bungee/BungeeCommandPreprocessor.java b/cloud-minecraft/cloud-bungee/src/main/java/cloud/commandframework/bungee/BungeeCommandPreprocessor.java new file mode 100644 index 00000000..5c88f7fb --- /dev/null +++ b/cloud-minecraft/cloud-bungee/src/main/java/cloud/commandframework/bungee/BungeeCommandPreprocessor.java @@ -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 + * @since 1.1.0 + */ +final class BungeeCommandPreprocessor implements CommandPreprocessor { + + private final BungeeCommandManager mgr; + + /** + * The Bungee Command Preprocessor for storing Bungee-specific contexts in the command contexts + * + * @param mgr The BungeeCommandManager + */ + BungeeCommandPreprocessor(final @NonNull BungeeCommandManager mgr) { + this.mgr = mgr; + } + + @Override + public void accept(final @NonNull CommandPreprocessingContext context) { + context.getCommandContext().store("ProxyServer", mgr.getOwningPlugin().getProxy()); + } + +} diff --git a/cloud-minecraft/cloud-bungee/src/main/java/cloud/commandframework/bungee/arguments/PlayerArgument.java b/cloud-minecraft/cloud-bungee/src/main/java/cloud/commandframework/bungee/arguments/PlayerArgument.java index e59dfcbc..53f8c4dd 100644 --- a/cloud-minecraft/cloud-bungee/src/main/java/cloud/commandframework/bungee/arguments/PlayerArgument.java +++ b/cloud-minecraft/cloud-bungee/src/main/java/cloud/commandframework/bungee/arguments/PlayerArgument.java @@ -52,7 +52,6 @@ import java.util.stream.Collectors; public final class PlayerArgument extends CommandArgument { private PlayerArgument( - final @NonNull ProxyServer proxyServer, final boolean required, final @NonNull String name, final @Nullable BiFunction, String, List> suggestionProvider, @@ -62,7 +61,7 @@ public final class PlayerArgument extends CommandArgument { super( required, name, - new PlayerParser<>(proxyServer), + new PlayerParser<>(), "", TypeToken.get(ProxiedPlayer.class), suggestionProvider, @@ -74,21 +73,16 @@ public final class PlayerArgument extends CommandArgument { * Create a new argument builder * * @param name Argument name - * @param proxyServer Proxy server instance * @param Command sender type * @return Constructed builder **/ public static CommandArgument.@NonNull Builder newBuilder( - final @NonNull String name, - final @NonNull ProxyServer proxyServer + final @NonNull String name ) { return new Builder( - name, - proxyServer + name ).withParser( - new PlayerParser<>( - proxyServer - ) + new PlayerParser<>() ); } @@ -96,49 +90,40 @@ public final class PlayerArgument extends CommandArgument { * Create a new required player argument * * @param name Argument name - * @param proxyServer Proxy server instance * @param Command sender type * @return Created argument */ public static CommandArgument of( - final @NonNull String name, - final @NonNull ProxyServer proxyServer + final @NonNull String name ) { - return PlayerArgument.newBuilder(name, proxyServer).asRequired().build(); + return PlayerArgument.newBuilder(name).asRequired().build(); } /** * Create a new optional player argument * * @param name Argument name - * @param proxyServer Proxy server instance * @param Command sender type * @return Created argument */ public static CommandArgument optional( - final @NonNull String name, - final @NonNull ProxyServer proxyServer + final @NonNull String name ) { - return PlayerArgument.newBuilder(name, proxyServer).asOptional().build(); + return PlayerArgument.newBuilder(name).asOptional().build(); } public static final class Builder extends CommandArgument.Builder { - 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 extends CommandArgument { public static final class PlayerParser implements ArgumentParser { - 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 extends CommandArgument { commandContext )); } - final ProxiedPlayer player = this.proxyServer.getPlayer(input); + final ProxiedPlayer player = commandContext.get("ProxyServer").getPlayer(input); if (player == null) { return ArgumentParseResult.failure( new PlayerParseException( @@ -194,7 +166,11 @@ public final class PlayerArgument extends CommandArgument { final @NonNull CommandContext commandContext, final @NonNull String input ) { - return this.proxyServer.getPlayers().stream().map(ProxiedPlayer::getDisplayName).collect(Collectors.toList()); + return commandContext.get("ProxyServer") + .getPlayers() + .stream() + .map(ProxiedPlayer::getDisplayName) + .collect(Collectors.toList()); } @Override diff --git a/cloud-minecraft/cloud-bungee/src/main/java/cloud/commandframework/bungee/arguments/ServerArgument.java b/cloud-minecraft/cloud-bungee/src/main/java/cloud/commandframework/bungee/arguments/ServerArgument.java index c20c4d47..29c93031 100644 --- a/cloud-minecraft/cloud-bungee/src/main/java/cloud/commandframework/bungee/arguments/ServerArgument.java +++ b/cloud-minecraft/cloud-bungee/src/main/java/cloud/commandframework/bungee/arguments/ServerArgument.java @@ -52,7 +52,6 @@ import java.util.function.BiFunction; public final class ServerArgument extends CommandArgument { private ServerArgument( - final @NonNull ProxyServer proxyServer, final boolean required, final @NonNull String name, final @Nullable BiFunction, String, List> suggestionsProvider, @@ -62,7 +61,7 @@ public final class ServerArgument extends CommandArgument { super( required, name, - new ServerParser<>(proxyServer), + new ServerParser<>(), "", TypeToken.get(ServerInfo.class), suggestionsProvider, @@ -74,21 +73,16 @@ public final class ServerArgument extends CommandArgument { * Create a new argument builder * * @param name Argument name - * @param proxyServer Proxy server instance * @param Command sender type * @return Constructed builder */ public static CommandArgument.@NonNull Builder newBuilder( - final @NonNull String name, - final @NonNull ProxyServer proxyServer + final @NonNull String name ) { return new Builder( - name, - proxyServer + name ).withParser( - new ServerParser<>( - proxyServer - ) + new ServerParser<>() ); } @@ -96,48 +90,39 @@ public final class ServerArgument extends CommandArgument { * Create a new required server argument * * @param name Argument name - * @param proxyServer Proxy server instance * @param Command sender type * @return Created argument */ public static @NonNull CommandArgument of( - final @NonNull String name, - final @NonNull ProxyServer proxyServer + final @NonNull String name ) { - return ServerArgument.newBuilder(name, proxyServer).asRequired().build(); + return ServerArgument.newBuilder(name).asRequired().build(); } /** * Create a new optional server argument * * @param name Argument name - * @param proxyServer Proxy server instance * @param Command sender type * @return Created argument */ public static @NonNull CommandArgument optional( - final @NonNull String name, - final @NonNull ProxyServer proxyServer + final @NonNull String name ) { - return ServerArgument.newBuilder(name, proxyServer).asOptional().build(); + return ServerArgument.newBuilder(name).asOptional().build(); } public static final class Builder extends CommandArgument.Builder { - 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 extends CommandArgument { public static final class ServerParser implements ArgumentParser { - 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 extends CommandArgument { commandContext )); } - final ServerInfo server = this.proxyServer.getServerInfo(input); + final ServerInfo server = commandContext.get("ProxyServer").getServerInfo(input); if (server == null) { return ArgumentParseResult.failure( new ServerParseException( @@ -192,7 +164,7 @@ public final class ServerArgument extends CommandArgument { final @NonNull CommandContext commandContext, final @NonNull String input ) { - return new ArrayList<>(this.proxyServer.getServers().keySet()); + return new ArrayList<>(commandContext.get("ProxyServer").getServers().keySet()); } } diff --git a/examples/example-bungee/src/main/java/cloud/commandframework/examples/bungee/ExamplePlugin.java b/examples/example-bungee/src/main/java/cloud/commandframework/examples/bungee/ExamplePlugin.java index 8c93c41e..5478c6a9 100644 --- a/examples/example-bungee/src/main/java/cloud/commandframework/examples/bungee/ExamplePlugin.java +++ b/examples/example-bungee/src/main/java/cloud/commandframework/examples/bungee/ExamplePlugin.java @@ -111,8 +111,8 @@ public final class ExamplePlugin extends Plugin { .meta("description", "Confirm a pending command") .handler(this.confirmationManager.createConfirmationExecutionHandler())); - final CommandArgument playerArgument = PlayerArgument.of("player", this.getProxy()); - final CommandArgument serverArgument = ServerArgument.of("server", this.getProxy()); + final CommandArgument playerArgument = PlayerArgument.of("player"); + final CommandArgument serverArgument = ServerArgument.of("server"); // // Create a player command