SpongeAPI icon indicating copy to clipboard operation
SpongeAPI copied to clipboard

Event Context Calculators

Open ItsDoot opened this issue 3 years ago • 3 comments

In the same vein of how permission context calculators work, event context calculators would automatically add EventContextKeys 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);
    // ...
}

ItsDoot avatar Mar 03 '21 04:03 ItsDoot

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();
}

gabizou avatar Mar 03 '21 05:03 gabizou

Why would you want calculator be a Function, rather than just the function itself?

dualspiral avatar Mar 03 '21 09:03 dualspiral

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).

gabizou avatar Mar 04 '21 05:03 gabizou