Why is this bugging out?

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

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

    Zach_1919

    So I am coding this plugin where when you throw something and it hits the ground, something special happens. I am using a repeating task for this, constantly checking if the block below the item is not air, and as soon as it's anything other than air, the task get's cancelled after the special effect happens. But, if I throw one, then throw another before the first one hits the ground, the task never gets cancelled and it constantly repeats the special effect. Can someone point out to me why it does this, and maybe a possible solution?

    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.util.Vector;
     
    public class Main extends JavaPlugin implements Listener
    {   
        int taskID;
        public void onEnable()
        {
            getServer().getPluginManager().registerEvents(this, this);
            this.saveDefaultConfig();
        }
        private void cancelTask()
        {
            Bukkit.getScheduler().cancelTask(taskID);
        }
        @EventHandler
        public void onInteract(PlayerInteractEvent event)
        {
            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);
                    }
                    taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
                    {
                        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);
                                    di.remove();
                                }
                                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);
                                        }
                                    }
                                    di.remove();
                                }
                                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);
                                        }
                                    }
                                    di.remove();
                                }
                                else if(di.getItemStack().getType().equals(Material.WOOL))
                                {
                                    for(Entity e : di.getNearbyEntities(2, 2, 2))
                                    {
                                        if(e instanceof Player)
                                        {
                                            Player p = (Player) e;
                                            p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 300, 1));
                                        }
                                    }
                                    di.remove();
                                }
                                else
                                {
                                    di.remove();
                                }
                                cancelTask();
                            }
                        }
                    }, 0L, 4L);
                }
            }
        }
    }
    Thanks in advance :)
     
  2. Offline

    chasechocolate

    I strongly recommend the use of BukkitRunnables, it makes canceling tasks much easier. Here is an example:
    Code:java
    1. new BukkitRunnable(){
    2. @Override
    3. public void run(){
    4. if(something){
    5. doSomething();
    6. } else {
    7. this.cancel();
    8. }
    9. }
    10. }.runTaskTimer(<plugin instance>, 0L, 4L);
     
  3. Offline

    Zach_1919

    chasechocolate I know, but I did that before and it threw an error. Check out the post at the bottom of is. The method I am using works, except for this bug. Was I getting an error in the post I linked because I didn't extend BukkitRunnable?

    http://forums.bukkit.org/threads/cancelling-repeating-tasks.151037/

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page