diff --git a/CHANGELOG.md b/CHANGELOG.md index a272678f..c562237f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added LocationArgument to cloud-bukkit - Added ServerArgument to cloud-velocity - Added LockableCommandManager to cloud-core + - Added VelocityCommandPreprocessor to cloud-velocity ## [1.0.2] - 2020-10-18 diff --git a/README.md b/README.md index 6e110cba..c0208240 100644 --- a/README.md +++ b/README.md @@ -185,7 +185,7 @@ repositories { ```groovy dependencies { - implementation 'cloud.commandframework:cloud-PLATFORM:1.0.2' + implementation 'cloud.commandframework:cloud-PLATFORM:1.1.0-SNAPSHOT' } ``` diff --git a/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/TextColorArgument.java b/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/TextColorArgument.java index e1d93eab..009a57b1 100644 --- a/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/TextColorArgument.java +++ b/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/TextColorArgument.java @@ -47,7 +47,7 @@ import java.util.regex.Pattern; /** * Parser for color codes. *

- * Accepts {@link NamedTextColor NamedTextColors}, Legacy Minecraft {@literal &} color codes, Hex Codes (#RRGGBB) + * Accepts {@link NamedTextColor NamedTextColors}, Legacy Minecraft {@literal &} color codes, and Hex Codes (#RRGGBB or RRGGBB) * * @param Command sender type * @since 1.1.0 diff --git a/cloud-minecraft/cloud-velocity/src/main/java/cloud/commandframework/velocity/VelocityCommandManager.java b/cloud-minecraft/cloud-velocity/src/main/java/cloud/commandframework/velocity/VelocityCommandManager.java index 0ff8ec21..994bcd58 100644 --- a/cloud-minecraft/cloud-velocity/src/main/java/cloud/commandframework/velocity/VelocityCommandManager.java +++ b/cloud-minecraft/cloud-velocity/src/main/java/cloud/commandframework/velocity/VelocityCommandManager.java @@ -89,11 +89,14 @@ public class VelocityCommandManager extends CommandManager { this.commandSenderMapper = commandSenderMapper; this.backwardsCommandSenderMapper = backwardsCommandSenderMapper; + /* Register Velocity Preprocessor */ + this.registerCommandPreProcessor(new VelocityCommandPreprocessor<>(this)); + /* Register Velocity Parsers */ this.getParserRegistry().registerParserSupplier(TypeToken.get(Player.class), parserParameters -> - new PlayerArgument.PlayerParser<>(proxyServer)); + new PlayerArgument.PlayerParser<>()); this.getParserRegistry().registerParserSupplier(TypeToken.get(RegisteredServer.class), parserParameters -> - new ServerArgument.ServerParser<>(proxyServer)); + new ServerArgument.ServerParser<>()); /* Register default captions */ if (this.getCaptionRegistry() instanceof FactoryDelegatingCaptionRegistry) { diff --git a/cloud-minecraft/cloud-velocity/src/main/java/cloud/commandframework/velocity/VelocityCommandPreprocessor.java b/cloud-minecraft/cloud-velocity/src/main/java/cloud/commandframework/velocity/VelocityCommandPreprocessor.java new file mode 100644 index 00000000..0dfa4f40 --- /dev/null +++ b/cloud-minecraft/cloud-velocity/src/main/java/cloud/commandframework/velocity/VelocityCommandPreprocessor.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.velocity; + +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 Velocity specific objects + * + * @param + * @since 1.1.0 + */ +final class VelocityCommandPreprocessor implements CommandPreprocessor { + + private final VelocityCommandManager mgr; + + /** + * The Velocity Command Preprocessor for storing Velocity-specific contexts in the command contexts + * + * @param mgr The VelocityCommandManager + */ + VelocityCommandPreprocessor(final @NonNull VelocityCommandManager mgr) { + this.mgr = mgr; + } + + @Override + public void accept(final @NonNull CommandPreprocessingContext context) { + context.getCommandContext().store("ProxyServer", mgr.getProxyServer()); + } + +} diff --git a/cloud-minecraft/cloud-velocity/src/main/java/cloud/commandframework/velocity/arguments/PlayerArgument.java b/cloud-minecraft/cloud-velocity/src/main/java/cloud/commandframework/velocity/arguments/PlayerArgument.java index 415c49ff..64d45985 100644 --- a/cloud-minecraft/cloud-velocity/src/main/java/cloud/commandframework/velocity/arguments/PlayerArgument.java +++ b/cloud-minecraft/cloud-velocity/src/main/java/cloud/commandframework/velocity/arguments/PlayerArgument.java @@ -53,7 +53,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> suggestionsProvider, @@ -63,7 +62,7 @@ public final class PlayerArgument extends CommandArgument { super( required, name, - new PlayerParser<>(proxyServer), + new PlayerParser<>(), "", TypeToken.get(Player.class), suggestionsProvider, @@ -75,21 +74,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<>() ); } @@ -97,49 +91,38 @@ 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 @NonNull 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 @NonNull 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 - ) { + private Builder(final @NonNull String name) { super(TypeToken.get(Player.class), name); - this.proxyServer = proxyServer; } @Override public @NonNull CommandArgument<@NonNull C, @NonNull Player> build() { return new PlayerArgument<>( - this.proxyServer, this.isRequired(), this.getName(), this.getSuggestionsProvider(), @@ -152,19 +135,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( - @NonNull final ProxyServer proxyServer - ) { - this.proxyServer = proxyServer; - } - @Override public @NonNull ArgumentParseResult<@NonNull Player> parse( @NonNull final CommandContext<@NonNull C> commandContext, @@ -177,7 +147,7 @@ public final class PlayerArgument extends CommandArgument { commandContext )); } - final Player player = this.proxyServer.getPlayer(input).orElse(null); + final Player player = commandContext.get("ProxyServer").getPlayer(input).orElse(null); if (player == null) { return ArgumentParseResult.failure( new PlayerParseException( @@ -195,7 +165,8 @@ public final class PlayerArgument extends CommandArgument { final @NonNull CommandContext commandContext, final @NonNull String input ) { - return this.proxyServer.getAllPlayers().stream().map(Player::getUsername).collect(Collectors.toList()); + return commandContext.get("ProxyServer").getAllPlayers() + .stream().map(Player::getUsername).collect(Collectors.toList()); } @Override diff --git a/cloud-minecraft/cloud-velocity/src/main/java/cloud/commandframework/velocity/arguments/ServerArgument.java b/cloud-minecraft/cloud-velocity/src/main/java/cloud/commandframework/velocity/arguments/ServerArgument.java index b8d414c1..e553518a 100644 --- a/cloud-minecraft/cloud-velocity/src/main/java/cloud/commandframework/velocity/arguments/ServerArgument.java +++ b/cloud-minecraft/cloud-velocity/src/main/java/cloud/commandframework/velocity/arguments/ServerArgument.java @@ -53,7 +53,6 @@ import java.util.stream.Collectors; 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, @@ -63,7 +62,7 @@ public final class ServerArgument extends CommandArgument(proxyServer), + new ServerParser<>(), "", TypeToken.get(RegisteredServer.class), suggestionsProvider, @@ -75,21 +74,16 @@ public final class ServerArgument extends CommandArgument 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<>() ); } @@ -97,48 +91,35 @@ public final class ServerArgument extends CommandArgument 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 + * @param name Argument name + * @param Command sender type * @return Created argument */ - public static @NonNull CommandArgument optional( - final @NonNull String name, - final @NonNull ProxyServer proxyServer - ) { - return ServerArgument.newBuilder(name, proxyServer).asOptional().build(); + public static @NonNull CommandArgument optional(final @NonNull String name) { + 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 - ) { + private Builder(final @NonNull String name) { super(TypeToken.get(RegisteredServer.class), name); - this.proxyServer = proxyServer; } @Override public @NonNull CommandArgument<@NonNull C, @NonNull RegisteredServer> build() { return new ServerArgument<>( - this.proxyServer, this.isRequired(), this.getName(), this.getSuggestionsProvider(), @@ -150,19 +131,6 @@ public final class ServerArgument extends CommandArgument implements ArgumentParser { - private final ProxyServer proxyServer; - - /** - * Create a new server parser - * - * @param proxyServer Proxy server instance - */ - public ServerParser( - @NonNull final ProxyServer proxyServer - ) { - this.proxyServer = proxyServer; - } - @Override public @NonNull ArgumentParseResult<@NonNull RegisteredServer> parse( @NonNull final CommandContext<@NonNull C> commandContext, @@ -175,7 +143,7 @@ public final class ServerArgument extends CommandArgumentget("ProxyServer").getServer(input).orElse(null); if (server == null) { return ArgumentParseResult.failure( new ServerParseException( @@ -193,7 +161,11 @@ public final class ServerArgument extends CommandArgument commandContext, final @NonNull String input ) { - return this.proxyServer.getAllServers().stream().map(s -> s.getServerInfo().getName()).collect(Collectors.toList()); + return commandContext.get("ProxyServer") + .getAllServers() + .stream() + .map(s -> s.getServerInfo().getName()) + .collect(Collectors.toList()); } } diff --git a/examples/example-velocity/src/main/java/cloud/commandframework/examples/velocity/ExampleVelocityPlugin.java b/examples/example-velocity/src/main/java/cloud/commandframework/examples/velocity/ExampleVelocityPlugin.java index 9ae17693..58cdcd14 100644 --- a/examples/example-velocity/src/main/java/cloud/commandframework/examples/velocity/ExampleVelocityPlugin.java +++ b/examples/example-velocity/src/main/java/cloud/commandframework/examples/velocity/ExampleVelocityPlugin.java @@ -82,7 +82,7 @@ public final class ExampleVelocityPlugin { ); commandManager.command( commandManager.commandBuilder("example") - .argument(PlayerArgument.of("player", this.server)) + .argument(PlayerArgument.of("player")) .handler(context -> { final Player player = context.get("player"); context.getSender().sendMessage( @@ -98,7 +98,7 @@ public final class ExampleVelocityPlugin { ); commandManager.command( commandManager.commandBuilder("example-server") - .argument(ServerArgument.of("server", this.server)) + .argument(ServerArgument.of("server")) .handler(context -> { final RegisteredServer server = context.get("server"); context.getSender().sendMessage(