Add "depending plugin" warning

This commit is contained in:
Frank van der Heijden 2021-07-31 00:45:48 +02:00
parent d1aa18b30b
commit 1b4997869d
No known key found for this signature in database
GPG key ID: B808721C2DD5B5B8
17 changed files with 257 additions and 43 deletions

View file

@ -7,6 +7,7 @@ import cloud.commandframework.context.CommandContext;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
@ -14,6 +15,7 @@ import java.util.function.IntFunction;
import net.frankheijden.serverutils.common.commands.arguments.JarFilesArgument;
import net.frankheijden.serverutils.common.commands.arguments.PluginArgument;
import net.frankheijden.serverutils.common.commands.arguments.PluginsArgument;
import net.frankheijden.serverutils.common.config.MessagesResource;
import net.frankheijden.serverutils.common.config.ServerUtilsConfig;
import net.frankheijden.serverutils.common.entities.results.AbstractResult;
import net.frankheijden.serverutils.common.entities.results.CloseablePluginResults;
@ -30,7 +32,7 @@ import net.frankheijden.serverutils.common.utils.ListFormat;
public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?, ?>, P, C extends ServerCommandSender<?>>
extends ServerUtilsCommand<U, C> {
private final IntFunction<P[]> arrayCreator;
protected final IntFunction<P[]> arrayCreator;
protected CommandServerUtils(U plugin, IntFunction<P[]> arrayCreator) {
super(plugin, "serverutils");
@ -40,7 +42,7 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
@Override
public void register(CommandManager<C> manager, Command.Builder<C> builder) {
addArgument(new JarFilesArgument<>(true, "jarFiles", plugin));
addArgument(new PluginsArgument<>(true, "plugins", plugin, arrayCreator));
addArgument(new PluginsArgument<>(true, "plugins", new PluginsArgument.PluginsParser<>(plugin, arrayCreator)));
addArgument(new PluginArgument<>(true, "plugin", plugin));
addArgument(CommandArgument.<C, String>ofType(String.class, "command")
.manager(manager)
@ -57,10 +59,18 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
.argument(getArgument("jarFiles"))
.handler(this::handleLoadPlugin));
manager.command(buildSubcommand(builder, "unloadplugin")
.argument(getArgument("plugins"))
.argument(new PluginsArgument<>(
true,
"plugins",
new PluginsArgument.PluginsParser<>(plugin, arrayCreator, getRawPath("unloadplugin"))
))
.handler(this::handleUnloadPlugin));
manager.command(buildSubcommand(builder, "reloadplugin")
.argument(getArgument("plugins"))
.argument(new PluginsArgument<>(
true,
"plugins",
new PluginsArgument.PluginsParser<>(plugin, arrayCreator, getRawPath("reloadplugin"))
))
.handler(this::handleReloadPlugin));
manager.command(buildSubcommand(builder, "watchplugin")
.argument(getArgument("plugin"))
@ -170,6 +180,10 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
C sender = context.getSender();
List<P> plugins = Arrays.asList(context.get("plugins"));
if (checkDependingPlugins(context, sender, plugins, "unloadplugin")) {
return;
}
PluginResults<P> disableResults = plugin.getPluginManager().disablePlugins(plugins);
for (PluginResult<P> disableResult : disableResults.getResults()) {
if (!disableResult.isSuccess() && disableResult.getResult() != Result.ALREADY_DISABLED) {
@ -187,10 +201,59 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
C sender = context.getSender();
List<P> plugins = Arrays.asList(context.get("plugins"));
if (checkDependingPlugins(context, sender, plugins, "reloadplugin")) {
return;
}
PluginResults<P> reloadResult = plugin.getPluginManager().reloadPlugins(plugins);
reloadResult.sendTo(sender, "reload");
}
protected boolean checkDependingPlugins(CommandContext<C> context, C sender, List<P> plugins, String subcommand) {
if (context.flags().contains("force")) return false;
AbstractPluginManager<P, ?> pluginManager = plugin.getPluginManager();
MessagesResource messages = plugin.getMessagesResource();
boolean hasDependingPlugins = false;
for (P plugin : plugins) {
String pluginId = pluginManager.getPluginId(plugin);
List<P> dependingPlugins = pluginManager.getPluginsDependingOn(pluginId);
if (!dependingPlugins.isEmpty()) {
String prefixString = messages.getMessage(
"serverutils.depending_plugins.prefix",
"%what%", pluginId
);
String dependingPluginsString = ListBuilder.create(dependingPlugins)
.format(p -> messages.getMessage(
"serverutils.depending_plugins.format",
"%plugin%", pluginManager.getPluginId(p)
))
.seperator(messages.getMessage("serverutils.depending_plugins.separator"))
.lastSeperator(messages.getMessage("serverutils.depending_plugins.last_separator"))
.toString();
messages.sendRawMessage(sender, prefixString + dependingPluginsString);
hasDependingPlugins = true;
}
}
if (hasDependingPlugins) {
String flagPath = getRawPath(subcommand) + ".flags.force";
String forceFlag = plugin.getCommandsResource().getAllFlagAliases(flagPath).stream()
.min(Comparator.comparingInt(String::length))
.orElse("-f");
messages.sendMessage(sender,
"serverutils.depending_plugins.override",
"%command%", context.getRawInputJoined() + " " + forceFlag
);
}
return hasDependingPlugins;
}
private void handleWatchPlugin(CommandContext<C> context) {
C sender = context.getSender();
P pluginArg = context.get("plugin");

View file

@ -7,7 +7,9 @@ import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.flags.CommandFlag;
import cloud.commandframework.permission.CommandPermission;
import cloud.commandframework.permission.Permission;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.frankheijden.serverutils.common.config.ServerUtilsConfig;
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
@ -58,9 +60,14 @@ public abstract class ServerUtilsCommand<U extends ServerUtilsPlugin<?, ?, C, ?,
*/
public Command.Builder<C> buildSubcommand(Command.Builder<C> builder, String subcommandName) {
CommandElement subcommand = parseSubcommand(subcommandName);
return builder
builder = builder
.literal(subcommand.getMain(), subcommand.getDescription(), subcommand.getAliases())
.permission(subcommand.getPermission());
for (CommandElement flagElement : subcommand.getFlags()) {
builder = builder.flag(createFlag(flagElement));
}
return builder;
}
/**
@ -76,7 +83,16 @@ public abstract class ServerUtilsCommand<U extends ServerUtilsPlugin<?, ?, C, ?,
.map(this::applyPrefix)
.toArray(String[]::new);
return new CommandElement(main, description, permission, displayInHelp, aliases);
List<CommandElement> flags = new ArrayList<>();
Object flagsObject = elementConfig.get("flags");
if (flagsObject instanceof ServerUtilsConfig) {
ServerUtilsConfig flagsConfig = ((ServerUtilsConfig) flagsObject);
for (String flagName : flagsConfig.getKeys()) {
flags.add(parseElement((ServerUtilsConfig) flagsConfig.get(flagName)));
}
}
return new CommandElement(main, description, permission, displayInHelp, aliases, flags);
}
/**
@ -86,15 +102,25 @@ public abstract class ServerUtilsCommand<U extends ServerUtilsPlugin<?, ?, C, ?,
return parseElement((ServerUtilsConfig) commandConfig.get("subcommands." + subcommandName));
}
public String getRawPath(String subcommandName) {
return "commands." + commandName + ".subcommands." + subcommandName;
}
/**
* Parses a flag from the config.
*/
public CommandFlag<Void> parseFlag(String flagName) {
CommandElement flag = parseElement((ServerUtilsConfig) commandConfig.get("flags." + flagName));
return CommandFlag.newBuilder(flag.getMain())
.withAliases(flag.getAliases())
.withPermission(flag.getPermission())
.withDescription(flag.getDescription())
return createFlag(parseElement((ServerUtilsConfig) commandConfig.get("flags." + flagName)));
}
/**
* Creates a flag from a CommandElement.
*/
public CommandFlag<Void> createFlag(CommandElement flagElement) {
return CommandFlag.newBuilder(flagElement.getMain())
.withAliases(flagElement.getAliases())
.withPermission(flagElement.getPermission())
.withDescription(flagElement.getDescription())
.build();
}
@ -124,19 +150,22 @@ public abstract class ServerUtilsCommand<U extends ServerUtilsPlugin<?, ?, C, ?,
private final CommandPermission permission;
private final boolean displayInHelp;
private final String[] aliases;
private final List<CommandElement> flags;
public CommandElement(
String main,
ArgumentDescription description,
CommandPermission permission,
boolean displayInHelp,
String... aliases
String[] aliases,
List<CommandElement> flags
) {
this.main = main;
this.description = description;
this.permission = permission;
this.displayInHelp = displayInHelp;
this.aliases = aliases;
this.flags = flags;
}
public String getMain() {
@ -158,5 +187,9 @@ public abstract class ServerUtilsCommand<U extends ServerUtilsPlugin<?, ?, C, ?,
public String[] getAliases() {
return aliases;
}
public List<CommandElement> getFlags() {
return flags;
}
}
}

View file

@ -6,9 +6,11 @@ import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
import io.leangen.geantyref.TypeToken;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Queue;
import java.util.Set;
import java.util.function.IntFunction;
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
@ -21,13 +23,12 @@ public class PluginsArgument<C extends ServerCommandSender<?>, P> extends Comman
public PluginsArgument(
boolean required,
String name,
ServerUtilsPlugin<P, ?, C, ?, ?> plugin,
IntFunction<P[]> arrayCreator
PluginsParser<C, P> parser
) {
super(
required,
name,
new PluginsParser<>(plugin, arrayCreator),
parser,
"",
new TypeToken<P[]>() {},
null
@ -38,10 +39,23 @@ public class PluginsArgument<C extends ServerCommandSender<?>, P> extends Comman
private final ServerUtilsPlugin<P, ?, C, ?, ?> plugin;
private final IntFunction<P[]> arrayCreator;
private final String commandConfigPath;
public PluginsParser(ServerUtilsPlugin<P, ?, C, ?, ?> plugin, IntFunction<P[]> arrayCreator) {
this(plugin, arrayCreator, null);
}
/**
* Constructs a new PluginsParser.
*/
public PluginsParser(
ServerUtilsPlugin<P, ?, C, ?, ?> plugin,
IntFunction<P[]> arrayCreator,
String commandConfigPath
) {
this.plugin = plugin;
this.arrayCreator = arrayCreator;
this.commandConfigPath = commandConfigPath;
}
@Override
@ -50,8 +64,13 @@ public class PluginsArgument<C extends ServerCommandSender<?>, P> extends Comman
return ArgumentParseResult.failure(new NoInputProvidedException(PluginsParser.class, context));
}
P[] plugins = arrayCreator.apply(inputQueue.size());
for (int i = 0; i < plugins.length; i++) {
Set<String> flags = plugin.getCommandsResource().getAllFlagAliases(commandConfigPath + ".flags.force");
int queueSize = inputQueue.size();
List<P> plugins = new ArrayList<>(queueSize);
for (int i = 0; i < queueSize; i++) {
if (flags.contains(inputQueue.peek())) continue;
Optional<P> pluginOptional = plugin.getPluginManager().getPlugin(inputQueue.peek());
if (!pluginOptional.isPresent()) {
return ArgumentParseResult.failure(new IllegalArgumentException(
@ -60,10 +79,10 @@ public class PluginsArgument<C extends ServerCommandSender<?>, P> extends Comman
}
inputQueue.remove();
plugins[i] = pluginOptional.get();
plugins.add(pluginOptional.get());
}
return ArgumentParseResult.success(plugins);
return ArgumentParseResult.success(plugins.stream().toArray(arrayCreator));
}
@Override

View file

@ -1,5 +1,8 @@
package net.frankheijden.serverutils.common.config;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
/**
@ -13,6 +16,26 @@ public class CommandsResource extends ServerUtilsResource {
super(plugin, COMMANDS_RESOURCE);
}
/**
* Retrieves all flag aliases for the given flag path.
*/
public Set<String> getAllFlagAliases(String path) {
Object flagObject = getConfig().get(path);
if (flagObject instanceof ServerUtilsConfig) {
ServerUtilsConfig flagConfig = (ServerUtilsConfig) flagObject;
Set<String> flagAliases = new HashSet<>();
flagAliases.add("--" + flagConfig.getString("main"));
for (String alias : flagConfig.getStringList("aliases")) {
flagAliases.add("-" + alias);
}
return flagAliases;
}
return Collections.emptySet();
}
@Override
public void migrate(int currentConfigVersion) {

View file

@ -1,6 +1,7 @@
package net.frankheijden.serverutils.common.providers;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
@ -46,6 +47,22 @@ public interface PluginProvider<P, D extends ServerUtilsPluginDescription> {
return Optional.empty();
}
/**
* Retrieves plugins which depend on the given plugin.
*/
default List<P> getPluginsDependingOn(String pluginId) {
List<P> plugins = new ArrayList<>();
for (P loadedPlugin : getPlugins()) {
ServerUtilsPluginDescription description = getLoadedPluginDescription(loadedPlugin);
if (description.getDependencies().contains(pluginId)) {
plugins.add(loadedPlugin);
}
}
return plugins;
}
Optional<P> getPlugin(String pluginId);
D getLoadedPluginDescription(P plugin);