From e8056edde0b62e2e4213c51aab851fc9e258ffe0 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Mon, 13 Mar 2017 23:09:27 +0100 Subject: [PATCH] Move important farming functions to new file --- mods/ITEMS/mcl_farming/init.lua | 81 +------------------ .../{gourd.lua => shared_functions.lua} | 72 +++++++++++++++++ 2 files changed, 76 insertions(+), 77 deletions(-) rename mods/ITEMS/mcl_farming/{gourd.lua => shared_functions.lua} (74%) diff --git a/mods/ITEMS/mcl_farming/init.lua b/mods/ITEMS/mcl_farming/init.lua index dbb5db194..19be53805 100644 --- a/mods/ITEMS/mcl_farming/init.lua +++ b/mods/ITEMS/mcl_farming/init.lua @@ -1,79 +1,9 @@ local init = os.clock() mcl_farming = {} -function mcl_farming:add_plant(full_grown, names, interval, chance) - minetest.register_abm({ - nodenames = names, - interval = interval, - chance = chance, - action = function(pos, node) - pos.y = pos.y-1 - if minetest.get_node(pos).name ~= "mcl_farming:soil_wet" and math.random(0, 9) > 0 then - return - end - pos.y = pos.y+1 - if not minetest.get_node_light(pos) then - return - end - if minetest.get_node_light(pos) < 10 then - return - end - local step = nil - for i,name in ipairs(names) do - if name == node.name then - step = i - break - end - end - if step == nil then - return - end - local new_node = {name=names[step+1]} - if new_node.name == nil then - new_node.name = full_grown - end - minetest.set_node(pos, new_node) - end -} ) -end - - -function mcl_farming:place_seed(itemstack, placer, pointed_thing, plantname) - local pt = pointed_thing - if not pt then - return - end - if pt.type ~= "node" then - return - end - - -- Use pointed node's on_rightclick function first, if present - local node = minetest.get_node(pt.under) - if placer and not placer:get_player_control().sneak then - if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then - return minetest.registered_nodes[node.name].on_rightclick(pt.under, node, placer, itemstack) or itemstack - end - end - - local pos = {x=pt.above.x, y=pt.above.y-1, z=pt.above.z} - local farmland = minetest.get_node(pos) - pos= {x=pt.above.x, y=pt.above.y, z=pt.above.z} - local place_s = minetest.get_node(pos) - - - if string.find(farmland.name, "mcl_farming:soil") and string.find(place_s.name, "air") then - minetest.add_node(pos, {name=plantname}) - else - return - end - - if not minetest.setting_getbool("creative_mode") then - itemstack:take_item() - end - return itemstack -end - - +-- IMPORTANT API AND HELPER FUNCTIONS -- +-- Contain functions for planting seed, addind plant growth and gourds (melon/pumpkin-like) +dofile(minetest.get_modpath("mcl_farming").."/shared_functions.lua") -- ========= SOIL ========= dofile(minetest.get_modpath("mcl_farming").."/soil.lua") @@ -84,10 +14,7 @@ dofile(minetest.get_modpath("mcl_farming").."/hoes.lua") -- ========= WHEAT ========= dofile(minetest.get_modpath("mcl_farming").."/wheat.lua") --- = HELPER FUNCTION FOR PUMPKIN AND MELON = -dofile(minetest.get_modpath("mcl_farming").."/gourd.lua") - --- ========= PUMPKIN ========= +-- ======= PUMPKIN ========= dofile(minetest.get_modpath("mcl_farming").."/pumpkin.lua") -- ========= MELON ========= diff --git a/mods/ITEMS/mcl_farming/gourd.lua b/mods/ITEMS/mcl_farming/shared_functions.lua similarity index 74% rename from mods/ITEMS/mcl_farming/gourd.lua rename to mods/ITEMS/mcl_farming/shared_functions.lua index 9a53863f2..2ca7fc155 100644 --- a/mods/ITEMS/mcl_farming/gourd.lua +++ b/mods/ITEMS/mcl_farming/shared_functions.lua @@ -1,3 +1,75 @@ +function mcl_farming:add_plant(full_grown, names, interval, chance) + minetest.register_abm({ + nodenames = names, + interval = interval, + chance = chance, + action = function(pos, node) + pos.y = pos.y-1 + if minetest.get_node(pos).name ~= "mcl_farming:soil_wet" and math.random(0, 9) > 0 then + return + end + pos.y = pos.y+1 + if not minetest.get_node_light(pos) then + return + end + if minetest.get_node_light(pos) < 10 then + return + end + local step = nil + for i,name in ipairs(names) do + if name == node.name then + step = i + break + end + end + if step == nil then + return + end + local new_node = {name=names[step+1]} + if new_node.name == nil then + new_node.name = full_grown + end + minetest.set_node(pos, new_node) + end + }) +end + +function mcl_farming:place_seed(itemstack, placer, pointed_thing, plantname) + local pt = pointed_thing + if not pt then + return + end + if pt.type ~= "node" then + return + end + + -- Use pointed node's on_rightclick function first, if present + local node = minetest.get_node(pt.under) + if placer and not placer:get_player_control().sneak then + if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then + return minetest.registered_nodes[node.name].on_rightclick(pt.under, node, placer, itemstack) or itemstack + end + end + + local pos = {x=pt.above.x, y=pt.above.y-1, z=pt.above.z} + local farmland = minetest.get_node(pos) + pos= {x=pt.above.x, y=pt.above.y, z=pt.above.z} + local place_s = minetest.get_node(pos) + + + if string.find(farmland.name, "mcl_farming:soil") and string.find(place_s.name, "air") then + minetest.add_node(pos, {name=plantname}) + else + return + end + + if not minetest.setting_getbool("creative_mode") then + itemstack:take_item() + end + return itemstack +end + + --[[ Helper function to create a gourd (e.g. melon, pumpkin), the connected stem nodes as - full_unconnected_stem: itemstring of the full-grown but unconnceted stem node. This node must already be done