Automatically provides key-value pairs for BentoBoxEvents (#775)

* Automatically provides key-value pairs for PremadeEvents

https://github.com/BentoBoxWorld/Challenges/pull/138

* Replaces PremadeEvent with BentoBoxEvent to make it look nicer.

* Cleaned up javadoc in BentoBoxEvent

* Cleaned up javadoc in PremadeEvent

* Added javadoc to async constructor in BentoBoxEvent
This commit is contained in:
tastybento 2019-06-18 11:17:51 -07:00 committed by Florian CUNY
parent 6c64fa020b
commit d4f54149c5
3 changed files with 79 additions and 12 deletions

View File

@ -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<String, Object> getKeyValues() {
try {
Map<String, Object> 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();
}
}
}

View File

@ -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;
}
}

View File

@ -26,6 +26,7 @@ public class AddonBaseEvent extends PremadeEvent {
/**
* @return the keyValues
*/
@Override
public Map<String, Object> getKeyValues() {
return keyValues;
}