Using the Bukkit API in asynchronous task

Discussion in 'Spigot Plugin Development' started by TheDarkSword01, Jun 6, 2017.

  1. Hello, I need to spawn items in the world through an asynchronous task so I repeat it many times .. but I can not use bukkit functions in asynchronous tasks! Some advice?

    (Bukkit Command I use: Bukkit.getWorld("world").dropItemNaturally(location, itemStack);
     
  2. Can you explain what is causing you to need an async task over sync?
     
  3. I'm programming a BedWars plugin and to spawn the resources I need an asynchronous task!
    (I'm sorry for my english!)
     
  4. Mas

    Mas

    Most features of the API can only be used from the main thread. If you're using an async task for calculations and such you can switch back to the main thread to call any Bukkit methods. AFAIK you can't interact with worlds/items at all from async threads.
     
  5. why do you need?
     
  6. I'm programming a BedWars plugin and to spawn the resources I need an asynchronous task!
    (I'm sorry for my english!)
     
  7. why you need an asynchronous task for it?
     
  8. If you used a synchronous task when objects were spawned, I could not run any action until it finishes running it.
    Do you know another method?
     
    #8 TheDarkSword01, Jun 6, 2017
    Last edited: Jun 7, 2017
  9. Could you resay this in your native language?
     
  10. I'm sorry.
    If you used a synchronous task when objects were spawned, I could not run any action until it finishes running it.
    Do you know another method?
     
  11. electronicboy

    IRC Staff

    how long does creating those objects take? If performance is a concern, your focus should be on that and seeing if you can solve it.
    the solution is to do what needs to be done sync, if you're doing something expensive, do it async, but place the object into the world sync.
     
  12. I run this in an async task : https://hastebin.com/nogadutusa.java
     
  13. I don't think creating or spawning items is that expensive. Thread.sleep however is a horrible idea- was that in your code before you made it async? If so that certainly explains the lag.
     
  14. You're a genius (I'm stupid), I did not do it right (Thread.sleep)!
     
    • Funny Funny x 1
  15. I only removed the Thread.sleep and works wonderfully .. Thanks a lot!
     
    • Friendly Friendly x 1