diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 3f640da..8b2b81c 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -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 diff --git a/games/minimal/mods/default/init.lua b/games/minimal/mods/default/init.lua index 4468d1e..1ffe248 100644 --- a/games/minimal/mods/default/init.lua +++ b/games/minimal/mods/default/init.lua @@ -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) diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index 9744aaa..e231a98 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -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),