Modularise project

This commit is contained in:
Frank van der Heijden 2020-07-03 15:52:56 +02:00
parent 2be3825438
commit 4416d55173
No known key found for this signature in database
GPG key ID: 26DA56488D314D11
55 changed files with 388 additions and 331 deletions

View file

@ -0,0 +1,56 @@
package net.frankheijden.serverutils.bukkit.managers;
import java.io.Closeable;
import java.io.IOException;
public class CloseableResult implements Closeable {
private Result result;
private final Closeable closeable;
/**
* Constructs a new closable result.
* Used for unloading / reloading a plugin.
* NB: The closable needs to be closed to fully ensure that the old plugin doesn't work anymore!
* @param result The result of the procedure
* @param closeable The closable of the procedure.
*/
public CloseableResult(Result result, Closeable closeable) {
this.result = result;
this.closeable = closeable;
}
public CloseableResult(Result result) {
this(result, null);
}
public CloseableResult(Closeable closeable) {
this(Result.SUCCESS, closeable);
}
public Result getResult() {
return result;
}
public CloseableResult set(Result result) {
this.result = result;
return this;
}
/**
* Attempts to close the closable, essentially wrapping it with try-catch.
*/
public void tryClose() {
if (closeable == null) return;
try {
close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public void close() throws IOException {
closeable.close();
}
}

View file

@ -0,0 +1,33 @@
package net.frankheijden.serverutils.bukkit.managers;
import org.bukkit.plugin.Plugin;
public class LoadResult {
private final Plugin plugin;
private final Result result;
private LoadResult(Plugin plugin, Result result) {
this.plugin = plugin;
this.result = result;
}
public LoadResult(Plugin plugin) {
this(plugin, Result.SUCCESS);
}
public LoadResult(Result result) {
this(null, result);
}
public Result getResult() {
return result;
}
public Plugin getPlugin() {
return plugin;
}
public boolean isSuccess() {
return plugin != null && result == Result.SUCCESS;
}
}

View file

@ -0,0 +1,306 @@
package net.frankheijden.serverutils.bukkit.managers;
import java.io.Closeable;
import java.io.File;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.frankheijden.serverutils.bukkit.ServerUtils;
import net.frankheijden.serverutils.bukkit.reflection.RCommandMap;
import net.frankheijden.serverutils.bukkit.reflection.RCraftServer;
import net.frankheijden.serverutils.bukkit.reflection.RCraftingManager;
import net.frankheijden.serverutils.bukkit.reflection.RJavaPlugin;
import net.frankheijden.serverutils.bukkit.reflection.RPluginClassLoader;
import net.frankheijden.serverutils.bukkit.reflection.RSimplePluginManager;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.PluginCommand;
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.UnknownDependencyException;
public class PluginManager {
/**
* 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.
*/
public static LoadResult loadPlugin(String jarFile) {
return loadPlugin(new File(ServerUtils.getInstance().getDataFolder().getParent(), jarFile));
}
/**
* Loads the specified file as a plugin.
* @param file The file to be loaded.
* @return The result of the loading procedure.
*/
public static LoadResult loadPlugin(File file) {
if (!file.exists()) return new LoadResult(Result.NOT_EXISTS);
Plugin loadedPlugin = getLoadedPlugin(file);
if (loadedPlugin != null) return new LoadResult(Result.ALREADY_LOADED);
Plugin plugin;
try {
plugin = Bukkit.getPluginManager().loadPlugin(file);
} catch (InvalidDescriptionException ex) {
return new LoadResult(Result.INVALID_DESCRIPTION);
} catch (UnknownDependencyException ex) {
return new LoadResult(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 LoadResult(Result.ALREADY_ENABLED);
}
}
ex.printStackTrace();
return new LoadResult(Result.ERROR);
}
if (plugin == null) return new LoadResult(Result.INVALID_PLUGIN);
plugin.onLoad();
return new LoadResult(plugin);
}
/**
* 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 static 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.
*/
public static Result disablePlugin(Plugin plugin) {
if (plugin == null) return Result.NOT_ENABLED;
if (!plugin.isEnabled()) return Result.ALREADY_DISABLED;
try {
Bukkit.getPluginManager().disablePlugin(plugin);
RCraftingManager.removeRecipesFor(plugin);
} catch (Exception ex) {
ex.printStackTrace();
return Result.ERROR;
}
unregisterCommands(plugin);
return Result.SUCCESS;
}
/**
* Unloads the specified plugin by name and cleans all traces within bukkit.
* @param pluginName The plugin to unload.
* @return The result of the unload.
*/
public static CloseableResult unloadPlugin(String pluginName) {
return unloadPlugin(Bukkit.getPluginManager().getPlugin(pluginName));
}
/**
* Unloads the specified plugin and cleans all traces within bukkit.
* @param plugin The plugin to unload.
* @return The result of the unload.
*/
public static CloseableResult unloadPlugin(Plugin plugin) {
if (plugin == null) return new CloseableResult(Result.NOT_EXISTS);
Closeable closeable;
try {
RSimplePluginManager.getPlugins(Bukkit.getPluginManager()).remove(plugin);
RSimplePluginManager.removeLookupName(Bukkit.getPluginManager(), plugin.getName());
closeable = RPluginClassLoader.clearClassLoader(RJavaPlugin.getClassLoader(plugin));
} catch (Exception ex) {
ex.printStackTrace();
return new CloseableResult(Result.ERROR);
}
return new CloseableResult(closeable);
}
/**
* Enables the specified plugin by name.
* @param pluginName The plugin to enable.
* @return The result of the enabling.
*/
public static 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.
*/
public static Result enablePlugin(Plugin plugin) {
if (plugin == null) return Result.NOT_EXISTS;
if (Bukkit.getPluginManager().isPluginEnabled(plugin.getName())) return Result.ALREADY_ENABLED;
Bukkit.getPluginManager().enablePlugin(plugin);
if (Bukkit.getPluginManager().isPluginEnabled(plugin.getName())) {
return Result.SUCCESS;
}
return Result.ERROR;
}
/**
* Reloads the specified plugin by name.
* @param pluginName The plugin to reload.
* @return The result of the reload.
*/
public static CloseableResult reloadPlugin(String pluginName) {
Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);
if (plugin == null) return new CloseableResult(Result.NOT_EXISTS);
return reloadPlugin(plugin);
}
/**
* Reloads the specified plugin.
* @param plugin The plugin to reload.
* @return The result of the reload.
*/
public static CloseableResult reloadPlugin(Plugin plugin) {
Result disableResult = disablePlugin(plugin);
if (disableResult != Result.SUCCESS && disableResult != Result.ALREADY_DISABLED) {
return new CloseableResult(disableResult);
}
CloseableResult result = unloadPlugin(plugin);
if (result.getResult() != Result.SUCCESS) return result;
File pluginFile = getPluginFile(plugin.getName());
if (pluginFile == null) return result.set(Result.FILE_DELETED);
LoadResult loadResult = loadPlugin(pluginFile);
if (!loadResult.isSuccess()) return result.set(loadResult.getResult());
return result.set(enablePlugin(loadResult.getPlugin()));
}
/**
* Retrieves all known commands registered to bukkit.
* @return A map with all known commands.
*/
public static Map<String, Command> getKnownCommands() {
try {
return RCommandMap.getKnownCommands(RCraftServer.getCommandMap());
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
/**
* Unregisters all commands from the specified plugin.
* @param plugin The plugin.
*/
public static void unregisterCommands(Plugin plugin) {
Map<String, Command> knownCommands = getKnownCommands();
if (knownCommands == null) return;
knownCommands.values().removeIf(c -> {
if (c instanceof PluginCommand) {
PluginCommand pc = (PluginCommand) c;
if (pc.getPlugin() == plugin) {
pc.unregister(RCraftServer.getCommandMap());
return true;
}
return false;
}
return false;
});
}
/**
* Retrieves a command from the command map.
* @param command The command string.
* @return The command.
*/
public static Command getCommand(String command) {
Map<String, Command> knownCommands = getKnownCommands();
if (knownCommands == null) return null;
return knownCommands.get(command);
}
/**
* Retrieves all file associations, i.e. all plugin loaders.
* @return A map with all pluginloaders.
*/
public static Map<Pattern, PluginLoader> getFileAssociations() {
try {
return RSimplePluginManager.getFileAssociations(Bukkit.getPluginManager());
} catch (IllegalAccessException ex) {
ex.printStackTrace();
return null;
}
}
/**
* Retrieves the PluginLoader for the input file.
* @param file The file.
* @return The appropiate PluginLoader.
*/
public static 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);
}
}
return null;
}
/**
* 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;
try {
descriptionFile = getPluginDescription(file);
} catch (InvalidDescriptionException ex) {
return null;
}
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);
}
/**
* Attempts to retrieve the plugin file by plugin name.
* @param pluginName The plugin name.
* @return The file, or null if invalid or not found.
*/
public static File getPluginFile(String pluginName) {
for (File file : ServerUtils.getInstance().getJars()) {
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;
}
}

View file

@ -0,0 +1,43 @@
package net.frankheijden.serverutils.bukkit.managers;
import net.frankheijden.serverutils.bukkit.config.Messenger;
import org.bukkit.command.CommandSender;
public enum Result {
NOT_EXISTS,
NOT_ENABLED,
ALREADY_LOADED,
ALREADY_ENABLED,
ALREADY_DISABLED,
FILE_DELETED,
INVALID_DESCRIPTION,
INVALID_PLUGIN,
UNKNOWN_DEPENDENCY,
ERROR,
SUCCESS;
private String arg;
Result() {
this.arg = "";
}
public Result arg(String arg) {
this.arg = arg;
return this;
}
/**
* Retrieves the associated message of the result
* and sends it to a CommandSender.
* @param sender The receiver.
* @param action The action which let to the result.
* @param what An associated variable.
*/
public void sendTo(CommandSender sender, String action, String what) {
Messenger.sendMessage(sender, "serverutils." + this.name().toLowerCase(),
"%action%", action,
"%what%", what,
"%arg%", arg);
}
}

View file

@ -0,0 +1,45 @@
package net.frankheijden.serverutils.bukkit.managers;
import net.frankheijden.serverutils.bukkit.ServerUtils;
public class VersionManager {
private static VersionManager instance;
private final ServerUtils plugin = ServerUtils.getInstance();
private final String currentVersion;
private String downloadedVersion;
/**
* Creates a new VersionManager instance.
* Used for automatic updating.
*/
public VersionManager() {
instance = this;
this.currentVersion = plugin.getDescription().getVersion();
this.downloadedVersion = currentVersion;
}
public static VersionManager getInstance() {
return instance;
}
public String getCurrentVersion() {
return currentVersion;
}
public String getDownloadedVersion() {
return downloadedVersion;
}
public boolean hasDownloaded() {
return !downloadedVersion.equals(currentVersion);
}
public boolean isDownloadedVersion(String version) {
return downloadedVersion.equals(version);
}
public void setDownloadedVersion(String downloadedVersion) {
this.downloadedVersion = downloadedVersion;
}
}