Solved scheduleAsyncRepeatingTask doesnt work!

Discussion in 'Plugin Development' started by Niv200, Aug 8, 2013.

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

    Niv200

    hello again xD ...
    after yesterday that i ask about timers with delayed task i figure it out.. but now.. i want to set velocity to Entity every 1 tick for 15 ticks... heres the code that i have now :
    (its doesnt seems to work... get errors from eclipse from every line! :eek:)
    Code:java
    1. Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(Main, new Runnable(), 1L, 15L{
    2. public void run() {
    3. Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "This is an announcement!");
    4. }
    5. }, 60, 100);

    any help? :O
     
  2. Offline

    xTrollxDudex

    Niv200
    You start the brace in the wrong place, and you can't exactly refer to the plugin like that.
     
  3. Offline

    Niv200

    i started it after player place block event then i checked for the loc and make it shoots dragon egg entity to the sky..
    after that i added that code...
    il edit it and give the code in a sec
    Code:java
    1. @EventHandler
    2. public void onInterEgg(PlayerInteractEvent e){
    3. if(e.getClickedBlock().getType() == Material.DRAGON_EGG && e.getAction() == Action.RIGHT_CLICK_BLOCK){
    4. e.setCancelled(true);
    5. e.getClickedBlock().setType(Material.AIR);
    6. Player p = e.getPlayer();
    7. Location loc = e.getClickedBlock().getLocation();
    8. Block b = e.getClickedBlock();
    9. final FallingBlock ent = b.getLocation().getWorld().spawnFallingBlock(loc, Material.DRAGON_EGG,(byte) 0);
    10. Location eloc = ent.getLocation();
    11. ParticleEffect.LARGE_SMOKE.play(eloc, 0, 0, 0, 0.05F, 25);
    12. ent.setFallDistance(0);
    13. Vector v = new Vector(0, 1, 0);
    14. ent.setVelocity(v);
    15. Bukkit.getScheduler().runTaskLater(Main, new Runnable() {
    16. @Override
    17. public void run() {
    18. if(ent.isOnGround() || ent.isDead()){
    19. ent.getLocation().getBlock().setType(Material.AIR);
    20. return;
    21. }else{
    22. ent.remove();
    23. }
    24. }
    25. }, 15L);
    26. }
    27. }

    im trying to put it before ent.setVelo... so il be able to launch it slow but for longer time...
     
  4. Offline

    xTrollxDudex

    Niv200
    Dude,
    PHP:
    Bukkit.getScheduler().scheduleAsyncRepeatingTask(/*plugin*/, new Runnable(){
    @
    Override
    public void run(){
    //do your code
    }
    }, 
    1L15L);
    Are you sure you want to send a message 1 tick later every 3/4 of a second?
     
  5. Offline

    Niv200

    i dont want to send message >< i want to set ent velocity to X every 1 tick for 15 ticks...
     
  6. Offline

    Dyprex

    Never access or modify bukkit objects from asynchronous tasks like scheduleAsyncRepeatingTask, because it's not thread-safe. Always use synchronous for doing that, in your case it's scheduleSyncRepeatingTask. (more information about that)
     
  7. Offline

    Niv200

    sorry i alredy looked at that one :/ still cant figure out how to use it... any help how can i set velocity to entity every 1 tick for 15 ticks?
     
  8. Offline

    xTrollxDudex

    Niv200
    Pssht you edited at the same time I posted that, how would I supposed to know your snippet in OP wasn't to broadcast a message :p

    Anyway, runTaskTimer is from BukkitRunnable, use scheduleSyncRepeatingTask instead
     
  9. Offline

    Niv200

    im confuse *~* the code above is the code without the set velocity every X time for X ticks.. its only make enderegg go to sky and explode.. how and where should i add it ? sorry im just confused as hell xD *~*
     
  10. Offline

    Tarestudio

    Niv200
    I think a BukkitRunnable is better here, it can cancel itself.
    Code:java
    1. class SetVelocityTask extends BukkitRunnable() {
    2. int count = 15;
    3. Entity entity;
    4. public SetVelocityTask(Entity p_entity) {
    5. entity = p_entity;
    6. }
    7. @Override
    8. public void run() {
    9. entity.setVelocity(new Vector(0, 1, 0));
    10. count--;
    11. if (count <= 0) {
    12. this.cancel;
    13. }
    14. }
    15. }
    16. SetVelocityTask task = new SetVelocityTask(ent);
    17. task.runTaskTimer(Main,1L,1L);
     
  11. Offline

    Niv200

    thx for the code :) but il be happy to understand what happend here... i dont understand the rask.runTastTimerXXXX
    and the public void run.. xD
     
  12. Offline

    Tarestudio

    Niv200
    Well its a nested class, not my codingstyle, but to give an idea its enough. The class extends BukkitRunnable, which has the nice feature it can control its own schedule. I added a counter with static value and the entity as handover for the constructor. As Bukkit is an extension of Runnable, you have to override the public void run() as well, that is the method called by the scheduler. In this method it just set the velocity of the entity and decrementing the counter. As soon as the conuter reached zero or less, it cancels the schedule.
    runTaskTimer is the equivalent to scheduleSyncRepeatingTask, but you have to use runTasktimer, so the task nows its own id and can cancel itself. First argument is the plugin this schedule belongs to, second is the initial delay (amount of ticks befor starting) and third is the interval in ticks.
    Understood?
     
  13. Offline

    Niv200

    thx :) got it now :p
    thx everyone for helping ! :p
     
Thread Status:
Not open for further replies.

Share This Page