Stop arrows from bouncing

Discussion in 'Spigot Plugin Development' started by NoobDoesMC, Apr 9, 2018.

  1. I shoot 5 successive arrows quickly from a player:

    Code (Java):
    BukkitTask barrageTask = new BukkitRunnable() {
      int arrowCounter = 0;

      @Override
      public void run() {
        if (arrowCounter == 5) {
          this.cancel();
          return;
        }
        Vector arrowVelocity = player.getLocation().getDirection().multiply(2.0);
        EntityArrow arrow = ((CraftArrow) player.launchProjectile(Arrow.class, arrowVelocity)).getHandle();
        arrow.fromPlayer = 0;
        arrowCounter++;
      }
    }.runTaskTimer(plugin, 0L, 1L);
    Only the first and maybe last/second-last arrows hit the entity target.

    The rest of the arrows bounce off, and often damage the shooter.

    How can I stop arrows from bouncing, or at least stop bounced arrows from damaging the shooter.
     
  2. It doesn't, probably same issue as https://www.spigotmc.org/threads/arrows-always-bounce.78530/

    I've also been trying
    Code (Java):
    Vector arrowVelocity = playerDirection.multiply(1.5).add(boostY);
    Arrow arrow = (Arrow) this.player.launchProjectile(Arrow.class, arrowVelocity);
    arrow.setBounce(false);
    EntityArrow entityArrow = ((CraftArrow) arrow).getHandle();
    entityArrow.fromPlayer = 0;
    but they still bounce.

    Anyone got a solution? Stop arrows from bouncing off entities and hitting other entities after they bounce?
     
  3. BananaPuncher714

    Supporter

    Set the entity's invulnerable time to 0 ticks after getting hit by an arrow. This will allow it to get damaged immediately and your arrows shouldn't bounce off.
     
    • Agree Agree x 1
  4. I've already experimented with this. I don't like this behaviour. Melee can also be used during the time they have a 0-tick hit cool down, and the barrage does way too much dmg if every arrow hits.

    I like the current damage interval, just want the arrows to not bounce.
     
  5. You could pair this with an Entity damage event and cancel damage that wouldn't happen with the normal invulnerable ticks.
     
  6. What's the normal invulnerable ticks?

    Is there a time since last damage tracker on entities?
     
  7. Get it with player.getNoDamageTicks() to find out, I don't remember.
    No, as far as I know. You could implement it yourself though.
     
  8. 20 ticks, you can change it though

    no, you have to implement it yourself
     
  9. Not sure about this but try .setShooter(Player) to stop it from damaging you, I think I read somewhere that this prevents damage to the shooter.
     
  10. Pretty sure the shooter is already the player, .getShooter().getName() returns that player's name