Getting Display Name of a shot arrow

Discussion in 'Plugin Development' started by Burnett1, Apr 21, 2013.

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

    Burnett1

    Is there any way to get an arrows display name from the ProjectileHitEvent. If not, how would i get if the arrow has a specific name when its shooting?

    Thanks for any help.
     
  2. Offline

    LucasEmanuel

    I would guess that you have to work with metadata :)
     
  3. Offline

    Burnett1

    Yes i have checked but i cant find a way to get the display name. Im thinking that the arrow is just a plain arrow entity and wont have the metadata of the ItemStack i made.
     
  4. Offline

    xize

    hmm what if you check if the damager is a instanceof Arrow? I did this with the DamageByEntityEvent it seems a arrow is a entity aswell then make the type Arrow and cast it to the damager, can you get the displayname from there??

    also maybe this is optional regarding what ive read from other posts
    Code:
    Arrow arrow = (Arrow) e.getDamager().getMetadata("DisplayName");
    
    it seems the list from getMetadata has a entry called DisplayName if I'm correct.

    edit I ment EntityDamageByEntityEvent:p
     
  5. Offline

    Burnett1

    k ill try thanks.

    xize
    I tried this but it didn't work, not sure if its how you do it.

    Code:
    @EventHandler
        public void projectileHitEvent(ProjectileHitEvent e){
     
                System.out.println("1");
       
                Arrow arrow = (Arrow) e.getEntity();
     
                if(!(arrow instanceof Arrow)) return;
       
                System.out.println("2");
     
                if(arrow.getMetadata("DisplayName").equals("Explosive Tip Arrow")){
           
                    System.out.println("3");
     
                    e.getEntity().getWorld().createExplosion(e.getEntity().getLocation(), 1);
                }
        }

    Edit: Just for reference here is the ItemStack

    Code:
            ItemStack explosiveTipBow = new ItemStack(Material.ARROW, 1);
            meta = explosiveTipBow.getItemMeta();
            meta.setDisplayName("Explosive Tip Arrow");
            explosiveTipBow.setItemMeta(meta);
     
            ShapedRecipe explosiveTipBowRecipe = new ShapedRecipe(explosiveTipBow).shape("*","%"," ").setIngredient('*', Material.SULPHUR).setIngredient('%', Material.ARROW);
     
            getServer().addRecipe(explosiveTipBowRecipe);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  6. Offline

    xize

    the code seems good though any errors like a npe?
    especially on line 7 of the first snippet?

    also try a try and catch expression with the meta maybe it shows something

    edit what if you change the getMetadata("DisplayName") to getMetadata("DisplayName").toString().equalsIgnorecase("Explosive Tip Arrow");
     
  7. Offline

    Burnett1

    No errors it gets two Message "2" and the does nothing.
     
  8. Offline

    xize

    hmm what you can try is to print out the getMetadata("DisplayName").toString(); to see if it's valid
     
  9. Offline

    Scyntrus

    When a player shoots an arrow, I don't think the metadata from the ItemStack is carried over to the Arrow entity. You should add a listener for ProjectileLaunchEvent and give the Arrow the metadata when it is fired.
     
  10. Offline

    Burnett1

    xize
    It prints out empty square brackets. "[]"
     
  11. Offline

    Chris32

    Arrow entities can't have a DisplayName NBT property. Only mobs can. I'm pretty sure of this, and haven't read the whole thread, so correct me if I'm wrong.
     
  12. Offline

    Burnett1

    So how do i know to tag the correct arrow? If i am correct its in the ProjectileLaunchEvent but how would i compare it to the itemstack there?
     
  13. Heh, good question. You can't get the itemstack there either. So I guess you just need to add a way the player can switch to using his special arrow (using commands or right-clicking with a bow) and check his inventory if he has that special arrow.
     
  14. Offline

    Burnett1

    ok thanks for your help.
     
  15. Offline

    LucasEmanuel

    These two lines:
    Code:
    Arrow arrow = (Arrow) e.getEntity();
     
    if(!(arrow instanceof Arrow)) return;
    Why do you check if it's an instance after you have parsed it? Wouldn't this throw a ParseException if the projectile was a fireball or snowball?
     
  16. Offline

    Burnett1

    I dont know i was just quick coding wasnt thinking. Was wanting to check if it was possible first.
     
  17. Offline

    ZeusAllMighty11

    LucasEmanuel

    No, because there is no parsing there. That is a ClassCastException
     
  18. Offline

    LucasEmanuel

    Hahaha oops, of course it is, silly me. :)
     
Thread Status:
Not open for further replies.

Share This Page