♻️ Reformat + Update .editorconfig

This commit is contained in:
Alexander Söderberg 2020-10-06 22:48:30 +02:00 committed by Alexander Söderberg
parent 8bdec87a74
commit 2aac3980d5
169 changed files with 4261 additions and 2448 deletions

View file

@ -95,9 +95,10 @@ public final class CloudBrigadierManager<C, S> {
* @param commandManager Command manager
* @param dummyContextProvider Provider of dummy context for completions
*/
public CloudBrigadierManager(final @NonNull CommandManager<C> commandManager,
final @NonNull Supplier<@NonNull CommandContext<C>>
dummyContextProvider) {
public CloudBrigadierManager(
final @NonNull CommandManager<C> commandManager,
final @NonNull Supplier<@NonNull CommandContext<C>> dummyContextProvider
) {
this.mappers = new HashMap<>();
this.defaultArgumentTypeSuppliers = new HashMap<>();
this.commandManager = commandManager;
@ -199,10 +200,12 @@ public final class CloudBrigadierManager<C, S> {
* @param <K> cloud argument type
* @param <O> Brigadier argument type value
*/
public <T, K extends ArgumentParser<C, T>, O> void registerMapping(final @NonNull TypeToken<K> argumentType,
final boolean nativeSuggestions,
final @NonNull Function<@NonNull ? extends K,
@NonNull ? extends ArgumentType<O>> mapper) {
public <T, K extends ArgumentParser<C, T>, O> void registerMapping(
final @NonNull TypeToken<K> argumentType,
final boolean nativeSuggestions,
final @NonNull Function<@NonNull ? extends K,
@NonNull ? extends ArgumentType<O>> mapper
) {
this.mappers.put(GenericTypeReflector.erase(argumentType.getType()), Pair.of(mapper, nativeSuggestions));
}
@ -212,8 +215,10 @@ public final class CloudBrigadierManager<C, S> {
* @param clazz Type to map
* @param supplier Supplier that supplies the argument type
*/
public void registerDefaultArgumentTypeSupplier(final @NonNull Class<?> clazz,
final @NonNull Supplier<@Nullable ArgumentType<?>> supplier) {
public void registerDefaultArgumentTypeSupplier(
final @NonNull Class<?> clazz,
final @NonNull Supplier<@Nullable ArgumentType<?>> supplier
) {
this.defaultArgumentTypeSuppliers.put(clazz, supplier);
}
@ -221,19 +226,23 @@ public final class CloudBrigadierManager<C, S> {
private <T, K extends ArgumentParser<?, ?>> @Nullable Pair<@NonNull ArgumentType<?>, @NonNull Boolean> getArgument(
final @NonNull TypeToken<?> valueType,
final @NonNull TypeToken<T> argumentType,
final @NonNull K argument) {
final @NonNull K argument
) {
final ArgumentParser<C, ?> commandArgument = (ArgumentParser<C, ?>) argument;
final Pair pair = this.mappers.get(GenericTypeReflector.erase(argumentType.getType()));
if (pair == null || pair.getFirst() == null) {
return this.createDefaultMapper(valueType, commandArgument);
}
return Pair.of((ArgumentType<?>) ((Function) pair.getFirst()).apply(commandArgument),
(boolean) pair.getSecond());
return Pair.of(
(ArgumentType<?>) ((Function) pair.getFirst()).apply(commandArgument),
(boolean) pair.getSecond()
);
}
private <T, K extends ArgumentParser<C, T>> @NonNull Pair<@NonNull ArgumentType<?>, @NonNull Boolean> createDefaultMapper(
final @NonNull TypeToken<?> clazz,
final @NonNull ArgumentParser<C, T> argument) {
final @NonNull ArgumentParser<C, T> argument
) {
final Supplier<ArgumentType<?>> argumentTypeSupplier = this.defaultArgumentTypeSuppliers
.get(GenericTypeReflector.erase(clazz.getType()));
final @Nullable ArgumentType<?> defaultType;
@ -258,20 +267,24 @@ public final class CloudBrigadierManager<C, S> {
* @param executor Command executor
* @return Literal command node
*/
public @NonNull LiteralCommandNode<S> createLiteralCommandNode(final @NonNull String label,
final @NonNull Command<C> cloudCommand,
final @NonNull BiPredicate<@NonNull S,
@NonNull CommandPermission> permissionChecker,
final boolean forceRegister,
final com.mojang.brigadier.@NonNull Command<S> executor) {
public @NonNull LiteralCommandNode<S> createLiteralCommandNode(
final @NonNull String label,
final @NonNull Command<C> cloudCommand,
final @NonNull BiPredicate<@NonNull S,
@NonNull CommandPermission> permissionChecker,
final boolean forceRegister,
final com.mojang.brigadier.@NonNull Command<S> executor
) {
final CommandTree.Node<CommandArgument<C, ?>> node = this.commandManager
.getCommandTree().getNamedNode(cloudCommand.getArguments().get(0).getName());
final SuggestionProvider<S> provider = (context, builder) -> this.buildSuggestions(node.getValue(), context, builder);
final LiteralArgumentBuilder<S> literalArgumentBuilder = LiteralArgumentBuilder
.<S>literal(label)
.requires(sender -> permissionChecker.test(sender, (CommandPermission) node.getNodeMeta()
.getOrDefault("permission",
Permission.empty())));
.getOrDefault(
"permission",
Permission.empty()
)));
if (forceRegister || (node.getValue() != null && node.getValue().getOwningCommand() != null)) {
literalArgumentBuilder.executes(executor);
}
@ -279,7 +292,8 @@ public final class CloudBrigadierManager<C, S> {
final LiteralCommandNode<S> constructedRoot = literalArgumentBuilder.build();
for (final CommandTree.Node<CommandArgument<C, ?>> child : node.getChildren()) {
constructedRoot.addChild(this.constructCommandNode(forceRegister, child,
permissionChecker, executor, provider).build());
permissionChecker, executor, provider
).build());
}
return constructedRoot;
}
@ -299,19 +313,25 @@ public final class CloudBrigadierManager<C, S> {
final @NonNull LiteralCommandNode<S> root,
final @NonNull SuggestionProvider<S> suggestionProvider,
final com.mojang.brigadier.@NonNull Command<S> executor,
final @NonNull BiPredicate<@NonNull S, @NonNull CommandPermission> permissionChecker) {
final @NonNull BiPredicate<@NonNull S, @NonNull CommandPermission> permissionChecker
) {
final LiteralArgumentBuilder<S> literalArgumentBuilder = LiteralArgumentBuilder.<S>literal(root.getLiteral())
.requires(sender -> permissionChecker.test(sender,
(CommandPermission) cloudCommand.getNodeMeta()
.getOrDefault("permission",
Permission.empty())));
.requires(sender -> permissionChecker.test(
sender,
(CommandPermission) cloudCommand.getNodeMeta()
.getOrDefault(
"permission",
Permission.empty()
)
));
if (cloudCommand.getValue() != null && cloudCommand.getValue().getOwningCommand() != null) {
literalArgumentBuilder.executes(executor);
}
final LiteralCommandNode<S> constructedRoot = literalArgumentBuilder.build();
for (final CommandTree.Node<CommandArgument<C, ?>> child : cloudCommand.getChildren()) {
constructedRoot.addChild(this.constructCommandNode(false, child, permissionChecker,
executor, suggestionProvider).build());
executor, suggestionProvider
).build());
}
return constructedRoot;
}
@ -321,10 +341,10 @@ public final class CloudBrigadierManager<C, S> {
final CommandTree.@NonNull Node<CommandArgument<C, ?>> root,
final @NonNull BiPredicate<@NonNull S, @NonNull CommandPermission> permissionChecker,
final com.mojang.brigadier.@NonNull Command<S> executor,
final SuggestionProvider<S> suggestionProvider) {
final SuggestionProvider<S> suggestionProvider
) {
if (root.getValue() instanceof CompoundArgument) {
@SuppressWarnings("unchecked")
final CompoundArgument<?, C, ?> compoundArgument = (CompoundArgument<?, C, ?>) root.getValue();
@SuppressWarnings("unchecked") final CompoundArgument<?, C, ?> compoundArgument = (CompoundArgument<?, C, ?>) root.getValue();
final Object[] parsers = compoundArgument.getParserTuple().toArray();
final Object[] types = compoundArgument.getTypes().toArray();
final Object[] names = compoundArgument.getNames().toArray();
@ -334,18 +354,24 @@ public final class CloudBrigadierManager<C, S> {
for (int i = parsers.length - 1; i >= 0; i--) {
@SuppressWarnings("unchecked") final ArgumentParser<C, ?> parser = (ArgumentParser<C, ?>) parsers[i];
final Pair<ArgumentType<?>, Boolean> pair = this.getArgument(TypeToken.get((Class<?>) types[i]),
TypeToken.get(parser.getClass()),
parser);
final Pair<ArgumentType<?>, Boolean> pair = this.getArgument(
TypeToken.get((Class<?>) types[i]),
TypeToken.get(parser.getClass()),
parser
);
final SuggestionProvider<S> provider = pair.getSecond() ? null : suggestionProvider;
final ArgumentBuilder<S, ?> fragmentBuilder = RequiredArgumentBuilder
.<S, Object>argument((String) names[i], (ArgumentType<Object>) pair.getFirst())
.suggests(provider)
.requires(sender -> permissionChecker.test(sender,
(CommandPermission) root.getNodeMeta()
.getOrDefault("permission",
Permission.empty())));
.requires(sender -> permissionChecker.test(
sender,
(CommandPermission) root.getNodeMeta()
.getOrDefault(
"permission",
Permission.empty()
)
));
argumentBuilders[i] = fragmentBuilder;
if (forceExecutor || (i == parsers.length - 1) && (root.isLeaf() || !root.getValue().isRequired())) {
@ -369,24 +395,33 @@ public final class CloudBrigadierManager<C, S> {
if (root.getValue() instanceof StaticArgument) {
argumentBuilder = LiteralArgumentBuilder.<S>literal(root.getValue().getName())
.requires(sender -> permissionChecker.test(sender, (CommandPermission) root.getNodeMeta()
.getOrDefault("permission",
Permission.empty())))
.getOrDefault(
"permission",
Permission.empty()
)))
.executes(executor);
} else {
final Pair<ArgumentType<?>, Boolean> pair = this.getArgument(root.getValue().getValueType(),
TypeToken.get(root.getValue().getParser().getClass()),
root.getValue().getParser());
final Pair<ArgumentType<?>, Boolean> pair = this.getArgument(
root.getValue().getValueType(),
TypeToken.get(root.getValue().getParser().getClass()),
root.getValue().getParser()
);
final SuggestionProvider<S> provider = pair.getSecond()
? null
: (context, builder) -> this.buildSuggestions(root.getValue(),
context, builder);
? null
: (context, builder) -> this.buildSuggestions(root.getValue(),
context, builder
);
argumentBuilder = RequiredArgumentBuilder
.<S, Object>argument(root.getValue().getName(), (ArgumentType<Object>) pair.getFirst())
.suggests(provider)
.requires(sender -> permissionChecker.test(sender,
(CommandPermission) root.getNodeMeta()
.getOrDefault("permission",
Permission.empty())));
.requires(sender -> permissionChecker.test(
sender,
(CommandPermission) root.getNodeMeta()
.getOrDefault(
"permission",
Permission.empty()
)
));
}
if (forceExecutor || root.isLeaf() || !root.getValue().isRequired()) {
argumentBuilder.executes(executor);
@ -400,7 +435,8 @@ public final class CloudBrigadierManager<C, S> {
private @NonNull CompletableFuture<Suggestions> buildSuggestions(
final @NonNull CommandArgument<C, ?> argument,
final com.mojang.brigadier.context.@NonNull CommandContext<S> s,
final @NonNull SuggestionsBuilder builder) {
final @NonNull SuggestionsBuilder builder
) {
final CommandContext<C> commandContext = this.dummyContextProvider.get();
final LinkedList<String> inputQueue = new LinkedList<>(Collections.singletonList(builder.getInput()));
final CommandPreprocessingContext<C> commandPreprocessingContext =

View file

@ -61,8 +61,10 @@ public final class BukkitBrigadierMapper<C> {
* @param commandManager The {@link BukkitCommandManager} to use for mapping
* @param brigadierManager The {@link CloudBrigadierManager} to use for mapping
*/
public BukkitBrigadierMapper(final @NonNull BukkitCommandManager<C> commandManager,
final @NonNull CloudBrigadierManager brigadierManager) {
public BukkitBrigadierMapper(
final @NonNull BukkitCommandManager<C> commandManager,
final @NonNull CloudBrigadierManager brigadierManager
) {
this.commandManager = commandManager;
this.brigadierManager = brigadierManager;
@ -90,8 +92,8 @@ public final class BukkitBrigadierMapper<C> {
this.mapComplexNMS(MultiplePlayerSelector.class, this.getEntitySelectorArgument(false, true));
} catch (final Exception e) {
this.commandManager.getOwningPlugin()
.getLogger()
.log(Level.WARNING, "Failed to map Bukkit types to NMS argument types", e);
.getLogger()
.log(Level.WARNING, "Failed to map Bukkit types to NMS argument types", e);
}
}
@ -100,8 +102,10 @@ public final class BukkitBrigadierMapper<C> {
* @param playersOnly Whether the selector is for players only (true), or for all entities (false)
* @return The NMS ArgumentType
*/
private Supplier<ArgumentType<?>> getEntitySelectorArgument(final boolean single,
final boolean playersOnly) {
private Supplier<ArgumentType<?>> getEntitySelectorArgument(
final boolean single,
final boolean playersOnly
) {
return () -> {
try {
final Constructor<?> constructor = this.getNMSArgument("Entity").getDeclaredConstructors()[0];
@ -132,8 +136,10 @@ public final class BukkitBrigadierMapper<C> {
* @param type Type to map
* @param constructor Constructor that construct the NMS argument type
*/
public void mapSimpleNMS(final @NonNull Class<?> type,
final @NonNull Constructor<?> constructor) {
public void mapSimpleNMS(
final @NonNull Class<?> type,
final @NonNull Constructor<?> constructor
) {
try {
this.brigadierManager.registerDefaultArgumentTypeSupplier(type, () -> {
try {
@ -145,9 +151,11 @@ public final class BukkitBrigadierMapper<C> {
});
} catch (final Exception e) {
this.commandManager.getOwningPlugin()
.getLogger()
.warning(String.format("Failed to map '%s' to a Mojang serializable argument type",
type.getCanonicalName()));
.getLogger()
.warning(String.format(
"Failed to map '%s' to a Mojang serializable argument type",
type.getCanonicalName()
));
}
}
@ -157,15 +165,20 @@ public final class BukkitBrigadierMapper<C> {
* @param type Type to map
* @param argumentTypeSupplier Supplier of the NMS argument type
*/
public void mapComplexNMS(final @NonNull Class<?> type,
final @NonNull Supplier<ArgumentType<?>> argumentTypeSupplier) {
public void mapComplexNMS(
final @NonNull Class<?> type,
final @NonNull Supplier<ArgumentType<?>> argumentTypeSupplier
) {
try {
this.brigadierManager.registerDefaultArgumentTypeSupplier(type, argumentTypeSupplier);
} catch (final Exception e) {
this.commandManager.getOwningPlugin()
.getLogger()
.warning(String.format("Failed to map '%s' to a Mojang serializable argument type",
type.getCanonicalName()));
.getLogger()
.warning(String.format(
"Failed to map '%s' to a Mojang serializable argument type",
type.getCanonicalName()
));
}
}
}

View file

@ -51,83 +51,91 @@ final class BukkitCommand<C> extends org.bukkit.command.Command implements Plugi
private final Command<C> cloudCommand;
@SuppressWarnings("unchecked")
BukkitCommand(final @NonNull String label,
final @NonNull List<@NonNull String> aliases,
final @NonNull Command<C> cloudCommand,
final @NonNull CommandArgument<C, ?> command,
final @NonNull BukkitCommandManager<C> manager) {
super(label,
cloudCommand.getCommandMeta().getOrDefault("description", ""),
"",
aliases);
BukkitCommand(
final @NonNull String label,
final @NonNull List<@NonNull String> aliases,
final @NonNull Command<C> cloudCommand,
final @NonNull CommandArgument<C, ?> command,
final @NonNull BukkitCommandManager<C> manager
) {
super(
label,
cloudCommand.getCommandMeta().getOrDefault("description", ""),
"",
aliases
);
this.command = command;
this.manager = manager;
this.cloudCommand = cloudCommand;
}
@Override
public boolean execute(final @NonNull CommandSender commandSender,
final @NonNull String s,
final @NonNull String @NonNull [] strings) {
public boolean execute(
final @NonNull CommandSender commandSender,
final @NonNull String s,
final @NonNull String @NonNull [] strings
) {
/* Join input */
final StringBuilder builder = new StringBuilder(this.command.getName());
for (final String string : strings) {
builder.append(" ").append(string);
}
final C sender = this.manager.getCommandSenderMapper().apply(commandSender);
this.manager.executeCommand(sender,
builder.toString())
.whenComplete(((commandResult, throwable) -> {
if (throwable != null) {
if (throwable instanceof CompletionException) {
throwable = throwable.getCause();
}
final Throwable finalThrowable = throwable;
if (throwable instanceof InvalidSyntaxException) {
this.manager.handleException(sender,
InvalidSyntaxException.class,
(InvalidSyntaxException) throwable, (c, e) ->
commandSender.sendMessage(
ChatColor.RED + "Invalid Command Syntax. "
+ "Correct command syntax is: "
+ ChatColor.GRAY + "/"
+ ((InvalidSyntaxException) finalThrowable)
.getCorrectSyntax())
);
} else if (throwable instanceof InvalidCommandSenderException) {
this.manager.handleException(sender,
InvalidCommandSenderException.class,
(InvalidCommandSenderException) throwable, (c, e) ->
commandSender.sendMessage(
ChatColor.RED + finalThrowable.getMessage())
);
} else if (throwable instanceof NoPermissionException) {
this.manager.handleException(sender,
NoPermissionException.class,
(NoPermissionException) throwable, (c, e) ->
commandSender.sendMessage(MESSAGE_NO_PERMS)
);
} else if (throwable instanceof NoSuchCommandException) {
this.manager.handleException(sender,
NoSuchCommandException.class,
(NoSuchCommandException) throwable, (c, e) ->
commandSender.sendMessage(MESSAGE_UNKNOWN_COMMAND)
);
} else if (throwable instanceof ArgumentParseException) {
this.manager.handleException(sender,
ArgumentParseException.class,
(ArgumentParseException) throwable, (c, e) ->
commandSender.sendMessage(
ChatColor.RED + "Invalid Command Argument: "
+ ChatColor.GRAY + finalThrowable.getCause()
.getMessage())
);
} else {
commandSender.sendMessage(throwable.getMessage());
throwable.printStackTrace();
}
this.manager.executeCommand(
sender,
builder.toString()
)
.whenComplete(((commandResult, throwable) -> {
if (throwable != null) {
if (throwable instanceof CompletionException) {
throwable = throwable.getCause();
}
}));
final Throwable finalThrowable = throwable;
if (throwable instanceof InvalidSyntaxException) {
this.manager.handleException(sender,
InvalidSyntaxException.class,
(InvalidSyntaxException) throwable, (c, e) ->
commandSender.sendMessage(
ChatColor.RED + "Invalid Command Syntax. "
+ "Correct command syntax is: "
+ ChatColor.GRAY + "/"
+ ((InvalidSyntaxException) finalThrowable)
.getCorrectSyntax())
);
} else if (throwable instanceof InvalidCommandSenderException) {
this.manager.handleException(sender,
InvalidCommandSenderException.class,
(InvalidCommandSenderException) throwable, (c, e) ->
commandSender.sendMessage(
ChatColor.RED + finalThrowable.getMessage())
);
} else if (throwable instanceof NoPermissionException) {
this.manager.handleException(sender,
NoPermissionException.class,
(NoPermissionException) throwable, (c, e) ->
commandSender.sendMessage(MESSAGE_NO_PERMS)
);
} else if (throwable instanceof NoSuchCommandException) {
this.manager.handleException(sender,
NoSuchCommandException.class,
(NoSuchCommandException) throwable, (c, e) ->
commandSender.sendMessage(MESSAGE_UNKNOWN_COMMAND)
);
} else if (throwable instanceof ArgumentParseException) {
this.manager.handleException(sender,
ArgumentParseException.class,
(ArgumentParseException) throwable, (c, e) ->
commandSender.sendMessage(
ChatColor.RED + "Invalid Command Argument: "
+ ChatColor.GRAY + finalThrowable.getCause()
.getMessage())
);
} else {
commandSender.sendMessage(throwable.getMessage());
throwable.printStackTrace();
}
}
}));
return true;
}
@ -137,16 +145,20 @@ final class BukkitCommand<C> extends org.bukkit.command.Command implements Plugi
}
@Override
public List<String> tabComplete(final @NonNull CommandSender sender,
final @NonNull String alias,
final @NonNull String @NonNull [] args)
public List<String> tabComplete(
final @NonNull CommandSender sender,
final @NonNull String alias,
final @NonNull String @NonNull [] args
)
throws IllegalArgumentException {
final StringBuilder builder = new StringBuilder(this.command.getName());
for (final String string : args) {
builder.append(" ").append(string);
}
return this.manager.suggest(this.manager.getCommandSenderMapper().apply(sender),
builder.toString());
return this.manager.suggest(
this.manager.getCommandSenderMapper().apply(sender),
builder.toString()
);
}
@Override

View file

@ -89,11 +89,13 @@ public class BukkitCommandManager<C> extends CommandManager<C> {
* @param backwardsCommandSenderMapper Function that maps the command sender type to {@link CommandSender}
* @throws Exception If the construction of the manager fails
*/
public BukkitCommandManager(final @NonNull Plugin owningPlugin,
final @NonNull Function<@NonNull CommandTree<C>,
@NonNull CommandExecutionCoordinator<C>> commandExecutionCoordinator,
final @NonNull Function<@NonNull CommandSender, @NonNull C> commandSenderMapper,
final @NonNull Function<@NonNull C, @NonNull CommandSender> backwardsCommandSenderMapper)
public BukkitCommandManager(
final @NonNull Plugin owningPlugin,
final @NonNull Function<@NonNull CommandTree<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 BukkitPluginRegistrationHandler<>());
((BukkitPluginRegistrationHandler<C>) this.getCommandRegistrationHandler()).initialize(this);
@ -108,14 +110,16 @@ public class BukkitCommandManager<C> extends CommandManager<C> {
int version = -1;
try {
final Matcher matcher = Pattern.compile("\\(MC: (\\d)\\.(\\d+)\\.?(\\d+?)?\\)")
.matcher(Bukkit.getVersion());
.matcher(Bukkit.getVersion());
if (matcher.find()) {
version = Integer.parseInt(matcher.toMatchResult().group(2),
VERSION_RADIX);
version = Integer.parseInt(
matcher.toMatchResult().group(2),
VERSION_RADIX
);
}
} catch (final Exception e) {
this.owningPlugin.getLogger().severe("Failed to determine Minecraft version "
+ "for cloud Bukkit capability detection");
+ "for cloud Bukkit capability detection");
}
this.minecraftVersion = version;
@ -132,13 +136,17 @@ public class BukkitCommandManager<C> extends CommandManager<C> {
/* Register Bukkit Parsers */
this.getParserRegistry().registerParserSupplier(TypeToken.get(World.class), params -> new WorldArgument.WorldParser<>());
this.getParserRegistry().registerParserSupplier(TypeToken.get(Material.class),
params -> new MaterialArgument.MaterialParser<>());
this.getParserRegistry().registerParserSupplier(
TypeToken.get(Material.class),
params -> new MaterialArgument.MaterialParser<>()
);
this.getParserRegistry()
.registerParserSupplier(TypeToken.get(Player.class), params -> new PlayerArgument.PlayerParser<>());
.registerParserSupplier(TypeToken.get(Player.class), params -> new PlayerArgument.PlayerParser<>());
this.getParserRegistry()
.registerParserSupplier(TypeToken.get(OfflinePlayer.class),
params -> new OfflinePlayerArgument.OfflinePlayerParser<>());
.registerParserSupplier(
TypeToken.get(OfflinePlayer.class),
params -> new OfflinePlayerArgument.OfflinePlayerParser<>()
);
/* Register Entity Selector Parsers */
this.getParserRegistry().registerParserSupplier(TypeToken.get(SingleEntitySelector.class), parserParameters ->
new SingleEntitySelectorArgument.SingleEntitySelectorParser<>());
@ -195,18 +203,20 @@ public class BukkitCommandManager<C> extends CommandManager<C> {
return this.backwardsCommandSenderMapper.apply(sender).hasPermission(permission);
}
protected final void setSplitAliases(final boolean value) {
this.splitAliases = value;
}
protected final boolean getSplitAliases() {
return this.splitAliases;
}
protected final void setSplitAliases(final boolean value) {
this.splitAliases = value;
}
protected final void checkBrigadierCompatibility() throws BrigadierFailureException {
if (!this.queryCapability(CloudBukkitCapabilities.BRIGADIER)) {
throw new BrigadierFailureException(BrigadierFailureReason.VERSION_TOO_LOW,
new IllegalArgumentException("Version: " + this.minecraftVersion));
throw new BrigadierFailureException(
BrigadierFailureReason.VERSION_TOO_LOW,
new IllegalArgumentException("Version: " + this.minecraftVersion)
);
}
}
@ -229,21 +239,27 @@ public class BukkitCommandManager<C> extends CommandManager<C> {
if (this.paper) {
if (this.minecraftVersion >= ASYNC_TAB_MINIMUM_VERSION) {
if (this.minecraftVersion >= PAPER_BRIGADIER_VERSION) {
return EnumSet.of(CloudBukkitCapabilities.NATIVE_BRIGADIER,
CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION,
CloudBukkitCapabilities.BRIGADIER);
return EnumSet.of(
CloudBukkitCapabilities.NATIVE_BRIGADIER,
CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION,
CloudBukkitCapabilities.BRIGADIER
);
} else if (this.minecraftVersion >= BRIGADIER_MINIMUM_VERSION) {
return EnumSet.of(CloudBukkitCapabilities.COMMODORE_BRIGADIER,
CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION,
CloudBukkitCapabilities.BRIGADIER);
return EnumSet.of(
CloudBukkitCapabilities.COMMODORE_BRIGADIER,
CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION,
CloudBukkitCapabilities.BRIGADIER
);
} else {
return EnumSet.of(CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION);
}
}
} else {
if (this.minecraftVersion >= BRIGADIER_MINIMUM_VERSION) {
return EnumSet.of(CloudBukkitCapabilities.COMMODORE_BRIGADIER,
CloudBukkitCapabilities.BRIGADIER);
return EnumSet.of(
CloudBukkitCapabilities.COMMODORE_BRIGADIER,
CloudBukkitCapabilities.BRIGADIER
);
}
}
return EnumSet.noneOf(CloudBukkitCapabilities.class);
@ -320,9 +336,11 @@ public class BukkitCommandManager<C> extends CommandManager<C> {
@Override
public String getMessage() {
return String.format("Could not initialize Brigadier mappings. Reason: %s (%s)",
this.reason.name().toLowerCase().replace("_", " "),
this.getCause() == null ? "" : this.getCause().getMessage());
return String.format(
"Could not initialize Brigadier mappings. Reason: %s (%s)",
this.reason.name().toLowerCase().replace("_", " "),
this.getCause() == null ? "" : this.getCause().getMessage()
);
}
}

View file

@ -41,4 +41,5 @@ final class BukkitPlayerSender extends BukkitCommandSender {
public @NonNull Player asPlayer() {
return (Player) this.getInternalSender();
}
}

View file

@ -79,7 +79,8 @@ public class BukkitPluginRegistrationHandler<C> implements CommandRegistrationHa
final String label;
final String prefixedLabel = String.format("%s:%s", this.bukkitCommandManager.getOwningPlugin().getName(),
commandArgument.getName()).toLowerCase();
commandArgument.getName()
).toLowerCase();
if (!(this.bukkitCommandManager.getCommandRegistrationHandler() instanceof CloudCommodoreManager)
&& bukkitCommands.containsKey(commandArgument.getName())) {
label = prefixedLabel;
@ -87,8 +88,7 @@ public class BukkitPluginRegistrationHandler<C> implements CommandRegistrationHa
label = commandArgument.getName();
}
@SuppressWarnings("unchecked")
final List<String> aliases = new ArrayList<>(((StaticArgument<C>) commandArgument).getAlternativeAliases());
@SuppressWarnings("unchecked") final List<String> aliases = new ArrayList<>(((StaticArgument<C>) commandArgument).getAlternativeAliases());
if (!label.contains(":")) {
aliases.add(prefixedLabel);
@ -99,11 +99,14 @@ public class BukkitPluginRegistrationHandler<C> implements CommandRegistrationHa
(this.bukkitCommandManager.getSplitAliases() ? Collections.<String>emptyList() : aliases),
(Command<C>) command,
(CommandArgument<C, ?>) commandArgument,
this.bukkitCommandManager);
this.bukkitCommandManager
);
this.registeredCommands.put(commandArgument, bukkitCommand);
this.commandMap.register(label,
this.bukkitCommandManager.getOwningPlugin().getName().toLowerCase(),
bukkitCommand);
this.commandMap.register(
label,
this.bukkitCommandManager.getOwningPlugin().getName().toLowerCase(),
bukkitCommand
);
this.registerExternal(label, command, bukkitCommand);
this.recognizedAliases.add(label);
@ -115,10 +118,12 @@ public class BukkitPluginRegistrationHandler<C> implements CommandRegistrationHa
Collections.emptyList(),
(Command<C>) command,
(CommandArgument<C, ?>) commandArgument,
this.bukkitCommandManager);
this.bukkitCommandManager
);
this.commandMap.register(alias, this.bukkitCommandManager.getOwningPlugin()
.getName().toLowerCase(),
bukkitCommand);
.getName().toLowerCase(),
bukkitCommand
);
this.registerExternal(alias, command, aliasCommand);
this.recognizedAliases.add(alias);
}
@ -138,9 +143,11 @@ public class BukkitPluginRegistrationHandler<C> implements CommandRegistrationHa
return this.recognizedAliases.contains(alias);
}
protected void registerExternal(final @NonNull String label,
final @NonNull Command<?> command,
final @NonNull BukkitCommand<C> bukkitCommand) {
protected void registerExternal(
final @NonNull String label,
final @NonNull Command<?> command,
final @NonNull BukkitCommand<C> bukkitCommand
) {
}
}

View file

@ -41,12 +41,12 @@ final class BukkitSynchronizer implements TaskSynchronizer {
@Override
public <I> CompletableFuture<Void> runSynchronous(final @NonNull I input, final @NonNull TaskConsumer<I> consumer) {
final CompletableFuture<Void> future = new CompletableFuture<>();
this.plugin.getServer().getScheduler().runTask(this.plugin, () -> {
consumer.accept(input);
future.complete(null);
});
return future;
final CompletableFuture<Void> future = new CompletableFuture<>();
this.plugin.getServer().getScheduler().runTask(this.plugin, () -> {
consumer.accept(input);
future.complete(null);
});
return future;
}
@Override

View file

@ -27,5 +27,8 @@ package cloud.commandframework.bukkit;
* Capabilities for the Bukkit module
*/
public enum CloudBukkitCapabilities {
BRIGADIER, COMMODORE_BRIGADIER, NATIVE_BRIGADIER, ASYNCHRONOUS_COMPLETION
BRIGADIER,
COMMODORE_BRIGADIER,
NATIVE_BRIGADIER,
ASYNCHRONOUS_COMPLETION
}

View file

@ -49,7 +49,7 @@ class CloudCommodoreManager<C> extends BukkitPluginRegistrationHandler<C> {
throws BukkitCommandManager.BrigadierFailureException {
if (!CommodoreProvider.isSupported()) {
throw new BukkitCommandManager.BrigadierFailureException(BukkitCommandManager
.BrigadierFailureReason.COMMODORE_NOT_PRESENT);
.BrigadierFailureReason.COMMODORE_NOT_PRESENT);
}
this.commandManager = commandManager;
this.commodore = CommodoreProvider.getCommodore(commandManager.getOwningPlugin());
@ -59,21 +59,27 @@ class CloudCommodoreManager<C> extends BukkitPluginRegistrationHandler<C> {
}
@Override
protected void registerExternal(final @NonNull String label,
final @NonNull Command<?> command,
final @NonNull BukkitCommand<C> bukkitCommand) {
protected void registerExternal(
final @NonNull String label,
final @NonNull Command<?> command,
final @NonNull BukkitCommand<C> bukkitCommand
) {
this.registerWithCommodore(label, command);
this.registerWithCommodore(String.format("%s:%s", bukkitCommand.getPlugin().getName(), label).toLowerCase(), command);
}
private void registerWithCommodore(final @NonNull String label,
final @NonNull Command<?> command) {
private void registerWithCommodore(
final @NonNull String label,
final @NonNull Command<?> command
) {
final com.mojang.brigadier.Command<?> cmd = o -> 1;
final LiteralCommandNode<?> literalCommandNode = this.brigadierManager
.<Object>createLiteralCommandNode(label, command, (o, p) -> {
final CommandSender sender = this.commodore.getBukkitSender(o);
return this.commandManager.hasPermission(this.commandManager.getCommandSenderMapper().apply(sender),
(CommandPermission) p);
return this.commandManager.hasPermission(
this.commandManager.getCommandSenderMapper().apply(sender),
(CommandPermission) p
);
}, false, cmd);
final CommandNode existingNode = this.commodore.getDispatcher().findNode(Collections.singletonList(label));
if (existingNode != null) {

View file

@ -43,8 +43,10 @@ public abstract class EntitySelector {
* @param selector The input string used to create this selector
* @param entities The List of Bukkit {@link Entity entities} to construct the {@link EntitySelector} from
*/
public EntitySelector(final @NonNull String selector,
final @NonNull List<@NonNull Entity> entities) {
public EntitySelector(
final @NonNull String selector,
final @NonNull List<@NonNull Entity> entities
) {
this.selector = selector;
this.entities = entities;
}

View file

@ -34,8 +34,10 @@ public class MultipleEntitySelector extends EntitySelector {
* @param selector The input string used to create this selector
* @param entities The List of Bukkit {@link Entity}s to construct the {@link EntitySelector} from
*/
public MultipleEntitySelector(final @NonNull String selector,
final @NonNull List<@NonNull Entity> entities) {
public MultipleEntitySelector(
final @NonNull String selector,
final @NonNull List<@NonNull Entity> entities
) {
super(selector, entities);
}

View file

@ -42,8 +42,10 @@ public class MultiplePlayerSelector extends MultipleEntitySelector {
* @param selector The input string used to create this selector
* @param entities The List of Bukkit {@link Entity}s to construct the {@link EntitySelector} from
*/
public MultiplePlayerSelector(final @NonNull String selector,
final @NonNull List<@NonNull Entity> entities) {
public MultiplePlayerSelector(
final @NonNull String selector,
final @NonNull List<@NonNull Entity> entities
) {
super(selector, entities);
entities.forEach(e -> {
if (e.getType() != EntityType.PLAYER) {

View file

@ -36,8 +36,10 @@ public final class SingleEntitySelector extends MultipleEntitySelector {
* @param selector The input string used to create this selector
* @param entities The List of Bukkit {@link Entity entities} to construct the {@link EntitySelector} from
*/
public SingleEntitySelector(final @NonNull String selector,
final @NonNull List<@NonNull Entity> entities) {
public SingleEntitySelector(
final @NonNull String selector,
final @NonNull List<@NonNull Entity> entities
) {
super(selector, entities);
if (entities.size() > 1) {
throw new IllegalArgumentException("More than 1 entity selected in single entity selector.");

View file

@ -37,8 +37,10 @@ public final class SinglePlayerSelector extends MultiplePlayerSelector {
* @param selector The input string used to create this selector
* @param entities The List of Bukkit {@link Entity entities} to construct the {@link EntitySelector} from
*/
public SinglePlayerSelector(final @NonNull String selector,
final @NonNull List<@NonNull Entity> entities) {
public SinglePlayerSelector(
final @NonNull String selector,
final @NonNull List<@NonNull Entity> entities
) {
super(selector, entities);
if (getPlayers().size() > 1) {
throw new IllegalArgumentException("More than 1 player selected in single player selector.");

View file

@ -44,11 +44,13 @@ import java.util.stream.Collectors;
*/
public class MaterialArgument<C> extends CommandArgument<C, Material> {
protected MaterialArgument(final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider) {
protected MaterialArgument(
final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider
) {
super(required, name, new MaterialParser<>(), defaultValue, Material.class, suggestionsProvider);
}
@ -93,8 +95,10 @@ public class MaterialArgument<C> extends CommandArgument<C, Material> {
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull CommandArgument<C, Material> optional(final @NonNull String name,
final @NonNull Material material) {
public static <C> @NonNull CommandArgument<C, Material> optional(
final @NonNull String name,
final @NonNull Material material
) {
return MaterialArgument.<C>newBuilder(name).asOptionalWithDefault(material.name().toLowerCase()).build();
}
@ -110,8 +114,10 @@ public class MaterialArgument<C> extends CommandArgument<C, Material> {
public static final class MaterialParser<C> implements ArgumentParser<C, Material> {
@Override
public @NonNull ArgumentParseResult<Material> parse(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
public @NonNull ArgumentParseResult<Material> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
@ -154,8 +160,9 @@ public class MaterialArgument<C> extends CommandArgument<C, Material> {
@Override
public String getMessage() {
return EnumSet.allOf(Material.class).stream().map(Material::name).map(String::toLowerCase)
.collect(Collectors.joining(", "));
.collect(Collectors.joining(", "));
}
}
}

View file

@ -49,11 +49,13 @@ import java.util.function.BiFunction;
@SuppressWarnings("unused")
public final class OfflinePlayerArgument<C> extends CommandArgument<C, OfflinePlayer> {
private OfflinePlayerArgument(final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider) {
private OfflinePlayerArgument(
final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider
) {
super(required, name, new OfflinePlayerParser<>(), defaultValue, OfflinePlayer.class, suggestionsProvider);
}
@ -98,8 +100,10 @@ public final class OfflinePlayerArgument<C> extends CommandArgument<C, OfflinePl
* @param <C> Command sender type
* @return Created component
*/
public static <C> @NonNull CommandArgument<C, OfflinePlayer> optional(final @NonNull String name,
final @NonNull String defaultPlayer) {
public static <C> @NonNull CommandArgument<C, OfflinePlayer> optional(
final @NonNull String name,
final @NonNull String defaultPlayer
) {
return OfflinePlayerArgument.<C>newBuilder(name).asOptionalWithDefault(defaultPlayer).build();
}
@ -118,7 +122,8 @@ public final class OfflinePlayerArgument<C> extends CommandArgument<C, OfflinePl
@Override
public @NonNull OfflinePlayerArgument<C> build() {
return new OfflinePlayerArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(),
this.getSuggestionsProvider());
this.getSuggestionsProvider()
);
}
}
@ -127,8 +132,10 @@ public final class OfflinePlayerArgument<C> extends CommandArgument<C, OfflinePl
public static final class OfflinePlayerParser<C> implements ArgumentParser<C, OfflinePlayer> {
@Override
public @NonNull ArgumentParseResult<OfflinePlayer> parse(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<String> inputQueue) {
public @NonNull ArgumentParseResult<OfflinePlayer> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
@ -146,8 +153,10 @@ public final class OfflinePlayerArgument<C> extends CommandArgument<C, OfflinePl
}
@Override
public @NonNull List<@NonNull String> suggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull String input) {
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
List<String> output = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers()) {
@ -156,6 +165,7 @@ public final class OfflinePlayerArgument<C> extends CommandArgument<C, OfflinePl
return output;
}
}
@ -188,5 +198,7 @@ public final class OfflinePlayerArgument<C> extends CommandArgument<C, OfflinePl
public String getMessage() {
return String.format("No player found for input '%s'.", input);
}
}
}

View file

@ -45,11 +45,13 @@ import java.util.function.BiFunction;
@SuppressWarnings("unused")
public final class PlayerArgument<C> extends CommandArgument<C, Player> {
private PlayerArgument(final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider) {
private PlayerArgument(
final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider
) {
super(required, name, new PlayerParser<>(), defaultValue, Player.class, suggestionsProvider);
}
@ -94,8 +96,10 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
* @param <C> Command sender type
* @return Created component
*/
public static <C> @NonNull CommandArgument<C, Player> optional(final @NonNull String name,
final @NonNull String defaultPlayer) {
public static <C> @NonNull CommandArgument<C, Player> optional(
final @NonNull String name,
final @NonNull String defaultPlayer
) {
return PlayerArgument.<C>newBuilder(name).asOptionalWithDefault(defaultPlayer).build();
}
@ -122,8 +126,10 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
public static final class PlayerParser<C> implements ArgumentParser<C, Player> {
@Override
public @NonNull ArgumentParseResult<Player> parse(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
public @NonNull ArgumentParseResult<Player> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
@ -141,8 +147,10 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
}
@Override
public @NonNull List<@NonNull String> suggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull String input) {
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
List<String> output = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers()) {
@ -151,6 +159,7 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
return output;
}
}
@ -183,5 +192,7 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
public String getMessage() {
return String.format("No player found for input '%s'.", input);
}
}
}

View file

@ -44,10 +44,12 @@ import java.util.stream.Collectors;
*/
public class WorldArgument<C> extends CommandArgument<C, World> {
protected WorldArgument(final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider) {
protected WorldArgument(
final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider
) {
super(required, name, new WorldParser<>(), defaultValue, World.class, suggestionsProvider);
}
@ -92,8 +94,10 @@ public class WorldArgument<C> extends CommandArgument<C, World> {
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull CommandArgument<C, World> optional(final @NonNull String name,
final @NonNull String defaultValue) {
public static <C> @NonNull CommandArgument<C, World> optional(
final @NonNull String name,
final @NonNull String defaultValue
) {
return WorldArgument.<C>newBuilder(name).asOptionalWithDefault(defaultValue).build();
}
@ -108,14 +112,17 @@ public class WorldArgument<C> extends CommandArgument<C, World> {
public @NonNull CommandArgument<C, World> build() {
return new WorldArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(), this.getSuggestionsProvider());
}
}
public static final class WorldParser<C> implements ArgumentParser<C, World> {
@Override
public @NonNull ArgumentParseResult<World> parse(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<String> inputQueue) {
public @NonNull ArgumentParseResult<World> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
@ -164,6 +171,7 @@ public class WorldArgument<C> extends CommandArgument<C, World> {
public String getMessage() {
return String.format("'%s' is not a valid Minecraft world", this.input);
}
}
}

View file

@ -41,13 +41,16 @@ import java.util.function.BiFunction;
public final class MultipleEntitySelectorArgument<C> extends CommandArgument<C, MultipleEntitySelector> {
private MultipleEntitySelectorArgument(final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider) {
private MultipleEntitySelectorArgument(
final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider
) {
super(required, name, new MultipleEntitySelectorParser<>(), defaultValue, MultipleEntitySelector.class,
suggestionsProvider);
suggestionsProvider
);
}
/**
@ -91,8 +94,10 @@ public final class MultipleEntitySelectorArgument<C> extends CommandArgument<C,
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull CommandArgument<C, MultipleEntitySelector> optional(final @NonNull String name,
final @NonNull String defaultEntitySelector) {
public static <C> @NonNull CommandArgument<C, MultipleEntitySelector> optional(
final @NonNull String name,
final @NonNull String defaultEntitySelector
) {
return MultipleEntitySelectorArgument.<C>newBuilder(name).asOptionalWithDefault(defaultEntitySelector).build();
}
@ -111,7 +116,8 @@ public final class MultipleEntitySelectorArgument<C> extends CommandArgument<C,
@Override
public @NonNull MultipleEntitySelectorArgument<C> build() {
return new MultipleEntitySelectorArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(),
this.getSuggestionsProvider());
this.getSuggestionsProvider()
);
}
}
@ -120,8 +126,10 @@ public final class MultipleEntitySelectorArgument<C> extends CommandArgument<C,
public static final class MultipleEntitySelectorParser<C> implements ArgumentParser<C, MultipleEntitySelector> {
@Override
public @NonNull ArgumentParseResult<MultipleEntitySelector> parse(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
public @NonNull ArgumentParseResult<MultipleEntitySelector> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue
) {
if (!commandContext.<Set<CloudBukkitCapabilities>>get("CloudBukkitCapabilities").contains(
CloudBukkitCapabilities.BRIGADIER)) {
return ArgumentParseResult.failure(
@ -142,6 +150,7 @@ public final class MultipleEntitySelectorArgument<C> extends CommandArgument<C,
return ArgumentParseResult.success(new MultipleEntitySelector(input, entities));
}
}
}

View file

@ -45,13 +45,16 @@ import java.util.function.BiFunction;
public final class MultiplePlayerSelectorArgument<C> extends CommandArgument<C, MultiplePlayerSelector> {
private MultiplePlayerSelectorArgument(final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider) {
private MultiplePlayerSelectorArgument(
final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider
) {
super(required, name, new MultiplePlayerSelectorParser<>(), defaultValue, MultiplePlayerSelector.class,
suggestionsProvider);
suggestionsProvider
);
}
/**
@ -95,8 +98,10 @@ public final class MultiplePlayerSelectorArgument<C> extends CommandArgument<C,
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull CommandArgument<C, MultiplePlayerSelector> optional(final @NonNull String name,
final @NonNull String defaultEntitySelector) {
public static <C> @NonNull CommandArgument<C, MultiplePlayerSelector> optional(
final @NonNull String name,
final @NonNull String defaultEntitySelector
) {
return MultiplePlayerSelectorArgument.<C>newBuilder(name).asOptionalWithDefault(defaultEntitySelector).build();
}
@ -115,7 +120,8 @@ public final class MultiplePlayerSelectorArgument<C> extends CommandArgument<C,
@Override
public @NonNull MultiplePlayerSelectorArgument<C> build() {
return new MultiplePlayerSelectorArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(),
this.getSuggestionsProvider());
this.getSuggestionsProvider()
);
}
}
@ -124,8 +130,10 @@ public final class MultiplePlayerSelectorArgument<C> extends CommandArgument<C,
public static final class MultiplePlayerSelectorParser<C> implements ArgumentParser<C, MultiplePlayerSelector> {
@Override
public @NonNull ArgumentParseResult<MultiplePlayerSelector> parse(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
public @NonNull ArgumentParseResult<MultiplePlayerSelector> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
@ -160,8 +168,10 @@ public final class MultiplePlayerSelectorArgument<C> extends CommandArgument<C,
}
@Override
public @NonNull List<@NonNull String> suggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull String input) {
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
List<String> output = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers()) {
@ -170,6 +180,7 @@ public final class MultiplePlayerSelectorArgument<C> extends CommandArgument<C,
return output;
}
}
}

View file

@ -54,4 +54,5 @@ public final class SelectorParseException extends IllegalArgumentException {
public String getMessage() {
return String.format("Selector '%s' is malformed.", input);
}
}

View file

@ -41,11 +41,13 @@ import java.util.function.BiFunction;
public final class SingleEntitySelectorArgument<C> extends CommandArgument<C, SingleEntitySelector> {
private SingleEntitySelectorArgument(final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider) {
private SingleEntitySelectorArgument(
final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider
) {
super(required, name, new SingleEntitySelectorParser<>(), defaultValue, SingleEntitySelector.class, suggestionsProvider);
}
@ -90,8 +92,10 @@ public final class SingleEntitySelectorArgument<C> extends CommandArgument<C, Si
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull CommandArgument<C, SingleEntitySelector> optional(final @NonNull String name,
final @NonNull String defaultEntitySelector) {
public static <C> @NonNull CommandArgument<C, SingleEntitySelector> optional(
final @NonNull String name,
final @NonNull String defaultEntitySelector
) {
return SingleEntitySelectorArgument.<C>newBuilder(name).asOptionalWithDefault(defaultEntitySelector).build();
}
@ -110,7 +114,8 @@ public final class SingleEntitySelectorArgument<C> extends CommandArgument<C, Si
@Override
public @NonNull SingleEntitySelectorArgument<C> build() {
return new SingleEntitySelectorArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(),
this.getSuggestionsProvider());
this.getSuggestionsProvider()
);
}
}
@ -119,8 +124,10 @@ public final class SingleEntitySelectorArgument<C> extends CommandArgument<C, Si
public static final class SingleEntitySelectorParser<C> implements ArgumentParser<C, SingleEntitySelector> {
@Override
public @NonNull ArgumentParseResult<SingleEntitySelector> parse(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
public @NonNull ArgumentParseResult<SingleEntitySelector> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue
) {
if (!commandContext.<Set<CloudBukkitCapabilities>>get("CloudBukkitCapabilities").contains(
CloudBukkitCapabilities.BRIGADIER)) {
return ArgumentParseResult.failure(
@ -146,6 +153,7 @@ public final class SingleEntitySelectorArgument<C> extends CommandArgument<C, Si
return ArgumentParseResult.success(new SingleEntitySelector(input, entities));
}
}
}

View file

@ -45,11 +45,13 @@ import java.util.function.BiFunction;
public final class SinglePlayerSelectorArgument<C> extends CommandArgument<C, SinglePlayerSelector> {
private SinglePlayerSelectorArgument(final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider) {
private SinglePlayerSelectorArgument(
final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider
) {
super(required, name, new SinglePlayerSelectorParser<>(), defaultValue, SinglePlayerSelector.class, suggestionsProvider);
}
@ -94,8 +96,10 @@ public final class SinglePlayerSelectorArgument<C> extends CommandArgument<C, Si
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull CommandArgument<C, SinglePlayerSelector> optional(final @NonNull String name,
final @NonNull String defaultEntitySelector) {
public static <C> @NonNull CommandArgument<C, SinglePlayerSelector> optional(
final @NonNull String name,
final @NonNull String defaultEntitySelector
) {
return SinglePlayerSelectorArgument.<C>newBuilder(name).asOptionalWithDefault(defaultEntitySelector).build();
}
@ -114,7 +118,8 @@ public final class SinglePlayerSelectorArgument<C> extends CommandArgument<C, Si
@Override
public @NonNull SinglePlayerSelectorArgument<C> build() {
return new SinglePlayerSelectorArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(),
this.getSuggestionsProvider());
this.getSuggestionsProvider()
);
}
}
@ -123,8 +128,10 @@ public final class SinglePlayerSelectorArgument<C> extends CommandArgument<C, Si
public static final class SinglePlayerSelectorParser<C> implements ArgumentParser<C, SinglePlayerSelector> {
@Override
public @NonNull ArgumentParseResult<SinglePlayerSelector> parse(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
public @NonNull ArgumentParseResult<SinglePlayerSelector> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
@ -163,8 +170,10 @@ public final class SinglePlayerSelectorArgument<C> extends CommandArgument<C, Si
}
@Override
public @NonNull List<@NonNull String> suggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull String input) {
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
List<String> output = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers()) {
@ -173,6 +182,7 @@ public final class SinglePlayerSelectorArgument<C> extends CommandArgument<C, Si
return output;
}
}
}

View file

@ -51,12 +51,16 @@ public final class BungeeCommand<C> extends Command implements TabExecutor {
private final cloud.commandframework.Command<C> cloudCommand;
@SuppressWarnings("unchecked")
BungeeCommand(final cloud.commandframework.@NonNull Command<C> cloudCommand,
final @NonNull CommandArgument<C, ?> command,
final @NonNull BungeeCommandManager<C> manager) {
super(command.getName(),
cloudCommand.getCommandPermission().toString(),
((StaticArgument<C>) command).getAlternativeAliases().toArray(new String[0]));
BungeeCommand(
final cloud.commandframework.@NonNull Command<C> cloudCommand,
final @NonNull CommandArgument<C, ?> command,
final @NonNull BungeeCommandManager<C> manager
) {
super(
command.getName(),
cloudCommand.getCommandPermission().toString(),
((StaticArgument<C>) command).getAlternativeAliases().toArray(new String[0])
);
this.command = command;
this.manager = manager;
this.cloudCommand = cloudCommand;
@ -70,86 +74,102 @@ public final class BungeeCommand<C> extends Command implements TabExecutor {
builder.append(" ").append(string);
}
final C sender = this.manager.getCommandSenderMapper().apply(commandSender);
this.manager.executeCommand(sender,
builder.toString())
.whenComplete(((commandResult, throwable) -> {
if (throwable != null) {
if (throwable instanceof CompletionException) {
throwable = throwable.getCause();
}
final Throwable finalThrowable = throwable;
if (throwable instanceof InvalidSyntaxException) {
this.manager.handleException(sender,
InvalidSyntaxException.class,
(InvalidSyntaxException) throwable, (c, e) ->
commandSender.sendMessage(
new ComponentBuilder("Invalid Command Syntax. Correct command syntax is: ")
.color(ChatColor.RED)
.append("/")
.color(ChatColor.GRAY)
.append(((InvalidSyntaxException) finalThrowable).getCorrectSyntax())
.color(ChatColor.GRAY)
.create()
)
);
} else if (throwable instanceof InvalidCommandSenderException) {
final Throwable finalThrowable1 = throwable;
this.manager.handleException(sender,
InvalidCommandSenderException.class,
(InvalidCommandSenderException) throwable, (c, e) ->
commandSender.sendMessage(new ComponentBuilder(finalThrowable1.getMessage())
.color(ChatColor.RED)
.create())
);
} else if (throwable instanceof NoPermissionException) {
this.manager.handleException(sender,
NoPermissionException.class,
(NoPermissionException) throwable, (c, e) ->
commandSender.sendMessage(new ComponentBuilder(MESSAGE_NO_PERMS)
.color(ChatColor.WHITE)
.create())
);
} else if (throwable instanceof NoSuchCommandException) {
this.manager.handleException(sender,
NoSuchCommandException.class,
(NoSuchCommandException) throwable, (c, e) ->
commandSender.sendMessage(new ComponentBuilder(MESSAGE_UNKNOWN_COMMAND)
.color(ChatColor.WHITE)
.create())
);
} else if (throwable instanceof ArgumentParseException) {
this.manager.handleException(sender,
ArgumentParseException.class,
(ArgumentParseException) throwable, (c, e) ->
commandSender.sendMessage(new ComponentBuilder("Invalid Command Argument: ")
.color(ChatColor.GRAY)
.append(finalThrowable.getCause().getMessage())
.create())
);
} else {
commandSender.sendMessage(new ComponentBuilder(throwable.getMessage()).create());
this.manager.getOwningPlugin().getLogger().warning(
String.format("(Cloud) Unknown exception type '%s' with cause '%s'",
throwable.getClass().getCanonicalName(),
throwable.getCause() == null ? "none"
: throwable.getCause()
.getClass().getCanonicalName())
);
throwable.printStackTrace();
}
}
}));
this.manager.executeCommand(
sender,
builder.toString()
)
.whenComplete(((commandResult, throwable) -> {
if (throwable != null) {
if (throwable instanceof CompletionException) {
throwable = throwable.getCause();
}
final Throwable finalThrowable = throwable;
if (throwable instanceof InvalidSyntaxException) {
this.manager.handleException(sender,
InvalidSyntaxException.class,
(InvalidSyntaxException) throwable, (c, e) ->
commandSender.sendMessage(
new ComponentBuilder(
"Invalid Command Syntax. Correct command syntax is: ")
.color(ChatColor.RED)
.append("/")
.color(ChatColor.GRAY)
.append(((InvalidSyntaxException) finalThrowable)
.getCorrectSyntax())
.color(ChatColor.GRAY)
.create()
)
);
} else if (throwable instanceof InvalidCommandSenderException) {
final Throwable finalThrowable1 = throwable;
this.manager.handleException(sender,
InvalidCommandSenderException.class,
(InvalidCommandSenderException) throwable, (c, e) ->
commandSender.sendMessage(new ComponentBuilder(
finalThrowable1.getMessage())
.color(ChatColor.RED)
.create())
);
} else if (throwable instanceof NoPermissionException) {
this.manager.handleException(sender,
NoPermissionException.class,
(NoPermissionException) throwable, (c, e) ->
commandSender.sendMessage(new ComponentBuilder(
MESSAGE_NO_PERMS)
.color(ChatColor.WHITE)
.create())
);
} else if (throwable instanceof NoSuchCommandException) {
this.manager.handleException(sender,
NoSuchCommandException.class,
(NoSuchCommandException) throwable, (c, e) ->
commandSender.sendMessage(new ComponentBuilder(
MESSAGE_UNKNOWN_COMMAND)
.color(ChatColor.WHITE)
.create())
);
} else if (throwable instanceof ArgumentParseException) {
this.manager.handleException(sender,
ArgumentParseException.class,
(ArgumentParseException) throwable, (c, e) ->
commandSender.sendMessage(new ComponentBuilder(
"Invalid Command Argument: ")
.color(ChatColor.GRAY)
.append(finalThrowable
.getCause()
.getMessage())
.create())
);
} else {
commandSender.sendMessage(new ComponentBuilder(throwable.getMessage()).create());
this.manager.getOwningPlugin().getLogger().warning(
String.format(
"(Cloud) Unknown exception type '%s' with cause '%s'",
throwable.getClass().getCanonicalName(),
throwable.getCause() == null ? "none"
: throwable.getCause()
.getClass().getCanonicalName()
)
);
throwable.printStackTrace();
}
}
}));
}
@Override
public Iterable<String> onTabComplete(final CommandSender sender,
final String[] args) {
public Iterable<String> onTabComplete(
final CommandSender sender,
final String[] args
) {
final StringBuilder builder = new StringBuilder(this.command.getName());
for (final String string : args) {
builder.append(" ").append(string);
}
return this.manager.suggest(this.manager.getCommandSenderMapper().apply(sender),
builder.toString());
return this.manager.suggest(
this.manager.getCommandSenderMapper().apply(sender),
builder.toString()
);
}
}

View file

@ -48,11 +48,13 @@ public class BungeeCommandManager<C> extends CommandManager<C> {
* @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,
final @NonNull Function<@NonNull CommandTree<C>,
@NonNull CommandExecutionCoordinator<C>> commandExecutionCoordinator,
final @NonNull Function<@NonNull CommandSender, @NonNull C> commandSenderMapper,
final @NonNull Function<@NonNull C, @NonNull CommandSender> backwardsCommandSenderMapper)
public BungeeCommandManager(
final @NonNull Plugin owningPlugin,
final @NonNull Function<@NonNull CommandTree<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);
@ -62,8 +64,10 @@ public class BungeeCommandManager<C> extends CommandManager<C> {
}
@Override
public final boolean hasPermission(final @NonNull C sender,
final @NonNull String permission) {
public final boolean hasPermission(
final @NonNull C sender,
final @NonNull String permission
) {
if (permission.isEmpty()) {
return true;
}

View file

@ -54,10 +54,11 @@ final class BungeePluginRegistrationHandler<C> implements CommandRegistrationHan
@SuppressWarnings("unchecked") final BungeeCommand<C> bungeeCommand = new BungeeCommand<>(
(Command<C>) command,
(CommandArgument<C, ?>) commandArgument,
this.bungeeCommandManager);
this.bungeeCommandManager
);
this.registeredCommands.put(commandArgument, bungeeCommand);
this.bungeeCommandManager.getOwningPlugin().getProxy().getPluginManager()
.registerCommand(this.bungeeCommandManager.getOwningPlugin(), bungeeCommand);
.registerCommand(this.bungeeCommandManager.getOwningPlugin(), bungeeCommand);
return true;
}

View file

@ -50,82 +50,88 @@ final class CloudburstCommand<C> extends PluginCommand<Plugin> {
private final CloudburstCommandManager<C> manager;
private final Command<C> cloudCommand;
CloudburstCommand(final @NonNull String label,
final @NonNull List<@NonNull String> aliases,
final @NonNull Command<C> cloudCommand,
final @NonNull CommandArgument<C, ?> command,
final @NonNull CloudburstCommandManager<C> manager) {
CloudburstCommand(
final @NonNull String label,
final @NonNull List<@NonNull String> aliases,
final @NonNull Command<C> cloudCommand,
final @NonNull CommandArgument<C, ?> command,
final @NonNull CloudburstCommandManager<C> manager
) {
super(manager.getOwningPlugin(), CommandData.builder(label)
.addAliases(aliases.toArray(new String[0]))
.addPermission(cloudCommand.getCommandPermission().toString())
.setDescription(cloudCommand.getCommandMeta().getOrDefault("description", ""))
.build());
.addAliases(aliases.toArray(new String[0]))
.addPermission(cloudCommand.getCommandPermission().toString())
.setDescription(cloudCommand.getCommandMeta().getOrDefault("description", ""))
.build());
this.command = command;
this.manager = manager;
this.cloudCommand = cloudCommand;
}
@Override
public boolean execute(final CommandSender commandSender,
final String commandLabel,
final String[] strings) {
public boolean execute(
final CommandSender commandSender,
final String commandLabel,
final String[] strings
) {
/* Join input */
final StringBuilder builder = new StringBuilder(this.command.getName());
for (final String string : strings) {
builder.append(" ").append(string);
}
final C sender = this.manager.getCommandSenderMapper().apply(commandSender);
this.manager.executeCommand(sender,
builder.toString())
.whenComplete(((commandResult, throwable) -> {
if (throwable != null) {
if (throwable instanceof CompletionException) {
throwable = throwable.getCause();
}
final Throwable finalThrowable = throwable;
if (throwable instanceof InvalidSyntaxException) {
this.manager.handleException(sender,
InvalidSyntaxException.class,
(InvalidSyntaxException) throwable, (c, e) ->
commandSender.sendMessage(
"Invalid Command Syntax. "
+ "Correct command syntax is: "
+ "/"
+ ((InvalidSyntaxException) finalThrowable)
.getCorrectSyntax())
);
} else if (throwable instanceof InvalidCommandSenderException) {
this.manager.handleException(sender,
InvalidCommandSenderException.class,
(InvalidCommandSenderException) throwable, (c, e) ->
commandSender.sendMessage(finalThrowable.getMessage())
);
} else if (throwable instanceof NoPermissionException) {
this.manager.handleException(sender,
NoPermissionException.class,
(NoPermissionException) throwable, (c, e) ->
commandSender.sendMessage(MESSAGE_NO_PERMS)
);
} else if (throwable instanceof NoSuchCommandException) {
this.manager.handleException(sender,
NoSuchCommandException.class,
(NoSuchCommandException) throwable, (c, e) ->
commandSender.sendMessage(MESSAGE_UNKNOWN_COMMAND)
);
} else if (throwable instanceof ArgumentParseException) {
this.manager.handleException(sender,
ArgumentParseException.class,
(ArgumentParseException) throwable, (c, e) ->
commandSender.sendMessage(
"Invalid Command Argument: "
+ finalThrowable.getCause().getMessage())
);
} else {
commandSender.sendMessage(throwable.getMessage());
throwable.printStackTrace();
}
this.manager.executeCommand(
sender,
builder.toString()
)
.whenComplete(((commandResult, throwable) -> {
if (throwable != null) {
if (throwable instanceof CompletionException) {
throwable = throwable.getCause();
}
}));
final Throwable finalThrowable = throwable;
if (throwable instanceof InvalidSyntaxException) {
this.manager.handleException(sender,
InvalidSyntaxException.class,
(InvalidSyntaxException) throwable, (c, e) ->
commandSender.sendMessage(
"Invalid Command Syntax. "
+ "Correct command syntax is: "
+ "/"
+ ((InvalidSyntaxException) finalThrowable)
.getCorrectSyntax())
);
} else if (throwable instanceof InvalidCommandSenderException) {
this.manager.handleException(sender,
InvalidCommandSenderException.class,
(InvalidCommandSenderException) throwable, (c, e) ->
commandSender.sendMessage(finalThrowable.getMessage())
);
} else if (throwable instanceof NoPermissionException) {
this.manager.handleException(sender,
NoPermissionException.class,
(NoPermissionException) throwable, (c, e) ->
commandSender.sendMessage(MESSAGE_NO_PERMS)
);
} else if (throwable instanceof NoSuchCommandException) {
this.manager.handleException(sender,
NoSuchCommandException.class,
(NoSuchCommandException) throwable, (c, e) ->
commandSender.sendMessage(MESSAGE_UNKNOWN_COMMAND)
);
} else if (throwable instanceof ArgumentParseException) {
this.manager.handleException(sender,
ArgumentParseException.class,
(ArgumentParseException) throwable, (c, e) ->
commandSender.sendMessage(
"Invalid Command Argument: "
+ finalThrowable.getCause().getMessage())
);
} else {
commandSender.sendMessage(throwable.getMessage());
throwable.printStackTrace();
}
}
}));
return true;
}

View file

@ -54,11 +54,13 @@ public class CloudburstCommandManager<C> extends CommandManager<C> {
* @param commandSenderMapper Function that maps {@link CommandSender} to the command sender type
* @param backwardsCommandSenderMapper Function that maps the command sender type to {@link CommandSender}
*/
public CloudburstCommandManager(final @NonNull Plugin owningPlugin,
final @NonNull Function<@NonNull CommandTree<C>,
@NonNull CommandExecutionCoordinator<C>> commandExecutionCoordinator,
final @NonNull Function<@NonNull CommandSender, @NonNull C> commandSenderMapper,
final @NonNull Function<@NonNull C, @NonNull CommandSender> backwardsCommandSenderMapper) {
public CloudburstCommandManager(
final @NonNull Plugin owningPlugin,
final @NonNull Function<@NonNull CommandTree<C>,
@NonNull CommandExecutionCoordinator<C>> commandExecutionCoordinator,
final @NonNull Function<@NonNull CommandSender, @NonNull C> commandSenderMapper,
final @NonNull Function<@NonNull C, @NonNull CommandSender> backwardsCommandSenderMapper
) {
super(commandExecutionCoordinator, new CloudburstPluginRegistrationHandler<>());
((CloudburstPluginRegistrationHandler<C>) this.getCommandRegistrationHandler()).initialize(this);
this.commandSenderMapper = commandSenderMapper;
@ -67,8 +69,10 @@ public class CloudburstCommandManager<C> extends CommandManager<C> {
}
@Override
public final boolean hasPermission(final @NonNull C sender,
final @NonNull String permission) {
public final boolean hasPermission(
final @NonNull C sender,
final @NonNull String permission
) {
return this.backwardsCommandSenderMapper.apply(sender).hasPermission(permission);
}

View file

@ -73,9 +73,11 @@ public final class MinecraftHelp<C> {
* @param audienceProvider Provider that maps the command sender type to {@link Audience}
* @param commandManager Command manager instance
*/
public MinecraftHelp(final @NonNull String commandPrefix,
final @NonNull AudienceProvider<C> audienceProvider,
final @NonNull CommandManager<C> commandManager) {
public MinecraftHelp(
final @NonNull String commandPrefix,
final @NonNull AudienceProvider<C> audienceProvider,
final @NonNull CommandManager<C> commandManager
) {
this.commandPrefix = commandPrefix;
this.audienceProvider = audienceProvider;
this.commandManager = commandManager;
@ -85,9 +87,9 @@ public final class MinecraftHelp<C> {
this.messageMap.put(MESSAGE_QUERY_QUERY, "<gray>Showing search results for query: \"<green>/<query></green>\"</gray>");
this.messageMap.put(MESSAGE_QUERY_AVAILABLE_COMMANDS, "<dark_gray>└─</dark_gray><gray> Available Commands:</gray>");
this.messageMap.put(MESSAGE_QUERY_COMMAND_SYNTAX, "<dark_gray> ├─</dark_gray> <green>"
+ "<click:run_command:cmdprefix><hover:show_text:\"<gray><description></gray>\">/<command></hover></click></green>");
+ "<click:run_command:cmdprefix><hover:show_text:\"<gray><description></gray>\">/<command></hover></click></green>");
this.messageMap.put(MESSAGE_QUERY_COMMAND_SYNTAX_LAST, "<dark_gray> └─</dark_gray> <green>"
+ "<click:run_command:cmdprefix><hover:show_text:\"<gray><description></gray>\">/<command></hover></click></green>");
+ "<click:run_command:cmdprefix><hover:show_text:\"<gray><description></gray>\">/<command></hover></click></green>");
this.messageMap.put(MESSAGE_QUERY_LONGEST_PATH, "<dark_gray>└─</dark_gray> <green>/<command></green>");
this.messageMap.put(MESSAGE_QUERY_SUGGESTION, "<dark_gray><indentation><prefix></dark_gray> <green><click:run_command:"
+ "<cmdprefix>><hover:show_text:\"<gray>Click to show help for this command</gray>\">"
@ -138,8 +140,10 @@ public final class MinecraftHelp<C> {
* @param key Message key
* @param message Message
*/
public void setMessage(final @NonNull String key,
final @NonNull String message) {
public void setMessage(
final @NonNull String key,
final @NonNull String message
) {
this.messageMap.put(key, message);
}
@ -149,19 +153,25 @@ public final class MinecraftHelp<C> {
* @param query Command query (without leading '/')
* @param recipient Recipient
*/
public void queryCommands(final @NonNull String query,
final @NonNull C recipient) {
public void queryCommands(
final @NonNull String query,
final @NonNull C recipient
) {
final Audience audience = this.getAudience(recipient);
audience.sendMessage(this.miniMessage.parse(this.messageMap.get(MESSAGE_HELP_HEADER)));
this.printTopic(recipient, query, this.commandManager.getCommandHelpHandler().queryHelp(recipient, query));
audience.sendMessage(this.miniMessage.parse(this.messageMap.get(MESSAGE_HELP_FOOTER)));
}
private void printTopic(final @NonNull C sender,
final @NonNull String query,
final CommandHelpHandler.@NonNull HelpTopic<C> helpTopic) {
this.getAudience(sender).sendMessage(this.miniMessage.parse(this.messageMap.get(MESSAGE_QUERY_QUERY),
Template.of("query", query)));
private void printTopic(
final @NonNull C sender,
final @NonNull String query,
final CommandHelpHandler.@NonNull HelpTopic<C> helpTopic
) {
this.getAudience(sender).sendMessage(this.miniMessage.parse(
this.messageMap.get(MESSAGE_QUERY_QUERY),
Template.of("query", query)
));
if (helpTopic instanceof CommandHelpHandler.IndexHelpTopic) {
this.printIndexHelpTopic(sender, (CommandHelpHandler.IndexHelpTopic<C>) helpTopic);
} else if (helpTopic instanceof CommandHelpHandler.MultiHelpTopic) {
@ -173,8 +183,10 @@ public final class MinecraftHelp<C> {
}
}
private void printIndexHelpTopic(final @NonNull C sender,
final CommandHelpHandler.@NonNull IndexHelpTopic<C> helpTopic) {
private void printIndexHelpTopic(
final @NonNull C sender,
final CommandHelpHandler.@NonNull IndexHelpTopic<C> helpTopic
) {
final Audience audience = this.getAudience(sender);
audience.sendMessage(this.miniMessage.parse(this.messageMap.get(MESSAGE_QUERY_AVAILABLE_COMMANDS)));
final Iterator<CommandHelpHandler.VerboseHelpEntry<C>> iterator = helpTopic.getEntries().iterator();
@ -182,30 +194,34 @@ public final class MinecraftHelp<C> {
final CommandHelpHandler.VerboseHelpEntry<C> entry = iterator.next();
final String description = entry.getDescription().isEmpty() ? "Click to show help for this command"
: entry.getDescription();
: entry.getDescription();
String message = this.messageMap.get(iterator.hasNext() ? MESSAGE_QUERY_COMMAND_SYNTAX
: MESSAGE_QUERY_COMMAND_SYNTAX_LAST);
: MESSAGE_QUERY_COMMAND_SYNTAX_LAST);
final String suggestedCommand = entry.getSyntaxString()
.replace("<", "")
.replace(">", "")
.replace("[", "")
.replace("]", "");
.replace("<", "")
.replace(">", "")
.replace("[", "")
.replace("]", "");
message = message.replace("<command>", entry.getSyntaxString())
.replace("<description>", description)
.replace("cmdprefix", this.commandPrefix + ' ' + suggestedCommand);
.replace("<description>", description)
.replace("cmdprefix", this.commandPrefix + ' ' + suggestedCommand);
audience.sendMessage(this.miniMessage.parse(message));
}
}
private void printMultiHelpTopic(final @NonNull C sender,
final CommandHelpHandler.@NonNull MultiHelpTopic<C> helpTopic) {
private void printMultiHelpTopic(
final @NonNull C sender,
final CommandHelpHandler.@NonNull MultiHelpTopic<C> helpTopic
) {
final Audience audience = this.getAudience(sender);
audience.sendMessage(this.miniMessage.parse(this.messageMap.get(MESSAGE_QUERY_LONGEST_PATH),
Template.of("command", helpTopic.getLongestPath())));
audience.sendMessage(this.miniMessage.parse(
this.messageMap.get(MESSAGE_QUERY_LONGEST_PATH),
Template.of("command", helpTopic.getLongestPath())
));
final int headerIndentation = helpTopic.getLongestPath().length();
final Iterator<String> iterator = helpTopic.getChildSuggestions().iterator();
while (iterator.hasNext()) {
@ -224,27 +240,35 @@ public final class MinecraftHelp<C> {
}
final String suggestedCommand = suggestion.replace("<", "")
.replace(">", "")
.replace("[", "")
.replace("]", "");
.replace(">", "")
.replace("[", "")
.replace("]", "");
audience.sendMessage(this.miniMessage.parse(this.messageMap.get(MESSAGE_QUERY_SUGGESTION),
Template.of("indentation", indentation.toString()),
Template.of("prefix", prefix),
Template.of("suggestion", suggestion),
Template.of("cmdprefix", this.commandPrefix + ' ' + suggestedCommand)));
audience.sendMessage(this.miniMessage.parse(
this.messageMap.get(MESSAGE_QUERY_SUGGESTION),
Template.of("indentation", indentation.toString()),
Template.of("prefix", prefix),
Template.of("suggestion", suggestion),
Template.of("cmdprefix", this.commandPrefix + ' ' + suggestedCommand)
));
}
}
private void printVerboseHelpTopic(final @NonNull C sender,
final CommandHelpHandler.@NonNull VerboseHelpTopic<C> helpTopic) {
private void printVerboseHelpTopic(
final @NonNull C sender,
final CommandHelpHandler.@NonNull VerboseHelpTopic<C> helpTopic
) {
final Audience audience = this.getAudience(sender);
final String command = this.commandManager.getCommandSyntaxFormatter()
.apply(helpTopic.getCommand().getArguments(), null);
audience.sendMessage(this.miniMessage.parse(this.messageMap.get(MESSAGE_QUERY_VERBOSE_SYNTAX),
Template.of("command", command)));
audience.sendMessage(this.miniMessage.parse(this.messageMap.get(MESSAGE_QUERY_VERBOSE_DESCRIPTION),
Template.of("description", helpTopic.getDescription())));
.apply(helpTopic.getCommand().getArguments(), null);
audience.sendMessage(this.miniMessage.parse(
this.messageMap.get(MESSAGE_QUERY_VERBOSE_SYNTAX),
Template.of("command", command)
));
audience.sendMessage(this.miniMessage.parse(
this.messageMap.get(MESSAGE_QUERY_VERBOSE_DESCRIPTION),
Template.of("description", helpTopic.getDescription())
));
audience.sendMessage(this.miniMessage.parse(this.messageMap.get(MESSAGE_QUERY_VERBOSE_ARGS)));
final Iterator<CommandArgument<C, ?>> iterator = helpTopic.getCommand().getArguments().iterator();
@ -262,7 +286,7 @@ public final class MinecraftHelp<C> {
}
String syntax = this.commandManager.getCommandSyntaxFormatter()
.apply(Collections.singletonList(argument), null);
.apply(Collections.singletonList(argument), null);
if (argument instanceof StaticArgument) {
syntax = this.messageMap.get(MESSAGE_QUERY_VERBOSE_LITERAL).replace("<syntax>", syntax);
@ -278,10 +302,12 @@ public final class MinecraftHelp<C> {
message = this.messageMap.get(MESSAGE_QUERY_VERBOSE_OPTIONAL);
}
audience.sendMessage(this.miniMessage.parse(message,
Template.of("prefix", prefix),
Template.of("syntax", syntax),
Template.of("description", description)));
audience.sendMessage(this.miniMessage.parse(
message,
Template.of("prefix", prefix),
Template.of("syntax", syntax),
Template.of("description", description)
));
}
}

View file

@ -52,16 +52,17 @@ class AsyncCommandSuggestionsListener<C> implements Listener {
if (this.paperCommandManager.getCommandTree().getNamedNode(arguments[0]) == null) {
return;
}
@SuppressWarnings("unchecked")
final BukkitPluginRegistrationHandler<C> bukkitPluginRegistrationHandler =
@SuppressWarnings("unchecked") final BukkitPluginRegistrationHandler<C> bukkitPluginRegistrationHandler =
(BukkitPluginRegistrationHandler<C>) this.paperCommandManager.getCommandRegistrationHandler();
if (!bukkitPluginRegistrationHandler.isRecognized(arguments[0])) {
return;
}
final CommandSender sender = event.getSender();
final C cloudSender = this.paperCommandManager.getCommandSenderMapper().apply(sender);
final List<String> suggestions = new ArrayList<>(this.paperCommandManager.suggest(cloudSender,
event.getBuffer().substring(1)));
final List<String> suggestions = new ArrayList<>(this.paperCommandManager.suggest(
cloudSender,
event.getBuffer().substring(1)
));
event.setCompletions(suggestions);
event.setHandled(true);
}

View file

@ -47,10 +47,12 @@ class PaperBrigadierListener<C> implements Listener {
PaperBrigadierListener(final @NonNull PaperCommandManager<C> paperCommandManager) {
this.paperCommandManager = paperCommandManager;
this.brigadierManager = new CloudBrigadierManager<>(this.paperCommandManager,
() -> new CommandContext<>(
this.paperCommandManager.getCommandSenderMapper()
.apply(Bukkit.getConsoleSender())));
this.brigadierManager = new CloudBrigadierManager<>(
this.paperCommandManager,
() -> new CommandContext<>(
this.paperCommandManager.getCommandSenderMapper()
.apply(Bukkit.getConsoleSender()))
);
new BukkitBrigadierMapper<>(this.paperCommandManager, this.brigadierManager);
}
@ -79,11 +81,13 @@ class PaperBrigadierListener<C> implements Listener {
final C sender = this.paperCommandManager.getCommandSenderMapper().apply(s.getBukkitSender());
return this.paperCommandManager.hasPermission(sender, p);
};
event.setLiteral(this.brigadierManager.createLiteralCommandNode(node,
event.getLiteral(),
event.getBrigadierCommand(),
event.getBrigadierCommand(),
permissionChecker));
event.setLiteral(this.brigadierManager.createLiteralCommandNode(
node,
event.getLiteral(),
event.getBrigadierCommand(),
event.getBrigadierCommand(),
permissionChecker
));
}
}

View file

@ -50,11 +50,13 @@ public class PaperCommandManager<C> extends BukkitCommandManager<C> {
* @param backwardsCommandSenderMapper Function that maps the command sender type to {@link CommandSender}
* @throws Exception If the construction of the manager fails
*/
public PaperCommandManager(final @NonNull Plugin owningPlugin,
final @NonNull Function<CommandTree<C>,
CommandExecutionCoordinator<C>> commandExecutionCoordinator,
final @NonNull Function<CommandSender, C> commandSenderMapper,
final @NonNull Function<C, CommandSender> backwardsCommandSenderMapper) throws
public PaperCommandManager(
final @NonNull Plugin owningPlugin,
final @NonNull Function<CommandTree<C>,
CommandExecutionCoordinator<C>> commandExecutionCoordinator,
final @NonNull Function<CommandSender, C> commandSenderMapper,
final @NonNull Function<C, CommandSender> backwardsCommandSenderMapper
) throws
Exception {
super(owningPlugin, commandExecutionCoordinator, commandSenderMapper, backwardsCommandSenderMapper);
@ -73,8 +75,10 @@ public class PaperCommandManager<C> extends BukkitCommandManager<C> {
} else {
try {
final PaperBrigadierListener<C> brigadierListener = new PaperBrigadierListener<>(this);
Bukkit.getPluginManager().registerEvents(brigadierListener,
this.getOwningPlugin());
Bukkit.getPluginManager().registerEvents(
brigadierListener,
this.getOwningPlugin()
);
this.setSplitAliases(true);
} catch (final Throwable e) {
throw new BrigadierFailureException(BrigadierFailureReason.PAPER_BRIGADIER_INITIALIZATION_FAILURE, e);

View file

@ -57,7 +57,8 @@ public class VelocityCommandManager<C> extends CommandManager<C> {
final @NonNull ProxyServer proxyServer,
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) {
final @NonNull Function<@NonNull C, @NonNull CommandSource> backwardsCommandSenderMapper
) {
super(commandExecutionCoordinator, new VelocityPluginRegistrationHandler<>());
((VelocityPluginRegistrationHandler<C>) this.getCommandRegistrationHandler()).initialize(this);
this.proxyServer = proxyServer;

View file

@ -23,10 +23,10 @@
//
package cloud.commandframework.velocity;
import cloud.commandframework.brigadier.CloudBrigadierManager;
import cloud.commandframework.Command;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.StaticArgument;
import cloud.commandframework.brigadier.CloudBrigadierManager;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.ArgumentParseException;
import cloud.commandframework.exceptions.InvalidCommandSenderException;
@ -56,11 +56,12 @@ final class VelocityPluginRegistrationHandler<C> implements CommandRegistrationH
void initialize(final @NonNull VelocityCommandManager<C> velocityCommandManager) {
this.manager = velocityCommandManager;
this.brigadierManager = new CloudBrigadierManager<>(velocityCommandManager,
() -> new CommandContext<>(
velocityCommandManager.getCommandSenderMapper()
.apply(velocityCommandManager.getProxyServer()
.getConsoleCommandSource()))
this.brigadierManager = new CloudBrigadierManager<>(
velocityCommandManager,
() -> new CommandContext<>(
velocityCommandManager.getCommandSenderMapper()
.apply(velocityCommandManager.getProxyServer()
.getConsoleCommandSource()))
);
}
@ -70,67 +71,107 @@ final class VelocityPluginRegistrationHandler<C> implements CommandRegistrationH
final List<String> aliases = ((StaticArgument<C>) argument).getAlternativeAliases();
final BrigadierCommand brigadierCommand = new BrigadierCommand(
this.brigadierManager.createLiteralCommandNode(command.getArguments().get(0).getName(), (Command<C>) command,
(c, p) -> this.manager.hasPermission(
this.manager.getCommandSenderMapper()
.apply(c), p), true,
commandContext -> {
final CommandSource source = commandContext.getSource();
final String input = commandContext.getInput();
final C sender = this.manager.getCommandSenderMapper().apply(source);
this.manager.executeCommand(sender, input).whenComplete((result, throwable) -> {
if (throwable != null) {
if (throwable instanceof CompletionException) {
throwable = throwable.getCause();
}
final Throwable finalThrowable = throwable;
if (throwable instanceof InvalidSyntaxException) {
this.manager.handleException(sender,
InvalidSyntaxException.class,
(InvalidSyntaxException) throwable, (c, e) ->
source.sendMessage(TextComponent.builder("Invalid Command Syntax. Correct command syntax is: ",
NamedTextColor.RED)
.append(e.getCorrectSyntax(), NamedTextColor.GRAY).build())
);
} else if (throwable instanceof InvalidCommandSenderException) {
this.manager.handleException(sender,
InvalidCommandSenderException.class,
(InvalidCommandSenderException) throwable, (c, e) ->
source.sendMessage(TextComponent.of(finalThrowable.getMessage()).color(NamedTextColor.RED))
);
} else if (throwable instanceof NoPermissionException) {
this.manager.handleException(sender,
NoPermissionException.class,
(NoPermissionException) throwable, (c, e) ->
source.sendMessage(TextComponent.of(MESSAGE_NO_PERMS))
);
} else if (throwable instanceof NoSuchCommandException) {
this.manager.handleException(sender,
NoSuchCommandException.class,
(NoSuchCommandException) throwable, (c, e) ->
source.sendMessage(TextComponent.of(MESSAGE_UNKNOWN_COMMAND))
);
} else if (throwable instanceof ArgumentParseException) {
this.manager.handleException(sender,
ArgumentParseException.class,
(ArgumentParseException) throwable, (c, e) ->
source.sendMessage(TextComponent.builder("Invalid Command Argument: ",
NamedTextColor.RED)
.append(finalThrowable.getCause().getMessage(),
NamedTextColor.GRAY)
.build())
);
} else {
source.sendMessage(TextComponent.of(throwable.getMessage()).color(NamedTextColor.RED));
throwable.printStackTrace();
}
}
});
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
})
(c, p) -> this.manager.hasPermission(
this.manager.getCommandSenderMapper()
.apply(c), p), true,
commandContext -> {
final CommandSource source = commandContext.getSource();
final String input = commandContext.getInput();
final C sender = this.manager.getCommandSenderMapper().apply(
source);
this.manager
.executeCommand(sender, input)
.whenComplete((result, throwable) -> {
if (throwable != null) {
if (throwable instanceof CompletionException) {
throwable = throwable.getCause();
}
final Throwable finalThrowable = throwable;
if (throwable instanceof InvalidSyntaxException) {
this.manager.handleException(
sender,
InvalidSyntaxException.class,
(InvalidSyntaxException) throwable,
(c, e) ->
source.sendMessage(
TextComponent
.builder(
"Invalid Command Syntax. Correct command syntax is: ",
NamedTextColor.RED
)
.append(
e.getCorrectSyntax(),
NamedTextColor.GRAY
)
.build())
);
} else if (throwable instanceof InvalidCommandSenderException) {
this.manager.handleException(
sender,
InvalidCommandSenderException.class,
(InvalidCommandSenderException) throwable,
(c, e) ->
source.sendMessage(
TextComponent
.of(finalThrowable
.getMessage())
.color(NamedTextColor.RED))
);
} else if (throwable instanceof NoPermissionException) {
this.manager.handleException(
sender,
NoPermissionException.class,
(NoPermissionException) throwable,
(c, e) ->
source.sendMessage(
TextComponent
.of(MESSAGE_NO_PERMS))
);
} else if (throwable instanceof NoSuchCommandException) {
this.manager.handleException(
sender,
NoSuchCommandException.class,
(NoSuchCommandException) throwable,
(c, e) ->
source.sendMessage(
TextComponent
.of(MESSAGE_UNKNOWN_COMMAND))
);
} else if (throwable instanceof ArgumentParseException) {
this.manager.handleException(
sender,
ArgumentParseException.class,
(ArgumentParseException) throwable,
(c, e) ->
source.sendMessage(
TextComponent
.builder(
"Invalid Command Argument: ",
NamedTextColor.RED
)
.append(
finalThrowable
.getCause()
.getMessage(),
NamedTextColor.GRAY
)
.build())
);
} else {
source.sendMessage(TextComponent
.of(throwable.getMessage())
.color(NamedTextColor.RED));
throwable.printStackTrace();
}
}
});
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
}
)
);
final CommandMeta commandMeta = this.manager.getProxyServer().getCommandManager()
.metaBuilder(brigadierCommand)
.aliases(aliases.toArray(new String[0])).build();
.metaBuilder(brigadierCommand)
.aliases(aliases.toArray(new String[0])).build();
aliases.forEach(this.manager.getProxyServer().getCommandManager()::unregister);
this.manager.getProxyServer().getCommandManager().register(commandMeta, brigadierCommand);
return true;