Refactor flower on_place function

This commit is contained in:
Wuzzy 2017-06-09 20:20:29 +02:00
parent c71cd66e50
commit 2a747e828a
2 changed files with 65 additions and 50 deletions

View file

@ -229,9 +229,13 @@ function mcl_util.layer_to_y(layer, minecraft_dimension)
return layer + mcl_vars.mg_overworld_min
end
-- on_place function for plants which can't grow on mycelium
function mcl_util.on_place_non_mycelium_plant(itemstack, placer, pointed_thing)
-- Returns a on_place function for plants
-- * condition: function(pos, node)
-- * A function which is called by the on_place function to check if the node can be placed
-- * Must return true, if placement is allowed, false otherwise
-- * pos, node: Position and node table of plant node
function mcl_util.generate_on_place_plant_function(condition)
return function(itemstack, placer, pointed_thing)
if pointed_thing.type ~= "node" then
-- no interaction possible with entities
return itemstack
@ -245,7 +249,7 @@ function mcl_util.on_place_non_mycelium_plant(itemstack, placer, pointed_thing)
end
end
local place_pos, soil_node
local place_pos
local def_under = minetest.registered_nodes[minetest.get_node(pointed_thing.under).name]
local def_above = minetest.registered_nodes[minetest.get_node(pointed_thing.above).name]
if def_under.buildable_to then
@ -255,20 +259,9 @@ function mcl_util.on_place_non_mycelium_plant(itemstack, placer, pointed_thing)
else
return itemstack
end
soil_node = minetest.get_node({x=place_pos.x, y=place_pos.y-1, z=place_pos.z})
local light_night = minetest.get_node_light(place_pos, 0.0)
local light_day = minetest.get_node_light(place_pos, 0.5)
local light_ok = false
if (light_night and light_night >= 8) or (light_day and light_day >= minetest.LIGHT_MAX) then
light_ok = true
end
-- Placement rules:
-- * Allowed on dirt or grass block
-- * Only with light level >= 8
if (soil_node.name == "mcl_core:dirt" or soil_node.name == "mcl_core:dirt_with_grass" or soil_node.name == "mcl_core:dirt_with_grass_snow") and light_ok then
-- Check placement rules
if (condition(place_pos, node) == true) then
local idef = itemstack:get_definition()
local new_itemstack, success = minetest.item_place_node(itemstack, placer, pointed_thing)
@ -282,3 +275,6 @@ function mcl_util.on_place_non_mycelium_plant(itemstack, placer, pointed_thing)
return itemstack
end
end

View file

@ -6,6 +6,25 @@ flower_tmp={}
-- Simple flower template
local smallflowerlongdesc = "This is a small flower. Small flowers are mainly used for dye production and can also be potted."
-- on_place function for flowers
local on_place_flower = mcl_util.generate_on_place_plant_function(function(pos, node)
local below = {x=pos.x, y=pos.y-1, z=pos.z}
local soil_node = minetest.get_node_or_nil(below)
if not soil_node then return end
--[[ Placement requirements:
* Dirt or grass block
* Light level >= 8 at any time or exposed to sunlight at day
]]
local light_night = minetest.get_node_light(pos, 0.0)
local light_day = minetest.get_node_light(pos, 0.5)
local light_ok = false
if (light_night and light_night >= 8) or (light_day and light_day >= minetest.LIGHT_MAX) then
light_ok = true
end
return (soil_node.name == "mcl_core:dirt" or soil_node.name == "mcl_core:dirt_with_grass" or soil_node.name == "mcl_core:dirt_with_grass_snow") and light_ok
end)
local function add_simple_flower(name, desc, image, simple_selection_box)
minetest.register_node("mcl_flowers:"..name, {
description = desc,
@ -21,7 +40,7 @@ local function add_simple_flower(name, desc, image, simple_selection_box)
groups = {dig_immediate=3,flammable=2,plant=1,flower=1,non_mycelium_plant=1,attached_node=1,dig_by_water=1,destroy_by_lava_flow=1,dig_by_piston=1,deco_block=1},
sounds = mcl_sounds.node_sound_leaves_defaults(),
node_placement_prediction = "",
on_place = mcl_util.on_place_non_mycelium_plant,
on_place = on_place_flower,
selection_box = {
type = "fixed",
fixed = simple_selection_box,
@ -82,7 +101,7 @@ minetest.register_node("mcl_flowers:tallgrass", {
end
end,
node_placement_prediction = "",
on_place = mcl_util.on_place_non_mycelium_plant,
on_place = on_place_flower,
_mcl_blast_resistance = 0,
_mcl_hardness = 0,
})
@ -111,7 +130,7 @@ minetest.register_node("mcl_flowers:fern", {
end,
drop = wheat_seed_drop,
node_placement_prediction = "",
on_place = mcl_util.on_place_non_mycelium_plant,
on_place = on_place_flower,
selection_box = {
type = "fixed",
fixed = { -4/16, -0.5, -4/16, 4/16, 7/16, 4/16 },