Snowball Firing Issue - Double Firing

Discussion in 'Plugin Development' started by 12boulla, Feb 27, 2017.

Thread Status:
Not open for further replies.
  1. Hi,

    I have recently got into Bukkit Plugin Development, and started a simple spleef mechanism. I have the following event handler that fires a snowball projectile when the player right clicks a diamond shovel:

    Code:
    @EventHandler
        public void onPlayerUse(PlayerInteractEvent event){
            Player p = event.getPlayer();
            if(p.getItemInHand().getType() == Material.DIAMOND_SPADE && (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)){
                Snowball snowball = p.getWorld().spawn(p.getEyeLocation(), Snowball.class);
                //p.launchProjectile(Snowball.class);
                snowball.setShooter(p);
                snowball.setVelocity(p.getLocation().getDirection().multiply(1.5));
                //event.getPlayer().getServer().dispatchCommand(p.getServer().getConsoleSender(),"playsound entity.blaze.shoot block " + p.getName() +" "+ p.getLocation().getBlockX()
                 //+ " " + p.getLocation().getBlockY() + " " + p.getLocation().getBlockZ() + " .5 1.5");
                p.playSound(p.getLocation(),"entity.blaze.shoot",0.4f,1.5f);
            }
        }
    This works fine, and fires a snowball when the user right clicks the shovel. However, I added the ability to break blocks when the egg lands, and at close range, with the shovel it destroys 2 blocks instead of only one. WIth a regular snowball it only destroys one:

    Code:
    @EventHandler
        public  void snowballHit(ProjectileHitEvent event) {
            if (event.getEntityType() == EntityType.SNOWBALL) {
                //event.getEntity().getWorld().createExplosion(event.getEntity().getLocation(), 1.0f, false);
                BlockIterator iterator = new BlockIterator(event.getEntity().getWorld(), event.getEntity().getLocation().toVector(), event.getEntity().getVelocity().normalize(), 0.0D, 70);
                Block hitBlock = null;
                while (iterator.hasNext()) {
                    hitBlock = iterator.next();
    
                    if (hitBlock.getTypeId() != 0) {
                        break;
                    }
                }
    
                if (hitBlock.getType() != Material.AIR) {
                    hitBlock.getWorld().playEffect(hitBlock.getLocation(), Effect.STEP_SOUND, hitBlock.getTypeId());
                    hitBlock.setType(Material.AIR);
                    event.getEntity().getServer().broadcastMessage("Hit Block :(");
                    event.getEntity().remove();
                }
            }
        }


    Is there something I'm doing wrong? Any help is greatly appreciated, thanks.
     
  2. Offline

    Irantwomiles

    have you tried it at normal speed and seeing if it does the same thing?
     
  3. Yes, it still has the issue. I've also tried just p.launchProjectile(Snowball.class) and is still does it.
     
  4. Offline

    Irantwomiles

    You can do this to check if its the #PlayerInteractEvent causing the issue.

    Create a hashmap<String, Long> and store the players name and system time + (1000 * 1). This will pretty much create a 1 second cooldown. Check if the cooldown is up and if it is fire the snow ball and put the player back in the map. I had this same issue with it pretty much double clicking and this solved my issue. This might not be ideal for you since I'm assuming its a gun, but worth a try.
     
  5. Offline

    MaxFireIce

    @12boulla
    Correct me if I'm wrong but:
    Code:java
    1. Snowball snowball = p.getWorld().spawn(p.getEyeLocation(), Snowball.class);
    2. //p.launchProjectile(Snowball.class);


    Wouldn't the spawn summon a snowball, and then you are shooting another snowball?
     
  6. Offline

    ipodtouch0218

    The PlayerInteractEvent fires twice because you have two hands, which means it has to do two checks. Check if e.getHand() != EquipmentSlot.HAND, and cancel the event.
     
Thread Status:
Not open for further replies.

Share This Page