Solved BukkitRunnable

Discussion in 'Plugin Development' started by bronzzze, Apr 15, 2015.

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

    bronzzze

    BukkitRunnable:
    Code:
    public class BukkitTaskBomb extends BukkitRunnable{
    Code:
    public void run() {
    .....
    System.out.println("test");
    }
    In other class I set
    Code:
    new BukkitTaskBomb(....).runTaskLater(main, 20*4)
    In console it print out test Like 50 times. how to make it so it only print it once.
     
  2. Offline

    teej107

    Don't use a loop
     
  3. Offline

    bronzzze

    Don't use loop where?
     
  4. Offline

    teej107

    @bronzzze You said it spams text like 50 times. You need to give more code because the code you gave doesn't prove that a spamming is happening.
     
    bronzzze likes this.
  5. Offline

    bronzzze

    this is whole bukkittask class
    Code:
    package me.bronzzze.bombs;
    
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.Sound;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class BukkitTaskBomb extends BukkitRunnable{
        Main plugin;
        Entity grenade;
        Player player;
        int radius;
      
      
        public BukkitTaskBomb(Main plugin, Entity grenade, int radius) {
            this.plugin = plugin;
            this.grenade = grenade;
            this.radius = radius;
        }
    
         @SuppressWarnings("deprecation")
        public void run(){
           
             Location loc = grenade.getLocation();
                for (Location l : plugin.sphere.CreateExplosion(player, loc,
                        radius)){
                    l.getBlock().breakNaturally();
                grenade.remove();
                loc.getWorld().playSound(loc, Sound.EXPLODE, 1,
                        1);
                loc.getWorld().playEffect(loc,
                        Effect.EXPLOSION, 4);
                System.out.println("test");
                }
           
         }
    }
    
     
  6. Offline

    Hex_27

    @bronzzze
    This just means that somehow, your runnable is activated 50 times. Do show us where you used the BukkitTaskBomb.run

    Or that you somehow made it loop 50 times with the for statement.
     
  7. Offline

    bronzzze

    Code:
        @EventHandler
        public void onThrow(final PlayerInteractEvent event) {
            final Player player = event.getPlayer();
            World world = player.getWorld();
            ItemStack hand = (ItemStack) player.getItemInHand();
            if (event.getAction() == Action.RIGHT_CLICK_AIR
                    || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (hand != null
                        && hand.hasItemMeta()
                        && hand.getItemMeta().hasDisplayName()
                        && hand.getItemMeta()
                                .getDisplayName()
                                .contains(
                                        "" + ChatColor.GOLD + ChatColor.BOLD
                                                + "Small Bomb")) {
    
                    final ItemStack small = new ItemStack(Material.SLIME_BALL);
                    ItemMeta smallM = small.getItemMeta();
                    smallM.setDisplayName("" + ChatColor.GOLD + ChatColor.BOLD
                            + "Small Bomb");
                    small.setItemMeta(smallM);
                    small.addUnsafeEnchantment(Enchantment.DURABILITY, 1);
                    final Item smallbitem = world.dropItem(player.getEyeLocation(),
                            small);
                    player.getInventory().removeItem(small);
                    smallbitem.setVelocity(player.getEyeLocation().getDirection());
    
                    player.getWorld().playSound(player.getLocation(),
                            Sound.WITHER_SHOOT, 1, 2);
    
                    new BukkitTaskBomb(main, smallbitem, main.getConfig().getInt("SmallBomb.Radius")).runTaskLater(main,
                            20 *  main.getConfig().getInt("SmallBomb.ExplosionDelay"));
                    main.setGrenade(smallbitem, player);
    
                }
    
    
     
  8. Offline

    Hex_27

    @bronzzze Since there's not much of a problem here, I think its because the number of locations in value:plugin.sphere.CreateExplosion(player, loc, radius)
    is about 50. So maybe that's why it loops so many times.
     
    bronzzze likes this.
  9. Offline

    bronzzze

    Problem is only that Sound.Explosion sounds very loud beacuse of this. I will try to fix it. Thanks

    Edit:
    I just change from this:
    Code:
    public void run(){
            
             Location loc = grenade.getLocation();
                for (Location l : plugin.sphere.CreateExplosion(player, loc,
                        radius)){
                    l.getBlock().setTypeId(0);
                    l.getBlock().breakNaturally();
               
                grenade.remove();
                loc.getWorld().playSound(loc, Sound.EXPLODE, 1,
                        1);
                loc.getWorld().playEffect(loc,
                        Effect.EXPLOSION, 4);
                System.out.println("test");
               
            
         }
    }
    to this:
    Code:
    public void run(){
            
             Location loc = grenade.getLocation();
                for (Location l : plugin.sphere.CreateExplosion(player, loc,
                        radius)){
                    l.getBlock().setTypeId(0);
                    l.getBlock().breakNaturally();
                }
                grenade.remove();
                loc.getWorld().playSound(loc, Sound.EXPLODE, 1,
                        1);
                loc.getWorld().playEffect(loc,
                        Effect.EXPLOSION, 4);
                System.out.println("test");
               
            
         }
    Stupid mistake by me...
     
  10. Offline

    Hex_27

    @bronzzze if the sound is too loud, have a boolean before your run(), then play it on the first loop, then set the boolean to true. When looping again, check if the boolean is true. If it is, don't play the sound
     
  11. Offline

    bronzzze

    I fix it. I looped it wrong.
     
Thread Status:
Not open for further replies.

Share This Page