1 -- Wear out hoes, place soil
2 -- TODO Ignore group:flower
3 farming.hoe_on_use = function(itemstack, user, pointed_thing, uses)
4 local pt = pointed_thing
5 -- check if pointing at a node
9 if pt.type ~= "node" then
13 local under = minetest.get_node(pt.under)
14 local p = {x=pt.under.x, y=pt.under.y+1, z=pt.under.z}
15 local above = minetest.get_node(p)
17 -- return if any of the nodes is not registered
18 if not minetest.registered_nodes[under.name] then
21 if not minetest.registered_nodes[above.name] then
25 -- check if the node above the pointed thing is air
26 if above.name ~= "air" then
30 -- check if pointing at soil
31 if minetest.get_item_group(under.name, "soil") ~= 1 then
35 -- check if (wet) soil defined
36 local regN = minetest.registered_nodes
37 if regN[under.name].soil == nil or regN[under.name].soil.wet == nil or regN[under.name].soil.dry == nil then
41 if minetest.is_protected(pt.under, user:get_player_name()) then
42 minetest.record_protection_violation(pt.under, user:get_player_name())
45 if minetest.is_protected(pt.above, user:get_player_name()) then
46 minetest.record_protection_violation(pt.above, user:get_player_name())
51 -- turn the node into soil, wear out item and play sound
52 minetest.set_node(pt.under, {name = regN[under.name].soil.dry})
53 minetest.sound_play("default_dig_crumbly", {
58 if not minetest.setting_getbool("creative_mode") then
59 itemstack:add_wear(65535/(uses-1))
65 farming.register_hoe = function(name, def)
66 -- Check for : prefix (register new hoes in your mod's namespace)
67 if name:sub(1,1) ~= ":" then
71 if def.description == nil then
72 def.description = "Hoe"
74 if def.inventory_image == nil then
75 def.inventory_image = "unknown_item.png"
77 if def.recipe == nil then
80 {"","group:stick",""},
84 if def.max_uses == nil then
88 minetest.register_tool(name, {
89 description = def.description,
90 inventory_image = def.inventory_image,
91 on_use = function(itemstack, user, pointed_thing)
92 return farming.hoe_on_use(itemstack, user, pointed_thing, def.max_uses)
95 -- Register its recipe
96 if def.material == nil then
97 minetest.register_craft({
102 minetest.register_craft({
103 output = name:sub(2),
105 {def.material, def.material, ""},
106 {"", "group:stick", ""},
107 {"", "group:stick", ""}
111 minetest.register_craft({
112 output = name:sub(2),
114 {"", def.material, def.material},
115 {"", "group:stick", ""},
116 {"", "group:stick", ""}
123 farming.place_seed = function(itemstack, placer, pointed_thing, plantname)
124 local pt = pointed_thing
125 -- check if pointing at a node
129 if pt.type ~= "node" then
133 local under = minetest.get_node(pt.under)
134 local above = minetest.get_node(pt.above)
136 if minetest.is_protected(pt.under, placer:get_player_name()) then
137 minetest.record_protection_violation(pt.under, placer:get_player_name())
140 if minetest.is_protected(pt.above, placer:get_player_name()) then
141 minetest.record_protection_violation(pt.above, placer:get_player_name())
146 -- return if any of the nodes is not registered
147 if not minetest.registered_nodes[under.name] then
150 if not minetest.registered_nodes[above.name] then
154 -- check if pointing at the top of the node
155 if pt.above.y ~= pt.under.y+1 then
159 -- check if you can replace the node above the pointed node
160 if not minetest.registered_nodes[above.name].buildable_to then
164 -- check if pointing at soil
165 if minetest.get_item_group(under.name, "soil") < 2 then
169 -- add the node and remove 1 item from the itemstack
170 minetest.add_node(pt.above, {name = plantname, param2 = 1})
171 if not minetest.setting_getbool("creative_mode") then
172 itemstack:take_item()
178 farming.register_plant = function(name, def)
179 local mname = name:split(":")[1]
180 local pname = name:split(":")[2]
183 if not def.description then
184 def.description = "Seed"
186 if not def.inventory_image then
187 def.inventory_image = "unknown_item.png"
189 if not def.steps then
192 if not def.minlight then
195 if not def.maxlight then
198 if not def.fertility then
203 local g = {seed = 1, snappy = 3, attached_node = 1}
204 for k, v in pairs(def.fertility) do
207 minetest.register_node(":" .. mname .. ":seed_" .. pname, {
208 description = def.description,
209 tiles = {def.inventory_image},
210 inventory_image = def.inventory_image,
211 wield_image = def.inventory_image,
212 drawtype = "signlike",
215 paramtype2 = "wallmounted",
217 sunlight_propagates = true,
220 fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
222 fertility = def.fertility,
223 sounds = default.node_sound_dirt_defaults({
224 dug = {name = "default_grass_footstep", gain = 0.2},
225 place = {name = "default_place_node", gain = 0.25},
228 on_place = function(itemstack, placer, pointed_thing)
229 return farming.place_seed(itemstack, placer, pointed_thing, mname .. ":seed_" .. pname)
234 minetest.register_craftitem(":" .. mname .. ":" .. pname, {
235 description = pname:gsub("^%l", string.upper),
236 inventory_image = mname .. "_" .. pname .. ".png",
239 -- Register growing steps
243 {items = {mname .. ":" .. pname}, rarity = 9 - i},
244 {items = {mname .. ":" .. pname}, rarity= 18 - i * 2},
245 {items = {mname .. ":seed_" .. pname}, rarity = 9 - i},
246 {items = {mname .. ":seed_" .. pname}, rarity = 18 - i * 2},
249 local nodegroups = {snappy = 3, flammable = 2, plant = 1, not_in_creative_inventory = 1, attached_node = 1}
250 nodegroups[pname] = i
251 minetest.register_node(mname .. ":" .. pname .. "_" .. i, {
252 drawtype = "plantlike",
254 tiles = {mname .. "_" .. pname .. "_" .. i .. ".png"},
261 fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
264 sounds = default.node_sound_leaves_defaults(),
269 minetest.register_abm({
270 nodenames = {"group:" .. pname, "group:seed"},
271 neighbors = {"group:soil"},
274 action = function(pos, node)
275 local plant_height = minetest.get_item_group(node.name, pname)
277 -- return if already full grown
278 if plant_height == def.steps then
282 local node_def = minetest.registered_items[node.name] or nil
285 if minetest.get_item_group(node.name, "seed") and node_def.fertility then
286 local can_grow = false
287 local soil_node = minetest.get_node_or_nil({x = pos.x, y = pos.y - 1, z = pos.z})
288 if not soil_node then
291 for _, v in pairs(node_def.fertility) do
292 if minetest.get_item_group(soil_node.name, v) ~= 0 then
297 minetest.set_node(pos, {name = node.name:gsub("seed_", "") .. "_1"})
302 -- check if on wet soil
304 local n = minetest.get_node(pos)
305 if minetest.get_item_group(n.name, "soil") < 3 then
311 local ll = minetest.get_node_light(pos)
313 if not ll or ll < def.minlight or ll > def.maxlight then
318 minetest.set_node(pos, {name = mname .. ":" .. pname .. "_" .. plant_height + 1})
324 seed = mname .. ":seed_" .. pname,
325 harvest = mname .. ":" .. pname