1
0

Got rid of cWorld::GetAllPlayers() and implemented ForEachPlayer() more or less in Lua

Core now uses ForEachPlayer() to interact with connected players

git-svn-id: http://mc-server.googlecode.com/svn/trunk@260 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
faketruth 2012-02-14 19:14:23 +00:00
parent 46df5ee331
commit e7ea352f41
6 changed files with 127 additions and 87 deletions

View File

@ -1,3 +1,5 @@
local BlockData = {}
function OnBlockPlace( Block, Player ) function OnBlockPlace( Block, Player )
-- dont check if the direction is in the air -- dont check if the direction is in the air
@ -11,43 +13,8 @@ function OnBlockPlace( Block, Player )
return true return true
end end
local collision = false BlockData = Block
local World = Player:GetWorld() if( Player:GetWorld():ForEachPlayer( CheckCollision ) == false ) then
local PlayerList = World:GetAllPlayers()
-- check if a player occupies the placement location
for i, Player in ipairs( PlayerList ) do
-- drop the decimals, we only care about the full block X,Y,Z
local PlayerX = math.floor(Player:GetPosX(), 0)
local PlayerY = math.floor(Player:GetPosY(), 0)
local PlayerZ = math.floor(Player:GetPosZ(), 0)
local BlockX = Block.m_PosX
local BlockY = Block.m_PosY
local BlockZ = Block.m_PosZ
-- player height is 2 blocks, so we check the position and then offset it up one
-- so they can't place a block on there face
if Block.m_Direction == 0 then if PlayerY == BlockY-2 and PlayerX == BlockX and PlayerZ == BlockZ then collision = true end end
if Block.m_Direction == 1 then if PlayerY == BlockY+1 and PlayerX == BlockX and PlayerZ == BlockZ then collision = true end end
if Block.m_Direction == 2 then if PlayerY == BlockY and PlayerX == BlockX and PlayerZ == BlockZ-1 then collision = true end end
if Block.m_Direction == 2 then if PlayerY+1 == BlockY and PlayerX == BlockX and PlayerZ == BlockZ-1 then collision = true end end
if Block.m_Direction == 3 then if PlayerY == BlockY and PlayerX == BlockX and PlayerZ == BlockZ+1 then collision = true end end
if Block.m_Direction == 3 then if PlayerY+1 == BlockY and PlayerX == BlockX and PlayerZ == BlockZ+1 then collision = true end end
if Block.m_Direction == 4 then if PlayerY == BlockY and PlayerX == BlockX-1 and PlayerZ == BlockZ then collision = true end end
if Block.m_Direction == 4 then if PlayerY+1 == BlockY and PlayerX == BlockX-1 and PlayerZ == BlockZ then collision = true end end
if Block.m_Direction == 5 then if PlayerY == BlockY and PlayerX == BlockX+1 and PlayerZ == BlockZ then collision = true end end
if Block.m_Direction == 5 then if PlayerY+1 == BlockY and PlayerX == BlockX+1 and PlayerZ == BlockZ then collision = true end end
end
if collision then
return true return true
else else
return false return false
@ -58,3 +25,35 @@ function OnBlockPlace( Block, Player )
return false return false
end end
function CheckCollision( Player )
-- drop the decimals, we only care about the full block X,Y,Z
local PlayerX = math.floor(Player:GetPosX(), 0)
local PlayerY = math.floor(Player:GetPosY(), 0)
local PlayerZ = math.floor(Player:GetPosZ(), 0)
local BlockX = BlockData.m_PosX
local BlockY = BlockData.m_PosY
local BlockZ = BlockData.m_PosZ
-- player height is 2 blocks, so we check the position and then offset it up one
-- so they can't place a block on there face
local collision = false
if BlockData.m_Direction == 0 then if PlayerY == BlockY-2 and PlayerX == BlockX and PlayerZ == BlockZ then collision = true end end
if BlockData.m_Direction == 1 then if PlayerY == BlockY+1 and PlayerX == BlockX and PlayerZ == BlockZ then collision = true end end
if BlockData.m_Direction == 2 then if PlayerY == BlockY and PlayerX == BlockX and PlayerZ == BlockZ-1 then collision = true end end
if BlockData.m_Direction == 2 then if PlayerY+1 == BlockY and PlayerX == BlockX and PlayerZ == BlockZ-1 then collision = true end end
if BlockData.m_Direction == 3 then if PlayerY == BlockY and PlayerX == BlockX and PlayerZ == BlockZ+1 then collision = true end end
if BlockData.m_Direction == 3 then if PlayerY+1 == BlockY and PlayerX == BlockX and PlayerZ == BlockZ+1 then collision = true end end
if BlockData.m_Direction == 4 then if PlayerY == BlockY and PlayerX == BlockX-1 and PlayerZ == BlockZ then collision = true end end
if BlockData.m_Direction == 4 then if PlayerY+1 == BlockY and PlayerX == BlockX-1 and PlayerZ == BlockZ then collision = true end end
if BlockData.m_Direction == 5 then if PlayerY == BlockY and PlayerX == BlockX+1 and PlayerZ == BlockZ then collision = true end end
if BlockData.m_Direction == 5 then if PlayerY+1 == BlockY and PlayerX == BlockX+1 and PlayerZ == BlockZ then collision = true end end
return collision
end

View File

@ -1,16 +1,16 @@
local PlayerTable = {}
function HandlePlayerListCommand( Split, Player ) function HandlePlayerListCommand( Split, Player )
local World = Player:GetWorld() PlayerTable = {}
local PlayerList = World:GetAllPlayers() Player:GetWorld():ForEachPlayer( AppendToTable )
local Message = cChatColor.Green .. "Connected players: (".. cChatColor.White.. #PlayerList .. cChatColor.Green .. ")" local Message = cChatColor.Green .. "Connected players: (".. cChatColor.White.. #PlayerTable .. cChatColor.Green .. ")"
Player:SendMessage( Message ) Player:SendMessage( Message )
local PlayerTable = {}
for i, TempPlayer in ipairs( PlayerList ) do
local PlayerName = TempPlayer:GetName()
table.insert(PlayerTable, PlayerName )
end
Player:SendMessage( table.concat(PlayerTable, " ") ) Player:SendMessage( table.concat(PlayerTable, " ") )
return true return true
end end
function AppendToTable( Player )
table.insert(PlayerTable, Player:GetName() )
end

View File

@ -1,3 +1,6 @@
local PlayerHTML = ""
local PlayerNum = 0
function HandleRequest_PlayerList( Request ) function HandleRequest_PlayerList( Request )
local World = cRoot:Get():GetWorld() local World = cRoot:Get():GetWorld()
local Content = "" local Content = ""
@ -16,16 +19,12 @@ function HandleRequest_PlayerList( Request )
Content = Content .. "<p>Connected Players: <b>" .. World:GetNumPlayers() .. "</b></p>" Content = Content .. "<p>Connected Players: <b>" .. World:GetNumPlayers() .. "</b></p>"
Content = Content .. "<table>" Content = Content .. "<table>"
PlayerNum = 0
PlayerHTML = ""
World:ForEachPlayer( CreatePlayerList )
local PlayerList = World:GetAllPlayers() if( PlayerHTML ~= "" ) then
if( #PlayerList > 0 ) then Content = Content .. PlayerHTML
for i, Player in ipairs( PlayerList ) do
Content = Content .. "<tr>"
Content = Content .. "<td style='width: 10px;'>" .. i .. ".</td>"
Content = Content .. "<td>" .. Player:GetName() .. "</td>"
Content = Content .. "<td><a href='?playerlist-kick=" .. Player:GetName() .. "'>Kick</a></td>"
Content = Content .. "</tr>"
end
else else
Content = Content .. "<tr><td>None</td></tr>" Content = Content .. "<tr><td>None</td></tr>"
end end
@ -33,3 +32,12 @@ function HandleRequest_PlayerList( Request )
Content = Content .. "<br>" Content = Content .. "<br>"
return Content return Content
end end
function CreatePlayerList( Player, Data )
PlayerNum = PlayerNum + 1
PlayerHTML = PlayerHTML .. "<tr>"
PlayerHTML = PlayerHTML .. "<td style='width: 10px;'>" .. PlayerNum .. ".</td>"
PlayerHTML = PlayerHTML .. "<td>" .. Player:GetName() .. "</td>"
PlayerHTML = PlayerHTML .. "<td><a href='?playerlist-kick=" .. Player:GetName() .. "'>Kick</a></td>"
PlayerHTML = PlayerHTML .. "</tr>"
end

View File

@ -16,7 +16,17 @@
#include "md5/md5.h" #include "md5/md5.h"
static bool report_errors(lua_State* lua, int status)
{
if ( status!=0 )
{
std::string s = lua_tostring(lua, -1);
LOGERROR("-- %s", s.c_str() );
lua_pop(lua, 1);
return true;
}
return false;
}
/**************************** /****************************
@ -91,13 +101,59 @@ static int tolua_LOGERROR(lua_State* tolua_S)
static int tolua_cWorld_GetAllPlayers(lua_State* tolua_S) static int tolua_cWorld_ForEachPlayer(lua_State* tolua_S)
{ {
cWorld* self = (cWorld*) tolua_tousertype(tolua_S,1,0); cWorld* self = (cWorld*) tolua_tousertype(tolua_S,1,0);
lua_State* L = tolua_S;
self->GetAllPlayers(L); if( !lua_isfunction( tolua_S, 2 ) )
{
LOGWARN("Error in function call 'ForEachPlayer': Expected a function for parameter #1");
return 0;
}
int Reference = luaL_ref(tolua_S, LUA_REGISTRYINDEX);
if( Reference == LUA_REFNIL )
{
LOGWARN("Error in function call 'ForEachPlayer': Could not get function reference");
return 0;
}
class cLuaPlayerCallback : public cPlayerListCallback
{
virtual bool Item(cPlayer * a_Player) override
{
lua_rawgeti( LuaState, LUA_REGISTRYINDEX, Reference);
tolua_pushusertype( LuaState, a_Player, "cPlayer" );
int s = lua_pcall( LuaState, 1, 1, 0);
if( report_errors( LuaState, s ) )
{
return false;
}
if( lua_isboolean( LuaState, -1 ) )
{
return (tolua_toboolean( LuaState, -1, 0) > 0);
}
LOGINFO("Stack size: %i", lua_gettop(LuaState) );
return false;
}
public:
lua_State* LuaState;
int Reference;
} Callback;
Callback.LuaState = tolua_S;
Callback.Reference = Reference;
bool bRetVal = self->ForEachPlayer( &Callback );
luaL_unref( tolua_S, LUA_REGISTRYINDEX, Reference );
tolua_pushboolean( tolua_S, bRetVal );
return 1; return 1;
} }
@ -397,7 +453,7 @@ void ManualBindings::Bind( lua_State* tolua_S )
tolua_function(tolua_S,"Log",tolua_LOG); // Deprecated tolua_function(tolua_S,"Log",tolua_LOG); // Deprecated
tolua_beginmodule(tolua_S,"cWorld"); tolua_beginmodule(tolua_S,"cWorld");
tolua_function(tolua_S,"GetAllPlayers",tolua_cWorld_GetAllPlayers); tolua_function(tolua_S,"ForEachPlayer",tolua_cWorld_ForEachPlayer);
tolua_endmodule(tolua_S); tolua_endmodule(tolua_S);
tolua_beginmodule(tolua_S,"cPlugin"); tolua_beginmodule(tolua_S,"cPlugin");
tolua_function(tolua_S,"GetCommands",tolua_cPlugin_GetCommands); tolua_function(tolua_S,"GetCommands",tolua_cPlugin_GetCommands);

View File

@ -989,26 +989,6 @@ bool cWorld::ForEachPlayer(cPlayerListCallback * a_Callback)
void cWorld::GetAllPlayers( lua_State* L )
{
lua_createtable(L, m_Players.size(), 0);
int newTable = lua_gettop(L);
int index = 1;
cPlayerList::const_iterator iter = m_Players.begin();
while(iter != m_Players.end())
{
tolua_pushusertype( L, (*iter), "cPlayer" );
lua_rawseti(L, newTable, index);
++iter;
++index;
}
}
// TODO: This interface is dangerous! // TODO: This interface is dangerous!
cPlayer* cWorld::GetPlayer( const char* a_PlayerName ) cPlayer* cWorld::GetPlayer( const char* a_PlayerName )
{ {

View File

@ -76,13 +76,10 @@ public:
void AddPlayer( cPlayer* a_Player ); void AddPlayer( cPlayer* a_Player );
void RemovePlayer( cPlayer* a_Player ); void RemovePlayer( cPlayer* a_Player );
bool ForEachPlayer(cPlayerListCallback * a_Callback); // Calls the callback for each player in the list
// TODO: This interface is dangerous!
cPlayerList & GetAllPlayers() {return m_Players; }
typedef struct lua_State lua_State; typedef struct lua_State lua_State;
void GetAllPlayers( lua_State* L ); // >> EXPORTED IN MANUALBINDINGS << bool ForEachPlayer(cPlayerListCallback * a_Callback); // Calls the callback for each player in the list
// >> EXPORTED IN MANUALBINDINGS <<
unsigned int GetNumPlayers(); //tolua_export unsigned int GetNumPlayers(); //tolua_export
// TODO: This interface is dangerous // TODO: This interface is dangerous