Bukkit getting the name of the bow in ProjectileHitEvent

Discussion in 'Plugin Development' started by _Belknap_, Feb 27, 2014.

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

    _Belknap_

    Hello, I have a problem. I have this code setup so that when an arrow hits something and is shot from a bow that is called in gray, "Bone Bow", it throws out bone drops that explode on impact when touched by a player. Yet the bones still explode out for any bow that I use. Here is my code:
    Code:java
    1.  
    2. @EventHandler
    3. public void onHit(ProjectileHitEvent event) {
    4. Entity entity = event.getEntity();
    5. Location loc = entity.getLocation();
    6. if (entity instanceof Arrow) {
    7. Player shooter = (Player) ((Arrow) entity).getShooter();
    8. if (shooter instanceof Player) {
    9. if (shooter.getItemInHand().getItemMeta().hasDisplayName()) {
    10. if (shooter.getItemInHand().getItemMeta().getDisplayName() == ChatColor.GRAY + "Bone Bow") {
    11. }
    12. entity.getWorld().dropItem(loc.add(new Vector(0, 0, 0)), new ItemStack(Material.BONE)).setPickupDelay(2);
    13. entity.getWorld().dropItem(loc.add(new Vector(0, 0, 0)), new ItemStack(Material.BONE)).setVelocity(entity.getLocation().getDirection().multiply(1.6));
    14. entity.getWorld().dropItem(loc.add(new Vector(0, 0, 0)), new ItemStack(Material.BONE)).setPickupDelay(2);
    15. entity.getWorld().dropItem(loc.add(new Vector(0, 0, 0)), new ItemStack(Material.BONE)).setVelocity(entity.getLocation().getDirection().multiply(1.6));
    16. entity.getWorld().dropItem(loc.add(new Vector(0, 0, 0)), new ItemStack(Material.BONE)).setPickupDelay(2);
    17. entity.getWorld().dropItem(loc.add(new Vector(0, 0, 0)), new ItemStack(Material.BONE)).setVelocity(entity.getLocation().getDirection().multiply(1.6));
    18. entity.getWorld().dropItem(loc.add(new Vector(0, 0, 0)), new ItemStack(Material.BONE)).setPickupDelay(2);
    19. entity.getWorld().dropItem(loc.add(new Vector(0, 0, 0)), new ItemStack(Material.BONE)).setPickupDelay(2);
    20. entity.getWorld().dropItem(loc.add(new Vector(0, 0, 0)), new ItemStack(Material.BONE)).setVelocity(entity.getLocation().getDirection().multiply(1.6));
    21. entity.getWorld().dropItem(loc.add(new Vector(0, 0, 0)), new ItemStack(Material.BONE)).setPickupDelay(2);
    22. entity.getWorld().dropItem(loc.add(new Vector(0, 0, 0)), new ItemStack(Material.BONE)).setVelocity(entity.getLocation().getDirection().multiply(1.6));
    23. entity.getWorld().dropItem(loc.add(new Vector(0, 0, 0)), new ItemStack(Material.BONE)).setPickupDelay(2);
    24.  
    25. }


    Thanks in advance!
     
  2. Offline

    calebbfmv

    Erm mer gerd so much code, please use a for loop.
    No need for .add(new Vector()) just to .add(0,0,0);
     
  3. Offline

    Milkywayz

    Code:
    if (shooter.getItemInHand().getItemMeta().getDisplayName() == ChatColor.GRAY + "Bone Bow")
    Not the correct way to compare strings. Furthermore I believe that comparing strings with chat colors in them is a little tough, however it probably can be done.

    Also like calebbfmv suggested, please use a loop to avoid the massive code duplication eyesore.
     
  4. Offline

    Whomp54


    He could always use the "§" symbol in place of it and use the color code number... Ex: §b is light blue. I never use ChatColor codes.
     
  5. Offline

    Nashor

    Aye, == will not work for Strings. Use .equalsIgnoreCase() instead.
     
  6. Offline

    Wolfey

    ^

    _Belknap_, You are comparing the string object, which won't work, use Java's built-in string method instead (.equals() or .equalsIgnoreCase()).
     
  7. Offline

    Adamki11s

    _Belknap_
    The most efficient way of doing this would be to store the entity id's of arrows fired from a bone bow in a hashset. And then when the arrow hits check if the entity id is in the hashset, if so then you know it was fired from a bone bow and can apply your effects appropriately.
     
  8. Offline

    _Belknap_

    Adamki11s
    Can you explain to me what a hashset is? I've never tried anything with hashsets or hashmaps before
     
  9. Offline

    Codex Arcanum

    _Belknap_
    Passable seeming tutorial found in about a minute of googling here.
     
  10. Offline

    Rixterz

    You've closed the if{} condition before you drop the items. So regardless of the result of the if statement (whether the name is correct or not), the code will continue and drop the items. To fix: move the "}" from line 11 to line 24.
    I saw it straight away! Hope it helps!
    -Rixterz
     
Thread Status:
Not open for further replies.

Share This Page