BukkitRunnable help

Discussion in 'Plugin Development' started by ocomobock, May 21, 2013.

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

    ocomobock

    Show Spoiler
    Code:java
    1. if (commandLabel.equalsIgnoreCase("arrow") && args.length == 2)
    2. {
    3. try{
    4. final Player p1 = p;
    5. final Location loc = p1.getLocation();
    6. loc.add(0, 1, 0);
    7. final int time = Integer.parseInt(args[0]);
    8. final int times = Integer.parseInt(args[1]);
    9. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable() {
    10. public void run()
    11. {
    12. int t1 = times;
    13. if (t1 > 0)
    14. {
    15. Arrow arrow = p1.getWorld().spawn(loc, Arrow.class);
    16. arrow.setShooter(p1);
    17. t1--;
    18. }else
    19. {
    20. this.cancel();
    21. }
    22. }
    23. },time * 20, 0);
    24. {
    25. p.sendMessage(e.getMessage());
    26. }
    27. }else if (commandLabel.equalsIgnoreCase("arrow") && args.length != 2)
    28. {
    29. p.sendMessage(ChatColor.GOLD + "Usage: " + ChatColor.YELLOW + "/arrow + " + ChatColor.RED + "<time interval between shots> <amount of shots>");
    30. }


    So basically, when you do '/arrow 5 10', it shoots an arrow where the player is looking every 5 seconds 10 times. If the amount of times an arrow was shot is down to 0, it cancels.

    Well, that code shoots an arrow about every tick when I do /arrow 1 10, and it won't cancel.

    Could someone tell me what I'm doing wrong so I'll know how to do this in the future? I know some of the code in the actual run method is messed up; I didn't really get a chance to test it. But I just need to figure out why it won't cancel and why it shoots an arrow every tick.

    Thank you in advance
     
  2. Offline

    Nitnelave

    You messed up the arguments of the scheduling part. The time*20 is at the spot of the delay (time before it starts) whereas the 0 is the interval between repetitions.
     
  3. Offline

    ocomobock

    That worked, but now I need to figure out why it isn't cancelling.
     
  4. Offline

    minoneer

    it doesn't cancle, because each time the task is executed, it calls the run() method.

    Each time, you call this method, t1 is initialised and set to times, which is always 10. So it is always > 0. That's why the task is not cancelled.
     
  5. Offline

    ocomobock

    Yeah, I just figured that out. I can't think of any way to fix it though.

    Bump

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

    Sagacious_Zed Bukkit Docs

    ocomobock As minoneer explained, the scope of your t1 must exist outside of your method, or it will be reset every time the method is executed.
     
  7. Offline

    ocomobock

    Why didn't I notice thaaaaaaaat

    Somehow I had it in my head that the only thing that was allowed in BukkitRunnable was the run method and I didn't think about actually creating variables outside of the run method while still in the class.

    why don't I notice these things ._.

    thank you I guess
     
Thread Status:
Not open for further replies.

Share This Page