How do I get the arrow (ItemStack) shot by a bow

Discussion in 'Plugin Development' started by Jack Price-Burns, May 22, 2014.

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

    I was wondering if there was a way to get the arrow (in itemstack) that was shot by a bow im using the EntityShootBowEvent and checking if the projectile is an arrow and if the entity is a player here is my current code


    Code:java
    1. @EventHandler
    2. public void shootArrowEvent(EntityShootBowEvent event){
    3. if(event.getProjectile() instanceof Arrow){
    4. if(event.getEntity() instanceof Player){
    5. ItemStack bow = event.getBow();
    6. Arrow arrow = (Arrow) event.getProjectile();
    7. if(bow.getItemMeta().hasDisplayName()){
    8. if(bow.getItemMeta().getDisplayName().equals("explosion")){
    9. setMetaData(arrow, "explosion", false);
    10. }
    11. }
    12. }
    13. }
    14. }
    15.  
    16. @SuppressWarnings("deprecation")
    17. @EventHandler
    18. public void onProjectileHit(ProjectileHitEvent event)
    19. {
    20. if(event.getEntity() instanceof Arrow)
    21. {
    22. if(event.getEntity().getShooter() instanceof Player)
    23. {
    24. Arrow arrow = (Arrow) event.getEntity();
    25. World world = arrow.getLocation().getWorld();
    26. Location location = arrow.getLocation();
    27. if(hasMetadata(arrow, "explosion")){
    28. world.createExplosion(location, 3);
    29. arrow.remove();
    30. }
    31. else
    32. {
    33. if(arrow.getFireTicks() > 0)
    34. {
    35. Block block = location.getBlock();
    36. block.setType(Material.FIRE);
    37. arrow.remove();
    38. }
    39. }
    40. }
    41. }
    42. }

    I want to set the metadata on what arrow was shot not the name of the bow because people could just rename there bow also I would like to do the same with fire like if the arrow has a metadata called "fire" then when it hits the ground makes fire. Would it be possible to get the itemstack and how ??
     
  2. Offline

    tastybento

    So, just to be clear - the code you post above is how you are doing it now, but you don't want to do that and instead you want to set meta data for arrows that indicate that they are explosive arrows or fire arrows?
     
  3. Offline

    rsod

    Jack Price-Burns use lore, it can't be changed by player
     
    RAFA_GATO_FOFO likes this.
  4. Offline

    Bobit

    rsod

    Unless you place it as a block. Everything is removed then, I think.
     
  5. Offline

    Gater12

    Bobit
    A bow is an item... Not a placeable block.
     
  6. Offline

    Bobit

    Gater12

    Well, yeah, but it's worth noting for future reference.
     
  7. Offline

    AoH_Ruthless

    rsod Jack Price-Burns
    Metadata is better, especially if you don't need the data being visible to the player (If you have casted the arrow to it's MaterialData type (Arrow) then you can use arrow.setMetadata();)
     
  8. tastybento AoH_Ruthless Bobit Gater12 rsod

    Using the entity shoot bow event I would like to get the arrow that was shot not the projectile the item this is so I can check various things about it like the name the metadata or lores
     
  9. Offline

    metalhedd

    There's only one way to do this and its not pretty or performant. You need to listen for the PlayerInteractEvent first, during that event, if the player has a bow in their hand, you need to find all Arrow ItemStacks in their inventory and save them somewhere for comparison. Then when they EntityShootBowEvent fires, you check their inventory again and compare the stacks to see which one is missing an arrow. It's probably going to have a noticable performance impact. but there's no other way to accomplish this.
     
  10. metalhedd

    Oh man dat system thanks for the advice if I find anything out il post it here.
     
  11. *Bump*

    I was talking to one of my friends about this issue and he is really good at java he told me there is a way of doing this via NMS or something and this requires overriding a minecraft class file to make the arrow that is removed into a getArrowUsed() function could anyone who knows what he is talking about give me an example of this. My friend said he wont tell me because I wont learn anything from him telling me but also he gives me no help what so ever in trying to find the answer.
     
Thread Status:
Not open for further replies.

Share This Page