Get Player that use a bow

How can i get the Player that use a bow?

Are you trying to get the player from when a player shoots a bow? or just when they have a bow in their hand? Or From a damage event from another player being hit?

I want to teleport him to the arrow that he shoots

Ah ok so you want to listen to LaunchProjectileEvent which is when a player releases an arrow and get the player from the cause and then teleport him to the location of the arrow. Like so

@Listener
public void onArrowShot(LaunchProjectileEvent e){
    Player player = (Player).getCause().root();
    player.setLocation(e.getTargetEntity().getLocation());
}

more specifically, use event filtering on that code for the root being a player … otherwise, you’re going to have a dispenser, ghast, or snowman fire a projectile and throw an error because that code isn’t checking before casting… Event filtering for root+player will only trigger for player=based projectiles – then check for it being an arrow projectile, or else where he throws a snowball or egg will also work.

Be REALLY careful here, as @TheBoomer mentioned, you should be using event filtering, because there are many cases that projectiles are being launched but not from players, but from other entities.

The recommended way to listen to this:

@Listener
public void onArrowShotByPlayer(LaunchProjectileEvent event, @Root Player player) {
  player.setLocation(event.getTargetEntity().getLocation());
}

Though I should note that this only sets the player location to the location of the arrow at the time the arrow is launched, not where the arrow landed.

Ahh okay, didn’t even know about event filtering. Thanks for letting me know.

Also, i was playing around with this event and i couldn’t get it to fire, the only way i was getting it to work is by using UseItemStackEvent, which wouldn’t allow me to teleport to landed location, but anyways, LaunchProjectileEvent wasn’t firing which I thought to be really strange.

To teleport to the landed location you will want CollideBlockEvent.Impact involving an arrow with projectile source being a player.

Nothing worked ;(

how can i set the player to the location where the arrow landed?

Likely when the CollideBlockEvent.Impact is properly implemented.

I tested this but it happens nothing so i think i made a mistake(i think it didnt get the player):

@Listener
public void onArrowShotByPlayer(LaunchProjectileEvent event, @Root Player player) {
player.sendMessage(Text.of(“hi”));

}