Expand the bungee module

This commit is contained in:
allenclan23 2020-10-16 23:58:41 -04:00 committed by Alexander Söderberg
parent d86973f227
commit f73b713658
11 changed files with 785 additions and 3 deletions

View file

@ -0,0 +1,68 @@
//
// 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.bungee;
import cloud.commandframework.captions.Caption;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
/**
* Bungee specific {@link Caption caption keys}
*/
public final class BungeeCaptionKeys {
private static final Collection<Caption> RECOGNIZED_CAPTIONS = new LinkedList<>();
/**
* Variables: {input}
*/
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 BungeeCaptionKeys() {
}
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> getBungeeCaptionKeys() {
return Collections.unmodifiableCollection(RECOGNIZED_CAPTIONS);
}
}

View file

@ -25,9 +25,15 @@ package cloud.commandframework.bungee;
import cloud.commandframework.CommandManager;
import cloud.commandframework.CommandTree;
import cloud.commandframework.bungee.arguments.PlayerArgument;
import cloud.commandframework.bungee.arguments.ServerArgument;
import cloud.commandframework.captions.FactoryDelegatingCaptionRegistry;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.meta.SimpleCommandMeta;
import io.leangen.geantyref.TypeToken;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Plugin;
import org.checkerframework.checker.nullness.qual.NonNull;
@ -35,6 +41,16 @@ import java.util.function.Function;
public class BungeeCommandManager<C> extends CommandManager<C> {
/**
* Default caption for {@link BungeeCaptionKeys#ARGUMENT_PARSE_FAILURE_PLAYER}
*/
public static final String ARGUMENT_PARSE_FAILURE_PLAYER = "'{input}' is not a valid player";
/**
* Default caption for {@link BungeeCaptionKeys#ARGUMENT_PARSE_FAILURE_PLAYER}
*/
public static final String ARGUMENT_PARSE_FAILURE_SERVER = "'{input}' is not a valid server";
private final Plugin owningPlugin;
private final Function<CommandSender, C> commandSenderMapper;
private final Function<C, CommandSender> backwardsCommandSenderMapper;
@ -46,7 +62,6 @@ public class BungeeCommandManager<C> extends CommandManager<C> {
* @param commandExecutionCoordinator Coordinator provider
* @param commandSenderMapper Function that maps {@link CommandSender} to the command sender type
* @param backwardsCommandSenderMapper Function that maps the command sender type to {@link CommandSender}
* @throws Exception If the construction of the manager fails
*/
public BungeeCommandManager(
final @NonNull Plugin owningPlugin,
@ -54,13 +69,32 @@ public class BungeeCommandManager<C> extends CommandManager<C> {
@NonNull CommandExecutionCoordinator<C>> commandExecutionCoordinator,
final @NonNull Function<@NonNull CommandSender, @NonNull C> commandSenderMapper,
final @NonNull Function<@NonNull C, @NonNull CommandSender> backwardsCommandSenderMapper
)
throws Exception {
) {
super(commandExecutionCoordinator, new BungeePluginRegistrationHandler<>());
((BungeePluginRegistrationHandler<C>) this.getCommandRegistrationHandler()).initialize(this);
this.owningPlugin = owningPlugin;
this.commandSenderMapper = commandSenderMapper;
this.backwardsCommandSenderMapper = backwardsCommandSenderMapper;
/* Register Bungee Parsers */
this.getParserRegistry().registerParserSupplier(TypeToken.get(ProxiedPlayer.class), parserParameters ->
new PlayerArgument.PlayerParser<>(owningPlugin.getProxy()));
this.getParserRegistry().registerParserSupplier(TypeToken.get(ServerInfo.class), parserParameters ->
new ServerArgument.ServerParser<>(owningPlugin.getProxy()));
/* Register default captions */
if (this.getCaptionRegistry() instanceof FactoryDelegatingCaptionRegistry) {
final FactoryDelegatingCaptionRegistry<C> factoryDelegatingCaptionRegistry = (FactoryDelegatingCaptionRegistry<C>)
this.getCaptionRegistry();
factoryDelegatingCaptionRegistry.registerMessageFactory(
BungeeCaptionKeys.ARGUMENT_PARSE_FAILURE_PLAYER,
(context, key) -> ARGUMENT_PARSE_FAILURE_PLAYER
);
factoryDelegatingCaptionRegistry.registerMessageFactory(
BungeeCaptionKeys.ARGUMENT_PARSE_FAILURE_SERVER,
(context, key) -> ARGUMENT_PARSE_FAILURE_SERVER
);
}
}
@Override

View file

@ -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.bungee.arguments;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.bungee.BungeeCaptionKeys;
import cloud.commandframework.captions.CaptionVariable;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
import cloud.commandframework.exceptions.parsing.ParserException;
import io.leangen.geantyref.TypeToken;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
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 ProxiedPlayer players}
*
* @param <C> Command sender type
*/
public final class PlayerArgument<C> extends CommandArgument<C, ProxiedPlayer> {
private PlayerArgument(
final @NonNull ProxyServer proxyServer,
final boolean required,
final @NonNull String name,
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionProvider,
final @NonNull Collection<@NonNull BiFunction<@NonNull CommandContext<C>, @NonNull Queue<@NonNull String>,
@NonNull ArgumentParseResult<Boolean>>> argumentPreprocessors
) {
super(
required,
name,
new PlayerParser<>(proxyServer),
"",
TypeToken.get(ProxiedPlayer.class),
suggestionProvider,
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, ProxiedPlayer> 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> CommandArgument<C, ProxiedPlayer> 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> CommandArgument<C, ProxiedPlayer> 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, ProxiedPlayer> {
private final ProxyServer proxyServer;
private Builder(
final @NonNull String name,
final @NonNull ProxyServer proxyServer
) {
super(TypeToken.get(ProxiedPlayer.class), name);
this.proxyServer = proxyServer;
}
@Override
public @NonNull CommandArgument<@NonNull C, @NonNull ProxiedPlayer> build() {
return new PlayerArgument<>(
this.proxyServer,
this.isRequired(),
this.getName(),
this.getSuggestionsProvider(),
new LinkedList<>()
);
}
}
public static final class PlayerParser<C> implements ArgumentParser<C, ProxiedPlayer> {
private final ProxyServer proxyServer;
/**
* Create a new player parser
*
* @param proxyServer Proxy server instance
*/
public PlayerParser(
final @NonNull ProxyServer proxyServer
) {
this.proxyServer = proxyServer;
}
@Override
public @NonNull ArgumentParseResult<@NonNull ProxiedPlayer> 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(
PlayerParser.class,
commandContext
));
}
final ProxiedPlayer player = this.proxyServer.getPlayer(input);
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.getPlayers().stream().map(ProxiedPlayer::getDisplayName).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,
BungeeCaptionKeys.ARGUMENT_PARSE_FAILURE_PLAYER,
CaptionVariable.of("input", input)
);
}
}
}

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.bungee.arguments;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.bungee.BungeeCaptionKeys;
import cloud.commandframework.captions.CaptionVariable;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
import cloud.commandframework.exceptions.parsing.ParserException;
import io.leangen.geantyref.TypeToken;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.function.BiFunction;
/**
* Argument parser for {@link net.md_5.bungee.api.config.ServerInfo servers}
*
* @param <C> Command sender type
*/
public final class ServerArgument<C> extends CommandArgument<C, ServerInfo> {
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(ServerInfo.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, ServerInfo> newBuilder(
final @NonNull String name,
final @NonNull ProxyServer proxyServer
) {
return new Builder<C>(
name,
proxyServer
).withParser(
new ServerParser<>(
proxyServer
)
);
}
/**
* Create a new required 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, ServerInfo> 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, ServerInfo> 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, ServerInfo> {
private final ProxyServer proxyServer;
private Builder(
final @NonNull String name,
final @NonNull ProxyServer proxyServer
) {
super(TypeToken.get(ServerInfo.class), name);
this.proxyServer = proxyServer;
}
@Override
public @NonNull CommandArgument<@NonNull C, @NonNull ServerInfo> build() {
return new ServerArgument<>(
this.proxyServer,
this.isRequired(),
this.getName(),
this.getSuggestionsProvider(),
new LinkedList<>()
);
}
}
public static final class ServerParser<C> implements ArgumentParser<C, ServerInfo> {
private final ProxyServer proxyServer;
/**
* Create a new server parser
*
* @param proxyServer Proxy server instance
*/
public ServerParser(
final @NonNull ProxyServer proxyServer
) {
this.proxyServer = proxyServer;
}
@Override
public @NonNull ArgumentParseResult<@NonNull ServerInfo> 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 ServerInfo server = this.proxyServer.getServerInfo(input);
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 new ArrayList<>(this.proxyServer.getServers().keySet());
}
}
public static final class ServerParseException extends ParserException {
private ServerParseException(
final @NonNull String input,
final @NonNull CommandContext<?> context
) {
super(
ServerParser.class,
context,
BungeeCaptionKeys.ARGUMENT_PARSE_FAILURE_SERVER,
CaptionVariable.of("input", input)
);
}
}
}

View file

@ -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.
//
/**
* Bungee specific argument types
*/
package cloud.commandframework.bungee.arguments;