mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-02-10 17:31:32 +01:00
Adds ability to write key values back to the event from plugins
This commit is contained in:
parent
c8a85a9ba2
commit
a06987ccf4
@ -2,6 +2,7 @@ package world.bentobox.bentobox.api.events;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.Introspector;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@ -71,4 +72,26 @@ public abstract class BentoBoxEvent extends Event {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set values back to the event. Use {@link #getKeyValues()} to obtain the map
|
||||
* @param map - key value map (Name of key, value - object)
|
||||
* @since 1.15.1
|
||||
*/
|
||||
public void setKeyValues(Map<String, Object> map) {
|
||||
try {
|
||||
Arrays.stream(Introspector.getBeanInfo(this.getClass(), BentoBoxEvent.class).getPropertyDescriptors())
|
||||
// only get setters
|
||||
.filter(pd -> Objects.nonNull(pd.getWriteMethod()))
|
||||
.forEach(pd -> { // invoke method to set value
|
||||
if (map.containsKey(pd.getName())) {
|
||||
try {
|
||||
pd.getWriteMethod().invoke(this, map.get(pd.getName()));
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (IntrospectionException ignore) {}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user