Could not pass event ProjectileLaunchEvent when shooting

Discussion in 'Spigot Plugin Development' started by Necros, Jun 18, 2015.

  1. Hi to all.
    I'm developing my first plugin for my server: i'm trying to make a plugin that checks if a bow name is Wither or fireball. If the name is Wither the bow will shoot a WitherSkull, if Fireball a fireball... I get this error in the console...:


    A part of the source:

    (onEnable in the main)

    Code (Text):
    @Override
        public void onEnable(){
     
            getLogger().info("PluginEnabled");
            new PlayerListener(this);
        }
    And in the PlayerListener class:

    Code (Text):
    public PlayerListener(Plugin p)
    {
       Bukkit.getServer().getPluginManager().registerEvents(this, p);
    }
    @EventHandler
    public void onProjectileLaunch(ProjectileLaunchEvent event) {
        Entity p = (Arrow) event.getEntity();
        Arrow a = (Arrow)p;
    Entity pl = (Player) a.getShooter();
    if(((HumanEntity) pl).getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("Wither")){
        if (!(p instanceof Arrow)) {
            return;
            }
        if (!(((Projectile) p).getShooter() instanceof Player)) {
            return;
        }
        event.setCancelled(true);
        WitherSkull ws = (WitherSkull) ((ProjectileSource) pl).launchProjectile(WitherSkull.class);
        ws.setVelocity(a.getVelocity());
        ws.setIsIncendiary(true);
        ws.setYield(10.0F);
    }
    }
    }

    Please help me, i know i'm a noob newbie but i want to know why it isn't working...
     
  2. A projectile is not always an arrow.
    You are casting the entity to an Arrow, and after that you check if it's an arrow.
    You should do it the other way around, first check if it's an arrow, then cast to it.
     
    • Agree Agree x 1
    • Useful Useful x 1
  3. Also, what's the point of casting a.getShooter() to a player if the variable you're defining with it isn't even a player?
     
  4. Thanks, i'm looking for this, but seems i get the same errors with:
    Code (Text):

    @EventHandler
    public void onThrow(PlayerEggThrowEvent event){
       Player p = event.getPlayer();
     p.sendMessage(ChatColor.GRAY + "Easter"+ ChatColor.RED + "egg"+ ChatColor.GRAY + ". .... Ok lets stop.");  
    }
     
     
  5. Code (Text):
    if(((HumanEntity) pl).getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("Wither")){
    Check for item meta first and then for displayname
     
  6. Thanks to all for the replies, i solved. Thanks to
    I solved checking as he said.
    Code (Text):
        @EventHandler(priority = EventPriority.NORMAL)
        public void onProjectileLaunch(ProjectileLaunchEvent event) {
            Entity p = event.getEntity();
           
            if (!(p instanceof Arrow)) {
                return;
            }
            Arrow a = (Arrow) p;
            if (!(a.getShooter() instanceof Player)) {
                return;
            }
         
            Player pl = (Player) a.getShooter();
            if(pl.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("wither")){
            event.setCancelled(true);
            WitherSkull ws = (WitherSkull) pl.launchProjectile(WitherSkull.class);
            ws.setVelocity(a.getVelocity());
            ws.setIsIncendiary(true);
            ws.setYield(5.0F);
       
    }
     
    • Winner Winner x 1
  7. Oh, sorry guys, but i realized i get another error.... All works fine but when i shoot with a NON-named bow (without a custom display name) i get this error:
     
  8. Isn't there are hasDisplayName function?
    Or just check if getDisplayName isn't null
     
    • Winner Winner x 1
  9. oooouh, i totally forgot to check this, yeah, it's that.. Thank you again! I solved with:
    Code (Text):
          if(!pl.getItemInHand().getItemMeta().hasDisplayName() == false){
      blablabla
    }else {
    return;}
    Thank you again!
     
  10. Why would you do !condition == false !?
    o_O
     
  11. Lol yep i like this way xD

    Im twisted:cool:
     
  12. Twisted? Well at least you got that right.
     
    • Agree Agree x 1