diff --git a/src/main/java/world/bentobox/bentobox/api/events/BentoBoxEvent.java b/src/main/java/world/bentobox/bentobox/api/events/BentoBoxEvent.java new file mode 100644 index 000000000..5c059ecaf --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/api/events/BentoBoxEvent.java @@ -0,0 +1,75 @@ +package world.bentobox.bentobox.api.events; + +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +/** + * Provides the default methods expected when extending {@link Event}. + * @author tastybento + * @since 1.5.3 + */ +public abstract class BentoBoxEvent extends Event { + + private static final HandlerList handlers = new HandlerList(); + + /** + * The default constructor is defined for cleaner code. + * This constructor assumes the BentoBoxEvent is synchronous. + */ + public BentoBoxEvent() { + this(false); + } + + /** + * Explicitly declares a BentoBoxEvent as synchronous or asynchronous. + * @param async - true indicates the event will fire asynchronously, false + * by default from default constructor + */ + public BentoBoxEvent(boolean async) { + super(async); + } + + @Override + public HandlerList getHandlers() { + return getHandlerList(); + } + + public static HandlerList getHandlerList() { + return handlers; + } + + /** + * Get a map of key value pairs derived from the fields of this class by reflection. + * @return map + * @since 1.5.3 + */ + public Map getKeyValues() { + try { + Map map = new HashMap<>(); + Arrays.asList(Introspector.getBeanInfo(this.getClass(), PremadeEvent.class).getPropertyDescriptors()) + .stream() + // only get getters + .filter(pd -> Objects.nonNull(pd.getReadMethod())) + .forEach(pd -> { // invoke method to get value + try { + Object value = pd.getReadMethod().invoke(this); + if (value != null) { + map.put(pd.getName(), value); + } + } catch (Exception ignore) {} + }); + return map; + } catch (IntrospectionException e) { + // Oh well, nothing + return Collections.emptyMap(); + } + } +} diff --git a/src/main/java/world/bentobox/bentobox/api/events/PremadeEvent.java b/src/main/java/world/bentobox/bentobox/api/events/PremadeEvent.java index df520217e..a64097096 100644 --- a/src/main/java/world/bentobox/bentobox/api/events/PremadeEvent.java +++ b/src/main/java/world/bentobox/bentobox/api/events/PremadeEvent.java @@ -1,12 +1,13 @@ package world.bentobox.bentobox.api.events; import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; /** * Provides the default methods expected when extending {@link Event}. + * @deprecated As of 1.5.3, for removal. Use {@link BentoBoxEvent} instead. */ -public abstract class PremadeEvent extends Event { +@Deprecated +public abstract class PremadeEvent extends BentoBoxEvent { /** * The default constructor is defined for cleaner code. @@ -26,14 +27,4 @@ public abstract class PremadeEvent extends Event { super(async); } - private static final HandlerList handlers = new HandlerList(); - - @Override - public HandlerList getHandlers() { - return getHandlerList(); - } - - public static HandlerList getHandlerList() { - return handlers; - } } diff --git a/src/main/java/world/bentobox/bentobox/api/events/addon/AddonBaseEvent.java b/src/main/java/world/bentobox/bentobox/api/events/addon/AddonBaseEvent.java index ad64132e8..bce1918a5 100644 --- a/src/main/java/world/bentobox/bentobox/api/events/addon/AddonBaseEvent.java +++ b/src/main/java/world/bentobox/bentobox/api/events/addon/AddonBaseEvent.java @@ -26,6 +26,7 @@ public class AddonBaseEvent extends PremadeEvent { /** * @return the keyValues */ + @Override public Map getKeyValues() { return keyValues; }