Add VelocityCommandPreprocessor

This commit is contained in:
jmp 2020-10-20 16:16:09 -07:00 committed by Alexander Söderberg
parent ad3ca86f42
commit d86973f227
8 changed files with 94 additions and 92 deletions

View file

@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added LocationArgument to cloud-bukkit - Added LocationArgument to cloud-bukkit
- Added ServerArgument to cloud-velocity - Added ServerArgument to cloud-velocity
- Added LockableCommandManager to cloud-core - Added LockableCommandManager to cloud-core
- Added VelocityCommandPreprocessor to cloud-velocity
## [1.0.2] - 2020-10-18 ## [1.0.2] - 2020-10-18

View file

@ -185,7 +185,7 @@ repositories {
```groovy ```groovy
dependencies { dependencies {
implementation 'cloud.commandframework:cloud-PLATFORM:1.0.2' implementation 'cloud.commandframework:cloud-PLATFORM:1.1.0-SNAPSHOT'
} }
``` ```

View file

@ -47,7 +47,7 @@ import java.util.regex.Pattern;
/** /**
* Parser for color codes. * Parser for color codes.
* <p> * <p>
* 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 <C> Command sender type * @param <C> Command sender type
* @since 1.1.0 * @since 1.1.0

View file

@ -89,11 +89,14 @@ public class VelocityCommandManager<C> extends CommandManager<C> {
this.commandSenderMapper = commandSenderMapper; this.commandSenderMapper = commandSenderMapper;
this.backwardsCommandSenderMapper = backwardsCommandSenderMapper; this.backwardsCommandSenderMapper = backwardsCommandSenderMapper;
/* Register Velocity Preprocessor */
this.registerCommandPreProcessor(new VelocityCommandPreprocessor<>(this));
/* Register Velocity Parsers */ /* Register Velocity Parsers */
this.getParserRegistry().registerParserSupplier(TypeToken.get(Player.class), parserParameters -> this.getParserRegistry().registerParserSupplier(TypeToken.get(Player.class), parserParameters ->
new PlayerArgument.PlayerParser<>(proxyServer)); new PlayerArgument.PlayerParser<>());
this.getParserRegistry().registerParserSupplier(TypeToken.get(RegisteredServer.class), parserParameters -> this.getParserRegistry().registerParserSupplier(TypeToken.get(RegisteredServer.class), parserParameters ->
new ServerArgument.ServerParser<>(proxyServer)); 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.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 <C>
* @since 1.1.0
*/
final class VelocityCommandPreprocessor<C> implements CommandPreprocessor<C> {
private final VelocityCommandManager<C> mgr;
/**
* The Velocity Command Preprocessor for storing Velocity-specific contexts in the command contexts
*
* @param mgr The VelocityCommandManager
*/
VelocityCommandPreprocessor(final @NonNull VelocityCommandManager<C> mgr) {
this.mgr = mgr;
}
@Override
public void accept(final @NonNull CommandPreprocessingContext<C> context) {
context.getCommandContext().store("ProxyServer", mgr.getProxyServer());
}
}

View file

@ -53,7 +53,6 @@ import java.util.stream.Collectors;
public final class PlayerArgument<C> extends CommandArgument<C, Player> { public final class PlayerArgument<C> extends CommandArgument<C, Player> {
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>> suggestionsProvider, final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider,
@ -63,7 +62,7 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
super( super(
required, required,
name, name,
new PlayerParser<>(proxyServer), new PlayerParser<>(),
"", "",
TypeToken.get(Player.class), TypeToken.get(Player.class),
suggestionsProvider, suggestionsProvider,
@ -75,21 +74,16 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
* 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, Player> newBuilder( public static <C> CommandArgument.@NonNull Builder<C, Player> 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
)
); );
} }
@ -97,49 +91,38 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
* 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> @NonNull CommandArgument<C, Player> of( public static <C> @NonNull CommandArgument<C, Player> 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> @NonNull CommandArgument<C, Player> optional( public static <C> @NonNull CommandArgument<C, Player> 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, Player> { public static final class Builder<C> extends CommandArgument.Builder<C, Player> {
private final ProxyServer proxyServer; private Builder(final @NonNull String name) {
private Builder(
final @NonNull String name,
final @NonNull ProxyServer proxyServer
) {
super(TypeToken.get(Player.class), name); super(TypeToken.get(Player.class), name);
this.proxyServer = proxyServer;
} }
@Override @Override
public @NonNull CommandArgument<@NonNull C, @NonNull Player> build() { public @NonNull CommandArgument<@NonNull C, @NonNull Player> build() {
return new PlayerArgument<>( return new PlayerArgument<>(
this.proxyServer,
this.isRequired(), this.isRequired(),
this.getName(), this.getName(),
this.getSuggestionsProvider(), this.getSuggestionsProvider(),
@ -152,19 +135,6 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
public static final class PlayerParser<C> implements ArgumentParser<C, Player> { public static final class PlayerParser<C> implements ArgumentParser<C, Player> {
private final ProxyServer proxyServer;
/**
* Create a new player parser
*
* @param proxyServer Proxy server instance
*/
public PlayerParser(
@NonNull final ProxyServer proxyServer
) {
this.proxyServer = proxyServer;
}
@Override @Override
public @NonNull ArgumentParseResult<@NonNull Player> parse( public @NonNull ArgumentParseResult<@NonNull Player> parse(
@NonNull final CommandContext<@NonNull C> commandContext, @NonNull final CommandContext<@NonNull C> commandContext,
@ -177,7 +147,7 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
commandContext commandContext
)); ));
} }
final Player player = this.proxyServer.getPlayer(input).orElse(null); final Player player = commandContext.<ProxyServer>get("ProxyServer").getPlayer(input).orElse(null);
if (player == null) { if (player == null) {
return ArgumentParseResult.failure( return ArgumentParseResult.failure(
new PlayerParseException( new PlayerParseException(
@ -195,7 +165,8 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
final @NonNull CommandContext<C> commandContext, final @NonNull CommandContext<C> commandContext,
final @NonNull String input final @NonNull String input
) { ) {
return this.proxyServer.getAllPlayers().stream().map(Player::getUsername).collect(Collectors.toList()); return commandContext.<ProxyServer>get("ProxyServer").getAllPlayers()
.stream().map(Player::getUsername).collect(Collectors.toList());
} }
@Override @Override

View file

@ -53,7 +53,6 @@ import java.util.stream.Collectors;
public final class ServerArgument<C> extends CommandArgument<C, RegisteredServer> { public final class ServerArgument<C> extends CommandArgument<C, RegisteredServer> {
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,
@ -63,7 +62,7 @@ public final class ServerArgument<C> extends CommandArgument<C, RegisteredServer
super( super(
required, required,
name, name,
new ServerParser<>(proxyServer), new ServerParser<>(),
"", "",
TypeToken.get(RegisteredServer.class), TypeToken.get(RegisteredServer.class),
suggestionsProvider, suggestionsProvider,
@ -75,21 +74,16 @@ public final class ServerArgument<C> extends CommandArgument<C, RegisteredServer
* 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, RegisteredServer> newBuilder( public static <C> CommandArgument.@NonNull Builder<C, RegisteredServer> 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
)
); );
} }
@ -97,48 +91,35 @@ public final class ServerArgument<C> extends CommandArgument<C, RegisteredServer
* 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> @NonNull CommandArgument<C, RegisteredServer> of( public static <C> @NonNull CommandArgument<C, RegisteredServer> 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, RegisteredServer> optional( public static <C> @NonNull CommandArgument<C, RegisteredServer> optional(final @NonNull String name) {
final @NonNull String name, return ServerArgument.<C>newBuilder(name).asOptional().build();
final @NonNull ProxyServer proxyServer
) {
return ServerArgument.<C>newBuilder(name, proxyServer).asOptional().build();
} }
public static final class Builder<C> extends CommandArgument.Builder<C, RegisteredServer> { public static final class Builder<C> extends CommandArgument.Builder<C, RegisteredServer> {
private final ProxyServer proxyServer; private Builder(final @NonNull String name) {
private Builder(
final @NonNull String name,
final @NonNull ProxyServer proxyServer
) {
super(TypeToken.get(RegisteredServer.class), name); super(TypeToken.get(RegisteredServer.class), name);
this.proxyServer = proxyServer;
} }
@Override @Override
public @NonNull CommandArgument<@NonNull C, @NonNull RegisteredServer> build() { public @NonNull CommandArgument<@NonNull C, @NonNull RegisteredServer> build() {
return new ServerArgument<>( return new ServerArgument<>(
this.proxyServer,
this.isRequired(), this.isRequired(),
this.getName(), this.getName(),
this.getSuggestionsProvider(), this.getSuggestionsProvider(),
@ -150,19 +131,6 @@ public final class ServerArgument<C> extends CommandArgument<C, RegisteredServer
public static final class ServerParser<C> implements ArgumentParser<C, RegisteredServer> { public static final class ServerParser<C> implements ArgumentParser<C, RegisteredServer> {
private final ProxyServer proxyServer;
/**
* Create a new server parser
*
* @param proxyServer Proxy server instance
*/
public ServerParser(
@NonNull final ProxyServer proxyServer
) {
this.proxyServer = proxyServer;
}
@Override @Override
public @NonNull ArgumentParseResult<@NonNull RegisteredServer> parse( public @NonNull ArgumentParseResult<@NonNull RegisteredServer> parse(
@NonNull final CommandContext<@NonNull C> commandContext, @NonNull final CommandContext<@NonNull C> commandContext,
@ -175,7 +143,7 @@ public final class ServerArgument<C> extends CommandArgument<C, RegisteredServer
commandContext commandContext
)); ));
} }
final RegisteredServer server = this.proxyServer.getServer(input).orElse(null); final RegisteredServer server = commandContext.<ProxyServer>get("ProxyServer").getServer(input).orElse(null);
if (server == null) { if (server == null) {
return ArgumentParseResult.failure( return ArgumentParseResult.failure(
new ServerParseException( new ServerParseException(
@ -193,7 +161,11 @@ public final class ServerArgument<C> extends CommandArgument<C, RegisteredServer
final @NonNull CommandContext<C> commandContext, final @NonNull CommandContext<C> commandContext,
final @NonNull String input final @NonNull String input
) { ) {
return this.proxyServer.getAllServers().stream().map(s -> s.getServerInfo().getName()).collect(Collectors.toList()); return commandContext.<ProxyServer>get("ProxyServer")
.getAllServers()
.stream()
.map(s -> s.getServerInfo().getName())
.collect(Collectors.toList());
} }
} }

View file

@ -82,7 +82,7 @@ public final class ExampleVelocityPlugin {
); );
commandManager.command( commandManager.command(
commandManager.commandBuilder("example") commandManager.commandBuilder("example")
.argument(PlayerArgument.of("player", this.server)) .argument(PlayerArgument.of("player"))
.handler(context -> { .handler(context -> {
final Player player = context.get("player"); final Player player = context.get("player");
context.getSender().sendMessage( context.getSender().sendMessage(
@ -98,7 +98,7 @@ public final class ExampleVelocityPlugin {
); );
commandManager.command( commandManager.command(
commandManager.commandBuilder("example-server") commandManager.commandBuilder("example-server")
.argument(ServerArgument.of("server", this.server)) .argument(ServerArgument.of("server"))
.handler(context -> { .handler(context -> {
final RegisteredServer server = context.get("server"); final RegisteredServer server = context.get("server");
context.getSender().sendMessage( context.getSender().sendMessage(