]> git.lizzy.rs Git - minetest.git/blobdiff - src/script/cpp_api/s_base.cpp
Merge pull request #8776 from osjc/FixGetNode
[minetest.git] / src / script / cpp_api / s_base.cpp
index 293c774b0cb84f483949499fd07015c26025f902..e73f613ce6a1013071ac037eace60366db2c74cb 100644 (file)
@@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "util/string.h"
 #include "server.h"
 #ifndef SERVER
-#include "client.h"
+#include "client/client.h"
 #endif
 
 
@@ -293,7 +293,7 @@ void ScriptApiBase::stackDump(std::ostream &o)
                                break;
                        case LUA_TNUMBER:  /* numbers */ {
                                char buf[10];
-                               snprintf(buf, 10, "%lf", lua_tonumber(m_luastack, i));
+                               porting::mt_snprintf(buf, sizeof(buf), "%lf", lua_tonumber(m_luastack, i));
                                o << buf;
                                break;
                        }
@@ -384,14 +384,18 @@ void ScriptApiBase::objectrefGetOrCreate(lua_State *L,
 
 void ScriptApiBase::pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeReason &reason)
 {
-       if (reason.lua_reference >= 0) {
+       if (reason.hasLuaReference())
                lua_rawgeti(L, LUA_REGISTRYINDEX, reason.lua_reference);
-               luaL_unref(L, LUA_REGISTRYINDEX, reason.lua_reference);
-       } else
+       else
                lua_newtable(L);
 
-       lua_pushstring(L, reason.getTypeAsString().c_str());
-       lua_setfield(L, -2, "type");
+       lua_getfield(L, -1, "type");
+       bool has_type = (bool)lua_isstring(L, -1);
+       lua_pop(L, 1);
+       if (!has_type) {
+               lua_pushstring(L, reason.getTypeAsString().c_str());
+               lua_setfield(L, -2, "type");
+       }
 
        lua_pushstring(L, reason.from_mod ? "mod" : "engine");
        lua_setfield(L, -2, "from");
@@ -400,6 +404,10 @@ void ScriptApiBase::pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeR
                objectrefGetOrCreate(L, reason.object);
                lua_setfield(L, -2, "object");
        }
+       if (!reason.node.empty()) {
+               lua_pushstring(L, reason.node.c_str());
+               lua_setfield(L, -2, "node");
+       }
 }
 
 Server* ScriptApiBase::getServer()