-- DIES IST DIE VOLLSTÄNDIG KORRIGIERTE VERSION

local lib = potted_farming
local S = lib.S
local max_uses = lib.watering_can_max_uses
local watering_can_item = lib.modname .. ":watering_can"

local pot_def = {
	groups = {flammable = 2, crumbly = 2, cracky = 3, attached_node = 1, not_in_creative_inventory = 1},
	tiles = {
			"pot_with_soil_top.png",
			"pot_with_soil_bottom.png",
			"pot_with_soil_side.png",
			"pot_with_soil_side.png",
			"pot_with_soil_side.png",
			"pot_with_soil_side.png"

	}, -- tiles
	drawtype = "nodebox",
	paramtype = "light",
	use_texture_alpha = "clip",
	node_box = {
			type = "fixed",
			fixed = {
					{-0.1875, -0.5, -0.1875, 0.1875, -0.1875, 0.1875},                                -- base_center
					{-0.25, -0.375, -0.125, -0.1875, -0.1875, 0.125},                                -- base1
					{-0.125, -0.375, 0.1875, 0.125, -0.1875, 0.25},                                  -- base2
					{0.1875, -0.375, -0.125, 0.25, -0.1875, 0.125},                                  -- base3
					{-0.125, -0.375, -0.25, 0.125, -0.1875, -0.1875},                                -- base4
					{-0.5, -0.5, 0, 0.5, 0.5, 0},                                                   -- plant1X
					{0, -0.5, -0.5, 0, 0.5, 0.5},                                                   -- plant2Z

			} -- fixed

	}, -- node_box
	selection_box = {
			type = "fixed",
			fixed = {
					{-0.25, -0.5, -0.25, 0.25, -0.1875, 0.25},                                         -- selection

			}

	}, -- selection_box

	collision_box = {
			type = "fixed",
			fixed = {
					{-0.25, -0.5, -0.25, 0.25, -0.1875, 0.25},                                         -- selection

			}

	}, -- collsion_box

	on_rotate = function(pos, node)
		return false
	end,
}


function lib.is_acceptable_source (itemname)
	local name = itemname:split(":")[2]
	local plant = name:split("_")[1]

	if plant == nil then plant = ""	end

	local s = lib.plant_settings
	local pot = lib.modname .. ":pot_with_".. plant .."_1"
	local chkpot = minetest.registered_nodes[pot]

	if lib.plant_list[plant] and string.find(name , "_stem") and s[plant].definable and chkpot then
		return true, "plant", plant
	end
	for k,v in pairs(lib.mushroom_list) do
		pot = lib.modname .. ":pot_with_".. k .."_1"
		chkpot = minetest.registered_nodes[pot]
		if v[1] == itemname and chkpot then
			if s[k] ~= nil and s[k].definable then
				return true, "mushroom", k
			end
		end
	end
	for k,v in pairs(lib.fruit_tree_list) do
		pot = lib.modname .. ":pot_with_".. k .."_sapling"
		chkpot = minetest.registered_nodes[pot]
		if v[1] == itemname and chkpot then
			if s[k] ~= nil and s[k].definable then
				return true, "fruit_tree", k
			end
		end
	end

	return false, "no", nil

end


function lib.check_light(pos, min_light, max_light)
    local checkpos = pos
	if minetest.get_node(checkpos).name == "ignore" then
		return false
	end

    local pot_light = minetest.get_node_light(checkpos)
	local min = min_light
	local max = max_light

	if min < 1 then min = 1	end
	if max > 14 then max = 14	end

	if min > max then
		local t = max
		max = min
		min = t
	end

	if min <= pot_light and pot_light <= max then
		return true
	end

    return false

end


function lib.register_plant_abm(nodename, next_step_nodename, delay, percentage)
		local plant_name = nodename:split(":")[2]:split("_")[3]
		local potted_plant = lib.plant_settings[plant_name]
		minetest.register_abm({
        label = "growing_potted_".. plant_name .."_abm",
        nodenames = {nodename},
        interval = delay,
        chance = percentage,
        action = function(pos, node, active_object_count, active_object_count_wider)
            local nodepos = pos
            if(lib.check_light(nodepos, potted_plant.min_light, potted_plant.max_light ) ) then
                minetest.swap_node(nodepos, {name = next_step_nodename})
            end
        end,
    })
end


function lib.add_watering_can_wear(itemstack)
	if itemstack:get_name() == watering_can_item then
		itemstack:add_wear(65535 / (max_uses))
		local wear = itemstack:get_wear()
		if wear >= 65535 then
				itemstack:replace(lib.modname .. ":empty_watering_can")
		end
	end
	return itemstack
end


function lib.get_leaf_item_name(plant_name)
	local item_name = lib.modname .. ":" .. plant_name
	local p = lib.plant_settings.support[plant_name]
	if p ~= nil and p.can_swap  then
		local existing_item = p.itemname
		local mod_name = existing_item:split(":")[1]
		if minetest.registered_items[existing_item] and minetest.get_modpath(mod_name) then
				item_name = existing_item
		end
	end
	return item_name
end


function lib.register_plant(plant_name)
    local plant_desc = plant_name:gsub("_", " "):gsub("(%a)(%a+)",
                        function(a, b)
                            return string.upper(a) .. string.lower(b)
                        end)

		local leaf_item = lib.get_leaf_item_name(plant_name)
		if leaf_item:split(":")[1] == lib.modname then
			local craftitem_def = {
				description = S(plant_desc),
				inventory_image = lib.modname .. "_".. plant_name ..".png",
				groups = {},
			}
			craftitem_def.groups["food_".. plant_name] = 1
			minetest.register_craftitem(lib.modname .. ":" .. plant_name, table.copy(craftitem_def) )
		end

    minetest.register_craftitem(lib.modname .. ":" .. plant_name .."_stem", {
        description = S(plant_desc) .. " " .. S("Stem"),
        inventory_image = lib.modname .. "_".. plant_name .."_stem.png",
        groups = {stem = 1, flammable = 2,},
    })

    local plant_def = table.copy(pot_def)
	plant_def.description = S("Pot with ") .. S(plant_desc)

    plant_def.tiles[3] = "pot_with_soil_side.png^" .. lib.modname .. "_".. plant_name .."_1.png"
    plant_def.tiles[4] = "pot_with_soil_side.png^" .. lib.modname .. "_".. plant_name .."_1.png"
    plant_def.tiles[5] = "pot_with_soil_side.png^" .. lib.modname .. "_".. plant_name .."_1.png"
    plant_def.tiles[6] = "pot_with_soil_side.png^" .. lib.modname .. "_".. plant_name .."_1.png"
    plant_def.drop = {
        items = {
            {items = {lib.modname .. ":pot_with_soil"} },
            {items = {lib.modname .. ":" .. plant_name .."_stem"} },
        },
    }
    minetest.register_node(lib.modname .. ":pot_with_".. plant_name .."_1",  table.copy(plant_def) )
    lib.register_plant_abm(lib.modname .. ":pot_with_".. plant_name .."_1",
                                     lib.modname .. ":pot_with_".. plant_name .."_2", 30, 10)

    plant_def.tiles[3] = "pot_with_soil_side.png^" .. lib.modname .. "_".. plant_name .."_2.png"
    plant_def.tiles[4] = "pot_with_soil_side.png^" .. lib.modname .. "_".. plant_name .."_2.png"
    plant_def.tiles[5] = "pot_with_soil_side.png^" .. lib.modname .. "_".. plant_name .."_2.png"
    plant_def.tiles[6] = "pot_with_soil_side.png^" .. lib.modname .. "_".. plant_name .."_2.png"
    plant_def.drop = {
        items = {
            {items = {lib.modname .. ":pot_with_soil"} },
        },
    }
    minetest.register_node(lib.modname .. ":pot_with_".. plant_name .."_2",  table.copy(plant_def) )
    lib.register_plant_abm(lib.modname .. ":pot_with_".. plant_name .."_2",
                                         "potted_farming:pot_with_".. plant_name .."_3", 40, 15)

    plant_def.tiles[3] = "pot_with_soil_side.png^" .. lib.modname .. "_".. plant_name .."_3.png"
    plant_def.tiles[4] = "pot_with_soil_side.png^" .. lib.modname .. "_".. plant_name .."_3.png"
    plant_def.tiles[5] = "pot_with_soil_side.png^" .. lib.modname .. "_" ..plant_name .."_3.png"
    plant_def.tiles[6] = "pot_with_soil_side.png^" .. lib.modname .. "_".. plant_name .."_3.png"
    plant_def.drop = {
        items = {
            {items = {lib.modname .. ":pot_with_soil"} },
            {items = {leaf_item, leaf_item}, rarity = 1}, -- KORRIGIERT
            {items = {leaf_item }, rarity = 2},
            {items = {leaf_item }, rarity = 5},
            {items = {lib.modname .. ":" .. plant_name .."_stem" }, rarity = 5},
        },
    }
    plant_def.on_rightclick = function (pos, node, player, itemstack, pointed_thing)
        if player:is_player() then
            local itemname = itemstack:get_name()
            if itemname ~= watering_can_item then
				local nodepos = pos
				local q = math.random(2, 4)
				local stem = math.random(1, 5)
				local leftover
				local item = ItemStack({name = leaf_item, count = q})
				local inv = player:get_inventory()
				if inv:room_for_item("main", item) then
					leftover = inv:add_item("main", item)
					if not leftover:is_empty() then
						minetest.add_item(player:get_pos(), leftover)
					end
				else
					minetest.add_item(player:get_pos(), item)
				end
				local stem_item = lib.modname .. ":"..plant_name .."_stem"
				if stem == 1 then
					if inv:room_for_item("main", stem_item) then
						leftover = inv:add_item("main", stem_item)
						if not leftover:is_empty() then
							minetest.add_item(player:get_pos(), leftover)
						end
					else
						minetest.add_item(player:get_pos(), stem_item)
					end
				end
				local n = math.random(1, 3)
				minetest.sound_play("foliage-0".. n, {pos=nodepos, gain=1.2})
				minetest.swap_node(nodepos, {name = lib.modname .. ":pot_with_".. plant_name .."_2"})
            end
        end
        return itemstack
    end
    minetest.register_node(lib.modname.. ":pot_with_".. plant_name .."_3",  table.copy(plant_def) )
    lib.register_plant_abm(lib.modname .. ":pot_with_".. plant_name .."_3",
                            lib.modname .. ":pot_with_".. plant_name .."_4", 40, 30)

    local yellow_modifier = "^[colorize:yellow:80" -- KORRIGIERT
    plant_def.tiles[3] = "pot_with_soil_side.png^" .. lib.modname .. "_".. plant_name .."_4.png" .. yellow_modifier
    plant_def.tiles[4] = "pot_with_soil_side.png^" .. lib.modname .. "_".. plant_name .."_4.png" .. yellow_modifier
    plant_def.tiles[5] = "pot_with_soil_side.png^" .. lib.modname .. "_".. plant_name .."_4.png" .. yellow_modifier
    plant_def.tiles[6] = "pot_with_soil_side.png^" .. lib.modname .. "_".. plant_name .."_4.png" .. yellow_modifier
    plant_def.drop = {
        items = {
            {items = {lib.modname .. ":pot_with_soil"} },
            {items = {leaf_item, leaf_item}}, -- KORRIGIERT
            {items = {leaf_item }, rarity = 3},
        },
    }
    plant_def.on_rightclick = function (pos, node, player, itemstack, pointed_thing)
        if player:is_player() then
			local itemname = itemstack:get_name()
			if itemstack:is_empty() == false and itemname == watering_can_item then
				local nodepos = pos
				itemstack = lib.add_watering_can_wear(itemstack)
				local n = math.random(3, 4)
				minetest.sound_play("water-splash-0".. n, {pos=nodepos, gain=1.2})
				minetest.swap_node(nodepos, {name = lib.modname .. ":pot_with_".. plant_name .."_3"})
			end
        end
        return itemstack
	end
    minetest.register_node(lib.modname .. ":pot_with_" .. plant_name .."_4",  table.copy(plant_def) )
end


function lib.register_wild_variant(plant_name, nodes, s, min, max)
	local leaf_item = lib.get_leaf_item_name(plant_name)
    minetest.register_node(lib.modname .. ":wild_".. plant_name , {
        description = S("Wild ").. S(plant_name),
        paramtype = "light",
		paramtype2 = "facedir",
        walkable = false,
        drawtype = "plantlike",
		use_texture_alpha = "clip",
        tiles = {lib.modname .. "_wild_".. plant_name ..".png"},
        inventory_image = lib.modname .. "_wild_".. plant_name ..".png",
        wield_image = lib.modname .. "_wild_".. plant_name ..".png",
        groups = {  snappy = 3, dig_immediate = 1, flammable = 2, plant = 1,
                    flora = 1, attached_node = 1, not_in_creative_inventory = 1 },
        sounds = default.node_sound_leaves_defaults(),
        selection_box = {
            type = "fixed",
                fixed = { {-4 / 16, -0.5, -4 / 16, 4 / 16, 5 / 16, 4 / 16},	},
                },
        drop =  {
            items = {
                {items = {leaf_item, leaf_item}, rarity = 1}, -- KORRIGIERT
                {items = {leaf_item}, rarity = 2},
                {items = {lib.modname .. ":" .. plant_name .."_stem"}, rarity = 1},
            }
        },
    })
    minetest.register_decoration({
        deco_type = "simple",
        place_on = nodes,
        sidelen = 16,
        noise_params = {
            offset = 0,
            scale = s,
            spread = {x = 70, y = 70, z = 70},
            seed = 2570,
            octaves = 3,
            persist = 0.6
        },
        y_min = min,
        y_max = max,
        decoration = lib.modname .. ":wild_".. plant_name,
    })
end


function lib.register_mushroom(mushroom_name, full_mushroom_name)
	local mushroom_desc = mushroom_name:gsub("_", " "):gsub("(%a)(%a+)",
											function(a, b)
													return string.upper(a) .. string.lower(b)
											end)
	local mushroom_def = table.copy(pot_def)
	mushroom_def.description = S("Pot with ") .. S(mushroom_desc)

	mushroom_def.tiles[3] = "pot_with_soil_side.png^" .. lib.modname .. "_".. mushroom_name .."_1.png"
	mushroom_def.tiles[4] = "pot_with_soil_side.png^" .. lib.modname .. "_".. mushroom_name .."_1.png"
	mushroom_def.tiles[5] = "pot_with_soil_side.png^" .. lib.modname .. "_".. mushroom_name .."_1.png"
	mushroom_def.tiles[6] = "pot_with_soil_side.png^" .. lib.modname .. "_".. mushroom_name .."_1.png"
	mushroom_def.drop = {
			items = {
					{items = {lib.modname .. ":pot_with_soil"} },
					{items = {full_mushroom_name} },
					},
			}
	minetest.register_node(lib.modname .. ":pot_with_".. mushroom_name .."_1",  table.copy(mushroom_def) )
	lib.register_plant_abm(lib.modname .. ":pot_with_".. mushroom_name .."_1",
									 lib.modname .. ":pot_with_".. mushroom_name .."_2", 30, 15)

	mushroom_def.tiles[3] = "pot_with_soil_side.png^" .. lib.modname .. "_".. mushroom_name .."_2.png"
	mushroom_def.tiles[4] = "pot_with_soil_side.png^" .. lib.modname .. "_".. mushroom_name .."_2.png"
	mushroom_def.tiles[5] = "pot_with_soil_side.png^" .. lib.modname .. "_" ..mushroom_name .."_2.png"
	mushroom_def.tiles[6] = "pot_with_soil_side.png^" .. lib.modname .. "_".. mushroom_name .."_2.png"
	mushroom_def.drop = {
			items = {
					{items = {lib.modname .. ":pot_with_soil"} },
					{items = {full_mushroom_name } },
					{items = {full_mushroom_name }, rarity = 2},
			},
	}
	mushroom_def.on_rightclick = function (pos, node, player, itemstack, pointed_thing)
			if player:is_player() then
					local itemname = itemstack:get_name()
					if itemname ~= watering_can_item then
						local nodepos = pos
						local q = math.random(1, 2)
						local mush_item = ItemStack({name = full_mushroom_name, count = q})
						local leftover
						local inv = player:get_inventory()
						if inv:room_for_item("main", mush_item) then
								leftover = inv:add_item("main", mush_item)
								if not leftover:is_empty() then
									minetest.add_item(player:get_pos(), leftover)
								end
						else
								minetest.add_item(player:get_pos(), mush_item)
						end
						local n = math.random(1, 3)
						minetest.sound_play("foliage-0".. n, {pos=nodepos, gain=1.2})
						minetest.swap_node(nodepos, {name = lib.modname .. ":pot_with_".. mushroom_name .."_1"})
					end
			end
			return itemstack
	end
	minetest.register_node(lib.modname.. ":pot_with_".. mushroom_name .."_2",  table.copy(mushroom_def) )
	lib.register_plant_abm(lib.modname .. ":pot_with_".. mushroom_name .."_2",
									 lib.modname .. ":pot_with_".. mushroom_name .."_3", 30, 20)

	mushroom_def.tiles[3] = "pot_with_soil_side.png^" .. lib.modname .. "_".. mushroom_name .."_3.png"
	mushroom_def.tiles[4] = "pot_with_soil_side.png^" .. lib.modname .. "_".. mushroom_name .."_3.png"
	mushroom_def.tiles[5] = "pot_with_soil_side.png^" .. lib.modname .. "_" ..mushroom_name .."_3.png"
	mushroom_def.tiles[6] = "pot_with_soil_side.png^" .. lib.modname .. "_".. mushroom_name .."_3.png"
	mushroom_def.drop = {
			items = {
					{items = {lib.modname .. ":pot_with_soil"} },
					{items = {full_mushroom_name, full_mushroom_name}, rarity = 1}, -- KORRIGIERT
					{items = {full_mushroom_name }, rarity = 2},
					{items = {full_mushroom_name }, rarity = 4},
			},
	}
	mushroom_def.on_rightclick = function (pos, node, player, itemstack, pointed_thing)
			if player:is_player() then
					local itemname = itemstack:get_name()
					if itemname ~= watering_can_item then
						local nodepos = pos
						local q = math.random(2, 4)
						local mush_item = ItemStack({name = full_mushroom_name, count = q})
						local leftover
						local inv = player:get_inventory()
						if inv:room_for_item("main", mush_item) then
								leftover = inv:add_item("main", mush_item)
								if not leftover:is_empty() then
									minetest.add_item(player:get_pos(), leftover)
								end
						else
								minetest.add_item(nodepos, mush_item)
						end
						local n = math.random(1, 3)
						minetest.sound_play("foliage-0".. n, {pos=nodepos, gain=1.2})
						minetest.swap_node(nodepos, {name = lib.modname .. ":pot_with_".. mushroom_name .."_1"})
					end
			end
			return itemstack
	end
	minetest.register_node(lib.modname.. ":pot_with_".. mushroom_name .."_3",  table.copy(mushroom_def) )

	local yellow_modifier = "^[colorize:yellow:80" -- ANGEPASST
	mushroom_def.tiles[3] = "pot_with_soil_side.png^" .. lib.modname .. "_".. mushroom_name .."_4.png" .. yellow_modifier
	mushroom_def.tiles[4] = "pot_with_soil_side.png^" .. lib.modname .. "_".. mushroom_name .."_4.png" .. yellow_modifier
	mushroom_def.tiles[5] = "pot_with_soil_side.png^" .. lib.modname .. "_" ..mushroom_name .."_4.png" .. yellow_modifier
	mushroom_def.tiles[6] = "pot_with_soil_side.png^" .. lib.modname .. "_".. mushroom_name .."_4.png" .. yellow_modifier
	mushroom_def.drop = {
			items = {
					{items = {lib.modname .. ":pot_with_soil"} },
					{items = {full_mushroom_name, full_mushroom_name}, rarity = 1}, -- KORRIGIERT
					{items = {full_mushroom_name }, rarity = 2},
			},
	}
	mushroom_def.on_rightclick = function (pos, node, player, itemstack, pointed_thing)
			if player:is_player() then
					local itemname = itemstack:get_name()
					if itemname ~= watering_can_item then
						local nodepos = pos
						local q = math.random(1, 3)
						local mush_item = ItemStack({name = full_mushroom_name, count = q})
						local leftover
						local inv = player:get_inventory()
						if inv:room_for_item("main", mush_item) then
							leftover = inv:add_item("main", mush_item)
								if not leftover:is_empty() then
									minetest.add_item(player:get_pos(), leftover)
								end
						else
								minetest.add_item(nodepos, mush_item)
						end
						local n = math.random(1, 3)
						minetest.sound_play("foliage-0".. n, {pos=nodepos, gain=1.2})
						minetest.swap_node(nodepos, {name = lib.modname .. ":pot_with_soil"})
				end
			end
			return itemstack
	end
	minetest.register_node(lib.modname.. ":pot_with_".. mushroom_name .."_4",  table.copy(mushroom_def) )

    minetest.register_abm({
        label = "drying_potted_mushroom_abm",
        nodenames = {
				lib.modname.. ":pot_with_".. mushroom_name .."_1",
				lib.modname.. ":pot_with_".. mushroom_name .."_2",
				lib.modname.. ":pot_with_".. mushroom_name .."_3",
			},
        interval = 30,
        chance = 40,
        action = function(pos, node, active_object_count, active_object_count_wider)
				local potted_plant = lib.plant_settings[mushroom_name]
                local nodepos = pos
				local can_grow = lib.check_light(nodepos, potted_plant.min_light, potted_plant.max_light)
                if can_grow == false then
                    minetest.swap_node(nodepos, {name = lib.modname.. ":pot_with_".. mushroom_name .."_4"})
                end
        end,
	})
end


function lib.check_free_space_above(pot_pos)
	local above_pos = vector.add(pot_pos, vector.new(0, 1, 0))
	local above_node = minetest.get_node(above_pos)
	if above_node.name == "air" then
		return true
	end
	return false
end


function lib.register_sapling_abm(nodename, shrub_nodename, leaves_nodename, fruit , delay, percentage)
	local potted_plant = lib.plant_settings[fruit]
	minetest.register_abm({
			label = "growing_potted_".. fruit .."_sapling_abm",
			nodenames = {nodename},
			interval = delay,
			chance = percentage,
			action = function(pos, node, active_object_count, active_object_count_wider)
				local nodepos = pos
				local above_nodepos = vector.add(nodepos, vector.new(0, 1, 0))
				local enough_light = lib.check_light(above_nodepos, potted_plant.min_light, potted_plant.max_light)
				if lib.check_free_space_above(nodepos) and enough_light then
						minetest.swap_node(nodepos, {name = shrub_nodename})
						minetest.swap_node(above_nodepos, {name = leaves_nodename})
					end
			end,
	})
end


function lib.register_leaves_abm(nodename, fruit_leaves_nodename, thirsty_leaves_nodename, fruit, delay, percentage)
	local potted_plant = lib.plant_settings[fruit]
	minetest.register_abm({
			label = "growing_potted_".. fruit .."_leaves_abm",
			nodenames = {nodename},
			interval = delay,
			chance = percentage,
			action = function(pos, node, active_object_count, active_object_count_wider)
				local nodepos = pos
				local leaves_nodename = thirsty_leaves_nodename
				local enough_light = lib.check_light(nodepos, potted_plant.min_light, potted_plant.max_light )
				local meta = minetest.get_meta(nodepos)
				local pot_light = meta:get_int("lightlevel")
				if enough_light then
					local growth = math.random(1, 3) -- ANGEPASST
					if growth == 1 then
						leaves_nodename = fruit_leaves_nodename
					end
				end
				minetest.swap_node(nodepos, {name = leaves_nodename, param2 = 2} )
			end,
	})
end


-- ERSETZE DIE KOMPLETTE FUNKTION "lib.register_fruit_tree" MIT DIESEM BLOCK

function lib.register_fruit_tree (k, sapling_name, full_fruit_name, original_leaves_png, shrub_to_use)
	local fruit_mod_name = full_fruit_name:split(":")[1]
	local fruit_name = k
	local leaves_png = original_leaves_png
	if leaves_png == nil then
		leaves_png = "default_leaves.png"
	end
	if k == "banana" then
		leaves_png = "(".. leaves_png .. "^".. lib.modname .."_banana_leaf_mask.png^[makealpha:255,0,255)"
	end
	local shrub_def = table.copy(pot_def)
	shrub_def.description = S("Pot with ") .. S(fruit_name)

	shrub_def.tiles[3] = "pot_with_soil_side.png^" .. lib.modname .. "_".. fruit_name .."_sapling.png"
	shrub_def.tiles[4] = "pot_with_soil_side.png^" .. lib.modname .. "_".. fruit_name .."_sapling.png"
	shrub_def.tiles[5] = "pot_with_soil_side.png^" .. lib.modname .. "_" ..fruit_name .."_sapling.png"
	shrub_def.tiles[6] = "pot_with_soil_side.png^" .. lib.modname .. "_".. fruit_name .."_sapling.png"
	shrub_def.drop = {
			items = {
					{items = {lib.modname .. ":pot_with_soil"} },
					{items = {sapling_name} },
			},
	}
	minetest.register_node(lib.modname.. ":pot_with_".. fruit_name .."_sapling",  table.copy(shrub_def) )
	lib.register_sapling_abm(lib.modname.. ":pot_with_".. fruit_name .."_sapling",
													 shrub_to_use,
													 lib.modname.. ":".. fruit_name .."_leaves_1", fruit_name, 40, 15)

	local fruit_desc = fruit_name:gsub("(%a)(%a+)",
											function(a, b)
													return string.upper(a) .. string.lower(b)
											end)
	local leaves_def = {
		description = S(fruit_desc) .. " " .. S("(Leaves)") .. " 1",
		drawtype = "plantlike",
		visual_scale = 1.3,
		use_texture_alpha = "clip",
		walkable = false,
		paramtype = "light",
		paramtype2 = "meshoptions",
		place_param2 = 2,
		tiles = { leaves_png },
		sunlight_propagates = true,
		groups = {snappy = 3, attached_node = 1, leaves = 1, flammable = 2, not_in_creative_inventory = 1 },
		drop = {
			items = {
				{items = {sapling_name}, rarity = 5},
			}
		},
		sounds = default.node_sound_leaves_defaults(),
	}

	minetest.register_node(lib.modname.. ":".. fruit_name .."_leaves_1",  table.copy(leaves_def) )
	lib.register_leaves_abm(lib.modname.. ":".. fruit_name .."_leaves_1",
	 												lib.modname.. ":".. fruit_name .."_leaves_2",
													lib.modname.. ":".. fruit_name .."_leaves_3", fruit_name, 40, 5)

	leaves_def.description = S(fruit_desc) .. " " .. S("(Leaves)") .. " 2"
	leaves_def.tiles = {lib.modname .."_".. fruit_name .."_on_leaves.png^".. leaves_png }
	leaves_def.drop = {
		items = {
			{items = {full_fruit_name, full_fruit_name}},
		}
	}
	leaves_def.on_rightclick = function (pos, node, player, itemstack, pointed_thing)
			minetest.log("error", "[PF_HARVEST_DEBUG] Ernte-Funktion für Fruchtbaum ausgelöst.")
			if player and player:is_player() then
					local player_name = player:get_player_name()
					minetest.log("error", "[PF_HARVEST_DEBUG] Spieler: " .. player_name)
					local nodepos = pos
					local q = math.random(2, 3)
					local leftover
					
					minetest.log("error", "[PF_HARVEST_DEBUG] Item wird erstellt: " .. q .. "x " .. full_fruit_name)
					local item = ItemStack({name = full_fruit_name, count = q})

					if item:is_empty() then
						minetest.log("error", "[PF_HARVEST_DEBUG] FEHLER: Erstelltes ItemStack ist leer! Item existiert möglicherweise nicht.")
						return itemstack
					end

					local inv = player:get_inventory()
					if inv:room_for_item("main", item) then
							minetest.log("error", "[PF_HARVEST_DEBUG] Inventar hat Platz. Rufe inv:add_item auf.")
							leftover = inv:add_item("main", item)
							if not leftover:is_empty() then
								minetest.log("error", "[PF_HARVEST_DEBUG] inv:add_item hat einen Rest zurückgegeben. Droppe " .. leftover:to_string())
								minetest.add_item(player:get_pos(), leftover)
							else
								minetest.log("error", "[PF_HARVEST_DEBUG] inv:add_item hat keinen Rest zurückgegeben. Item sollte im Inventar sein.")
							end
					else
							minetest.log("error", "[PF_HARVEST_DEBUG] Inventar ist voll. Droppe Item in die Welt.")
							minetest.add_item(player:get_pos(), item)
					end
					
					minetest.log("error", "[PF_HARVEST_DEBUG] Spiele Sound ab und ändere den Knoten.")
					local n = math.random(1, 3)
					minetest.sound_play("foliage-0".. n, {pos=nodepos, gain=1.2})
					minetest.swap_node(nodepos, {name = lib.modname.. ":".. fruit_name .."_leaves_1", param2 = 2})
			else
				minetest.log("error", "[PF_HARVEST_DEBUG] FEHLER: 'player' Objekt ist ungültig oder kein Spieler.")
			end
			
			minetest.log("error", "[PF_HARVEST_DEBUG] Ernte-Funktion wird beendet.")
			return itemstack
	end
	minetest.register_node(lib.modname.. ":".. fruit_name .."_leaves_2",  table.copy(leaves_def) )

	leaves_def.description = S(fruit_desc) .. " " .. S("(Leaves)") .. " 3"
	leaves_def.tiles = { leaves_png .. "^[colorize:yellow:50" }
	leaves_def.drop = {}
	minetest.register_node(lib.modname.. ":".. fruit_name .."_leaves_3",  table.copy(leaves_def) )
end
