Getting Players

Discussion in 'Plugin Development' started by JarFile, Feb 12, 2015.

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

    JarFile

    So I have a PlayerDropItemEvent and I want to know if I can get the player that the player that dropped the item is looking at, or at least the entity. Is there any way I could do that? If so could you tell me and thanks!
    Code:
    @EventHandler
    public void ItemDrop(PlayerDropItemEvent e)
    {
        Player p = e.getPlayer();
        Player p2 = [? what do I put here for getting the player the player is looking at.]
    }
    
     
  2. Offline

    Skionz

    @JarFile Iterate through their eye of sight and check if any players are in it.
     
  3. Offline

    Justinian6

    get the location, pitch, yaw, etc. and calculate if anyone is within 3 blocks of them....

    - get neaarby players
    - see what direction players looking in
    - calculate direction (+x, -z, -y), etc.
     
  4. Offline

    JarFile

    @Justinian6 @Skionz Can you show me the code to do this? I have no idea on how to do it... I'm not a pro at java.
     
  5. Offline

    ReadySetPawn

    Don't Copy and Paste! Read this and then rewrite it in your own plugin. Copying and Pasting only hurts yourself as a developer.

    1. Create a for loop that iterates X amount of times. The variable X will be your range.

    Code:
    for (int x = 0; x < 3; x++){
    
    }
    2. Get the direction the player is facing.

    Code:
    Vector direction = p.getLocation().getDirection();
    
    for (int x = 0; x < 3; x++){
    
    }
    
    3. Multiply the direction by X each iteration and retrieve it's location.

    Code:
    Vector direction = p.getEyeLocation().getDirection();
    
    for (int x = 0; x < 3; x++){
    
    Location loc = direction.multply(x).toLocation(p.getWorld());
    
    }
    
    4. Check to see if anybody is at that location. You will need another for loop for this.

    Code:
    Vector direction = p.getLocation().getDirection();
    
    
    for (int x = 0; x < 3; x++){
    
    Location loc = direction.multply(x).toLocation(p.getWorld());
    
    for (Player player : p.getWorld().getPlayers()){
    Location l = player.getEyeLocation();
    if (l.getBlockX() == loc.getBlockX && l.getBlockY() == loc.getBlockY && l.getBlockZ() == loc.getBlockZ)
    return player;
    }
    
    }
    
    Also, this will have to be in a separate method that returns type Player.
     
    Last edited: Feb 15, 2015
    Justinian6 likes this.
  6. Offline

    Justinian6

    @ReadySetPawn Why use a for loop when it is unnecessary. Use a timer instead

    EDIT: NVM I see what you did... Just make sure the player isn't null
     
    ReadySetPawn likes this.
Thread Status:
Not open for further replies.

Share This Page