SpongeAPI
SpongeAPI copied to clipboard
Event Context Calculators
In the same vein of how permission context calculators work, event context calculators would automatically add EventContextKey
s to an event's context.
A rough outline:
@FunctionalInterface
public interface EventContextCalculator<T> {
Optional<T> calculate(Cause currentCause, EventContext currentContext);
}
public interface EventManager {
// ...
<T> void registerCalculator(TypeToken<? extends Event> eventType, EventContextKey<T> key, EventContextCalculator<T> calculator);
// ...
}
So, this is something along the lines that I'm thinking of:
public interface EventManager {
<T, E extends Event> void registerCalculator(TypeToken<E> token, EventContextSupplier<T> supplier);
}
public interface EventContextSupplier<T> {
EventContextKey<T> key();
// Cause has an accessor for the context, so you can freely take a context from the existing cause.
// And ultimately, a predicate is just a matter of returning an empty optional.
Function<Cause, Optional<T>> calculator();
}
Why would you want calculator
be a Function
, rather than just the function itself?
I'm debating on whether a function "needs to be called" versus something that can be broken down to predicates, potentially thinking of the possibility of slight exposure to the Object
being the current root, without having to construct a Cause
for the function, (half way to a PhaseState + Context). This being a possible workaround to the Object
being typed, or providing a frame
to populate based on the Object
source (or some Cause
that isn't completely constructed as Cause
is currently, or possibly a Cause
that is passed to each of the calculators to avoid a priority-by-registration-order bias).