forked from VoxeLibre/VoxeLibre
Refactor flower on_place function
This commit is contained in:
parent
c71cd66e50
commit
2a747e828a
|
@ -229,9 +229,13 @@ function mcl_util.layer_to_y(layer, minecraft_dimension)
|
||||||
return layer + mcl_vars.mg_overworld_min
|
return layer + mcl_vars.mg_overworld_min
|
||||||
end
|
end
|
||||||
|
|
||||||
-- on_place function for plants which can't grow on mycelium
|
-- Returns a on_place function for plants
|
||||||
|
-- * condition: function(pos, node)
|
||||||
function mcl_util.on_place_non_mycelium_plant(itemstack, placer, pointed_thing)
|
-- * 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
|
if pointed_thing.type ~= "node" then
|
||||||
-- no interaction possible with entities
|
-- no interaction possible with entities
|
||||||
return itemstack
|
return itemstack
|
||||||
|
@ -245,7 +249,7 @@ function mcl_util.on_place_non_mycelium_plant(itemstack, placer, pointed_thing)
|
||||||
end
|
end
|
||||||
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_under = minetest.registered_nodes[minetest.get_node(pointed_thing.under).name]
|
||||||
local def_above = minetest.registered_nodes[minetest.get_node(pointed_thing.above).name]
|
local def_above = minetest.registered_nodes[minetest.get_node(pointed_thing.above).name]
|
||||||
if def_under.buildable_to then
|
if def_under.buildable_to then
|
||||||
|
@ -255,20 +259,9 @@ function mcl_util.on_place_non_mycelium_plant(itemstack, placer, pointed_thing)
|
||||||
else
|
else
|
||||||
return itemstack
|
return itemstack
|
||||||
end
|
end
|
||||||
soil_node = minetest.get_node({x=place_pos.x, y=place_pos.y-1, z=place_pos.z})
|
|
||||||
|
|
||||||
|
-- Check placement rules
|
||||||
local light_night = minetest.get_node_light(place_pos, 0.0)
|
if (condition(place_pos, node) == true) then
|
||||||
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
|
|
||||||
local idef = itemstack:get_definition()
|
local idef = itemstack:get_definition()
|
||||||
local new_itemstack, success = minetest.item_place_node(itemstack, placer, pointed_thing)
|
local new_itemstack, success = minetest.item_place_node(itemstack, placer, pointed_thing)
|
||||||
|
|
||||||
|
@ -281,4 +274,7 @@ function mcl_util.on_place_non_mycelium_plant(itemstack, placer, pointed_thing)
|
||||||
end
|
end
|
||||||
|
|
||||||
return itemstack
|
return itemstack
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,25 @@ flower_tmp={}
|
||||||
-- Simple flower template
|
-- Simple flower template
|
||||||
local smallflowerlongdesc = "This is a small flower. Small flowers are mainly used for dye production and can also be potted."
|
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)
|
local function add_simple_flower(name, desc, image, simple_selection_box)
|
||||||
minetest.register_node("mcl_flowers:"..name, {
|
minetest.register_node("mcl_flowers:"..name, {
|
||||||
description = desc,
|
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},
|
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(),
|
sounds = mcl_sounds.node_sound_leaves_defaults(),
|
||||||
node_placement_prediction = "",
|
node_placement_prediction = "",
|
||||||
on_place = mcl_util.on_place_non_mycelium_plant,
|
on_place = on_place_flower,
|
||||||
selection_box = {
|
selection_box = {
|
||||||
type = "fixed",
|
type = "fixed",
|
||||||
fixed = simple_selection_box,
|
fixed = simple_selection_box,
|
||||||
|
@ -82,7 +101,7 @@ minetest.register_node("mcl_flowers:tallgrass", {
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
node_placement_prediction = "",
|
node_placement_prediction = "",
|
||||||
on_place = mcl_util.on_place_non_mycelium_plant,
|
on_place = on_place_flower,
|
||||||
_mcl_blast_resistance = 0,
|
_mcl_blast_resistance = 0,
|
||||||
_mcl_hardness = 0,
|
_mcl_hardness = 0,
|
||||||
})
|
})
|
||||||
|
@ -111,7 +130,7 @@ minetest.register_node("mcl_flowers:fern", {
|
||||||
end,
|
end,
|
||||||
drop = wheat_seed_drop,
|
drop = wheat_seed_drop,
|
||||||
node_placement_prediction = "",
|
node_placement_prediction = "",
|
||||||
on_place = mcl_util.on_place_non_mycelium_plant,
|
on_place = on_place_flower,
|
||||||
selection_box = {
|
selection_box = {
|
||||||
type = "fixed",
|
type = "fixed",
|
||||||
fixed = { -4/16, -0.5, -4/16, 4/16, 7/16, 4/16 },
|
fixed = { -4/16, -0.5, -4/16, 4/16, 7/16, 4/16 },
|
||||||
|
|
Loading…
Reference in New Issue