Handling event timing

So I’m working with a plugin that listens for InteractBlockEvent.Primary as well as the ChangeBlockEvent.Break event. It works fine for normal players, but for players using creative, both events will be triggered at the same time when the player hits a block. Having both events triggered is causing problems because the action is being done twice.

So what I’d like to do is set up a system that can add the necessary actions to a queue, then execute that queue on tick end. This way, when InteractBlockEvent fires, it adds the action, then ChangeBlockEvent can come along, check to see if the action already exists in the queue, and if so, just don’t bother to add it again.

The problem here is I don’t know how to figure out when the tick ends. I could use the Scheduler system to run a function every tick, but then I don’t know exactly when that function will be run.

I could also do something similar, and have the InteractBlockEvent flag that it’s done the action, then have the ChangeBlockEvent look for that flag before it runs the action, but then I still need to know when the tick ends so I can clear that flag.

I could also use some other code to check the game state to see if the action has already happened but that is prone to bugs.

Does anybody know how to figure out either what tick the game is current on, or what event is fired for ticks? Or barring that, any better ideas for how to accomplish what I’m trying to do? Note that any alternatives to InteractBlockEvent would be fine, I’m just trying to find out when the player hit a block (In Bukkit this was done via BlockDamageEvent, which doesn’t seem to have an alternative in Sponge).

Correct me if i am wrong but this seems unnecessarily complicated. Can you not just check for the players gamemode and if they are in creative you just don’t execute one of the listeners

1 Like

Well yeah, when you put it that way…

That does seem a lot simpler. I’ll try that and will see if it works for me.

Also what are you doing that you need to listen to both events?

I would imaging that just listening to interact would be enough. Block break is triggered by a lot of things that isn’t a player.

Ya know honestly I’m not entirely sure what it’s doing. This is a pretty old plugin I’m trying to port, so it might actually not need the block break event.

On the original topic (since it may be useful to me in the future), is there a way to figure out the server tick, or know when a tick is ended?

This may be what you’re looking for https://github.com/SpongePowered/SpongeAPI/blob/bleeding/src/main/java/org/spongepowered/api/Server.java#L292-L301

I don’t know if there is a way to detect the end of a tick and honestly I cannot think of a use case for that feature but if you really really wanted to you could use mixins to inject at the end of each tick.

1 Like

Ok that’s perfect, thank you! As long as I can figure out current tick I think there’s no need to know when it ended.