How to get the Player who shot an arrow?

Discussion in 'Plugin Development' started by TheJesterRace, Jan 27, 2016.

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

    TheJesterRace

    Hey,
    I'm trying to get the item the player holds when he fires an arrow (as they're able to fire arrows with multiple items in my plugin), therefore I need the player first. So this is basically what I'm doing:

    Code:
    @EventHandler
    public void onProjectileLaunch(ProjectileLaunchEvent e)
    {
        Plugin.getLogger().info("ProjectileLaunch event fired");
    
        ProjectileSource source = e.getEntity().getShooter();
           
        if (source instanceof Player)
        {
                Plugin.getLogger().info("Shooter is a player");
        }
    }
    The Problem is that when I'm firing an arrow, my method does not print "Shooter is a player" to my console althoug it tells me that the EventHandler is called. :(
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    TheJesterRace

    No!
    Omg i know - it's because I'm not telling the player to fire an arrow but the world to spawn one on the players location.. argh i totally forgot about that - could have tried that on my own earlier :rolleyes:
    Thanks for your fast answer!
     
    timtower likes this.
  4. Offline

    BobTheHamster9

  5. Offline

    TheJesterRace

    No, not really :/
    I tried a little around and came out with this:

    Code:
    // Spawn the arrow
    Arrow arrow = player.getWorld().spawnArrow(location, direction, 2, 0);
    
    // And set the shooter manually
    arrow.setShooter(player);
    The Problem is that I need the shooter in my ProjectileLaunchEvent which fires immediately after the first line (before the shooter is set). Is there any way to set the shooter before I spawn this arrow?
     
    Last edited: Jan 30, 2016
Thread Status:
Not open for further replies.

Share This Page