diff options
author | faketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2012-01-27 00:10:49 +0100 |
---|---|---|
committer | faketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2012-01-27 00:10:49 +0100 |
commit | 4e8128b9d738d1744cc883deaa9c294f43ba8242 (patch) | |
tree | b5badd0ccc771d95e066026876c06c58d673ee25 /source | |
parent | More new plugin awesomeness (diff) | |
download | cuberite-4e8128b9d738d1744cc883deaa9c294f43ba8242.tar cuberite-4e8128b9d738d1744cc883deaa9c294f43ba8242.tar.gz cuberite-4e8128b9d738d1744cc883deaa9c294f43ba8242.tar.bz2 cuberite-4e8128b9d738d1744cc883deaa9c294f43ba8242.tar.lz cuberite-4e8128b9d738d1744cc883deaa9c294f43ba8242.tar.xz cuberite-4e8128b9d738d1744cc883deaa9c294f43ba8242.tar.zst cuberite-4e8128b9d738d1744cc883deaa9c294f43ba8242.zip |
Diffstat (limited to 'source')
-rw-r--r-- | source/cPlugin_NewLua.cpp | 102 | ||||
-rw-r--r-- | source/cPlugin_NewLua.h | 17 |
2 files changed, 98 insertions, 21 deletions
diff --git a/source/cPlugin_NewLua.cpp b/source/cPlugin_NewLua.cpp index f81f2e95e..81e629ce4 100644 --- a/source/cPlugin_NewLua.cpp +++ b/source/cPlugin_NewLua.cpp @@ -80,11 +80,8 @@ bool cPlugin_NewLua::Initialize() // Call intialize function
- lua_getglobal(m_LuaState, "Initialize");
- if(!lua_isfunction(m_LuaState,-1))
+ if( !PushFunction("Initialize") )
{
- LOGWARN("Error in plugin %s: Could not find function Initialize()", m_Directory.c_str() );
- lua_pop(m_LuaState,1);
lua_close( m_LuaState );
m_LuaState = 0;
return false;
@@ -92,11 +89,9 @@ bool cPlugin_NewLua::Initialize() tolua_pushusertype(m_LuaState, this, "cPlugin_NewLua");
- // do the call (1 arguments, 1 result)
- int s = lua_pcall(m_LuaState, 1, 1, 0);
- if( report_errors( m_LuaState, s ) )
+
+ if( !CallFunction(1, 1, "Initialize") )
{
- LOGWARN("Error in plugin %s calling function Initialize()", m_Directory.c_str() );
lua_close( m_LuaState );
m_LuaState = 0;
return false;
@@ -116,20 +111,95 @@ bool cPlugin_NewLua::Initialize() void cPlugin_NewLua::Tick(float a_Dt)
{
- lua_getglobal(m_LuaState, "Tick");
+ if( !PushFunction("Tick") )
+ return;
+
+ tolua_pushnumber( m_LuaState, a_Dt );
+
+ CallFunction(1, 0, "Tick");
+}
+
+bool cPlugin_NewLua::OnPlayerJoin( cPlayer* a_Player )
+{
+ if( !PushFunction("OnPlayerJoin") )
+ return false;
+
+ tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
+
+ if( !CallFunction(1, 1, "OnPlayerJoin") )
+ return false;
+
+ bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
+ return bRetVal;
+}
+
+bool cPlugin_NewLua::OnLogin( cPacket_Login* a_PacketData )
+{
+ if( !PushFunction("OnLogin") )
+ return false;
+
+ tolua_pushusertype(m_LuaState, a_PacketData, "cPacket_Login");
+
+ if( !CallFunction(1, 1, "OnLogin") )
+ return false;
+
+ bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
+ return bRetVal;
+}
+
+bool cPlugin_NewLua::OnBlockPlace( cPacket_BlockPlace* a_PacketData, cPlayer* a_Player )
+{
+ if( !PushFunction("OnBlockPlace") )
+ return false;
+
+ tolua_pushusertype(m_LuaState, a_PacketData, "cPacket_BlockPlace");
+ tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
+
+ if( !CallFunction(2, 1, "OnBlockPlace") )
+ return false;
+
+ bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
+ return bRetVal;
+}
+
+bool cPlugin_NewLua::OnKilled( cPawn* a_Killed, cEntity* a_Killer )
+{
+ if( !PushFunction("OnKilled") )
+ return false;
+
+ tolua_pushusertype(m_LuaState, a_Killed, "cPawn");
+ tolua_pushusertype(m_LuaState, a_Killer, "cEntity");
+
+ if( !CallFunction(2, 1, "OnKilled") )
+ return false;
+
+ bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
+ return bRetVal;
+}
+
+
+
+
+// Helper functions
+bool cPlugin_NewLua::PushFunction( const char* a_FunctionName )
+{
+ lua_getglobal(m_LuaState, a_FunctionName);
if(!lua_isfunction(m_LuaState,-1))
{
- LOGWARN("Error in plugin %s: Could not find function Tick()", m_Directory.c_str() );
+ LOGWARN("Error in plugin %s: Could not find function %s()", m_Directory.c_str(), a_FunctionName );
lua_pop(m_LuaState,1);
- return;
+ return false;
}
+ return true;
+}
- tolua_pushnumber( m_LuaState, a_Dt );
-
- // do the call (1 arguments, 0 result)/
- int s = lua_pcall(m_LuaState, 1, 0, 0);
+bool cPlugin_NewLua::CallFunction( int a_NumArgs, int a_NumResults, const char* a_FunctionName )
+{
+ int s = lua_pcall(m_LuaState, a_NumArgs, a_NumResults, 0);
if( report_errors( m_LuaState, s ) )
{
- LOGWARN("Error in plugin %s calling function Tick()", m_Directory.c_str() );
+ LOGWARN("Error in plugin %s calling function %s()", m_Directory.c_str(), a_FunctionName );
+ return false;
}
+ return true;
}
\ No newline at end of file diff --git a/source/cPlugin_NewLua.h b/source/cPlugin_NewLua.h index 418865879..bdd7f1f7e 100644 --- a/source/cPlugin_NewLua.h +++ b/source/cPlugin_NewLua.h @@ -5,16 +5,23 @@ typedef struct lua_State lua_State;
-class cPlugin_NewLua : public cPlugin //tolua_export
-{ //tolua_export
-public: //tolua_export
+class cPlugin_NewLua : public cPlugin //tolua_export
+{ //tolua_export
+public: //tolua_export
cPlugin_NewLua( const char* a_PluginName );
~cPlugin_NewLua();
- virtual bool Initialize(); //tolua_export
- virtual void Tick(float a_Dt); //tolua_export
+ virtual bool Initialize(); //tolua_export
+ virtual void Tick(float a_Dt); //tolua_export
+ virtual bool OnPlayerJoin( cPlayer* a_Player ); //tolua_export
+ virtual bool OnLogin( cPacket_Login* a_PacketData ); //tolua_export
+ virtual bool OnBlockPlace( cPacket_BlockPlace* a_PacketData, cPlayer* a_Player ); // tolua_export
+ virtual bool OnKilled( cPawn* a_Killed, cEntity* a_Killer ); //tolua_export
private:
+ bool PushFunction( const char* a_FunctionName );
+ bool CallFunction( int a_NumArgs, int a_NumResults, const char* a_FunctionName ); // a_FunctionName is only used for error messages, nothing else
+
std::string m_Directory;
lua_State* m_LuaState;
};//tolua_export
\ No newline at end of file |