Solved Cancelling Repeating Tasks

Discussion in 'Plugin Development' started by Zach_1919, Jun 2, 2013.

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

    Zach_1919

    I am working on a plugin for a minigame I made up, and it's been going pretty well until now. I am making it so that when you right click it throws an item (done), then on impact, the item does something special (done-ish), then the task gets cancelled (HELP!!!). Here's my code:
    Code:
    package me.Zach_1919.BattleBlocks;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class Main extends JavaPlugin implements Listener
    {
        public Main plugin = null;
        public void onEnable()
        {
            getServer().getPluginManager().registerEvents(this, this);
            plugin = this;
        }
        @EventHandler
        public void onInteract(PlayerInteractEvent event)
        {
            final Player player = event.getPlayer();
            Action action = event.getAction();
            World world = player.getWorld();
            if(!(player.getItemInHand().getType().equals(Material.AIR)))
            {
                if(action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK))
                {
                    final Item di = world.dropItem(player.getEyeLocation(), new ItemStack(player.getItemInHand().getType()));
                    di.setVelocity(player.getEyeLocation().getDirection());
                    if(player.getItemInHand().getAmount() == 1)
                    {
                        player.setItemInHand(null);
                    }
                    else
                    {
                        player.getItemInHand().setAmount(player.getItemInHand().getAmount()-1);
                    }
                    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
                    {
                        public void run()
                        {
                            di.setPickupDelay(500);
                            if(!(di.getLocation().subtract(0,1,0).getBlock().getType().equals(Material.AIR)))
                            {
                                if(di.getItemStack().getType().equals(Material.TNT))
                                {
                                    di.getWorld().createExplosion(di.getLocation(), 1);
                                    di.remove();
                                }
                                else if(di.getItemStack().getType().equals(Material.GLOWSTONE))
                                {
                                    for(Entity e : di.getNearbyEntities(2, 2, 2))
                                    {
                                        if(e instanceof Player)
                                        {
                                            Player p = (Player) e;
                                            p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 15, 1));
                                        }
                                    }
                                    di.remove();
                                }
                                else if(di.getItemStack().getType().equals(Material.SOUL_SAND))
                                {
                                    for(Entity e : di.getNearbyEntities(2, 2, 2))
                                    {
                                        if(e instanceof Player)
                                        {
                                            Player p = (Player) e;
                                            p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 15, 1));
                                        }
                                    }
                                    di.remove();
                                }
                                else
                                {
                                    di.remove();
                                }
                                Bukkit.getScheduler().cancelTasks(plugin);
                            }
                        }
                    }, 0L, 4L);
                }
            }
        }
    }
    I think it has something to do with how I am declaring my class.
     
  2. Offline

    chasechocolate

    If you use BukkitRunnables, then you can easily do this.cancel() (requires the "this").
     
    whitehooder likes this.
  3. Offline

    Zach_1919

    chasechocolate That's not showing up for me...should I use this.done() instead?

    EDIT: Ohhh, BukkitRunnable instead of just Runnable. Got it

    chasechocolate Well, I put that in and the task isn't being cancelled and the console is being SPAMMED with errors on line 85 (the line with this.cancel;)

    Code:
    Code:
    package me.Zach_1919.BattleBlocks;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scheduler.BukkitRunnable;
     
    public class Main extends JavaPlugin implements Listener
    {   
        public void onEnable()
        {
            getServer().getPluginManager().registerEvents(this, this);
        }
        @EventHandler
        public void onInteract(PlayerInteractEvent event)
        {
            final Player player = event.getPlayer();
            Action action = event.getAction();
            World world = player.getWorld();
            if(!(player.getItemInHand().getType().equals(Material.AIR)))
            {
                if(action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK))
                {
                    final Item di = world.dropItem(player.getEyeLocation(), new ItemStack(player.getItemInHand().getType()));
                    di.setVelocity(player.getEyeLocation().getDirection());
                    if(player.getItemInHand().getAmount() == 1)
                    {
                        player.setItemInHand(null);
                    }
                    else
                    {
                        player.getItemInHand().setAmount(player.getItemInHand().getAmount()-1);
                    }
                    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable()
                    {
                        public void run()
                        {
                            di.setPickupDelay(100);
                            if(!(di.getLocation().subtract(0,1,0).getBlock().getType().equals(Material.AIR)))
                            {
                                if(di.getItemStack().getType().equals(Material.TNT))
                                {
                                    di.getWorld().createExplosion(di.getLocation(), 1);
                                    di.remove();
                                }
                                else if(di.getItemStack().getType().equals(Material.GLOWSTONE))
                                {
                                    for(Entity e : di.getNearbyEntities(2, 2, 2))
                                    {
                                        if(e instanceof Player)
                                        {
                                            Player p = (Player) e;
                                            p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 100, 1));
                                        }
                                    }
                                    di.remove();
                                }
                                else if(di.getItemStack().getType().equals(Material.SOUL_SAND))
                                {
                                    for(Entity e : di.getNearbyEntities(2, 2, 2))
                                    {
                                        if(e instanceof Player)
                                        {
                                            Player p = (Player) e;
                                            p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 100, 1));
                                        }
                                    }
                                    di.remove();
                                }
                                else
                                {
                                    di.remove();
                                }
                                this.cancel();
                            }
                        }
                    }, 0L, 4L);
                }
            }
        }
    }
    Bump

    Again....bump

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

    zeeveener

    Can you post the exact error that you are getting? Also, have you tried using runTaskTimerLater instead of scheduleSyncRepeatingTask?
     
  5. Offline

    Zach_1919

    Well, I need to do repeating task, because I constantly need to check if the item has hit the ground yet, and those times can vary based on the angle of release, unless runTaskTimerLater does something that I don't think it does...

    Code:
    Code:
    package me.Zach_1919.BattleBlocks;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.util.Vector;
     
    public class Main extends JavaPlugin implements Listener
    { 
        public void onEnable()
        {
            getServer().getPluginManager().registerEvents(this, this);
        }
        @EventHandler
        public void onInteract(PlayerInteractEvent event)
        {
            final Player player = event.getPlayer();
            Action action = event.getAction();
            World world = player.getWorld();
            if(!(player.getItemInHand().getType().equals(Material.AIR)))
            {
                if(action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK))
                {
                    final Item di = world.dropItem(player.getEyeLocation(), new ItemStack(player.getItemInHand().getType()));
                    di.setVelocity(player.getEyeLocation().getDirection());
                    if(player.getItemInHand().getAmount() == 1)
                    {
                        player.setItemInHand(null);
                    }
                    else
                    {
                        player.getItemInHand().setAmount(player.getItemInHand().getAmount()-1);
                    }
                    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable()
                    {
                        public void run()
                        {
                            di.setPickupDelay(100);
                            if(!(di.getLocation().subtract(0,1,0).getBlock().getType().equals(Material.AIR)))
                            {
                                if(di.getItemStack().getType().equals(Material.TNT))
                                {
                                    di.getWorld().createExplosion(di.getLocation(), 1);
                                    di.remove();
                                }
                                else if(di.getItemStack().getType().equals(Material.GLOWSTONE))
                                {
                                    for(Entity e : di.getNearbyEntities(2, 2, 2))
                                    {
                                        if(e instanceof Player)
                                        {
                                            Player p = (Player) e;
                                            p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 300, 1));
                                        }
                                    }
                                    di.remove();
                                }
                                else if(di.getItemStack().getType().equals(Material.SOUL_SAND))
                                {
                                    for(Entity e : di.getNearbyEntities(2, 2, 2))
                                    {
                                        if(e instanceof Player)
                                        {
                                            Player p = (Player) e;
                                            p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 300, 1));
                                        }
                                    }
                                    di.remove();
                                }
                                else if(di.getItemStack().getType().equals(Material.GOLD_BLOCK))
                                {
                                    for(Entity e : di.getNearbyEntities(2, 2, 2))
                                    {
                                        if(e instanceof Player)
                                        {
                                            Player p = (Player) e;
                                            p.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 10, 1));
                                        }
                                    }
                                    di.remove();
                                }
                                else if(di.getItemStack().getType().equals(Material.WORKBENCH))
                                {
                                    di.getWorld().spawnArrow(di.getLocation(), new Vector(1,1,0), 1, 1);
                                    di.getWorld().spawnArrow(di.getLocation(), new Vector(-1,1,0), 1, 1);
                                    di.getWorld().spawnArrow(di.getLocation(), new Vector(0,1,1), 1, 1);
                                    di.getWorld().spawnArrow(di.getLocation(), new Vector(0,1,-1), 1, 1);
                                    di.getWorld().spawnArrow(di.getLocation(), new Vector(1,1,1), 1, 1);
                                    di.getWorld().spawnArrow(di.getLocation(), new Vector(1,1,-1), 1, 1);
                                    di.getWorld().spawnArrow(di.getLocation(), new Vector(-1,1,1), 1, 1);
                                    di.getWorld().spawnArrow(di.getLocation(), new Vector(-1,1,-1), 1, 1);
                                }
                                else if(di.getItemStack().getType().equals(Material.NETHERRACK))
                                {
                                    for(Entity e : di.getNearbyEntities(2, 2, 2))
                                    {
                                        if(e instanceof Player)
                                        {
                                            Player p = (Player) e;
                                            p.setFireTicks(100);
                                        }
                                    }
                                }
                                else if(di.getItemStack().getType().equals(Material.OBSIDIAN))
                                {
                                    for(int x = -4; x <= 4; x++)
                                    {
                                        for(int z = -4; z <= 4; z++)
                                        {
                                            di.getWorld().spawnEntity(di.getLocation().getBlock().getRelative(x, 10, z).getLocation(), EntityType.PRIMED_TNT);
                                        }
                                    }
                                }
                                else
                                {
                                    di.remove();
                                }
                                this.cancel();
                            }
                        }
                    }, 0L, 4L);
                }
            }
        }
    }
    Error:
    [​IMG]

    I think it may have something to do with me having to schedule it before actually running it, because that's what the beginning says....

    EDIT: the error is on the line that says
    Code:
    this.cancel();
     
  6. Offline

    CluelessDev

    If you declare an attribute of your class as the taskID when you first schedule the repeating task, you can make a private class that can be called to from within your runnable to let it cancel itself.

    Code:
    int taskID;// - This definition should be an attribute of the class you're working with.
     
    taskID = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable()
    {
        int count = 5;
        @Override
        public void run()
        {
            player.sendMessage(count + "...");
            count--;
            if(count == 0)
            {
                cancelTask();
            }
        }
    }, 0, 20);
     
    private void cancelTask()
    {
        plugin.getServer().getScheduler().cancelTask(taskID);
    }
    This isn't the entire piece, but the three parts you'd need for the way I explained it. If you replace if(count == 0) for whatever conditions you want the task to cancel itself under, it should work.
     
  7. Offline

    Zach_1919

    CluelessDev Can that all stay in my main class or do I need to make a new class for it? I'm not that good with the different classes thing...
     
  8. Offline

    CluelessDev

    As far as I'm aware it should work fine in the main class.
     
  9. Offline

    Zach_1919

    CluelessDev Alright thanks. Also, how do you make that plugin variable? I don't know how to define those because all te expamples I've seen are confusing. Is there a difference between declaring your plugin and your class?
     
  10. Offline

    CluelessDev

    If you're in the main JavaPlugin extension you can just use "this" to refer to the plugin variable. The "Plugin" type is referring to the class that you initiate the plugin from (the one that has onLoad/onDisable, etc). So if I'm right about where you're calling this from you can use:

    plugin.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable()...
     
  11. Offline

    Zach_1919

    CluelessDev Well where does the variable plugin come from though? Is it already there? Also, just for future reference, how do I make instances of plugins and classes to use in different classes. All the examples I've seen are like:
    Code:
    public Plugin plugin;
     
    public <plugin> instance(<MainClassInstance> instance)
    {
      plugin = instance;
    }
    That confuses me. It would really help me if you could show me specific examples with class names in them and tell me which class names are which.

    CluelessDev Well anyway, your way of cancelling the task works! Thank you so much!

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

    CluelessDev


    An instance of your plugin is created when the plugin is loaded. Your main class is the one that extends JavaPlugin that your plugin.yml refers to. The plugin variable would be obtained by using the "this" keyword when your in your main class.

    So if you're creating new instances of classes when the plugin is enabled, you can give them a parameter of type Plugin. Then when you create the instance from your main class, feed it "this" as the argument. Alternatively you could use "Bukkit.getPluginManager.getPlugin("<Plugin Name>");" But that way just seems a little roundabout to me.
     
  13. Offline

    Zach_1919

    CluelessDev Hmmm, still a bit cloudy, but that was just a side question. You fixed my main question, and I will be eternally grateful :). *following*
     
Thread Status:
Not open for further replies.

Share This Page