]> git.lizzy.rs Git - xdecor.git/blob - src/hive.lua
Add listring to move honey to inventory
[xdecor.git] / src / hive.lua
1 local hive = {}
2 local honey_max = 16
3
4 function hive.construct(pos)
5         local meta = minetest.get_meta(pos)
6         local inv = meta:get_inventory()
7
8         local formspec = [[ size[8,5;]
9                         label[0.5,0;Bees are busy making honey...]
10                         image[6,0;1,1;hive_bee.png]
11                         image[5,0;1,1;hive_layout.png]
12                         list[context;honey;5,0;1,1;]
13                         list[current_player;main;0,1.35;8,4;]
14                         listring[current_player;main]
15                         listring[context;honey] ]]
16                         ..xbg..default.get_hotbar_bg(0,1.35)
17
18         meta:set_string("formspec", formspec)
19         meta:set_string("infotext", "Artificial Hive")
20         inv:set_size("honey", 1)
21
22         local timer = minetest.get_node_timer(pos)
23         timer:start(math.random(64, 128))
24 end
25
26 function hive.timer(pos)
27         local time = (minetest.get_timeofday() or 0) * 24000
28         if time < 5500 or time > 18500 then return true end
29
30         local inv = minetest.get_meta(pos):get_inventory()
31         local honeystack = inv:get_stack("honey", 1)
32         local honey = honeystack:get_count()
33
34         local radius = 4
35         local minp = vector.add(pos, -radius)
36         local maxp = vector.add(pos, radius)
37         local flowers = minetest.find_nodes_in_area_under_air(minp, maxp, "group:flower")
38
39         if #flowers > 2 and honey < honey_max then
40                 inv:add_item("honey", "xdecor:honey")
41         elseif honey == honey_max then
42                 local timer = minetest.get_node_timer(pos)
43                 timer:stop() return true
44         end
45         return true
46 end
47
48 xdecor.register("hive", {
49         description = "Artificial Hive",
50         tiles = {"xdecor_hive_top.png", "xdecor_hive_top.png",
51                  "xdecor_hive_side.png", "xdecor_hive_side.png",
52                  "xdecor_hive_side.png", "xdecor_hive_front.png"},
53         groups = {choppy=3, oddly_breakable_by_hand=2, flammable=1},
54         on_construct = hive.construct,
55         on_timer = hive.timer,
56         can_dig = function(pos)
57                 local inv = minetest.get_meta(pos):get_inventory()
58                 return inv:is_empty("honey")
59         end,
60         on_punch = function(_, _, puncher)
61                 puncher:set_hp(puncher:get_hp() - 2)
62         end,
63         allow_metadata_inventory_put = function() return 0 end,
64         on_metadata_inventory_take = function(pos, _, _, stack)
65                 if stack:get_count() == honey_max then
66                         local timer = minetest.get_node_timer(pos)
67                         timer:start(math.random(64, 128))
68                 end
69         end
70 })
71
72 -- Craft items
73
74 minetest.register_craftitem("xdecor:honey", {
75         description = "Honey",
76         inventory_image = "xdecor_honey.png",
77         wield_image = "xdecor_honey.png",
78         groups = {food_honey = 1, food_sugar = 1, flammable = 2, not_in_creative_inventory=1},
79         on_use = minetest.item_eat(2)
80 })
81
82 -- Recipes
83
84 minetest.register_craft({
85         output = "xdecor:hive",
86         recipe = {
87                 {"group:stick", "group:stick", "group:stick"},
88                 {"default:paper", "default:paper", "default:paper"},
89                 {"group:stick", "group:stick", "group:stick"}
90         }
91 })