]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/lua_api/l_inventory.cpp
Script API: Check that SAOs are still usable before attempting to use them
[dragonfireclient.git] / src / script / lua_api / l_inventory.cpp
index b0d43090c3aeb0c73cc1a70eae358c820c0e057f..6e7afa4a4e67bf67575341f79af1ee72b1397d6e 100644 (file)
@@ -336,7 +336,7 @@ int InvRef::l_contains_item(lua_State *L)
        InventoryList *list = getlist(L, ref, listname);
        bool match_meta = false;
        if (lua_isboolean(L, 4))
-               match_meta = lua_toboolean(L, 4);
+               match_meta = readParam<bool>(L, 4);
        if (list) {
                lua_pushboolean(L, list->containsItem(item, match_meta));
        } else {
@@ -495,28 +495,29 @@ int ModApiInventory::l_get_inventory(lua_State *L)
                v3s16 pos = check_v3s16(L, -1);
                loc.setNodeMeta(pos);
 
-               if(getServer(L)->getInventory(loc) != NULL)
+               if (getServer(L)->getInventory(loc) != NULL)
                        InvRef::create(L, loc);
                else
                        lua_pushnil(L);
                return 1;
-       } else {
-               NO_MAP_LOCK_REQUIRED;
-               if(type == "player"){
-                       std::string name = checkstringfield(L, 1, "name");
-                       loc.setPlayer(name);
-               } else if(type == "detached"){
-                       std::string name = checkstringfield(L, 1, "name");
-                       loc.setDetached(name);
-               }
+       }
 
-               if(getServer(L)->getInventory(loc) != NULL)
-                       InvRef::create(L, loc);
-               else
-                       lua_pushnil(L);
-               return 1;
-               // END NO_MAP_LOCK_REQUIRED;
+       NO_MAP_LOCK_REQUIRED;
+       if (type == "player") {
+               std::string name = checkstringfield(L, 1, "name");
+               loc.setPlayer(name);
+       } else if (type == "detached") {
+               std::string name = checkstringfield(L, 1, "name");
+               loc.setDetached(name);
        }
+
+       if (getServer(L)->getInventory(loc) != NULL)
+               InvRef::create(L, loc);
+       else
+               lua_pushnil(L);
+       return 1;
+       // END NO_MAP_LOCK_REQUIRED;
+
 }
 
 // create_detached_inventory_raw(name, [player_name])
@@ -524,7 +525,7 @@ int ModApiInventory::l_create_detached_inventory_raw(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
        const char *name = luaL_checkstring(L, 1);
-       const char *player = lua_isstring(L, 2) ? lua_tostring(L, 2) : "";
+       std::string player = readParam<std::string>(L, 2, "");
        if (getServer(L)->createDetachedInventory(name, player) != NULL) {
                InventoryLocation loc;
                loc.setDetached(name);
@@ -535,8 +536,18 @@ int ModApiInventory::l_create_detached_inventory_raw(lua_State *L)
        return 1;
 }
 
+// remove_detached_inventory_raw(name)
+int ModApiInventory::l_remove_detached_inventory_raw(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       const std::string &name = luaL_checkstring(L, 1);
+       lua_pushboolean(L, getServer(L)->removeDetachedInventory(name));
+       return 1;
+}
+
 void ModApiInventory::Initialize(lua_State *L, int top)
 {
        API_FCT(create_detached_inventory_raw);
+       API_FCT(remove_detached_inventory_raw);
        API_FCT(get_inventory);
 }