Add initial multi plugin management
This commit is contained in:
parent
df55162d73
commit
94e4693b5e
54 changed files with 1988 additions and 937 deletions
|
|
@ -2,6 +2,7 @@ package net.frankheijden.serverutils.bukkit;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitPlugin;
|
||||
import net.frankheijden.serverutils.bukkit.managers.BukkitPluginManager;
|
||||
import net.frankheijden.serverutils.bukkit.reflection.RCraftServer;
|
||||
|
|
@ -65,14 +66,14 @@ public class ServerUtils extends JavaPlugin {
|
|||
String commandString = StringUtils.join(":", split, 1);
|
||||
PluginCommand pluginCommand = Bukkit.getPluginCommand(commandString);
|
||||
|
||||
Plugin plugin = getPlugin().getPluginManager().getPlugin(split[0]);
|
||||
if (plugin == null) {
|
||||
Optional<Plugin> pluginOptional = getPlugin().getPluginManager().getPlugin(split[0]);
|
||||
if (!pluginOptional.isPresent()) {
|
||||
getLogger().warning("Unknown plugin '" + split[0] + "' in disabled-commands!");
|
||||
continue;
|
||||
} else if (pluginCommand == null) {
|
||||
getLogger().warning("Unknown command '" + commandString + "' in disabled-commands!");
|
||||
continue;
|
||||
} else if (!plugin.getName().equalsIgnoreCase(pluginCommand.getPlugin().getName())) {
|
||||
} else if (pluginOptional.get().getName().equalsIgnoreCase(pluginCommand.getPlugin().getName())) {
|
||||
// No output here, plugin didn't match!
|
||||
continue;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package net.frankheijden.serverutils.bukkit.commands;
|
|||
import cloud.commandframework.CommandManager;
|
||||
import cloud.commandframework.arguments.CommandArgument;
|
||||
import cloud.commandframework.context.CommandContext;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
|
@ -18,7 +19,7 @@ import net.frankheijden.serverutils.bukkit.reflection.RDedicatedServer;
|
|||
import net.frankheijden.serverutils.bukkit.utils.ReloadHandler;
|
||||
import net.frankheijden.serverutils.bukkit.utils.VersionReloadHandler;
|
||||
import net.frankheijden.serverutils.common.commands.CommandServerUtils;
|
||||
import net.frankheijden.serverutils.common.entities.Result;
|
||||
import net.frankheijden.serverutils.common.entities.results.PluginResults;
|
||||
import net.frankheijden.serverutils.common.utils.FormatBuilder;
|
||||
import net.frankheijden.serverutils.common.utils.ForwardFilter;
|
||||
import net.frankheijden.serverutils.common.utils.ListBuilder;
|
||||
|
|
@ -46,7 +47,7 @@ public class BukkitCommandServerUtils extends CommandServerUtils<BukkitPlugin, P
|
|||
}
|
||||
|
||||
public BukkitCommandServerUtils(BukkitPlugin plugin) {
|
||||
super(plugin);
|
||||
super(plugin, Plugin[]::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -72,10 +73,10 @@ public class BukkitCommandServerUtils extends CommandServerUtils<BukkitPlugin, P
|
|||
.build());
|
||||
|
||||
manager.command(buildSubcommand(builder, "enableplugin")
|
||||
.argument(getArgument("plugin"))
|
||||
.argument(getArgument("plugins"))
|
||||
.handler(this::handleEnablePlugin));
|
||||
manager.command(buildSubcommand(builder, "disableplugin")
|
||||
.argument(getArgument("plugin"))
|
||||
.argument(getArgument("plugins"))
|
||||
.handler(this::handleDisablePlugin));
|
||||
manager.command(buildSubcommand(builder, "reloadconfig")
|
||||
.argument(getArgument("config"))
|
||||
|
|
@ -84,18 +85,18 @@ public class BukkitCommandServerUtils extends CommandServerUtils<BukkitPlugin, P
|
|||
|
||||
private void handleEnablePlugin(CommandContext<BukkitCommandSender> context) {
|
||||
BukkitCommandSender sender = context.getSender();
|
||||
String pluginName = context.get("plugin");
|
||||
List<Plugin> plugins = Arrays.asList(context.get("plugins"));
|
||||
|
||||
Result result = plugin.getPluginManager().enablePlugin(pluginName);
|
||||
result.sendTo(sender, "enabl", pluginName);
|
||||
PluginResults<Plugin> result = plugin.getPluginManager().enablePlugins(plugins);
|
||||
result.sendTo(sender, "enabl");
|
||||
}
|
||||
|
||||
private void handleDisablePlugin(CommandContext<BukkitCommandSender> context) {
|
||||
BukkitCommandSender sender = context.getSender();
|
||||
String pluginName = context.get("plugin");
|
||||
List<Plugin> plugins = Arrays.asList(context.get("plugins"));
|
||||
|
||||
Result result = plugin.getPluginManager().disablePlugin(pluginName);
|
||||
result.sendTo(sender, "disabl", pluginName);
|
||||
PluginResults<Plugin> result = plugin.getPluginManager().disablePlugins(plugins);
|
||||
result.sendTo(sender, "disabl");
|
||||
}
|
||||
|
||||
private void handleReloadConfig(CommandContext<BukkitCommandSender> context) {
|
||||
|
|
@ -148,12 +149,11 @@ public class BukkitCommandServerUtils extends CommandServerUtils<BukkitPlugin, P
|
|||
protected FormatBuilder createPluginInfo(
|
||||
FormatBuilder builder,
|
||||
Function<Consumer<ListBuilder<String>>, String> listBuilderFunction,
|
||||
String pluginName
|
||||
Plugin bukkitPlugin
|
||||
) {
|
||||
Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);
|
||||
PluginDescriptionFile description = plugin.getDescription();
|
||||
PluginDescriptionFile description = bukkitPlugin.getDescription();
|
||||
|
||||
builder.add("Name", plugin.getName())
|
||||
builder.add("Name", bukkitPlugin.getName())
|
||||
.add("Full Name", description.getFullName())
|
||||
.add("Version", description.getVersion())
|
||||
.add("Website", description.getWebsite())
|
||||
|
|
|
|||
|
|
@ -1,20 +0,0 @@
|
|||
package net.frankheijden.serverutils.bukkit.entities;
|
||||
|
||||
import net.frankheijden.serverutils.common.entities.LoadResult;
|
||||
import net.frankheijden.serverutils.common.entities.Result;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class BukkitLoadResult extends LoadResult<Plugin> {
|
||||
|
||||
private BukkitLoadResult(Plugin obj, Result result) {
|
||||
super(obj, result);
|
||||
}
|
||||
|
||||
public BukkitLoadResult(Plugin obj) {
|
||||
this(obj, Result.SUCCESS);
|
||||
}
|
||||
|
||||
public BukkitLoadResult(Result result) {
|
||||
this(null, result);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package net.frankheijden.serverutils.bukkit.entities;
|
||||
|
||||
import cloud.commandframework.bukkit.CloudBukkitCapabilities;
|
||||
import cloud.commandframework.execution.CommandExecutionCoordinator;
|
||||
import cloud.commandframework.paper.PaperCommandManager;
|
||||
import java.io.File;
|
||||
|
|
@ -16,12 +17,7 @@ import org.bukkit.command.CommandSender;
|
|||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
public class BukkitPlugin extends ServerUtilsPlugin<
|
||||
Plugin,
|
||||
BukkitTask,
|
||||
BukkitCommandSender,
|
||||
CommandSender
|
||||
> {
|
||||
public class BukkitPlugin extends ServerUtilsPlugin<Plugin, BukkitTask, BukkitCommandSender, CommandSender, BukkitPluginDescription> {
|
||||
|
||||
private final ServerUtils plugin;
|
||||
private final BukkitPluginManager pluginManager;
|
||||
|
|
@ -45,8 +41,9 @@ public class BukkitPlugin extends ServerUtilsPlugin<
|
|||
|
||||
@Override
|
||||
protected PaperCommandManager<BukkitCommandSender> newCommandManager() {
|
||||
PaperCommandManager<BukkitCommandSender> commandManager;
|
||||
try {
|
||||
return new PaperCommandManager<>(
|
||||
commandManager = new PaperCommandManager<>(
|
||||
plugin,
|
||||
CommandExecutionCoordinator.simpleCoordinator(),
|
||||
chatProvider::get,
|
||||
|
|
@ -55,6 +52,17 @@ public class BukkitPlugin extends ServerUtilsPlugin<
|
|||
} catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
|
||||
if (commandManager.queryCapability(CloudBukkitCapabilities.BRIGADIER)) {
|
||||
commandManager.registerBrigadier();
|
||||
handleBrigadier(commandManager.brigadierManager());
|
||||
}
|
||||
|
||||
if (commandManager.queryCapability(CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION)) {
|
||||
commandManager.registerAsynchronousCompletions();
|
||||
}
|
||||
|
||||
return commandManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -0,0 +1,59 @@
|
|||
package net.frankheijden.serverutils.bukkit.entities;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import net.frankheijden.serverutils.common.entities.ServerUtilsPluginDescription;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
|
||||
public class BukkitPluginDescription implements ServerUtilsPluginDescription {
|
||||
|
||||
private final PluginDescriptionFile descriptionFile;
|
||||
private final File file;
|
||||
private final String author;
|
||||
private final Set<String> dependencies;
|
||||
|
||||
/**
|
||||
* Constructs a new BukkitPluginDescription.
|
||||
*/
|
||||
public BukkitPluginDescription(PluginDescriptionFile descriptionFile, File file) {
|
||||
this.descriptionFile = descriptionFile;
|
||||
this.file = file;
|
||||
this.author = String.join(", ", this.descriptionFile.getAuthors());
|
||||
this.dependencies = new HashSet<>(descriptionFile.getDepend());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return this.descriptionFile.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return this.descriptionFile.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVersion() {
|
||||
return this.descriptionFile.getVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAuthor() {
|
||||
return this.author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getFile() {
|
||||
return this.file;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getDependencies() {
|
||||
return this.dependencies;
|
||||
}
|
||||
|
||||
public PluginDescriptionFile getDescriptionFile() {
|
||||
return descriptionFile;
|
||||
}
|
||||
}
|
||||
|
|
@ -9,10 +9,11 @@ import java.util.Collections;
|
|||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitLoadResult;
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitPluginDescription;
|
||||
import net.frankheijden.serverutils.bukkit.events.BukkitPluginDisableEvent;
|
||||
import net.frankheijden.serverutils.bukkit.events.BukkitPluginEnableEvent;
|
||||
import net.frankheijden.serverutils.bukkit.events.BukkitPluginLoadEvent;
|
||||
|
|
@ -24,8 +25,10 @@ import net.frankheijden.serverutils.bukkit.reflection.RJavaPlugin;
|
|||
import net.frankheijden.serverutils.bukkit.reflection.RJavaPluginLoader;
|
||||
import net.frankheijden.serverutils.bukkit.reflection.RPluginClassLoader;
|
||||
import net.frankheijden.serverutils.bukkit.reflection.RSimplePluginManager;
|
||||
import net.frankheijden.serverutils.common.entities.CloseableResult;
|
||||
import net.frankheijden.serverutils.common.entities.Result;
|
||||
import net.frankheijden.serverutils.common.entities.results.CloseablePluginResults;
|
||||
import net.frankheijden.serverutils.common.entities.results.PluginResults;
|
||||
import net.frankheijden.serverutils.common.entities.results.Result;
|
||||
import net.frankheijden.serverutils.common.entities.exceptions.InvalidPluginDescriptionException;
|
||||
import net.frankheijden.serverutils.common.events.PluginEvent;
|
||||
import net.frankheijden.serverutils.common.managers.AbstractPluginManager;
|
||||
import org.bukkit.Bukkit;
|
||||
|
|
@ -35,11 +38,11 @@ import org.bukkit.command.PluginIdentifiableCommand;
|
|||
import org.bukkit.plugin.InvalidDescriptionException;
|
||||
import org.bukkit.plugin.InvalidPluginException;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
import org.bukkit.plugin.PluginLoader;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.UnknownDependencyException;
|
||||
|
||||
public class BukkitPluginManager implements AbstractPluginManager<Plugin> {
|
||||
public class BukkitPluginManager extends AbstractPluginManager<Plugin, BukkitPluginDescription> {
|
||||
|
||||
private static BukkitPluginManager instance;
|
||||
|
||||
|
|
@ -51,122 +54,101 @@ public class BukkitPluginManager implements AbstractPluginManager<Plugin> {
|
|||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the specified file as a plugin.
|
||||
* @param jarFile The name of the file in the plugins/ folder.
|
||||
* @return The result of the loading procedure.
|
||||
*/
|
||||
@Override
|
||||
public BukkitLoadResult loadPlugin(String jarFile) {
|
||||
return loadPlugin(new File(getPluginsFolder(), jarFile));
|
||||
}
|
||||
public PluginResults<Plugin> loadPluginDescriptions(List<BukkitPluginDescription> descriptions) {
|
||||
PluginResults<Plugin> pluginResults = new PluginResults<>();
|
||||
|
||||
/**
|
||||
* Loads the specified file as a plugin.
|
||||
* @param file The file to be loaded.
|
||||
* @return The result of the loading procedure.
|
||||
*/
|
||||
@Override
|
||||
public BukkitLoadResult loadPlugin(File file) {
|
||||
if (!file.exists()) return new BukkitLoadResult(Result.NOT_EXISTS);
|
||||
for (BukkitPluginDescription description : descriptions) {
|
||||
String pluginId = description.getId();
|
||||
|
||||
Plugin loadedPlugin = getLoadedPlugin(file);
|
||||
if (loadedPlugin != null) return new BukkitLoadResult(Result.ALREADY_LOADED);
|
||||
|
||||
Plugin plugin;
|
||||
try {
|
||||
plugin = Bukkit.getPluginManager().loadPlugin(file);
|
||||
} catch (InvalidDescriptionException ex) {
|
||||
return new BukkitLoadResult(Result.INVALID_DESCRIPTION);
|
||||
} catch (UnknownDependencyException ex) {
|
||||
return new BukkitLoadResult(Result.UNKNOWN_DEPENDENCY.arg(ex.getMessage()));
|
||||
} catch (InvalidPluginException ex) {
|
||||
if (ex.getCause() instanceof IllegalArgumentException) {
|
||||
IllegalArgumentException e = (IllegalArgumentException) ex.getCause();
|
||||
if (e.getMessage().equalsIgnoreCase("Plugin already initialized!")) {
|
||||
return new BukkitLoadResult(Result.ALREADY_ENABLED);
|
||||
Plugin plugin;
|
||||
try {
|
||||
plugin = Bukkit.getPluginManager().loadPlugin(description.getFile());
|
||||
} catch (InvalidDescriptionException ex) {
|
||||
return pluginResults.addResult(pluginId, Result.INVALID_DESCRIPTION);
|
||||
} catch (UnknownDependencyException ex) {
|
||||
return pluginResults.addResult(pluginId, Result.UNKNOWN_DEPENDENCY.arg(ex.getMessage()));
|
||||
} catch (InvalidPluginException ex) {
|
||||
if (ex.getCause() instanceof IllegalArgumentException) {
|
||||
IllegalArgumentException e = (IllegalArgumentException) ex.getCause();
|
||||
if (e.getMessage().equalsIgnoreCase("Plugin already initialized!")) {
|
||||
return pluginResults.addResult(pluginId, Result.ALREADY_ENABLED);
|
||||
}
|
||||
}
|
||||
ex.printStackTrace();
|
||||
return pluginResults.addResult(pluginId, Result.ERROR);
|
||||
}
|
||||
ex.printStackTrace();
|
||||
return new BukkitLoadResult(Result.ERROR);
|
||||
|
||||
if (plugin == null) return pluginResults.addResult(pluginId, Result.INVALID_PLUGIN);
|
||||
Bukkit.getPluginManager().callEvent(new BukkitPluginLoadEvent(plugin, PluginEvent.Stage.PRE));
|
||||
plugin.onLoad();
|
||||
Bukkit.getPluginManager().callEvent(new BukkitPluginLoadEvent(plugin, PluginEvent.Stage.POST));
|
||||
pluginResults.addResult(pluginId, plugin);
|
||||
}
|
||||
|
||||
if (plugin == null) return new BukkitLoadResult(Result.INVALID_PLUGIN);
|
||||
Bukkit.getPluginManager().callEvent(new BukkitPluginLoadEvent(plugin, PluginEvent.Stage.PRE));
|
||||
plugin.onLoad();
|
||||
Bukkit.getPluginManager().callEvent(new BukkitPluginLoadEvent(plugin, PluginEvent.Stage.POST));
|
||||
return new BukkitLoadResult(plugin);
|
||||
return pluginResults;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables the specified plugin by name and cleans all commands and recipes of the plugin.
|
||||
* @param pluginName The plugin to disable.
|
||||
* @return The result of the disable call.
|
||||
*/
|
||||
public Result disablePlugin(String pluginName) {
|
||||
return disablePlugin(Bukkit.getPluginManager().getPlugin(pluginName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables the specified plugin and cleans all commands and recipes of the plugin.
|
||||
* @param plugin The plugin to disable.
|
||||
* @return The result of the disable call.
|
||||
*/
|
||||
@Override
|
||||
public Result disablePlugin(Plugin plugin) {
|
||||
if (plugin == null) return Result.NOT_ENABLED;
|
||||
if (!plugin.isEnabled()) return Result.ALREADY_DISABLED;
|
||||
Bukkit.getPluginManager().callEvent(new BukkitPluginDisableEvent(plugin, PluginEvent.Stage.PRE));
|
||||
try {
|
||||
Bukkit.getPluginManager().disablePlugin(plugin);
|
||||
RCraftingManager.removeRecipesFor(plugin);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return Result.ERROR;
|
||||
public PluginResults<Plugin> disableOrderedPlugins(List<Plugin> plugins) {
|
||||
PluginResults<Plugin> disableResults = new PluginResults<>();
|
||||
|
||||
for (Plugin plugin : plugins) {
|
||||
String pluginId = getPluginId(plugin);
|
||||
if (!isPluginEnabled(pluginId)) return disableResults.addResult(pluginId, Result.ALREADY_DISABLED);
|
||||
|
||||
Bukkit.getPluginManager().callEvent(new BukkitPluginDisableEvent(plugin, PluginEvent.Stage.PRE));
|
||||
try {
|
||||
Bukkit.getPluginManager().disablePlugin(plugin);
|
||||
RCraftingManager.removeRecipesFor(plugin);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return disableResults.addResult(pluginId, Result.ERROR);
|
||||
}
|
||||
|
||||
unregisterCommands(plugin);
|
||||
Bukkit.getPluginManager().callEvent(new BukkitPluginDisableEvent(plugin, PluginEvent.Stage.POST));
|
||||
|
||||
disableResults.addResult(pluginId, plugin);
|
||||
}
|
||||
unregisterCommands(plugin);
|
||||
Bukkit.getPluginManager().callEvent(new BukkitPluginDisableEvent(plugin, PluginEvent.Stage.POST));
|
||||
return Result.SUCCESS;
|
||||
|
||||
return disableResults;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unloads the specified plugin by name and cleans all traces within bukkit.
|
||||
* @param pluginName The plugin to unload.
|
||||
* @return The result of the unload.
|
||||
*/
|
||||
@Override
|
||||
public CloseableResult unloadPlugin(String pluginName) {
|
||||
return unloadPlugin(Bukkit.getPluginManager().getPlugin(pluginName));
|
||||
}
|
||||
public CloseablePluginResults<Plugin> unloadOrderedPlugins(List<Plugin> plugins) {
|
||||
CloseablePluginResults<Plugin> unloadResults = new CloseablePluginResults<>();
|
||||
|
||||
/**
|
||||
* Unloads the specified plugin and cleans all traces within bukkit.
|
||||
* @param plugin The plugin to unload.
|
||||
* @return The result of the unload.
|
||||
*/
|
||||
@Override
|
||||
public CloseableResult unloadPlugin(Plugin plugin) {
|
||||
if (plugin == null) return new CloseableResult(Result.NOT_EXISTS);
|
||||
Bukkit.getPluginManager().callEvent(new BukkitPluginUnloadEvent(plugin, PluginEvent.Stage.PRE));
|
||||
List<Closeable> closeables = new ArrayList<>();
|
||||
try {
|
||||
RSimplePluginManager.getPlugins(Bukkit.getPluginManager()).remove(plugin);
|
||||
RSimplePluginManager.removeLookupName(Bukkit.getPluginManager(), plugin.getName());
|
||||
for (Plugin plugin : plugins) {
|
||||
String pluginId = getPluginId(plugin);
|
||||
Bukkit.getPluginManager().callEvent(new BukkitPluginUnloadEvent(plugin, PluginEvent.Stage.PRE));
|
||||
|
||||
ClassLoader loader = RJavaPlugin.getClassLoader(plugin);
|
||||
RPluginClassLoader.clearClassLoader(loader);
|
||||
addIfInstance(closeables, (Closeable) () -> {
|
||||
Map<String, Class<?>> classes = RPluginClassLoader.getClasses(loader);
|
||||
RJavaPluginLoader.removeClasses(getPluginLoader(getPluginFile(plugin)), classes.keySet());
|
||||
});
|
||||
addIfInstance(closeables, loader);
|
||||
addIfInstance(closeables, RJavaPlugin.clearJavaPlugin(plugin));
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return new CloseableResult(Result.ERROR);
|
||||
List<Closeable> closeables = new ArrayList<>();
|
||||
try {
|
||||
RSimplePluginManager.getPlugins(Bukkit.getPluginManager()).remove(plugin);
|
||||
RSimplePluginManager.removeLookupName(Bukkit.getPluginManager(), pluginId);
|
||||
|
||||
ClassLoader loader = RJavaPlugin.getClassLoader(plugin);
|
||||
RPluginClassLoader.clearClassLoader(loader);
|
||||
addIfInstance(closeables, (Closeable) () -> {
|
||||
Map<String, Class<?>> classes = RPluginClassLoader.getClasses(loader);
|
||||
getPluginLoader(getPluginFile(plugin)).ifPresent(pluginLoader -> {
|
||||
RJavaPluginLoader.removeClasses(pluginLoader, classes.keySet());
|
||||
});
|
||||
});
|
||||
addIfInstance(closeables, loader);
|
||||
addIfInstance(closeables, RJavaPlugin.clearJavaPlugin(plugin));
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return unloadResults.addResult(pluginId, Result.ERROR);
|
||||
}
|
||||
|
||||
Bukkit.getPluginManager().callEvent(new BukkitPluginUnloadEvent(plugin, PluginEvent.Stage.POST));
|
||||
|
||||
unloadResults.addResult(pluginId, plugin, closeables);
|
||||
}
|
||||
Bukkit.getPluginManager().callEvent(new BukkitPluginUnloadEvent(plugin, PluginEvent.Stage.POST));
|
||||
return new CloseableResult(closeables);
|
||||
|
||||
return unloadResults;
|
||||
}
|
||||
|
||||
private static void addIfInstance(List<Closeable> list, Object obj) {
|
||||
|
|
@ -175,68 +157,29 @@ public class BukkitPluginManager implements AbstractPluginManager<Plugin> {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables the specified plugin by name.
|
||||
* @param pluginName The plugin to enable.
|
||||
* @return The result of the enabling.
|
||||
*/
|
||||
public Result enablePlugin(String pluginName) {
|
||||
return enablePlugin(Bukkit.getPluginManager().getPlugin(pluginName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables the specified plugin.
|
||||
* @param plugin The plugin to enable.
|
||||
* @return The result of the enabling.
|
||||
*/
|
||||
@Override
|
||||
public Result enablePlugin(Plugin plugin) {
|
||||
if (plugin == null) return Result.NOT_EXISTS;
|
||||
if (Bukkit.getPluginManager().isPluginEnabled(plugin.getName())) return Result.ALREADY_ENABLED;
|
||||
protected PluginResults<Plugin> enableOrderedPlugins(List<Plugin> plugins) {
|
||||
PluginResults<Plugin> enableResults = new PluginResults<>();
|
||||
PluginManager bukkitPluginManager = Bukkit.getPluginManager();
|
||||
for (Plugin plugin : plugins) {
|
||||
String pluginId = getPluginId(plugin);
|
||||
bukkitPluginManager.callEvent(new BukkitPluginEnableEvent(plugin, PluginEvent.Stage.PRE));
|
||||
bukkitPluginManager.enablePlugin(plugin);
|
||||
|
||||
Bukkit.getPluginManager().callEvent(new BukkitPluginEnableEvent(plugin, PluginEvent.Stage.PRE));
|
||||
Bukkit.getPluginManager().enablePlugin(plugin);
|
||||
RCraftServer.syncCommands();
|
||||
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled(plugin.getName())) return Result.ERROR;
|
||||
Bukkit.getPluginManager().callEvent(new BukkitPluginEnableEvent(plugin, PluginEvent.Stage.POST));
|
||||
return Result.SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads the specified plugin by name.
|
||||
* @param pluginName The plugin to reload.
|
||||
* @return The result of the reload.
|
||||
*/
|
||||
@Override
|
||||
public Result reloadPlugin(String pluginName) {
|
||||
Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);
|
||||
if (plugin == null) return Result.NOT_EXISTS;
|
||||
return reloadPlugin(plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads the specified plugin.
|
||||
* @param plugin The plugin to reload.
|
||||
* @return The result of the reload.
|
||||
*/
|
||||
@Override
|
||||
public Result reloadPlugin(Plugin plugin) {
|
||||
Result disableResult = disablePlugin(plugin);
|
||||
if (disableResult != Result.SUCCESS && disableResult != Result.ALREADY_DISABLED) {
|
||||
return disableResult;
|
||||
if (!bukkitPluginManager.isPluginEnabled(plugin.getName())) {
|
||||
return enableResults.addResult(pluginId, Result.ERROR);
|
||||
}
|
||||
bukkitPluginManager.callEvent(new BukkitPluginEnableEvent(plugin, PluginEvent.Stage.POST));
|
||||
enableResults.addResult(pluginId, plugin);
|
||||
}
|
||||
|
||||
CloseableResult result = unloadPlugin(plugin);
|
||||
if (result.getResult() != Result.SUCCESS) return result.getResult();
|
||||
result.tryClose();
|
||||
RCraftServer.syncCommands();
|
||||
return enableResults;
|
||||
}
|
||||
|
||||
File pluginFile = getPluginFile(plugin.getName());
|
||||
if (pluginFile == null) return Result.FILE_DELETED;
|
||||
|
||||
BukkitLoadResult loadResult = loadPlugin(pluginFile);
|
||||
if (!loadResult.isSuccess()) return loadResult.getResult();
|
||||
return enablePlugin(loadResult.get());
|
||||
@Override
|
||||
public boolean isPluginEnabled(String pluginId) {
|
||||
return Bukkit.getPluginManager().isPluginEnabled(pluginId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -366,45 +309,30 @@ public class BukkitPluginManager implements AbstractPluginManager<Plugin> {
|
|||
* @param file The file.
|
||||
* @return The appropiate PluginLoader.
|
||||
*/
|
||||
public static PluginLoader getPluginLoader(File file) {
|
||||
public static Optional<PluginLoader> getPluginLoader(File file) {
|
||||
Map<Pattern, PluginLoader> fileAssociations = getFileAssociations();
|
||||
if (fileAssociations == null) return null;
|
||||
|
||||
for (Pattern filter : fileAssociations.keySet()) {
|
||||
Matcher match = filter.matcher(file.getName());
|
||||
if (match.find()) {
|
||||
return fileAssociations.get(filter);
|
||||
if (fileAssociations != null) {
|
||||
for (Map.Entry<Pattern, PluginLoader> entry : fileAssociations.entrySet()) {
|
||||
Matcher match = entry.getKey().matcher(file.getName());
|
||||
if (match.find()) {
|
||||
return Optional.ofNullable(entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a loaded plugin associated to a jar file.
|
||||
* @param file The jar file.
|
||||
* @return The already loaded plugin, or null if none.
|
||||
*/
|
||||
public static Plugin getLoadedPlugin(File file) {
|
||||
PluginDescriptionFile descriptionFile;
|
||||
@Override
|
||||
public Optional<BukkitPluginDescription> getPluginDescription(File file) throws InvalidPluginDescriptionException {
|
||||
if (!file.exists()) return Optional.empty();
|
||||
|
||||
Optional<PluginLoader> loader = getPluginLoader(file);
|
||||
if (!loader.isPresent()) throw new InvalidPluginDescriptionException("Plugin loader is not present!");
|
||||
try {
|
||||
descriptionFile = getPluginDescription(file);
|
||||
return Optional.of(new BukkitPluginDescription(loader.get().getPluginDescription(file), file));
|
||||
} catch (InvalidDescriptionException ex) {
|
||||
return null;
|
||||
throw new InvalidPluginDescriptionException(ex);
|
||||
}
|
||||
if (descriptionFile == null) return null;
|
||||
return Bukkit.getPluginManager().getPlugin(descriptionFile.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the PluginDescriptionFile of a jar file.
|
||||
* @param file The jar file.
|
||||
* @return The PluginDescriptionFile.
|
||||
* @throws InvalidDescriptionException Iff the PluginDescriptionFile is invalid.
|
||||
*/
|
||||
public static PluginDescriptionFile getPluginDescription(File file) throws InvalidDescriptionException {
|
||||
PluginLoader loader = getPluginLoader(file);
|
||||
if (loader == null) return null;
|
||||
return loader.getPluginDescription(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -412,29 +340,14 @@ public class BukkitPluginManager implements AbstractPluginManager<Plugin> {
|
|||
return RJavaPlugin.getFile(plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to retrieve the plugin file by plugin name.
|
||||
* @param pluginName The plugin name.
|
||||
* @return The file, or null if invalid or not found.
|
||||
*/
|
||||
@Override
|
||||
public File getPluginFile(String pluginName) {
|
||||
for (File file : getPluginJars()) {
|
||||
PluginDescriptionFile descriptionFile;
|
||||
try {
|
||||
descriptionFile = getPluginDescription(file);
|
||||
} catch (InvalidDescriptionException ex) {
|
||||
return null;
|
||||
}
|
||||
if (descriptionFile == null) return null;
|
||||
if (descriptionFile.getName().equals(pluginName)) return file;
|
||||
}
|
||||
return null;
|
||||
public Optional<Plugin> getPlugin(String pluginName) {
|
||||
return Optional.ofNullable(Bukkit.getPluginManager().getPlugin(pluginName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Plugin getPlugin(String pluginName) {
|
||||
return Bukkit.getPluginManager().getPlugin(pluginName);
|
||||
public BukkitPluginDescription getLoadedPluginDescription(Plugin plugin) {
|
||||
return new BukkitPluginDescription(plugin.getDescription(), getPluginFile(plugin));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -455,7 +368,7 @@ public class BukkitPluginManager implements AbstractPluginManager<Plugin> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String getPluginName(Plugin plugin) {
|
||||
public String getPluginId(Plugin plugin) {
|
||||
return plugin.getName();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
"ep"
|
||||
],
|
||||
"permission": "serverutils.enableplugin",
|
||||
"description": "Enables the specified plugin.",
|
||||
"description": "Enables the specified plugin(s).",
|
||||
"display-in-help": true
|
||||
},
|
||||
"disableplugin": {
|
||||
|
|
@ -17,7 +17,7 @@
|
|||
"dp"
|
||||
],
|
||||
"permission": "serverutils.disableplugin",
|
||||
"description": "Disables the specified plugin.",
|
||||
"description": "Disables the specified plugin(s).",
|
||||
"display-in-help": true
|
||||
},
|
||||
"reloadconfig": {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue