ProjectileHitEvent Question

Discussion in 'Plugin Development' started by Felixx61, Jun 7, 2013.

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

    Felixx61

    Hi all,

    So I've got a method SnowballMissEvent that is supposed to spawn a snowball at the location where it hits as long as it it is not a Player or Goal. I've tried several methods but can't seem to figure out how to check if it hit a player, and if it did to obviously event.setCancelled(true).

    Here's what I have:

    Code:
      //when player throws snowball and misses player/goal 
      @EventHandler
        public void SnowballMissEvent(ProjectileHitEvent event) {
            if (event.getEntity() instanceof Snowball) {
                Snowball sb = (Snowball) event.getEntity();
                Player thrower = (Player) sb.getShooter();
               
                if (sb.getShooter() != null && (this.plug.isPlaying(thrower.getName()))) {
                        Location loc = sb.getLocation();
                        ItemStack item = new ItemStack(Material.SNOW_BALL, 1);
                        loc.getWorld().dropItem(loc, item);
                        thrower = null;
                }
            }
        }
    Thanks!
     
  2. Offline

    Jogy34

    EntityDamageByEntityEvent. You check if that damager is a projectile then a snowball and then if the damaged was a player.
     
    Felixx61 likes this.
  3. Offline

    Felixx61


    How would you check if the damaged was a player?

    I've got this and it works, but now when it hits a block it will not spawn the item above:

    Code:
        //when player throws snowball and misses player/goal 
        @EventHandler
          public void SnowballMissEvent(EntityDamageByEntityEvent event) {
              if (((event.getEntity() instanceof Player) && (event.getDamager() instanceof Snowball))) {
                  Snowball sb = (Snowball) event.getDamager();
                  Player catcher = (Player) event.getEntity();
                  Player thrower = (Player) sb.getShooter();
                 
                  if (sb.getShooter() != null && (this.plug.isPlaying(thrower.getName()))) {
                      if (event.getEntity() != null){
                          event.setCancelled(true);
                      }
                      else{
                          Location loc = sb.getLocation();
                          ItemStack item = new ItemStack(Material.SNOW_BALL, 1);
                          loc.getWorld().dropItem(loc, item);
                          thrower = null;
                      }
                  }
              }
          }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  4. Offline

    Jogy34

    The ProjectileHitEvent is fired when a projectile hits a block.
     
  5. Offline

    Felixx61


    Via Bukkit API "Called when a projectile hits an object" ... which would make sense because before when it would hit a player it would also be fired.

    Thanks for your help
     
  6. Offline

    Jogy34

    I didn't think that the ProjectilHitEvent was called when a snowball hit a player.
     
Thread Status:
Not open for further replies.

Share This Page