1.16.x Make a task execute before an event on the same tick

Discussion in 'Spigot Plugin Development' started by joehot200, Oct 18, 2020 at 5:19 PM.

  1. joehot200


    Let's say I put a task

    Code (Java):

    new BukkitRunnable() {
                public void run() {
            }.runTaskLater(m.javaPluginClass, 1);
    And an event

    Code (Java):

        public void onPlace(BlockPlaceEvent e) {
            Player p = e.getPlayer();
    on the same tick.

    If the event (e.g. a block placement) occurs in the same tick, the "event" text will occur before the "task" text.

    Is there any way to make the task execute before the event on the same tick?
    #1 joehot200, Oct 18, 2020 at 5:19 PM
    Last edited: Oct 18, 2020 at 5:28 PM
  2. are you trying to look into the future? lol
    If I read that correctly your goal is to execute the runnable task before a block is placed, but you wonder how to essentially know if a block is being placed without the event's help. If that's the case I would say it's not possible unless nms has some hacky way but I doubt it.
  3. Use a competitve lock and whichever gets executed first obtains the lock get the task done.
  4. I am guessing that this is just an abstract exmaple of a more complex problem.
    What you want, in the direct form described in the question, is probably (you'll have to take a look at the implementation to make sure) not possible, and the reason for it would be that, every tick, the events are passed to the handlers before the schedulers are executed. Again, I am not sure, but, this makes most sense.

    You could have some sort of workaround, for example, delaying the code in the event handler, but, this is probably not what you're looking for.
  5. joehot200


    I am, in fact, a magical wizard who has the ability to detect events before they take place.

    Let's say I listened for the block placement packet, so knew that the block was going to be placed on the next tick.

    But I need the task to be done synchronously, surely a lock would therefore be useless? The execution order on a single thread is the same.

    I've come to the same conclusion, I wasn't sure if there were any workarounds, hacky solutions or 'special' tasks (think of Unity's PreUpdate() and Update() for example where PreUpdate() gets executed before normal update code).
    • Funny Funny x 1
  6. So I don't really understand what you're trying to do exactly but here's some information. Events get fired when something happens, they aren't scheduled to happen later whereas the bukkit runnable is & will be executed at a later time. I'm wondering what the reason that you need the task to happen first is? Couldn't you just have that earlier in the events method so it gets ran first?
  7. Cheat, have your event tell your task its about to do stuff so the task can fire first.
  8. cant you just store the reference to your task somewhere and manually call the run method from the event method in the event it needs to be executed in the time that you need it to?

    honestly seems like an xy problem
  9. thought that might be where you were going with it but your terminology was a bit confusing