✨ Add VelocityCommandPreprocessor
This commit is contained in:
parent
ad3ca86f42
commit
d86973f227
8 changed files with 94 additions and 92 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -185,7 +185,7 @@ repositories {
|
|||
|
||||
```groovy
|
||||
dependencies {
|
||||
implementation 'cloud.commandframework:cloud-PLATFORM:1.0.2'
|
||||
implementation 'cloud.commandframework:cloud-PLATFORM:1.1.0-SNAPSHOT'
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ import java.util.regex.Pattern;
|
|||
/**
|
||||
* Parser for color codes.
|
||||
* <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
|
||||
* @since 1.1.0
|
||||
|
|
|
|||
|
|
@ -89,11 +89,14 @@ public class VelocityCommandManager<C> extends CommandManager<C> {
|
|||
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) {
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -53,7 +53,6 @@ import java.util.stream.Collectors;
|
|||
public final class PlayerArgument<C> extends CommandArgument<C, Player> {
|
||||
|
||||
private PlayerArgument(
|
||||
final @NonNull ProxyServer proxyServer,
|
||||
final boolean required,
|
||||
final @NonNull String name,
|
||||
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider,
|
||||
|
|
@ -63,7 +62,7 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
|
|||
super(
|
||||
required,
|
||||
name,
|
||||
new PlayerParser<>(proxyServer),
|
||||
new PlayerParser<>(),
|
||||
"",
|
||||
TypeToken.get(Player.class),
|
||||
suggestionsProvider,
|
||||
|
|
@ -75,21 +74,16 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
|
|||
* 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, Player> 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<>()
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -97,49 +91,38 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
|
|||
* 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> @NonNull CommandArgument<C, Player> 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> @NonNull CommandArgument<C, Player> 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, Player> {
|
||||
|
||||
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<C> extends CommandArgument<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
|
||||
public @NonNull ArgumentParseResult<@NonNull Player> parse(
|
||||
@NonNull final CommandContext<@NonNull C> commandContext,
|
||||
|
|
@ -177,7 +147,7 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
|
|||
commandContext
|
||||
));
|
||||
}
|
||||
final Player player = this.proxyServer.getPlayer(input).orElse(null);
|
||||
final Player player = commandContext.<ProxyServer>get("ProxyServer").getPlayer(input).orElse(null);
|
||||
if (player == null) {
|
||||
return ArgumentParseResult.failure(
|
||||
new PlayerParseException(
|
||||
|
|
@ -195,7 +165,8 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
|
|||
final @NonNull CommandContext<C> commandContext,
|
||||
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
|
||||
|
|
|
|||
|
|
@ -53,7 +53,6 @@ import java.util.stream.Collectors;
|
|||
public final class ServerArgument<C> extends CommandArgument<C, RegisteredServer> {
|
||||
|
||||
private ServerArgument(
|
||||
final @NonNull ProxyServer proxyServer,
|
||||
final boolean required,
|
||||
final @NonNull String name,
|
||||
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider,
|
||||
|
|
@ -63,7 +62,7 @@ public final class ServerArgument<C> extends CommandArgument<C, RegisteredServer
|
|||
super(
|
||||
required,
|
||||
name,
|
||||
new ServerParser<>(proxyServer),
|
||||
new ServerParser<>(),
|
||||
"",
|
||||
TypeToken.get(RegisteredServer.class),
|
||||
suggestionsProvider,
|
||||
|
|
@ -75,21 +74,16 @@ public final class ServerArgument<C> extends CommandArgument<C, RegisteredServer
|
|||
* 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, RegisteredServer> 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<>()
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -97,48 +91,35 @@ public final class ServerArgument<C> extends CommandArgument<C, RegisteredServer
|
|||
* 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> @NonNull CommandArgument<C, RegisteredServer> 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, RegisteredServer> optional(
|
||||
final @NonNull String name,
|
||||
final @NonNull ProxyServer proxyServer
|
||||
) {
|
||||
return ServerArgument.<C>newBuilder(name, proxyServer).asOptional().build();
|
||||
public static <C> @NonNull CommandArgument<C, RegisteredServer> optional(final @NonNull String name) {
|
||||
return ServerArgument.<C>newBuilder(name).asOptional().build();
|
||||
}
|
||||
|
||||
public static final class Builder<C> extends CommandArgument.Builder<C, RegisteredServer> {
|
||||
|
||||
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<C> extends CommandArgument<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
|
||||
public @NonNull ArgumentParseResult<@NonNull RegisteredServer> parse(
|
||||
@NonNull final CommandContext<@NonNull C> commandContext,
|
||||
|
|
@ -175,7 +143,7 @@ public final class ServerArgument<C> extends CommandArgument<C, RegisteredServer
|
|||
commandContext
|
||||
));
|
||||
}
|
||||
final RegisteredServer server = this.proxyServer.getServer(input).orElse(null);
|
||||
final RegisteredServer server = commandContext.<ProxyServer>get("ProxyServer").getServer(input).orElse(null);
|
||||
if (server == null) {
|
||||
return ArgumentParseResult.failure(
|
||||
new ServerParseException(
|
||||
|
|
@ -193,7 +161,11 @@ public final class ServerArgument<C> extends CommandArgument<C, RegisteredServer
|
|||
final @NonNull CommandContext<C> commandContext,
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue