Added ServerArgument to Velocity

This commit is contained in:
allenclan23 2020-10-16 23:36:30 -04:00 committed by Alexander Söderberg
parent c7c286eb7a
commit 8d69767c15
5 changed files with 252 additions and 2 deletions

View file

@ -42,6 +42,11 @@ public final class VelocityCaptionKeys {
*/
public static final Caption ARGUMENT_PARSE_FAILURE_PLAYER = of("argument.parse.failure.player");
/**
* Variables: {input}
*/
public static final Caption ARGUMENT_PARSE_FAILURE_SERVER = of("argument.parse.failure.server");
private VelocityCaptionKeys() {
}

View file

@ -54,6 +54,11 @@ public class VelocityCommandManager<C> extends CommandManager<C> {
*/
public static final String ARGUMENT_PARSE_FAILURE_PLAYER = "'{input}' is not a valid player";
/**
* Default caption for {@link VelocityCaptionKeys#ARGUMENT_PARSE_FAILURE_SERVER}
*/
public static final String ARGUMENT_PARSE_FAILURE_SERVER = "'{input}' is not a valid server";
private final ProxyServer proxyServer;
private final Function<CommandSource, C> commandSenderMapper;
private final Function<C, CommandSource> backwardsCommandSenderMapper;
@ -82,8 +87,14 @@ public class VelocityCommandManager<C> extends CommandManager<C> {
if (this.getCaptionRegistry() instanceof FactoryDelegatingCaptionRegistry) {
final FactoryDelegatingCaptionRegistry<C> factoryDelegatingCaptionRegistry = (FactoryDelegatingCaptionRegistry<C>)
this.getCaptionRegistry();
factoryDelegatingCaptionRegistry.registerMessageFactory(VelocityCaptionKeys.ARGUMENT_PARSE_FAILURE_PLAYER,
(context, key) -> ARGUMENT_PARSE_FAILURE_PLAYER);
factoryDelegatingCaptionRegistry.registerMessageFactory(
VelocityCaptionKeys.ARGUMENT_PARSE_FAILURE_PLAYER,
(context, key) -> ARGUMENT_PARSE_FAILURE_PLAYER
);
factoryDelegatingCaptionRegistry.registerMessageFactory(
VelocityCaptionKeys.ARGUMENT_PARSE_FAILURE_SERVER,
(context, key) -> ARGUMENT_PARSE_FAILURE_SERVER
);
}
}

View file

@ -0,0 +1,216 @@
//
// 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.arguments;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.captions.CaptionVariable;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
import cloud.commandframework.exceptions.parsing.ParserException;
import cloud.commandframework.velocity.VelocityCaptionKeys;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import io.leangen.geantyref.TypeToken;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
/**
* Argument parser for {@link RegisteredServer servers}
*
* @param <C> Command sender type
*/
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,
final @NonNull Collection<@NonNull BiFunction<@NonNull CommandContext<C>, @NonNull Queue<@NonNull String>,
@NonNull ArgumentParseResult<Boolean>>> argumentPreprocessors
) {
super(
required,
name,
new ServerParser<>(proxyServer),
"",
TypeToken.get(RegisteredServer.class),
suggestionsProvider,
argumentPreprocessors
);
}
/**
* 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
) {
return new Builder<C>(
name,
proxyServer
).withParser(
new ServerParser<>(
proxyServer
)
);
}
/**
* 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
) {
return ServerArgument.<C>newBuilder(name, proxyServer).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 final class Builder<C> extends CommandArgument.Builder<C, RegisteredServer> {
private final ProxyServer proxyServer;
private Builder(
final @NonNull String name,
final @NonNull ProxyServer proxyServer
) {
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(),
new LinkedList<>()
);
}
}
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,
@NonNull final Queue<@NonNull String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NoInputProvidedException(
ServerParser.class,
commandContext
));
}
final RegisteredServer server = this.proxyServer.getServer(input).orElse(null);
if (server == null) {
return ArgumentParseResult.failure(
new ServerParseException(
input,
commandContext
)
);
}
inputQueue.remove();
return ArgumentParseResult.success(server);
}
@Override
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
return this.proxyServer.getAllServers().stream().map(s -> s.getServerInfo().getName()).collect(Collectors.toList());
}
}
public static final class ServerParseException extends ParserException {
private ServerParseException(
final @NonNull String input,
final @NonNull CommandContext<?> context
) {
super(
ServerParser.class,
context,
VelocityCaptionKeys.ARGUMENT_PARSE_FAILURE_SERVER,
CaptionVariable.of("input", input)
);
}
}
}