This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Bubba's profile picture

(Question) Get EntityPlayer from string

Started by Bubba, 22 November 2012 - 08:45 AM
Bubba #1
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.
Orwell #2
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");
Bubba #3
Posted 23 November 2012 - 11:38 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");

This is perfect! I can't believe I didnt see this when I was going through the world class ;)/>/>
thislooksfun #4
Posted 30 April 2013 - 11: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!