Solved Calling an event from BukkitRunnable async

Discussion in 'Spigot Plugin Development' started by torpkev, Oct 11, 2019.

Thread Status:
Not open for further replies.
  1. I have a BukkitRunnable async task that does some stuff, and once it is done, I'd like for it to call a custom event

    Code (Java):

    new BukkitRunnable() {
        @Override
        public void run() {
            while (!destroyed) {
               // .. do stuff
               if (condition) {
                 EndEvent event = new EndEvent();
                 Bukkit.getPluginManager().callEvent(event);
                 return;
               }
            }
       }
    }.runTaskAsynchronously(plugin);
     
    But I'm getting an error telling me that EndEvent may only be triggered synchronously

    Is there a way I can call my event when my condition is met?

    My only other thought is to mark a variable as finished = true have a scheduled task that checks for true so it can either THEN call the event (not even sure if that would work well)

    Any help would be appreciated
     
  2. Code (Java):
    Bukkit.getScheduler().runTask(plugin, () -> Bukkit.getPluginManager().callEvent(event));
     
    • Agree Agree x 1
    • Optimistic Optimistic x 1
  3. SteelPhoenix

    Moderator

    In the newer versions, spigot enforces sync event calls to be called on the main server thread.
    You can make your event async using the constructor Event#Event(boolean isAsync)
     
    • Like Like x 1
  4. That worked fantastically.. thanks

    I might be dim here, but I'm not seeing how I would use that.

    So the event is actually like

    public EndEvent(SomeObject blah) {
    this.blah = blah;
    }

    And I can see I can use:

    if (this.isAsynchronous()) {
    // ..
    }

    But how would I pass that isAsync in?
     
    • Agree Agree x 1
  5. SteelPhoenix

    Moderator

Thread Status:
Not open for further replies.