Set arrow velocity and damage.

Discussion in 'Plugin Development' started by GeorgeeeHD, Dec 29, 2013.

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

    GeorgeeeHD

    Ok, so I'm using p.launchProjectile(Arrow.class); inside of a PlayerInteractEvent. When they right click an item, it launches an arrow, however, it is too slow and only does 0.5 hearts to a player.

    How can I make the arrows shoot faster and do more damage to players it hits? Thanks!
     
  2. Offline

    NathanWolf

    launchProjectile will return the Arrow entity, which you can then modify.
     
  3. Offline

    GeorgeeeHD

    but how :O
     
  4. Offline

    NathanWolf

    Hrm... ok, so that link wasn't helpful and it seems like I was wrong about Arrow having useful methods. It seems you have to use NMS to set arrow damage (power):

    Here's what I do:

    Code:java
    1. Arrow arrow = null;
    2. Location location = player.getLocation();
    3. location.setX(location.getX() + direction.getX() * (1 + Math.random() * arrowCount));
    4. location.setY(location.getY() + 1.5f);
    5. location.setZ(location.getZ() + direction.getZ() * (1 + Math.random() * arrowCount));
    6.  
    7. arrow = player.getWorld().spawnArrow(location, direction, speed, spread);
    8.  
    9. if (arrow == null)
    10. {
    11. sendMessage("One of your arrows fizzled");
    12. return SpellResult.FAILURE;
    13. }
    14.  
    15. arrow.setShooter(player);
    16.  
    17. if (useFire) {
    18. arrow.setFireTicks(300);
    19. }
    20.  
    21. // Hackily make this an infinite arrow and set damage
    22. try {
    23. Method getHandleMethod = arrow.getClass().getMethod("getHandle");
    24. Object handle = getHandleMethod.invoke(arrow);
    25. Field fromPlayerField = handle.getClass().getField("fromPlayer");
    26. fromPlayerField.setInt(handle, 2);
    27. Method setDamageMethod = handle.getClass().getMethod("b", Double.TYPE);
    28. setDamageMethod.invoke(handle, damage);
    29. } catch (Throwable ex) {
    30. ex.printStackTrace();
    31. }
    32. arrow.setTicksLived(300);
     
Thread Status:
Not open for further replies.

Share This Page