From d75335e10fb8bab60ae9585dbab8f02ddc27281a Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Wed, 4 May 2022 19:12:01 +0200 Subject: [PATCH] Refactor rp_farming --- mods/rp_farming/api.lua | 29 ++++++++++++++++------------- mods/rp_farming/nodes.lua | 38 +++++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/mods/rp_farming/api.lua b/mods/rp_farming/api.lua index 9941733d..99455af1 100644 --- a/mods/rp_farming/api.lua +++ b/mods/rp_farming/api.lua @@ -85,20 +85,26 @@ function farming.place_plant(itemstack, placer, pointed_thing) return itemstack end -function farming.next_stage(pos, under, underdef, name, plant) +-- Grow plant to next stage. +-- Returns true if plant has grown, false if not (e.g. because of max stage) +function farming.next_stage(pos, plant_name) local my_node = minetest.get_node(pos) - if my_node.name == name .. "_1" then - minetest.set_node(pos, {name = name .. "_2"}) - elseif my_node.name == name .. "_2" then - minetest.set_node(pos, {name = name .. "_3"}) - elseif my_node.name == name .. "_3" then - minetest.set_node(pos, {name = name .. "_4"}) + if my_node.name == plant_name .. "_1" then + minetest.set_node(pos, {name = plant_name .. "_2"}) + return true + elseif my_node.name == plant_name .. "_2" then + minetest.set_node(pos, {name = plant_name .. "_3"}) + return true + elseif my_node.name == plant_name .. "_3" then + minetest.set_node(pos, {name = plant_name .. "_4"}) -- Stop the timer on the node so no more growing occurs until needed minetest.get_node_timer(pos):stop() + return true end + return false end function farming.grow_plant(pos, name) @@ -129,16 +135,13 @@ function farming.grow_plant(pos, name) -- Grow and check for rain and fertilizer - local under = minetest.get_node({x = pos.x, y = pos.y - 1, z = pos.z}) - local underdef = minetest.registered_nodes[under.name] - - farming.next_stage(pos, under, underdef, name, plant) + farming.next_stage(pos, name) if minetest.get_item_group(under.name, "plantable_fertilizer") > 0 then - farming.next_stage(pos, under, underdef, name, plant) + farming.next_stage(pos, name) end if weather.weather == "storm" then - farming.next_stage(pos, under, underdef, name, plant) + farming.next_stage(pos, name) end end diff --git a/mods/rp_farming/nodes.lua b/mods/rp_farming/nodes.lua index f0b24aa8..f64b3b5d 100644 --- a/mods/rp_farming/nodes.lua +++ b/mods/rp_farming/nodes.lua @@ -27,8 +27,9 @@ minetest.register_node( type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, -0.5+(4/16), 0.5} }, - groups = {snappy=3, handy=2, attached_node=1, seed=1}, - sounds=rp_sounds.node_sound_leaves_defaults() + groups = {snappy=3, handy=2, attached_node=1, seed=1, farming_plant=1, plant_wheat=1}, + sounds=rp_sounds.node_sound_leaves_defaults(), + _rp_farming_plant_name = "rp_farming:wheat", } ) @@ -54,8 +55,9 @@ minetest.register_node( type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, -0.5+(4/16), 0.5} }, - groups = {snappy=3, handy=2, attached_node=1, not_in_craft_guide = 1, not_in_creative_inventory = 1}, - sounds=rp_sounds.node_sound_leaves_defaults() + groups = {snappy=3, handy=2, attached_node=1, farming_plant=1, plant_wheat=2, not_in_craft_guide = 1, not_in_creative_inventory = 1}, + sounds=rp_sounds.node_sound_leaves_defaults(), + _rp_farming_plant_name = "rp_farming:wheat", } ) @@ -81,8 +83,9 @@ minetest.register_node( type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, -0.5+(4/16), 0.5} }, - groups = {snappy=3, handy=2, attached_node=1, not_in_craft_guide = 1, not_in_creative_inventory = 1}, - sounds=rp_sounds.node_sound_leaves_defaults() + groups = {snappy=3, handy=2, attached_node=1, farming_plant=1, plant_wheat=3, not_in_craft_guide = 1, not_in_creative_inventory = 1}, + sounds=rp_sounds.node_sound_leaves_defaults(), + _rp_farming_plant_name = "rp_farming:wheat", } ) @@ -111,8 +114,9 @@ minetest.register_node( type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, -0.5+(4/16), 0.5} }, - groups = {snappy=3, handy=2, attached_node=1, not_in_craft_guide = 1, not_in_creative_inventory = 1}, - sounds=rp_sounds.node_sound_leaves_defaults() + groups = {snappy=3, handy=2, attached_node=1, farming_plant=1, plant_wheat=4, not_in_craft_guide = 1, not_in_creative_inventory = 1}, + sounds=rp_sounds.node_sound_leaves_defaults(), + _rp_farming_plant_name = "rp_farming:wheat", } ) @@ -140,8 +144,9 @@ minetest.register_node( type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, -0.5+(4/16), 0.5} }, - groups = {snappy=3, handy=2, attached_node=1, seed=1}, - sounds=rp_sounds.node_sound_leaves_defaults() + groups = {snappy=3, handy=2, attached_node=1, farming_plant=1, plant_cotton=1, seed=1}, + sounds=rp_sounds.node_sound_leaves_defaults(), + _rp_farming_plant_name = "rp_farming:cotton", } ) @@ -167,8 +172,9 @@ minetest.register_node( type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, -0.5+(4/16), 0.5} }, - groups = {snappy=3, handy=2, attached_node=1, not_in_craft_guide = 1, not_in_creative_inventory = 1}, - sounds=rp_sounds.node_sound_leaves_defaults() + groups = {snappy=3, handy=2, attached_node=1, farming_plant=1, plant_cotton=2, not_in_craft_guide = 1, not_in_creative_inventory = 1}, + sounds=rp_sounds.node_sound_leaves_defaults(), + _rp_farming_plant_name = "rp_farming:cotton", } ) @@ -194,8 +200,9 @@ minetest.register_node( type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, -0.5+(4/16), 0.5} }, - groups = {snappy=3, handy=2, attached_node=1, not_in_craft_guide = 1, not_in_creative_inventory = 1}, - sounds=rp_sounds.node_sound_leaves_defaults() + groups = {snappy=3, handy=2, attached_node=1, farming_plant=1, plant_cotton=3, not_in_craft_guide = 1, not_in_creative_inventory = 1}, + sounds=rp_sounds.node_sound_leaves_defaults(), + _rp_farming_plant_name = "rp_farming:cotton", } ) @@ -260,8 +267,9 @@ minetest.register_node( type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, -0.5+(4/16), 0.5} }, - groups = {snappy=3, handy=2, attached_node=1, not_in_craft_guide = 1, not_in_creative_inventory = 1}, + groups = {snappy=3, handy=2, attached_node=1, farming_plant=1, plant_cotton=4, not_in_craft_guide = 1, not_in_creative_inventory = 1}, sounds = rp_sounds.node_sound_leaves_defaults(), + _rp_farming_plant_name = "rp_farming:cotton", -- Trim cotton with shears _on_trim = trim_cotton,