Determining if a LivingEntity is a Player

Discussion in 'Plugin Development' started by Derek Peterson, Jan 26, 2011.

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

    Derek Peterson

    Hi all!

    Is there currently a way to tell if a LivingEntity is a Player, and if so, obtain the Player object?

    I'm trying to edit the EntityDamage events so Players only get hurt under certain situations, but I want all other entities to still be able to be hurt.

    If anybody can shed any light on how to tell if the entities involved with events are Players, I'd really appreciate it!

    Thanks
    --- merged: Jan 27, 2011 6:02 AM ---
    I solved my problem. I'm not sure if it's the best solution, though. I also haven't tested it, so I'm not 100% sure it works. But in case other people are having this problem:

    Code:
    public boolean isEntityPlayer(Entity entity){
        	int id = entity.getEntityId();
        	Player[] players = getServer().getOnlinePlayers();
            for(int x = 0; x < players.length; x++){
            	Player tempPlayer = players[x];
            	if(tempPlayer.getEntityId() == id){
            		return true;
        		}
            }
            return false;
        }
        public Player getPlayerFromEntity(Entity entity){
        	int id = entity.getEntityId();
        	Player[] players = getServer().getOnlinePlayers();
            for(int x = 0; x < players.length; x++){
            	Player tempPlayer = players[x];
            	if(tempPlayer.getEntityId() == id){
            		return tempPlayer;
            	}
            }
            return null;
        }
     
  2. Offline

    void420

    Try this:
    Code:
    if (entity instanceof Player) {
      Player player = (Player)entity;
    }
     
  3. Offline

    8e8

    What void said is what you want. Your current method is very inefficient, but creative lol.
     
  4. Offline

    Derek Peterson

    Aha! That's perfect, thanks!
     
Thread Status:
Not open for further replies.

Share This Page