Post-Event Action

This is something I really hated in Bukkit, and I was wondering if it were possible to implement into Sponge.

It is the ability to make some code run after an event has fired.
Let me give an example:
If you listen for when a player’s gamemode changes from creative to something else, anything you do in the event is when the player is still in creative mode. What I’m thinking is that you would give the event a Runnable, and it would execute that code after the event has run, when the player has changed into the new gamemode.

Is this something that is possible, or would it be too hard to implement? Feel free to give some feedback.

2 Likes

I actually have put some thought into the very same idea myself. I figure one way this could be done is to map Event classes to Futures which would be completed the next time the event happens. Only one Future would need to exist for any event that has someone “waiting” for it so the overhead would be trivial.

Additionally, it would be nice if the last event that was called could be accessed.

You say the Futures would be completed the next time an event happens, does that mean if a player changed their gamemode and you wanted to run something after that it wouldn’t run until they changed their gamemode again?

Should be as simple as scheduling a runnable on the next tick.

@Listener
public void onGameModeChange(ChangeGameModeEvent event) {
    Sponge.getScheduler().createTaskBuilder().execute(() -> doGameModeChange(event)).submit(this);
}

private void doGameModeChange(ChangeGameModeEvent event) {
}
1 Like

I’ll use that for now.

No not exactly. It would be used to chain events together if you want (but thats kind of a separate thing) so like “on x event do something on the next y event”.

For post events there would be an accessible Future instance in the event itself that would be completed when the event is, well, completed. Although thinking about it more Im not sure Future should actually be used since thats usually associated with async computations but rather making an event more Future-like. This would also get rid of the need to have a bunch of “pre” and “post” events like we do currently.

Like some sort of callback?