How to get onDisable to wait for my runnable to finish before continueing to shut down.

Discussion in 'Plugin Development' started by Paxination, Dec 16, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Paxination

    Dang. long title....lol. Any rate, i need the server to wait on shutdown till my runnable is done, which wont take more than a couple secs.
     
  2. Offline

    The_Doctor_123

    Is your task running sync or async? The server won't shut down as long as it's executing your code.
     
  3. Offline

    Conarnar

    Simple question. You can't. What are you trying to do anyways.
     
  4. Offline

    Paxination

    The_Doctor_123 Conarnar

    Its Sync. My plugin is using items to create a custom aura effect by spawning items randomly around the player in a 1 block radius, each tick. So 20 items per second. I prevent the items from stacking by randomly naming them with a random number generator. I prevent players from picking them up by adding lore that reads "NO". Right after the items are created, it sets their velocity just enough to barely shoot up, then 1 sec later, it removes them with a runtasklater.

    It all works fine except for the fact that if a players aura is turned on when the server shuts down, the items will be laying on the ground when its restarted.

    I know after 5 mins they will despawn. But I dont want them hanging around after the fact.
     
  5. Offline

    DrMedia

    Put all the items in a list and onDisable run through the list and set each item to ItemStack(Material.AIR);.
     
  6. Offline

    RawCode

    you can.

    park main thread and do what you want, then unpark thread, this is JVM feature.

    in case of cleanup, there are shutdown hooks in java for this.
     
  7. Offline

    DrMedia

    The server can still shut-down. My way would work better.
     
  8. Offline

    RawCode

    server can't shutdown with main thread parked.
     
  9. Offline

    DrMedia

    I believe they can.

    Plus, my way would work if the server crashed.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  10. Offline

    RawCode

    Your method wont work on crash, nothing will.
     
  11. Offline

    Paxination

    Yup, even I know that, if the server crashes, nothing is executing at all.
     
  12. Offline

    DrMedia

    it will as it deletes the items in the ArrayList; if there are no items in the ArrayList then there will be no items spawned.
     
  13. Offline

    Paxination

    DrMedia

    It wont, the server is no longer running if it crashes. So there is no code to execute.

    RawCode

    What about getServer().getScheduler().getPendingTasks()?

    If it does what I read it does, in my onDisable() I could add something like....

    while(getPendingTask()){
    //do nothing here
    }

    it looks like the simplest way to go.
     
  14. Offline

    The_Doctor_123

    Paxination
    Once shutdown starts, you may not access any of the API. I'm not exactly sure if there's a way around your problem except listening for the "stop" command.
     
  15. Offline

    Garris0n

  16. Offline

    Paxination

    Yeah that idea didnt work. It just hung on in the WHILE loop.

    Garris0n That isnt working either.

    I am assuming its because I am randomly naming them so they dont stack up.

    I guess i'll try thedocs or drmedias ways.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  17. Offline

    Garris0n

    What part of it isn't working? setPickupDelay won't stop them from stacking, you still have to randomly name them. Everything else should work though...
     
  18. Offline

    DrMedia

    To make it crash safe place the items in array list; on enable (in case server crashes) then check that list.. If its empty, find all entities in the location of the items and remove them.
     
  19. Offline

    1Rogue

    Garris0n likes this.
  20. Offline

    breezeyboy

    It would work because the a api is always available until the plugin disables, the server shutdown can be cancelled and delayed because Bukkit waits for the on disable in the plugin to finish before fully shouting downs, if you couldn't access the api on disable then it would make the method useless

    You can

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  21. Offline

    The_Doctor_123

    breezeyboy
    At least part of the API, if I recall correctly.
     
  22. Offline

    breezeyboy

    The op dont want to cancel the tasks

    I dont see what part would

    This should work

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  23. Offline

    The_Doctor_123

    breezeyboy
    Could be wrong.. haven't really fooled around with plugins in a long time. :p The only reason I know the part of the API I do is from all the repetitive questions/answers.

    Also, you could've condensed those 3(EDIT: 4) replies above into 1..
     
  24. Offline

    breezeyboy

    Yea but im on my phone and its hard to edit
     
  25. Offline

    The_Doctor_123

    breezeyboy
    I know how that is.. I'm on an iPad right now. You could quote all the people at once and you'll have all the quotes down in the reply box.
     
  26. Offline

    breezeyboy

    [quotoThe_Doctor_123, post: 2093034, member: 90859177"]breezeyboy
    I know how that is.. I'm on an iPad right now. You could quote all the people at once and you'll have all the quotes down in the reply box.[/quote]
    You dont know, half way though typing it starts putting the letters in the quote
     
  27. Offline

    The_Doctor_123

    breezeyboy
    I do know. It's not fun being on a forum with a mobile device.
     
  28. Offline

    1Rogue


    From what I understand he doesn't want to interrupt the tasks. If .onDisable() is being called because bukkit is shutting down you can't continue running the tasks after the entire program terminates.

    And if he wanted to handle reloads he just simply doesn't have to have it called in .onDisable(). So I still fail to see where my previous post was incorrect.
     
  29. Offline

    Paxination

    The getPendingTask didnt work, it just hung in the WHILE loop. Nothing else worked. I eventually added the entities to a list and in onDisable, I iterate through it and remove them my self. The only other thing i did try was parking the main thread which sounded more complicated than i wanted.

    I didnt want to cancel the task, as there is no point since the server is shutting down. I dont bother handling reloads as all my admins/ops have been instructed not to use /reload as most plugins dont handle them properly and things get messy when using it.

    Any rate, its working. Thanks for the debate!

    Garris0n
    setTicksLived is whats not working. Like I said I am assuming it was because I named them. Named entities do not despawn.

    Also you edited that after I read it, I didnt see the setpickupdelay. That could help instead of using an event to check each items lore and canceling.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  30. Offline

    Garris0n

    Named LivingEntities*, the items would despawn just fine.
     
Thread Status:
Not open for further replies.

Share This Page