Bukkit Scheduler Problem

Discussion in 'Plugin Development' started by benfah, Dec 22, 2014.

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

    benfah

    Hey, I have a problem with the Bukkit schedulers, I wrote a plugin that allows to use Slimeballs as grenades, the problem is that when I throw two grenades, then it does not work. Here is the Code(thanks in advance):


    Code:
    package me.benfah.main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Effect;
    import org.bukkit.EntityEffect;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    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;
    
    public class Main extends JavaPlugin implements Listener {
    
       
        public int schedule;
        public int schedule2;
        public int schedule3;
        public int schedule4;
        public int schedule5;
       
       
        public void onEnable(){
           
            getServer().getPluginManager().registerEvents(this, this);
           
        }
       
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onInteract(PlayerInteractEvent e) throws InterruptedException{
           
            Player p = e.getPlayer();
           
           
           
           
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
                if(p.getInventory().getItemInHand().getType() == Material.SLIME_BALL){
                   
                    final Item ent = p.getWorld().dropItem(p.getEyeLocation(), new ItemStack(Material.SLIME_BALL));       
                    ent.setVelocity(p.getEyeLocation().getDirection().multiply(1.5));
                    p.getInventory().remove(p.getItemInHand());
                    ent.setPickupDelay(25000);
                   
                    Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
       
                            @Override
                            public void run() {
                               
                                ent.getWorld().playSound(ent.getLocation(),Sound.ITEM_PICKUP, 2, 2);
                                ent.getWorld().playEffect(ent.getLocation(),Effect.SMOKE, 1);
                                if(ent.isDead()){
                                    Bukkit.getScheduler().cancelTask(schedule);
                                   
                                }
                            }
                           
                           
                        }, 0L, 1L);
                   
                   
                       
                    Bukkit.getScheduler().scheduleAsyncDelayedTask(this, new Runnable(){
    
                        @Override
                        public void run() {
                            Location loc = ent.getLocation();
                            ent.getWorld().createExplosion(loc.getX(), loc.getY(), loc.getZ(), 4, false, false);
                            ent.remove();
                            Bukkit.getScheduler().cancelTask(schedule);
                           
                        }
                       
                       
                       
                    }, 20L*2L);
                   
                }
                   
            }
               
           
           
           
           
           
           
           
           
           
        }
    
       
       
    }
    
    
     
  2. Offline

    ColonelHedgehog

    You can't use Async schedulers for most BukkitAPI methods—particularly, things dealing with Worlds. I suggest you use a Sync scheduler instead.
     
  3. Offline

    TheMintyMate

    @benfah
    @ColonelHedgehog

    Or even better....
    Code:
    new BukkitRunnable(){
       public void run(){
          //code
       }
    }.runTaskLater(plugin,20);
    
    //or...
    
    new BukkitRunnable(){
       public void run(){
          //code
          if (a variable is true){
             this.cancel(); /*cancel task*/
             return;
          }
       }
    }.runTaskTimer(plugin,0,40);
    
    - Minty
     
    leon3001 likes this.
Thread Status:
Not open for further replies.

Share This Page