Cuberite Forum
How do you get a player from their name. - Printable Version

+- Cuberite Forum (https://forum.cuberite.org)
+-- Forum: Plugins (https://forum.cuberite.org/forum-1.html)
+--- Forum: Plugin Discussion (https://forum.cuberite.org/forum-8.html)
+--- Thread: How do you get a player from their name. (/thread-1210.html)



How do you get a player from their name. - bearbin - 08-07-2013

The question is as the title says.


RE: How do you get a player from their name. - NiLSPACE - 08-07-2013

local Callback = function(Player)
Player:SendMessage("Info")
end
cRoot:Get():FindAndDoWithPlayer("PlayerName", Callback)
I think that should do it.


RE: How do you get a player from their name. - xoft - 08-07-2013

As usual, due to multithreading, you cannot get the cPlayer object directly, you can only have a callback be called on it, exactly as STR wrote.
There's even a hackier way to write this in Lua:
cRoot:Get():DoWithPlayer("PlayerName", 
  function(a_Player)
    a_Player:SendMessage("Info");
  end
);

Note that there are two similar functions, DoWithPlayer() and FindAndDoWithPlayer(). The first one requires an exact username match, the second one uses the closest username (you might want to check the username in the callback). I'm not sure now what exactly matches, but I believe substrings do, maybe even one-letter typos.

Oh, wait, the DoWithPlayer() seems to be implemented only on cWorld, not on cRoot. But you should be calling them on a cWorld anyway, since you probably know, which world the player is at.


RE: How do you get a player from their name. - bearbin - 08-27-2013

Reading through the API section of the wiki, it says there's cWorld:GetPlayer() which takes a player name and gives you a cplayer. Would this be an alternative to ugly callbacks?


RE: How do you get a player from their name. - FakeTruth - 08-27-2013

You cannot do that, because it would not be threadsafe


RE: How do you get a player from their name. - NiLSPACE - 08-27-2013

The server uses callbacks because if you would use cWorld::GetPlayer() the player object might change while you are changing stuff.


RE: How do you get a player from their name. - xoft - 08-27-2013

The cWorld section of the wiki is probably the most outdated thing on that wiki. It desperately needs improving, but it's so huge that no-one has been daring enough to actually do it.