Loop delay not functioning correctly.

Discussion in 'Plugin Development' started by MitchBourke, Dec 29, 2013.

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

    MitchBourke

    Hello, I am trying to shoot 10 fireworks with a 1 second delay between each. The code I have all functions other than the delay code. Here is what I am using:
    Code:java
    1. for(int i = 0; i < 10; i++){
    2. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    3. public void run() {
    4. try {
    5. Firework.playFirework(Shrine.getWorld(), new Location(Shrine.getWorld(), ShrineLoc.getX(), ShrineLoc.getY() + 1, ShrineLoc.getZ()), plugin.getRandomEffect());
    6. } catch (Exception e) {
    7. e.printStackTrace();
    8. }
    9. }}, 20L);
    10. }


    There are no errors in the console or anything of that nature, the delay simply doesn't work as I'd hoped, can someone point me in the right direction? Thanks.
     
  2. Offline

    Ronbo

    MitchBourke
    It would help to explain the problem a little more than it "doesn't work as [you] hoped" :p

    Edit: I figured out the issue though, just change 20L to 20L * i.
    You are scheduling 10 tasks right now, but all of them run in 20 ticks. Change it to 20L * i to space them out by 20 ticks each.
     
  3. Offline

    Commander9292

    MitchBourke Why not just use a syncRepeatingTask and add to an integer each time it loops and if the int is 10 cancel the event and shoot fireworks each time.
     
  4. Offline

    MitchBourke

    Ronbo That worked great, thanks a lot.
     
  5. Offline

    BillyGalbreath

    The reason its not working is because all the loops are starting at the same time, delaying at the same time, then firing at the same time.

    Here's how to schedule only one repeating task that will cancel itself after so many runs:

    Code:java
    1.  
    2. public class MyPlugin extends JavaPlugin {
    3. public void onEnable() {
    4. Bukkit.getScheduler().runTaskTimer(plugin, new RunMe(this), 0, 20);
    5. }
    6. }
    7.  
    8.  
    9. public class RunMe extends BukkitRunnable {
    10. private MyPlugin plugin;
    11. private int counter = 0;
    12.  
    13. public RunMe(MyPlugin plugin) {
    14. this.plugin = plugin;
    15. }
    16.  
    17. public void run() {
    18. if (counter < 10) {
    19. try {
    20. Firework.playFirework(Shrine.getWorld(), new Location(Shrine.getWorld(), ShrineLoc.getX(), ShrineLoc.getY() + 1, ShrineLoc.getZ()), plugin.getRandomEffect());
    21. } catch (Exception e) {
    22. e.printStackTrace();
    23. }
    24. counter++;
    25. return;
    26. }
    27. this.cancel();
    28. }
    29. }
    30.  
     
  6. Offline

    MitchBourke

    The code now works as it should (Plays 10 firework effects 1 second apart) although it appears to skip the odd firework, for example it will run 1, 3, 4, 5, 7, 8 and 10, not playing fireworks on 2, 6 and 9. I can't figure out why this is?
     
  7. Offline

    BillyGalbreath

    Using which fix? Ronbo's or mine?
     
  8. Offline

    MitchBourke

    Ronbo's currently.
     
  9. Offline

    BillyGalbreath

    Then possibly server/client/network lag since there are 10 tasks running at the same time. Try my solution, which should not have any lag issues because theres only 1 task running.
     
  10. Offline

    MitchBourke

    I attempted to use your version though when I tried using the second block of code in another task, the one that is in another class, I got an error when implementing 'BukkitRunnable' as it said it could not be found?
     
  11. Offline

    BillyGalbreath

    My bad, it extends it. Fixed above post. :p
     
  12. Offline

    MitchBourke

    I need to put:

    Code:java
    1. public void run() {
    2. if (counter < 10) {
    3. try {
    4. Firework.playFirework(Shrine.getWorld(), new Location(Shrine.getWorld(), ShrineLoc.getX(), ShrineLoc.getY() + 1, ShrineLoc.getZ()), plugin.getRandomEffect());
    5. } catch (Exception e) {
    6. e.printStackTrace();
    7. }
    8. counter++;
    9. return;
    10. }

    In another class file, how would I do that?
     
  13. Offline

    BillyGalbreath

    Just make a new file... I'm not understanding the last question.

    MyPlugin.java
    Code:java
    1.  
    2. public class MyPlugin extends JavaPlugin {
    3. public void onEnable() {
    4. Bukkit.getScheduler().runTaskTimer(plugin, new RunMe(this), 0, 20);
    5. }
    6. }
    7.  

    RunMe.java
    Code:java
    1.  
    2. public class RunMe extends BukkitRunnable {
    3. private MyPlugin plugin;
    4. private int counter = 0;
    5.  
    6. public RunMe(MyPlugin plugin) {
    7. this.plugin = plugin;
    8. }
    9.  
    10. public void run() {
    11. if (counter < 10) {
    12. try {
    13. Firework.playFirework(Shrine.getWorld(), new Location(Shrine.getWorld(), ShrineLoc.getX(), ShrineLoc.getY() + 1, ShrineLoc.getZ()), plugin.getRandomEffect());
    14. } catch (Exception e) {
    15. e.printStackTrace();
    16. }
    17. counter++;
    18. return;
    19. }
    20. this.cancel();
    21. }
    22. }
    23.  
     
  14. Offline

    MitchBourke

    Well, when I put the run method in the other class file none of the variables used are declared and therefore I get errors that I can't seem to fix. I'd like to put the method in another class file, how would I do this?
     
  15. Offline

    BillyGalbreath

    That's a basic Java question dealing with scope. If you show me all of your code I could show you exactly where to place this so it works.
     
Thread Status:
Not open for further replies.

Share This Page