]> git.lizzy.rs Git - minetest.git/blobdiff - src/scriptapi.cpp
Fix script error reporting a bit
[minetest.git] / src / scriptapi.cpp
index 951ff6f83b89eda8e6ebbd5e6674493c78d1d501..db6b7e86e22ce27addaa58793eae9ed582c904ee 100644 (file)
@@ -402,6 +402,8 @@ static void setfloatfield(lua_State *L, int table,
 static void inventory_set_list_from_lua(Inventory *inv, const char *name,
                lua_State *L, int tableindex, IGameDef *gamedef, int forcesize=-1)
 {
+       if(tableindex < 0)
+               tableindex = lua_gettop(L) + 1 + tableindex;
        // If nil, delete list
        if(lua_isnil(L, tableindex)){
                inv->deleteList(name);
@@ -1099,7 +1101,10 @@ static int l_register_craft(lua_State *L)
                                width = colcount;
                        } else {
                                if(colcount != width){
-                                       script_error(L, "error: %s\n", "Invalid crafting recipe");
+                                       std::string error;
+                                       error += "Invalid crafting recipe (output=\""
+                                                       + output + "\")";
+                                       throw LuaError(error);
                                }
                        }
                        // removes value, keeps key for next iteration
@@ -1808,7 +1813,7 @@ class ObjectRef
                        // Return
                        lua_pushboolean(L, added);
                        if(!added)
-                               lua_pushstring(L, "does not fit");
+                               lua_pushstring(L, "failed to add item");
                        return 2;
                } catch(SerializationError &e){
                        // Return
@@ -2467,6 +2472,21 @@ void scriptapi_export(lua_State *L, Server *server)
        ObjectRef::Register(L);
 }
 
+bool scriptapi_loadmod(lua_State *L, const std::string &scriptpath,
+               const std::string &modname)
+{
+       bool success = false;
+
+       try{
+               success = script_load(L, scriptpath.c_str());
+       }
+       catch(LuaError &e){
+               errorstream<<"Error loading mod: "<<e.what()<<std::endl;
+       }
+
+       return success;
+}
+
 void scriptapi_add_environment(lua_State *L, ServerEnvironment *env)
 {
        realitycheck(L);
@@ -2693,6 +2713,15 @@ bool scriptapi_on_respawnplayer(lua_State *L, ServerActiveObject *player)
        return positioning_handled_by_some;
 }
 
+void scriptapi_get_creative_inventory(lua_State *L, ServerRemotePlayer *player)
+{
+       lua_getglobal(L, "minetest");
+       lua_getfield(L, -1, "creative_inventory");
+       luaL_checktype(L, -1, LUA_TTABLE);
+       inventory_set_list_from_lua(&player->inventory, "main", L, -1,
+                       player->getEnv()->getGameDef(), PLAYER_INVENTORY_SIZE);
+}
+
 /*
        craftitem
 */
@@ -3208,9 +3237,9 @@ void scriptapi_luaentity_step(lua_State *L, u16 id, float dtime)
                script_error(L, "error running function 'on_step': %s\n", lua_tostring(L, -1));
 }
 
-// Calls entity:on_punch(ObjectRef puncher)
+// Calls entity:on_punch(ObjectRef puncher, time_from_last_punch)
 void scriptapi_luaentity_punch(lua_State *L, u16 id,
-               ServerActiveObject *puncher)
+               ServerActiveObject *puncher, float time_from_last_punch)
 {
        realitycheck(L);
        assert(lua_checkstack(L, 20));
@@ -3228,8 +3257,9 @@ void scriptapi_luaentity_punch(lua_State *L, u16 id,
        luaL_checktype(L, -1, LUA_TFUNCTION);
        lua_pushvalue(L, object); // self
        objectref_get_or_create(L, puncher); // Clicker reference
+       lua_pushnumber(L, time_from_last_punch);
        // Call with 2 arguments, 0 results
-       if(lua_pcall(L, 2, 0, 0))
+       if(lua_pcall(L, 3, 0, 0))
                script_error(L, "error running function 'on_punch': %s\n", lua_tostring(L, -1));
 }