Add InvRef:is_empty(listname) and make chests/furnaces not diggable if not empty...
authordarkrose <lisa@ltmnet.com>
Sun, 3 Jun 2012 10:37:55 +0000 (20:37 +1000)
committerPerttu Ahola <celeron55@gmail.com>
Sun, 3 Jun 2012 19:31:02 +0000 (22:31 +0300)
doc/lua_api.txt
games/minimal/mods/default/init.lua
src/scriptapi.cpp

index 3f640da4c2c53339e02e5aadc4fc30fab57b9b74..8b2b81c7512d5fa1124fb1705bb11c40b2a10b2e 100644 (file)
@@ -926,6 +926,7 @@ Player-only: (no-op for other objects)
 
 InvRef: Reference to an inventory
 methods:
+- is_empty(listname): return true if list is empty
 - get_size(listname): get size of a list
 - set_size(listname, size): set size of a list
 - get_stack(listname, i): get a copy of stack index i in list
index 4468d1e0e7d3fa8c658f9269194ab800437596eb..1ffe24837442d738ec6b69f2b80c117ab0dd67b9 100644 (file)
@@ -1147,6 +1147,11 @@ minetest.register_node("default:chest", {
                local inv = meta:get_inventory()
                inv:set_size("main", 8*4)
        end,
+       can_dig = function(pos,player)
+               local meta = minetest.env:get_meta(pos);
+               local inv = meta:get_inventory()
+               return inv:is_empty("main")
+       end,
     on_metadata_inventory_move = function(pos, from_list, from_index,
                        to_list, to_index, count, player)
                minetest.log("action", player:get_player_name()..
@@ -1200,6 +1205,11 @@ minetest.register_node("default:chest_locked", {
                local inv = meta:get_inventory()
                inv:set_size("main", 8*4)
        end,
+       can_dig = function(pos,player)
+               local meta = minetest.env:get_meta(pos);
+               local inv = meta:get_inventory()
+               return inv:is_empty("main")
+       end,
     on_metadata_inventory_move = function(pos, from_list, from_index,
                        to_list, to_index, count, player)
                local meta = minetest.env:get_meta(pos)
@@ -1270,6 +1280,18 @@ minetest.register_node("default:furnace", {
                inv:set_size("src", 1)
                inv:set_size("dst", 4)
        end,
+       can_dig = function(pos,player)
+               local meta = minetest.env:get_meta(pos);
+               local inv = meta:get_inventory()
+               if not inv:is_empty("fuel") then
+                       return false
+               elseif not inv:is_empty("dst") then
+                       return false
+               elseif not inv:is_empty("src") then
+                       return false
+               end
+               return true
+       end,
 })
 
 minetest.register_node("default:furnace_active", {
@@ -1291,6 +1313,18 @@ minetest.register_node("default:furnace_active", {
                inv:set_size("src", 1)
                inv:set_size("dst", 4)
        end,
+       can_dig = function(pos,player)
+               local meta = minetest.env:get_meta(pos);
+               local inv = meta:get_inventory()
+               if not inv:is_empty("fuel") then
+                       return false
+               elseif not inv:is_empty("dst") then
+                       return false
+               elseif not inv:is_empty("src") then
+                       return false
+               end
+               return true
+       end,
 })
 
 function hacky_swap_node(pos,name)
index 9744aaa33509c0655122eae37b2fa7c12e561f32..e231a9879c7a33807369802eaea9c51101c48dd8 100644 (file)
@@ -1718,6 +1718,20 @@ private:
                return 0;
        }
 
+       // is_empty(self, listname) -> true/false
+       static int l_is_empty(lua_State *L)
+       {
+               InvRef *ref = checkobject(L, 1);
+               const char *listname = luaL_checkstring(L, 2);
+               InventoryList *list = getlist(L, ref, listname);
+               if(list && list->getUsedSlots() > 0){
+                       lua_pushboolean(L, false);
+               } else {
+                       lua_pushboolean(L, true);
+               }
+               return 1;
+       }
+
        // get_size(self, listname)
        static int l_get_size(lua_State *L)
        {
@@ -1944,6 +1958,7 @@ public:
 };
 const char InvRef::className[] = "InvRef";
 const luaL_reg InvRef::methods[] = {
+       method(InvRef, is_empty),
        method(InvRef, get_size),
        method(InvRef, set_size),
        method(InvRef, get_stack),