Dirty bugfix
[feedlot.git] / init.lua
1 -- feedlot mod Copyright (C) 2017 Gabriel PĂ©rez-Cerezo
2
3 local modpath = minetest.get_modpath( "feedlot" )
4 dofile(modpath.."/fakeplayer.lua")
5
6 local function get_formspec(pos)
7 local spos = pos.x .. "," ..pos.y .. "," .. pos.z
8 local xbg = default.gui_bg .. default.gui_bg_img .. default.gui_slots
9 -- local detached_inv = minetest.create_detached_inventory("mailbox_"..owner)
10 return "size[8,5.5]" .. xbg .. default.get_hotbar_bg(0, 1.5) ..
11 "label[0,0;Feedlot]" ..
12 "list[current_name;main;3.5,0;1,1;]" ..
13 "list[current_player;main;0,1.5;8,1;]" ..
14 "list[current_player;main;0,2.75;8,3;8]" ..
15 -- "listring[detached:mailbox_" .. owner .. ";drop]" ..
16 "listring[current_player;main]"
17 end
18 local function get_milker_formspec(pos)
19 return "size[8,8.5]"..
20 default.gui_bg..
21 default.gui_bg_img..
22 default.gui_slots..
23 "label[0,0;Milker]"..
24 "label[0,1;Buckets:]"..
25 "label[3.5,1;Milk:]"..
26 "list[current_name;bucket;1.5,1;1,1;]"..
27 "list[current_name;main;4.75,0.96;3,2;]"..
28 "list[current_player;main;0,4.25;8,1;]"..
29 "list[current_player;main;0,5.5;8,3;8]"..
30 "listring[current_name;dst]"..
31 "listring[current_player;main]"..
32 "listring[current_name;src]"..
33 "listring[current_player;main]"..
34 default.get_hotbar_bg(0, 4.25)
35 end
36
37
38 local function on_construct(pos)
39 local node = minetest.get_node(pos)
40 local meta = minetest.get_meta(pos)
41 local inv = meta:get_inventory()
42 if node.name == "feedlot:milker" then
43 meta:set_string("formspec", get_milker_formspec(pos))
44 inv:set_size("bucket", 1)
45 inv:set_size("main", 6)
46 else
47 meta:set_string("formspec", get_formspec(pos))
48 inv:set_size("main", 1)
49 end
50 end
51
52 local function can_dig(pos)
53 local meta = minetest.get_meta(pos)
54 local inv = meta:get_inventory()
55 return inv:is_empty("main")
56 end
57 local tube = {
58 insert_object = function(pos, node, stack, direction)
59 local meta = minetest.get_meta(pos)
60 local inv = meta:get_inventory()
61 if node.name == "feedlot:milker" then
62 if stack:get_name() == "bucket:bucket_empty" and inv:room_for_item("bucket", stack) then
63 inv:add_item("bucket", stack)
64 return ItemStack(nil)
65 else
66 return stack
67 end
68 end
69
70 if inv:room_for_item("main", stack) and stack:get_name() ~= "bucket:bucket_empty" then
71 inv:set_stack("main", 1, stack)
72 return ItemStack(nil)
73 else
74 return stack
75 end
76 end,
77 can_insert = function(pos, node, stack, direction)
78 local meta = minetest.get_meta(pos)
79 local inv = meta:get_inventory()
80 if node.name == "feedlot:milker" then
81 if stack:get_name() == "bucket:bucket_empty" then
82 return inv:room_for_item("bucket", stack)
83 else
84 return false
85 end
86 else
87 return inv:room_for_item("main", stack) and stack:get_name() ~= "bucket:bucket_empty"
88 end
89 end,
90 input_inventory = "main",
91 connect_sides = {left = 0, right = 0, front = 0, back = 0, top = 0, bottom = 1}}
92
93 minetest.register_node("feedlot:feedlot", {
94 description = "Feedlot",
95 tiles = {
96 "default_wood.png^feedlot_feed.png",
97 "default_wood.png",
98 "default_wood.png",
99 "default_wood.png",
100 "default_wood.png",
101 "default_wood.png"
102 },
103 drawtype = "nodebox",
104 paramtype = "light",
105 node_box = {
106 type = "fixed",
107 fixed = {
108 {-0.5, -0.5, -0.5, 0.5, -0.1875, -0.4375}, -- NodeBox6
109 {0.4375, -0.5, -0.5, 0.5, -0.1875, 0.5}, -- NodeBox7
110 {-0.5, -0.5, 0.4375, 0.5, -0.1875, 0.5}, -- NodeBox8
111 {-0.5, -0.5, -0.5, -0.4375, -0.1875, 0.5}, -- NodeBox9
112 {-0.4375, -0.5, -0.4375, 0.4375, -0.375, 0.4375}, -- NodeBox10
113 }
114 },
115 allow_metadata_inventory_put = function (_, _, _, stack, _)
116 if stack:get_name() == "bucket:bucket_empty" then
117 return 0
118 else
119 return stack:get_count()
120 end
121 end,
122 on_construct = on_construct,
123 can_dig = can_dig,
124 groups = {snappy = 3, tubedevice = 1, tubedevice_receiver = 1},
125 tube = tube
126 })
127 minetest.register_node("feedlot:milker", {
128 description = "Milker",
129 tiles = {
130 "feedlot_milker_top.png",
131 "feedlot_milker_top.png",
132 "feedlot_milker_side.png",
133 "feedlot_milker_side.png",
134 "feedlot_milker_side.png",
135 "feedlot_milker_side.png",
136 },
137 paramtype = "light",
138 allow_metadata_inventory_put = function (_, listname, _, stack, _)
139 if listname == "bucket" and stack:get_name() == "bucket:bucket_empty" then
140 return stack:get_count()
141 else
142 return 0
143 end
144 end,
145 allow_metadata_inventory_move = function (_, _, _, dst, _, count)
146 return 0
147 end,
148 on_construct = on_construct,
149 can_dig = can_dig,
150 groups = {snappy = 3, tubedevice = 1, tubedevice_receiver = 1},
151 tube = tube
152 })
153
154
155
156 local function is_mob(obj)
157 return obj.get_luaentity and obj:get_luaentity().name and string.sub(obj:get_luaentity().name,1,string.len("mob"))=="mob"
158 end
159
160
161 minetest.register_abm({
162 label = "Feeding and milking",
163 nodenames = {"feedlot:feedlot", "feedlot:milker"},
164 interval = 5,
165 chance = 1,
166
167 action = function (pos, node)
168 local fake_player = feedlotFakePlayer.create(pos, "fake_player")
169 local radius = 3
170 if node.name == "feedlot:milker" then
171 radius = 5
172 end
173 local objs = minetest.get_objects_inside_radius(pos, radius)
174 for _,obj in ipairs(objs) do
175 if not obj:is_player() and obj:get_armor_groups().fleshy and obj:get_armor_groups().fleshy > 0 and is_mob(obj) then
176 -- obj:on_rightclick(fake_player)
177 if obj.get_luaentity().on_rightclick then
178 obj:get_luaentity():on_rightclick(fake_player)
179 end
180 -- minetest.chat_send_all("animal fed")
181 end
182 end
183 end,
184 })
185
186 minetest.register_craft({
187 output = "feedlot:feedlot",
188 recipe = {
189 {"","",""},
190 {"default:wood", "farming:seed_wheat", "default:wood"},
191 {"default:wood", "default:wood", "default:wood"}
192 }})
193 minetest.register_craft({
194 output = "feedlot:milker",
195 recipe = {
196 {"default:steel_ingot","","default:steel_ingot"},
197 {"default:steel_ingot", "bucket:bucket_empty", "default:steel_ingot"},
198 {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}
199 }})