Solved How to wait a few seconds?

Discussion in 'Spigot Plugin Development' started by yhl, Apr 29, 2017.

  1. yhl


    So this is a really nooby question, but how would I put an item in a gui wait a few seconds then do it again for the next slot then after a while stop? I tried thread.sleep(2000); but instead of putting an item then stopping then doing it again, it waited until the whole try/catch thing was over then put it. Heres my code:
  2. Use a BukkitRunnable. A quick search on google will find how to use them
  3. yhl


    Yeah but how would it stop
  4. BukkitRunnable has the method runTaskLater
  5. yhl


    Would it work to put a scheduler in a loop, so it would stop after a number of times?
  6. It also has a #runTaskTimer method, so the task it will be called every X ticks.
    If you want to stop the runnable, use #cancel method
  7. yhl


    Yeah I got that, but now I need to figure out how to make the block be put in the next slot so it doesnt place it in the same slot every time
  8. Every time the runnable is called, increase a variable and use the Inventory#setItem method
  9. yhl


    Well, I found another way anyway but I got another error(not really an error, just something rlly annoying). It only makes the first block green but then stops :(
  10. as said, use a repeating task, run it every x ticks, then cancel it when needed. ex:

    Code (Java):

    new BukkitRunnable() {
         int i = 0;
         public void run() {
              if (i < 10) {
                   inventory.setItem(i, new ItemStack(Material.LOG));
              cancel(); //cancels the repeating task
    • Funny Funny x 1
  11. miner.getItem(11) != null
    You are checking if the item in the 11th slot is not null, then you are setting it. If it is empty, nothing happend
  12. sothatsit


    You will break all plugins that have tasks running if you use Bukkit.getScheduler().cancelAllTasks(). That is a silly thing to think you can do.

    But as Parozzz pointed out, you are checking if the slots are not empty and then setting them. You need to check if they are empty and if they are set them.

    "i < 9"*
  13. yhl


    When I put cancel(); it gives me an error (thats why I did cancelAllTasks @sothatsit )
  14. What error did you get the console will show you what line is creating the error.

    Sent from my iPhone using Tapatalk
  15. Code (Java):

    //start loop
    Integer timer = Bukkit.getScheduler().scheduleSyncRepeatingTask(PLUGIN, new Runnable() {
        public void run() {
           //code here

            //stop so it does not loop
    }, 20, 20);
    //first number is after how many ticks to start and the other is the time for loop (also in ticks)
  16. yhl


    Why an integer?
  17. that is the timer id, used to stop it with Bukkit.getScheduler().cancelTask(timer);
  18. yhl


    It says, that it wasnt initialized or something.
  19. Can you send the full error?
  20. yhl


    The local variable timer may not have been initialized