]> 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 54ff8c49574f7c31578548b5cae172a8b06c9aaf..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
 
 
@@ -133,7 +133,7 @@ int ScriptApiBase::luaPanic(lua_State *L)
 {
        std::ostringstream oss;
        oss << "LUA PANIC: unprotected error in call to Lua API ("
-               << lua_tostring(L, -1) << ")";
+               << readParam<std::string>(L, -1) << ")";
        FATAL_ERROR(oss.str().c_str());
        // NOTREACHED
        return 0;
@@ -184,7 +184,7 @@ void ScriptApiBase::loadScript(const std::string &script_path)
        }
        ok = ok && !lua_pcall(L, 0, 0, error_handler);
        if (!ok) {
-               std::string error_msg = lua_tostring(L, -1);
+               std::string error_msg = readParam<std::string>(L, -1);
                lua_pop(L, 2); // Pop error message and error handler
                throw ModError("Failed to load and run script from " +
                                script_path + ":\n" + error_msg);
@@ -286,14 +286,14 @@ void ScriptApiBase::stackDump(std::ostream &o)
                int t = lua_type(m_luastack, i);
                switch (t) {
                        case LUA_TSTRING:  /* strings */
-                               o << "\"" << lua_tostring(m_luastack, i) << "\"";
+                               o << "\"" << readParam<std::string>(m_luastack, i) << "\"";
                                break;
                        case LUA_TBOOLEAN:  /* booleans */
-                               o << (lua_toboolean(m_luastack, i) ? "true" : "false");
+                               o << (readParam<bool>(m_luastack, i) ? "true" : "false");
                                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()