Add a inventory to bookshelf, in which books are unstackable
authorarsdragonfly <arsdragonfly@gmail.com>
Sat, 22 Jun 2013 09:00:23 +0000 (17:00 +0800)
committerBlockMen <nmuelll@web.de>
Sat, 5 Jul 2014 13:57:45 +0000 (15:57 +0200)
mods/default/nodes.lua

index 008867d3ad3ade886e93a00a3f1b98d0815d601c..89fc74b179b6ac594a0481046d3ae3f52a9820a6 100644 (file)
@@ -382,6 +382,56 @@ minetest.register_node("default:bookshelf", {
        is_ground_content = false,
        groups = {choppy=3,oddly_breakable_by_hand=2,flammable=3},
        sounds = default.node_sound_wood_defaults(),
+       on_construct = function(pos)
+               local meta = minetest.env:get_meta(pos)
+               meta:set_string("formspec","size[8,7;]list[context;books;0,0;8,2;]list[current_player;main;0,3;8,4;]")
+               local inv = meta:get_inventory()
+               inv:set_size("books", 8*2)
+       end,
+       can_dig = function(pos,player)
+               local meta = minetest.env:get_meta(pos);
+               local inv = meta:get_inventory()
+               return inv:is_empty("books")
+       end,
+
+       allow_metadata_inventory_put = function(pos, listname, index, stack, player)
+               local meta = minetest.env:get_meta(pos)
+               local inv = meta:get_inventory()
+               if listname == "books" then
+                       if stack:get_name() == "default:book" then
+                               return 1
+                       else
+                               return 0
+                       end
+               end
+       end,
+
+       allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
+               local meta = minetest.env:get_meta(pos)
+               local inv = meta:get_inventory()
+               local stack = inv:get_stack(from_list, from_index)
+               local to_stack = inv:get_stack(to_list, to_index)
+               if to_list == "books" then
+                       if stack:get_name() == "default:book" and to_stack:is_empty() then
+                               return 1
+                       else
+                               return 0
+                       end
+               end
+       end,
+
+       on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
+               minetest.log("action", player:get_player_name()..
+                          " moves stuff in bookshelf at "..minetest.pos_to_string(pos))
+       end,
+       on_metadata_inventory_put = function(pos, listname, index, stack, player)
+               minetest.log("action", player:get_player_name()..
+                          " moves stuff to bookshelf at "..minetest.pos_to_string(pos))
+       end,
+       on_metadata_inventory_take = function(pos, listname, index, stack, player)
+               minetest.log("action", player:get_player_name()..
+                          " takes stuff from bookshelf at "..minetest.pos_to_string(pos))
+       end,
 })
 
 minetest.register_node("default:glass", {