diff options
author | Mattes D <github@xoft.cz> | 2014-01-15 11:45:25 +0100 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2014-01-15 11:45:25 +0100 |
commit | eb89de4c88ef3c2048af571eaa29726d08148e24 (patch) | |
tree | 783438ae17a67c6953c8170fffcdde8dbf944d50 /src/Bindings | |
parent | Merge pull request #538 from mc-server/minecarts (diff) | |
parent | added cWorld::ScheduleTask Function (diff) | |
download | cuberite-eb89de4c88ef3c2048af571eaa29726d08148e24.tar cuberite-eb89de4c88ef3c2048af571eaa29726d08148e24.tar.gz cuberite-eb89de4c88ef3c2048af571eaa29726d08148e24.tar.bz2 cuberite-eb89de4c88ef3c2048af571eaa29726d08148e24.tar.lz cuberite-eb89de4c88ef3c2048af571eaa29726d08148e24.tar.xz cuberite-eb89de4c88ef3c2048af571eaa29726d08148e24.tar.zst cuberite-eb89de4c88ef3c2048af571eaa29726d08148e24.zip |
Diffstat (limited to 'src/Bindings')
-rw-r--r-- | src/Bindings/ManualBindings.cpp | 62 |
1 files changed, 58 insertions, 4 deletions
diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index a9368f613..b12fa5f03 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -941,10 +941,6 @@ protected: } } ; - - - - static int tolua_cWorld_QueueTask(lua_State * tolua_S) { // Binding for cWorld::QueueTask @@ -980,7 +976,65 @@ static int tolua_cWorld_QueueTask(lua_State * tolua_S) return 0; } +class cLuaScheduledWorldTask : + public cWorld::cScheduledTask +{ +public: + cLuaScheduledWorldTask(cPluginLua & a_Plugin, int a_FnRef, int a_Ticks) : + cScheduledTask(a_Ticks), + m_Plugin(a_Plugin), + m_FnRef(a_FnRef) + { + } + +protected: + cPluginLua & m_Plugin; + int m_FnRef; + + // cWorld::cTask overrides: + virtual void Run(cWorld & a_World) override + { + m_Plugin.Call(m_FnRef, &a_World); + } +}; + + +static int tolua_cWorld_ScheduleTask(lua_State * tolua_S) +{ + // Binding for cWorld::ScheduleTask + // Params: function, Ticks + + // Retrieve the cPlugin from the LuaState: + cPluginLua * Plugin = GetLuaPlugin(tolua_S); + if (Plugin == NULL) + { + // An error message has been already printed in GetLuaPlugin() + return 0; + } + + // Retrieve the args: + cWorld * self = (cWorld *)tolua_tousertype(tolua_S, 1, 0); + if (self == NULL) + { + return lua_do_error(tolua_S, "Error in function call '#funcname#': Not called on an object instance"); + } + if (!lua_isfunction(tolua_S, 2)) + { + return lua_do_error(tolua_S, "Error in function call '#funcname#': Expected a function for parameter #1"); + } + + // Create a reference to the function: + int FnRef = luaL_ref(tolua_S, LUA_REGISTRYINDEX); + if (FnRef == LUA_REFNIL) + { + return lua_do_error(tolua_S, "Error in function call '#funcname#': Could not get function reference of parameter #1"); + } + + int Ticks = (int) tolua_tonumber (tolua_S, 3, 0); + self->ScheduleTask(new cLuaScheduledWorldTask(*Plugin, FnRef,Ticks)); + return 0; +} |