Add InvRef:is_empty(listname) and make chests/furnaces not diggable if not empty in minimal game

master
darkrose 2012-06-03 20:37:55 +10:00 committed by Perttu Ahola
parent a149c6ecde
commit 3e419ffb38
3 changed files with 50 additions and 0 deletions

View 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

View 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)

View 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),