mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-12-26 11:07:39 +01:00
Adds a generic event to expose addon info via K,V pairs.
https://github.com/BentoBoxWorld/bentobox/issues/352#issuecomment-441471105 See addon-event branch for Level addon.
This commit is contained in:
parent
5f57417ccc
commit
d9da30e866
@ -1,5 +1,7 @@
|
||||
package world.bentobox.bentobox.api.events.addon;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import world.bentobox.bentobox.api.addons.Addon;
|
||||
import world.bentobox.bentobox.api.events.PremadeEvent;
|
||||
|
||||
@ -10,13 +12,22 @@ import world.bentobox.bentobox.api.events.PremadeEvent;
|
||||
public class AddonBaseEvent extends PremadeEvent {
|
||||
|
||||
private final Addon addon;
|
||||
private final Map<String, Object> keyValues;
|
||||
|
||||
public AddonBaseEvent(Addon addon) {
|
||||
public AddonBaseEvent(Addon addon, Map<String, Object> keyValues) {
|
||||
super();
|
||||
this.addon = addon;
|
||||
this.keyValues = keyValues;
|
||||
}
|
||||
|
||||
public Addon getAddon() {
|
||||
return addon;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the keyValues
|
||||
*/
|
||||
public Map<String, Object> getKeyValues() {
|
||||
return keyValues;
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,13 @@
|
||||
package world.bentobox.bentobox.api.events.addon;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import world.bentobox.bentobox.api.addons.Addon;
|
||||
|
||||
public class AddonEvent {
|
||||
|
||||
|
||||
public enum Reason {
|
||||
ENABLE,
|
||||
DISABLE,
|
||||
@ -11,39 +15,51 @@ public class AddonEvent {
|
||||
UNKNOWN
|
||||
}
|
||||
|
||||
public static AddonEventBuilder builder() {
|
||||
|
||||
public AddonEventBuilder builder() {
|
||||
return new AddonEventBuilder();
|
||||
}
|
||||
|
||||
public static class AddonEnableEvent extends AddonBaseEvent {
|
||||
private AddonEnableEvent(Addon addon) {
|
||||
// Final variables have to be declared in the constuctor
|
||||
super(addon);
|
||||
public class AddonEnableEvent extends AddonBaseEvent {
|
||||
private AddonEnableEvent(Addon addon, Map<String, Object> keyValues) {
|
||||
// Final variables have to be declared in the constructor
|
||||
super(addon, keyValues);
|
||||
}
|
||||
}
|
||||
public static class AddonDisableEvent extends AddonBaseEvent {
|
||||
private AddonDisableEvent(Addon addon) {
|
||||
// Final variables have to be declared in the constuctor
|
||||
super(addon);
|
||||
public class AddonDisableEvent extends AddonBaseEvent {
|
||||
private AddonDisableEvent(Addon addon, Map<String, Object> keyValues) {
|
||||
// Final variables have to be declared in the constructor
|
||||
super(addon, keyValues);
|
||||
}
|
||||
}
|
||||
public static class AddonLoadEvent extends AddonBaseEvent {
|
||||
private AddonLoadEvent(Addon addon) {
|
||||
// Final variables have to be declared in the constuctor
|
||||
super(addon);
|
||||
public class AddonLoadEvent extends AddonBaseEvent {
|
||||
private AddonLoadEvent(Addon addon, Map<String, Object> keyValues) {
|
||||
// Final variables have to be declared in the constructor
|
||||
super(addon, keyValues);
|
||||
}
|
||||
}
|
||||
public static class AddonGeneralEvent extends AddonBaseEvent {
|
||||
private AddonGeneralEvent(Addon addon) {
|
||||
// Final variables have to be declared in the constuctor
|
||||
super(addon);
|
||||
public class AddonGeneralEvent extends AddonBaseEvent {
|
||||
private AddonGeneralEvent(Addon addon, Map<String, Object> keyValues) {
|
||||
// Final variables have to be declared in the constructor
|
||||
super(addon, keyValues);
|
||||
}
|
||||
}
|
||||
|
||||
public static class AddonEventBuilder {
|
||||
public class AddonEventBuilder {
|
||||
// Here field are NOT final. They are just used for the building.
|
||||
private Addon addon;
|
||||
private Reason reason = Reason.UNKNOWN;
|
||||
private Map<String, Object> keyValues = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Add a map of key-value pairs to the event. Use this to transfer data from the addon to the external world.
|
||||
* @param keyValues - map
|
||||
* @return AddonEvent
|
||||
*/
|
||||
public AddonEventBuilder keyValues(Map<String, Object> keyValues) {
|
||||
this.keyValues = keyValues;
|
||||
return this;
|
||||
}
|
||||
|
||||
public AddonEventBuilder addon(Addon addon) {
|
||||
this.addon = addon;
|
||||
@ -58,13 +74,13 @@ public class AddonEvent {
|
||||
public AddonBaseEvent build() {
|
||||
switch (reason) {
|
||||
case ENABLE:
|
||||
return new AddonEnableEvent(addon);
|
||||
return new AddonEnableEvent(addon, keyValues);
|
||||
case DISABLE:
|
||||
return new AddonDisableEvent(addon);
|
||||
return new AddonDisableEvent(addon, keyValues);
|
||||
case LOAD:
|
||||
return new AddonLoadEvent(addon);
|
||||
return new AddonLoadEvent(addon, keyValues);
|
||||
default:
|
||||
return new AddonGeneralEvent(addon);
|
||||
return new AddonGeneralEvent(addon, keyValues);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,20 +29,20 @@ import world.bentobox.bentobox.api.addons.exceptions.InvalidAddonFormatException
|
||||
import world.bentobox.bentobox.api.events.addon.AddonEvent;
|
||||
|
||||
/**
|
||||
* @author Tastybento, ComminQ
|
||||
* @author tastybento, ComminQ
|
||||
*/
|
||||
public class AddonsManager {
|
||||
|
||||
private static final String LOCALE_FOLDER = "locales";
|
||||
private List<Addon> addons;
|
||||
private List<AddonClassLoader> loaders;
|
||||
private Map<Addon, AddonClassLoader> loaders;
|
||||
private final Map<String, Class<?>> classes = new HashMap<>();
|
||||
private BentoBox plugin;
|
||||
|
||||
public AddonsManager(BentoBox plugin) {
|
||||
this.plugin = plugin;
|
||||
addons = new ArrayList<>();
|
||||
loaders = new ArrayList<>();
|
||||
loaders = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -68,7 +68,7 @@ public class AddonsManager {
|
||||
addons.forEach(addon -> {
|
||||
try {
|
||||
addon.onEnable();
|
||||
Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.ENABLE).build());
|
||||
Bukkit.getPluginManager().callEvent(new AddonEvent().builder().addon(addon).reason(AddonEvent.Reason.ENABLE).build());
|
||||
addon.setState(Addon.State.ENABLED);
|
||||
plugin.log("Enabling " + addon.getDescription().getName() + "...");
|
||||
} catch (NoClassDefFoundError | NoSuchMethodError e) {
|
||||
@ -119,8 +119,6 @@ public class AddonsManager {
|
||||
YamlConfiguration data = addonDescription(jar);
|
||||
// Load the addon
|
||||
AddonClassLoader addonClassLoader = new AddonClassLoader(this, data, f, this.getClass().getClassLoader());
|
||||
// Add to the list of loaders
|
||||
loaders.add(addonClassLoader);
|
||||
|
||||
// Get the addon itself
|
||||
addon = addonClassLoader.getAddon();
|
||||
@ -135,9 +133,11 @@ public class AddonsManager {
|
||||
}
|
||||
plugin.getLocalesManager().loadLocalesFromFile(addon.getDescription().getName());
|
||||
// Fire the load event
|
||||
Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.LOAD).build());
|
||||
Bukkit.getPluginManager().callEvent(new AddonEvent().builder().addon(addon).reason(AddonEvent.Reason.LOAD).build());
|
||||
// Add it to the list of addons
|
||||
addons.add(addon);
|
||||
// Add to the list of loaders
|
||||
loaders.put(addon, addonClassLoader);
|
||||
// Run the onLoad.
|
||||
addon.onLoad();
|
||||
} catch (Exception e) {
|
||||
@ -154,12 +154,12 @@ public class AddonsManager {
|
||||
addons.forEach(addon -> {
|
||||
if (addon.isEnabled()) {
|
||||
addon.onDisable();
|
||||
Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.DISABLE).build());
|
||||
Bukkit.getPluginManager().callEvent(new AddonEvent().builder().addon(addon).reason(AddonEvent.Reason.DISABLE).build());
|
||||
plugin.log("Disabling " + addon.getDescription().getName() + "...");
|
||||
}
|
||||
});
|
||||
|
||||
loaders.forEach(l -> {
|
||||
loaders.values().forEach(l -> {
|
||||
try {
|
||||
l.close();
|
||||
} catch (IOException ignore) {
|
||||
@ -173,12 +173,8 @@ public class AddonsManager {
|
||||
return addons;
|
||||
}
|
||||
|
||||
public List<AddonClassLoader> getLoaders() {
|
||||
return loaders;
|
||||
}
|
||||
|
||||
public void setLoaders(List<AddonClassLoader> loaders) {
|
||||
this.loaders = loaders;
|
||||
public AddonClassLoader getLoader(final Addon addon) {
|
||||
return loaders.get(addon);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -187,7 +183,7 @@ public class AddonsManager {
|
||||
* @return Class - the class
|
||||
*/
|
||||
public Class<?> getClassByName(final String name) {
|
||||
return classes.getOrDefault(name, loaders.stream().map(l -> l.findClass(name, false)).filter(Objects::nonNull).findFirst().orElse(null));
|
||||
return classes.getOrDefault(name, loaders.values().stream().map(l -> l.findClass(name, false)).filter(Objects::nonNull).findFirst().orElse(null));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -268,4 +264,6 @@ public class AddonsManager {
|
||||
addons.clear();
|
||||
addons.addAll(sortedAddons.values());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user