✨ Improve the velocity module
This commit is contained in:
parent
9761c0fadf
commit
edc5249244
10 changed files with 579 additions and 2 deletions
|
|
@ -0,0 +1,88 @@
|
|||
//
|
||||
// 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.CommandTree;
|
||||
import cloud.commandframework.execution.CommandExecutionCoordinator;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Key;
|
||||
import com.google.inject.util.Types;
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Injection module that allows for {@link VelocityCommandManager} to be injectable
|
||||
*
|
||||
* @param <C> Command sender type
|
||||
*/
|
||||
public final class CloudInjectionModule<C> extends AbstractModule {
|
||||
|
||||
private final Class<C> commandSenderType;
|
||||
private final Function<@NonNull CommandTree<C>, @NonNull CommandExecutionCoordinator<C>> commandExecutionCoordinator;
|
||||
private final Function<@NonNull CommandSource, @NonNull C> commandSenderMapper;
|
||||
private final Function<@NonNull C, @NonNull CommandSource> backwardsCommandSenderMapper;
|
||||
|
||||
/**
|
||||
* Create a new child injection module
|
||||
*
|
||||
* @param commandSenderType Your command sender type
|
||||
* @param commandExecutionCoordinator Command execution coordinator
|
||||
* @param commandSenderMapper Mapper from command source to the custom command sender type
|
||||
* @param backwardsCommandSenderMapper Mapper from the custom command sender type to a velocity command source
|
||||
*/
|
||||
public CloudInjectionModule(
|
||||
final @NonNull Class<C> commandSenderType,
|
||||
final @NonNull Function<@NonNull CommandTree<C>, @NonNull CommandExecutionCoordinator<C>> commandExecutionCoordinator,
|
||||
final @NonNull Function<@NonNull CommandSource, @NonNull C> commandSenderMapper,
|
||||
final @NonNull Function<@NonNull C, @NonNull CommandSource> backwardsCommandSenderMapper
|
||||
) {
|
||||
this.commandSenderType = commandSenderType;
|
||||
this.commandExecutionCoordinator = commandExecutionCoordinator;
|
||||
this.commandSenderMapper = commandSenderMapper;
|
||||
this.backwardsCommandSenderMapper = backwardsCommandSenderMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
final Type commandTreeType = Types.newParameterizedType(CommandTree.class, this.commandSenderType);
|
||||
final Type commandExecutionCoordinatorType = Types.newParameterizedType(CommandExecutionCoordinator.class,
|
||||
this.commandSenderType);
|
||||
final Type executorFunction = Types.newParameterizedType(Function.class, commandTreeType,
|
||||
commandExecutionCoordinatorType);
|
||||
final Key executorFunctionKey = Key.get(executorFunction);
|
||||
this.bind(executorFunctionKey).toInstance(this.commandExecutionCoordinator);
|
||||
final Type commandSenderMapperFunction = Types.newParameterizedType(Function.class, CommandSource.class,
|
||||
this.commandSenderType);
|
||||
final Key commandSenderMapperFunctionKey = Key.get(commandSenderMapperFunction);
|
||||
this.bind(commandSenderMapperFunctionKey).toInstance(this.commandSenderMapper);
|
||||
final Type backwardsCommandSenderMapperFunction = Types.newParameterizedType(Function.class, this.commandSenderType,
|
||||
CommandSource.class);
|
||||
final Key backwardsCommandSenderMapperFunctionKey = Key.get(backwardsCommandSenderMapperFunction);
|
||||
this.bind(backwardsCommandSenderMapperFunctionKey).toInstance(this.backwardsCommandSenderMapper);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
//
|
||||
// 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.captions.Caption;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* Velocity specific {@link Caption caption keys}
|
||||
*/
|
||||
public final class VelocityCaptionKeys {
|
||||
|
||||
private static final Collection<Caption> RECOGNIZED_CAPTIONS = new LinkedList<>();
|
||||
|
||||
/**
|
||||
* Variables: {input}
|
||||
*/
|
||||
public static final Caption ARGUMENT_PARSE_FAILURE_PLAYER = of("argument.parse.failure.player");
|
||||
|
||||
private VelocityCaptionKeys() {
|
||||
}
|
||||
|
||||
private static @NonNull Caption of(final @NonNull String key) {
|
||||
final Caption caption = Caption.of(key);
|
||||
RECOGNIZED_CAPTIONS.add(caption);
|
||||
return caption;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an immutable collection containing all standard caption keys
|
||||
*
|
||||
* @return Immutable collection of keys
|
||||
*/
|
||||
public static @NonNull Collection<@NonNull Caption> getVelocityCaptionKeys() {
|
||||
return Collections.unmodifiableCollection(RECOGNIZED_CAPTIONS);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -25,9 +25,13 @@ package cloud.commandframework.velocity;
|
|||
|
||||
import cloud.commandframework.CommandManager;
|
||||
import cloud.commandframework.CommandTree;
|
||||
import cloud.commandframework.captions.FactoryDelegatingCaptionRegistry;
|
||||
import cloud.commandframework.execution.CommandExecutionCoordinator;
|
||||
import cloud.commandframework.meta.CommandMeta;
|
||||
import cloud.commandframework.meta.SimpleCommandMeta;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Module;
|
||||
import com.google.inject.Singleton;
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
|
@ -35,12 +39,21 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
|||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* {@link CommandManager} implementation for Velocity
|
||||
* {@link CommandManager} implementation for Velocity.
|
||||
* <p>
|
||||
* This can be injected if {@link CloudInjectionModule} is registered in the
|
||||
* injector. This can be achieved by using {@link com.google.inject.Injector#createChildInjector(Module...)}
|
||||
*
|
||||
* @param <C> Command sender type
|
||||
*/
|
||||
@Singleton
|
||||
public class VelocityCommandManager<C> extends CommandManager<C> {
|
||||
|
||||
/**
|
||||
* Default caption for {@link VelocityCaptionKeys#ARGUMENT_PARSE_FAILURE_PLAYER}
|
||||
*/
|
||||
public static final String ARGUMENT_PARSE_FAILURE_PLAYER = "'{input}' is not a valid player";
|
||||
|
||||
private final ProxyServer proxyServer;
|
||||
private final Function<CommandSource, C> commandSenderMapper;
|
||||
private final Function<C, CommandSource> backwardsCommandSenderMapper;
|
||||
|
|
@ -53,6 +66,7 @@ public class VelocityCommandManager<C> extends CommandManager<C> {
|
|||
* @param commandSenderMapper Function that maps {@link CommandSource} to the command sender type
|
||||
* @param backwardsCommandSenderMapper Function that maps the command sender type to {@link CommandSource}
|
||||
*/
|
||||
@Inject
|
||||
public VelocityCommandManager(
|
||||
final @NonNull ProxyServer proxyServer,
|
||||
final @NonNull Function<@NonNull CommandTree<C>, @NonNull CommandExecutionCoordinator<C>> commandExecutionCoordinator,
|
||||
|
|
@ -64,6 +78,13 @@ public class VelocityCommandManager<C> extends CommandManager<C> {
|
|||
this.proxyServer = proxyServer;
|
||||
this.commandSenderMapper = commandSenderMapper;
|
||||
this.backwardsCommandSenderMapper = backwardsCommandSenderMapper;
|
||||
/* Register default captions */
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -0,0 +1,224 @@
|
|||
//
|
||||
// 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.ParserException;
|
||||
import cloud.commandframework.velocity.VelocityCaptionKeys;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
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 Player players}
|
||||
*
|
||||
* @param <C> Command sender type
|
||||
*/
|
||||
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,
|
||||
final @NonNull Collection<@NonNull BiFunction<@NonNull CommandContext<C>, @NonNull Queue<@NonNull String>,
|
||||
@NonNull ArgumentParseResult<Boolean>>> argumentPreprocessors
|
||||
) {
|
||||
super(
|
||||
required,
|
||||
name,
|
||||
new PlayerParser<>(proxyServer),
|
||||
"",
|
||||
TypeToken.get(Player.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, Player> newBuilder(
|
||||
final @NonNull String name,
|
||||
final @NonNull ProxyServer proxyServer
|
||||
) {
|
||||
return new Builder<C>(
|
||||
name,
|
||||
proxyServer
|
||||
).withParser(
|
||||
new PlayerParser<>(
|
||||
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, Player> of(
|
||||
final @NonNull String name,
|
||||
final @NonNull ProxyServer proxyServer
|
||||
) {
|
||||
return PlayerArgument.<C>newBuilder(name, proxyServer).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
|
||||
) {
|
||||
return PlayerArgument.<C>newBuilder(name, proxyServer).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
|
||||
) {
|
||||
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(),
|
||||
new LinkedList<>()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
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,
|
||||
@NonNull final Queue<@NonNull String> inputQueue
|
||||
) {
|
||||
final String input = inputQueue.peek();
|
||||
if (input == null) {
|
||||
return ArgumentParseResult.failure(
|
||||
new NullPointerException(
|
||||
"No input was provided"
|
||||
)
|
||||
);
|
||||
}
|
||||
final Player player = this.proxyServer.getPlayer(input).orElse(null);
|
||||
if (player == null) {
|
||||
return ArgumentParseResult.failure(
|
||||
new PlayerParseException(
|
||||
input,
|
||||
commandContext
|
||||
)
|
||||
);
|
||||
}
|
||||
inputQueue.remove();
|
||||
return ArgumentParseResult.success(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull List<@NonNull String> suggestions(
|
||||
final @NonNull CommandContext<C> commandContext,
|
||||
final @NonNull String input
|
||||
) {
|
||||
return this.proxyServer.getAllPlayers().stream().map(Player::getUsername).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isContextFree() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static final class PlayerParseException extends ParserException {
|
||||
|
||||
private PlayerParseException(
|
||||
final @NonNull String input,
|
||||
final @NonNull CommandContext<?> context
|
||||
) {
|
||||
super(
|
||||
PlayerParser.class,
|
||||
context,
|
||||
VelocityCaptionKeys.ARGUMENT_PARSE_FAILURE_PLAYER,
|
||||
CaptionVariable.of("input", input)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
/**
|
||||
* Velocity specific argument types
|
||||
*/
|
||||
package cloud.commandframework.velocity.arguments;
|
||||
Loading…
Add table
Add a link
Reference in a new issue