Solved How to wait a few seconds?

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

  1. yhl

    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: https://hastebin.com/ofoxibilen.scala
     
  2. Use a BukkitRunnable. A quick search on google will find how to use them
     
  3. yhl

    yhl

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

    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

    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

    yhl

    Well, I found another way anyway but I got another error(not really an error, just something rlly annoying). https://hastebin.com/rareredoju.cs 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;
         @Override
         public void run() {
              if (i < 10) {
                   inventory.setItem(i, new ItemStack(Material.LOG));
                   i++;
                   return;
              }
              cancel(); //cancels the repeating task
          }
    }.runTaskTimer(...);
     
    • 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

    Patron

    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

    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() {
        @Override
        public void run() {
           //code here

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

    yhl

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

    yhl

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

    yhl

    The local variable timer may not have been initialized