Prevents duplicate addon loading.

https://github.com/BentoBoxWorld/BentoBox/issues/1339
This commit is contained in:
tastybento 2020-05-02 18:49:58 -07:00
parent e39db644b9
commit 304867c1f8
2 changed files with 31 additions and 1 deletions

View File

@ -269,4 +269,13 @@ public final class AddonDescription {
return this;
}
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "AddonDescription [" + (name != null ? "name=" + name + ", " : "")
+ (version != null ? "version=" + version : "") + "]";
}
}

View File

@ -98,7 +98,17 @@ public class AddonsManager {
// try loading the addon
// Get description in the addon.yml file
YamlConfiguration data = addonDescription(jar);
// Check if the addon is already loaded (duplicate version?)
String main = data.getString("main");
if (main != null) {
if (this.getAddonByMainClassName(main).isPresent()) {
getAddonByMainClassName(main).ifPresent(a -> {
plugin.logError("Duplicate addon! Addon " + a.getDescription().getName() + " " + a.getDescription().getVersion() + " has already been loaded!");
plugin.logError("Remove the duplicate and restart!");
});
return;
}
}
// Load the addon
addonClassLoader = new AddonClassLoader(this, data, f, this.getClass().getClassLoader());
@ -341,6 +351,17 @@ public class AddonsManager {
return addons.stream().filter(a -> a.getDescription().getName().equalsIgnoreCase(name)).map(a -> (T) a).findFirst();
}
/**
* Gets the addon by main class name
* @param name - main class name
* @return Optional addon object
*/
@NonNull
@SuppressWarnings("unchecked")
public <T extends Addon> Optional<T> getAddonByMainClassName(@NonNull String name){
return addons.stream().filter(a -> a.getDescription().getMain().equalsIgnoreCase(name)).map(a -> (T) a).findFirst();
}
@NonNull
private YamlConfiguration addonDescription(@NonNull JarFile jar) throws InvalidAddonFormatException, IOException, InvalidConfigurationException {
// Obtain the addon.yml file