Solved ProjectileHitEvent help

Discussion in 'Spigot Plugin Development' started by Poofyjerry, Feb 22, 2020.

  1. Code (Text):

    package me.cadeheinberg.ItemCommander;

    import org.bukkit.Material;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import net.md_5.bungee.api.ChatColor;

    public class ListenerClass implements Listener {
     
      @EventHandler
      public static void onInteract(PlayerInteractEvent e) {
     
        if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {  
          ItemStack item = e.getItem();
          if(item.getType().equals(Material.GOLDEN_SHOVEL)){
            if(item.getItemMeta().getDisplayName().equals(ChatColor.GOLD + "LIL SHOTTY")) {
              Player player = e.getPlayer();
              player.performCommand("pfall");
            }
          }
          if(item.getType().equals(Material.IRON_AXE)) {
            if(item.getItemMeta().getDisplayName().equals(ChatColor.LIGHT_PURPLE + "Booster Axe")) {
              Player player = e.getPlayer();
              player.performCommand("plaunch");
            }
          }
        }
      }
     //RIGHT HERE
      @SuppressWarnings("unlikely-arg-type")
      @EventHandler
      public static void onCollison(ProjectileHitEvent e) {
        if(e.getEntity().equals(Snowball.class)){
          Entity entity = e.getHitEntity();
          entity.sendMessage("You've been hit by snowball!");
        }
        if(e.getEntity().equals(Arrow.class)){
          Entity entity = e.getHitEntity();
          entity.sendMessage("You've been hit by arrow!");
        }
      }
    }
     
    Hi I am trying to have an action done to the entity that is hit by a snowball or arrow. I eventually want the entity to be set on fire but for now sending them a message will do. When I have two players in game i shoot the one with an arrow and a snowball and nothing happens. The snowball doesn't even deal any sort of knockback or damage so idk if it's even registering a projectile hit event.

    Am I using the right code or do I have the right idea in mind? Im really new to plugin developement

    PS the code for the ProjectileHitEvent is at the bottom of the class, I included the other stuff bc idk if having two events in one class messes something up bc one is more general than the other possibly?
     
    #1 Poofyjerry, Feb 22, 2020
    Last edited: Apr 13, 2020
  2. Try
    e.getEntity().getType() == EntityType.SNOWBALL
    e.getEntity().getType() == EntityType.ARROW
     
  3. Hey this worked thank you! Is there any way I can fix this issue...
    Everytime a snowball hits a block I get this error because the block is not an entity. How
    can I check for this and avoid geting the error?
    Here:
     
  4. Just check if entity is a null.
    Code (Java):
      @EventHandler
      public static void onProjectileHit(ProjectileHitEvent e) {
        Entity entity = e.getHitEntity();
        if(entity == null) return;
        switch(entity.getType()) {
          case SNOWBALL: entity.sendMessage("You've been hit by snowball!"); break;
          case ARROW: entity.sendMessage("You've been hit by arrow!"); break;
        }
      }
     

  5. You will want to surround the:
    Code (Text):
      public static void onCollison(ProjectileHitEvent e) {
        if(e.getEntity().equals(Snowball.class)){
          Entity entity = e.getHitEntity();
          entity.sendMessage("You've been hit by snowball!");
        }
        if(e.getEntity().getType() == EntityType.ARROW){
          Entity entity = e.getHitEntity();
          entity.sendMessage("You've been hit by arrow!");
        }
      }
    }
    in an if statement checking to see if the hit entity is a player, and then go from there. such as:

    Code (Text):
      public static void onCollison(ProjectileHitEvent e) {
       if(e.getHitEntity instanceof Player){
          if(e.getEntity().equals(Snowball.class)){
          Entity entity = e.getHitEntity();
          entity.sendMessage("You've been hit by snowball!");
        }
        if(e.getEntity().getType() == EntityType.ARROW){
          Entity entity = e.getHitEntity();
          entity.sendMessage("You've been hit by arrow!");
        }
      }
     }
    }
     
    • Like Like x 1
  6. thank you
     
  7. thank you!
     
  8. Are your problems solved? If so please mark the thread as "Solved"! :)

    Happy Coding!