Better detection for file changes when reloading

- Added file_deleted message
This commit is contained in:
Frank van der Heijden 2020-06-27 11:47:20 +02:00
parent c2049503a7
commit 518ee782da
No known key found for this signature in database
GPG key ID: 26DA56488D314D11
3 changed files with 24 additions and 13 deletions

View file

@ -175,19 +175,11 @@ public class PluginManager {
CloseableResult result = unloadPlugin(plugin); CloseableResult result = unloadPlugin(plugin);
if (result.getResult() != Result.SUCCESS) return result; if (result.getResult() != Result.SUCCESS) return result;
File pluginFile; File pluginFile = getPluginFile(plugin.getName());
try { if (pluginFile == null) return result.set(Result.FILE_DELETED);
pluginFile = RPlugin.getPluginFile(plugin);
} catch (InvocationTargetException | IllegalAccessException ex) {
ex.printStackTrace();
return result.set(Result.ERROR);
}
LoadResult loadResult = loadPlugin(pluginFile); LoadResult loadResult = loadPlugin(pluginFile);
if (!loadResult.isSuccess()) { if (!loadResult.isSuccess()) return result.set(loadResult.getResult());
Result r = loadResult.getResult();
return result.set((r == Result.NOT_EXISTS) ? Result.FILE_CHANGED : r);
}
return result.set(enablePlugin(loadResult.getPlugin())); return result.set(enablePlugin(loadResult.getPlugin()));
} }
@ -294,4 +286,23 @@ public class PluginManager {
if (loader == null) return null; if (loader == null) return null;
return loader.getPluginDescription(file); 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

@ -9,7 +9,7 @@ public enum Result {
ALREADY_LOADED, ALREADY_LOADED,
ALREADY_ENABLED, ALREADY_ENABLED,
ALREADY_DISABLED, ALREADY_DISABLED,
FILE_CHANGED, FILE_DELETED,
INVALID_DESCRIPTION, INVALID_DESCRIPTION,
INVALID_PLUGIN, INVALID_PLUGIN,
UNKNOWN_DEPENDENCY, UNKNOWN_DEPENDENCY,

View file

@ -7,7 +7,7 @@ serverutils:
already_loaded: "&cAn error occurred while %action%ing &4%what%&c, plugin is already loaded!" already_loaded: "&cAn error occurred while %action%ing &4%what%&c, plugin is already loaded!"
already_enabled: "&cAn error occurred while %action%ing &4%what%&c, plugin is already enabled!" already_enabled: "&cAn error occurred while %action%ing &4%what%&c, plugin is already enabled!"
already_disabled: "&cAn error occurred while %action%ing &4%what%&c, plugin is already disabled!" already_disabled: "&cAn error occurred while %action%ing &4%what%&c, plugin is already disabled!"
file_changed: "&cAccessing the jar file while %action%ing &4%what%&c went wrong, please load the plugin manually!" file_deleted: "&cAccessing the jar file while %action%ing &4%what%&c went wrong, plugin has been deleted!"
invalid_description: "&cAn error occurred while %action%ing &4%what%&c, plugin doesn't have a valid description!" invalid_description: "&cAn error occurred while %action%ing &4%what%&c, plugin doesn't have a valid description!"
invalid_plugin: "&cAn error occurred while %action%ing &4%what%&c, plugin is invalid!" invalid_plugin: "&cAn error occurred while %action%ing &4%what%&c, plugin is invalid!"
unknown_dependency: "&cAn error occurred while %action%ing &4%what%&c, plugin has a dependeny which is not loaded: &4%arg%" unknown_dependency: "&cAn error occurred while %action%ing &4%what%&c, plugin has a dependeny which is not loaded: &4%arg%"