From 008addf5d746b2d158f9116e4f6302861b42852f Mon Sep 17 00:00:00 2001 From: faketruth Date: Thu, 16 Feb 2012 17:20:28 +0000 Subject: Got rid of dangerous GetEntity(), not using DoWithEntity() git-svn-id: http://mc-server.googlecode.com/svn/trunk@278 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/Bindings.cpp | 37 +------------------------------------ source/Bindings.h | 2 +- source/cClientHandle.cpp | 26 ++++++++++++++++++++------ source/cWorld.cpp | 9 ++++----- source/cWorld.h | 8 +++----- 5 files changed, 29 insertions(+), 53 deletions(-) (limited to 'source') diff --git a/source/Bindings.cpp b/source/Bindings.cpp index e9d23fa26..a8d99330f 100644 --- a/source/Bindings.cpp +++ b/source/Bindings.cpp @@ -1,6 +1,6 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 02/15/12 23:43:42. +** Generated automatically by tolua++-1.0.92 on 02/16/12 18:16:16. */ #ifndef __cplusplus @@ -9587,40 +9587,6 @@ static int tolua_AllToLua_cWorld_GetPlayer00(lua_State* tolua_S) } #endif //#ifndef TOLUA_DISABLE -/* method: GetEntity of class cWorld */ -#ifndef TOLUA_DISABLE_tolua_AllToLua_cWorld_GetEntity00 -static int tolua_AllToLua_cWorld_GetEntity00(lua_State* tolua_S) -{ -#ifndef TOLUA_RELEASE - tolua_Error tolua_err; - if ( - !tolua_isusertype(tolua_S,1,"cWorld",0,&tolua_err) || - !tolua_isnumber(tolua_S,2,0,&tolua_err) || - !tolua_isnoobj(tolua_S,3,&tolua_err) - ) - goto tolua_lerror; - else -#endif - { - cWorld* self = (cWorld*) tolua_tousertype(tolua_S,1,0); - int a_UniqueID = ((int) tolua_tonumber(tolua_S,2,0)); -#ifndef TOLUA_RELEASE - if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetEntity'", NULL); -#endif - { - cEntity* tolua_ret = (cEntity*) self->GetEntity(a_UniqueID); - tolua_pushusertype(tolua_S,(void*)tolua_ret,"cEntity"); - } - } - return 1; -#ifndef TOLUA_RELEASE - tolua_lerror: - tolua_error(tolua_S,"#ferror in function 'GetEntity'.",&tolua_err); - return 0; -#endif -} -#endif //#ifndef TOLUA_DISABLE - /* method: SetBlock of class cWorld */ #ifndef TOLUA_DISABLE_tolua_AllToLua_cWorld_SetBlock00 static int tolua_AllToLua_cWorld_SetBlock00(lua_State* tolua_S) @@ -17594,7 +17560,6 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S) tolua_function(tolua_S,"GetHeight",tolua_AllToLua_cWorld_GetHeight00); tolua_function(tolua_S,"GetNumPlayers",tolua_AllToLua_cWorld_GetNumPlayers00); tolua_function(tolua_S,"GetPlayer",tolua_AllToLua_cWorld_GetPlayer00); - tolua_function(tolua_S,"GetEntity",tolua_AllToLua_cWorld_GetEntity00); tolua_function(tolua_S,"SetBlock",tolua_AllToLua_cWorld_SetBlock00); tolua_function(tolua_S,"FastSetBlock",tolua_AllToLua_cWorld_FastSetBlock00); tolua_function(tolua_S,"GetBlock",tolua_AllToLua_cWorld_GetBlock00); diff --git a/source/Bindings.h b/source/Bindings.h index 23e74ba4d..860db990f 100644 --- a/source/Bindings.h +++ b/source/Bindings.h @@ -1,6 +1,6 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 02/15/12 23:43:42. +** Generated automatically by tolua++-1.0.92 on 02/16/12 18:16:17. */ /* Exported function */ diff --git a/source/cClientHandle.cpp b/source/cClientHandle.cpp index a28d8a56c..c8f8e82ed 100644 --- a/source/cClientHandle.cpp +++ b/source/cClientHandle.cpp @@ -1532,13 +1532,27 @@ void cClientHandle::HandleUseEntity(cPacket_UseEntity * a_Packet) { return; } - cWorld * World = m_Player->GetWorld(); - cEntity * Entity = World->GetEntity(a_Packet->m_TargetID); - if ((Entity != NULL) && Entity->IsA("cPawn")) + + class cDamageEntity : public cEntityCallback { - cPawn * Pawn = (cPawn *)Entity; - Pawn->TakeDamage(1, m_Player); - } + virtual bool Item(cEntity * a_Entity) override + { + if( a_Entity->IsA("cPawn") ) + { + reinterpret_cast< cPawn* >( a_Entity )->TakeDamage(Damage, Instigator ); + } + return true; + } + public: + int Damage; + cEntity * Instigator; + } Callback; + + Callback.Damage = 1; // TODO: Find proper damage from current item equipped + Callback.Instigator = m_Player; + + cWorld * World = m_Player->GetWorld(); + World->DoWithEntity( a_Packet->m_TargetID, Callback ); } diff --git a/source/cWorld.cpp b/source/cWorld.cpp index 8e95b5bd5..fc6941250 100644 --- a/source/cWorld.cpp +++ b/source/cWorld.cpp @@ -1168,18 +1168,17 @@ void cWorld::SendPlayerList(cPlayer * a_DestPlayer) -// TODO: This interface is dangerous! -cEntity * cWorld::GetEntity( int a_UniqueID ) +bool cWorld::DoWithEntity( int a_UniqueID, cEntityCallback & a_Callback ) { cCSLock Lock(m_CSEntities); for (cEntityList::iterator itr = m_AllEntities.begin(); itr != m_AllEntities.end(); ++itr ) { if( (*itr)->GetUniqueID() == a_UniqueID ) { - return *itr; + return a_Callback.Item(*itr); } - } - return NULL; + } // for itr - m_AllEntities[] + return false; } diff --git a/source/cWorld.h b/source/cWorld.h index c47fa70e8..025d0782a 100644 --- a/source/cWorld.h +++ b/source/cWorld.h @@ -34,6 +34,7 @@ class cWorldGenerator; // The generator that actually generates the chunks for class cChunkGenerator; // The thread responsible for generating chunks typedef std::list< cPlayer * > cPlayerList; typedef cItemCallback cPlayerListCallback; +typedef cItemCallback cEntityCallback; @@ -102,12 +103,9 @@ public: void AddEntity( cEntity* a_Entity ); void RemoveEntityFromChunk( cEntity * a_Entity); - - // TODO: This interface is dangerous! - cEntityList & GetEntities(void) {return m_AllEntities; } - // TODO: This interface is dangerous! - cEntity * GetEntity( int a_UniqueID ); //tolua_export + // TODO: Export to Lua + bool DoWithEntity( int a_UniqueID, cEntityCallback & a_Callback ); void SetBlock( int a_X, int a_Y, int a_Z, char a_BlockType, char a_BlockMeta ); //tolua_export void FastSetBlock( int a_X, int a_Y, int a_Z, char a_BlockType, char a_BlockMeta ); //tolua_export -- cgit v1.2.3