Jump to content




(Question) Get EntityPlayer from string


  • You cannot reply to this topic
3 replies to this topic

#1 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 22 November 2012 - 09:45 AM

Hello,

I am trying to get an EntityPlayer handle from their username. For example:
public EntityPlayer getPlayerHandle(String name) //The user will input the name into this field
{
//This is the part I am having trouble with. Ideally I would be able to just do the following:
EntityPlayer player = EntityPlayer.getPlayer(name);
return player
}

The way I had been getting the info about the player before was by getting the entityLiving from onBlockPlacedBy() like this:
EntityLiving entityLiving = null;
public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving) {
			entityLiving = par5EntityLiving;
}

Unfortunately for my purposes I do not want the player to have to place the peripheral block to receive the Entity handle. Is there any way to accomplish what I want to do?

Any help would be greatly appreciated.

#2 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 23 November 2012 - 11:31 AM

Do you have access to the world object from where you want to use it? Then you could do:
PlayerEntity player = world.getPlayerEntityByName("username");

Edit: If you're doing this in a TileEntity Class you can do this:
PlayerEntity player = this.worldObj.getPlayerEntityByName("username");


#3 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 23 November 2012 - 11:38 AM

View PostOrwell, on 23 November 2012 - 11:31 AM, said:

Do you have access to the world object from where you want to use it? Then you could do:
PlayerEntity player = world.getPlayerEntityByName("username");

Edit: If you're doing this in a TileEntity Class you can do this:
PlayerEntity player = this.worldObj.getPlayerEntityByName("username");

This is perfect! I can't believe I didnt see this when I was going through the world class ;)/>

#4 thislooksfun

  • Members
  • 25 posts
  • LocationBehind you

Posted 30 April 2013 - 10:50 PM

I stumbled across this topic in search of a way to do this server-side. For anyone else struggling with this, use:
EntityPlayerMP player = MinecraftServer.getServer().getConfigurationManager().getPlayerForUsername("username");
Hope this helps someone!





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users