-- core.register_on_mods_loaded(function()
	-- some mods clear this craft by its recipe
	core.clear_craft({ output = 'sleeping_mat:mat' })
	core.register_craft({
		output = "sleeping_mat:mat",
		type = "shapeless",
		recipe = {
			"group:grass", "group:grass", "group:grass",
			"group:grass", "group:grass", "group:grass"
		},
	})
-- end)

local bucket_bucket_empty = "bucket:bucket_empty"
local wooden_bucket_empty = "wooden_bucket:bucket_wood_empty"

if core.global_exists('bucket') then
	local groups = table.copy(core.registered_items[bucket_bucket_empty].groups)
	groups['bucket_empty'] = 1
	core.override_item(bucket_bucket_empty, {
		groups = groups
	})
end

if core.registered_items[wooden_bucket_empty] ~= nil then
	local groups = table.copy(core.registered_items[wooden_bucket_empty].groups)
	groups['bucket_empty'] = 1
	groups['tool'] = 1
	core.override_item(wooden_bucket_empty, {
		groups = groups
	})
end

if core.global_exists('animalia') then
	local campfire_cookable = {
		"animalia:song_bird_egg", "animalia:chicken_egg", "animalia:turkey_egg",
		'animalia:poultry_raw', "animalia:venison_raw", "animalia:porkchop_raw",
		"animalia:rat_raw", "animalia:mutton_raw", "animalia:beef_raw",
	}
	for _, item in ipairs(campfire_cookable) do
		local groups = core.registered_items[item].groups
		groups.campfire_cookable = 1
		core.override_item(item, {
			groups = groups
		})
	end
end

if core.global_exists('charcoal') then
	core.override_item('charcoal:charcoal', {
		groups = { coal = 1, flammable = 1 }
	})
end

if core.registered_items['pigiron:charcoal_lump'] and core.global_exists('charcoal') then
	core.clear_craft({ output = 'pigiron:charcoal_lump' })
	core.clear_craft({
		recipe = {
			{ "pigiron:charcoal_lump" },
			{ "group:stick" }
		}
	})
	core.register_alias_force('pigiron:charcoal_lump', 'charcoal:charcoal')

	core.override_item('pigiron:iron_charcoal_mix', {
		inventory_image = "pigiron_iron_ingot.png^charcoal_charcoal.png"
	})
end

-- core.register_on_mods_loaded(function()
-- 	if core.global_exists('beds') then
-- 		if core.global_exists('emote') then
-- 			for node_name, def in pairs(core.registered_nodes) do
-- 				if def.groups and def.groups.bed then
-- 					core.override_item(node_name, {
-- 						on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
-- 							emote.start(clicker, 'lay')
-- 							def.on_rightclick(pos, node, clicker, itemstack, pointed_thing)
-- 							emote.stop(clicker)
-- 							return pointed_thing
-- 						end
-- 					})
-- 				end
-- 			end
-- 		end
-- 	end
-- end)

if core.global_exists('openion_yeast') then
	local bottle = core.registered_items["openion_yeast:bottle_of_yeast"]
	core.register_node(":openion_yeast:bottle_of_yeast", {
		description = bottle.description,
		drawtype = "plantlike",
		tiles = { bottle.inventory_image },
		inventory_image = bottle.inventory_image,
		wield_image = bottle.inventory_image,
		paramtype = "light",
		is_ground_content = false,
		walkable = false,
		selection_box = {
			type = "fixed",
			fixed = { -0.25, -0.5, -0.25, 0.25, 0.3, 0.25 }
		},
		groups = { vessel = 1, dig_immediate = 3, attached_node = 1 },
		sounds = xcompat.sounds.node_sound_glass_defaults(),
	})

	core.clear_craft({ output = "openion_yeast:yeast_barrel" })
	core.register_craft({
		output = "openion_yeast:yeast_barrel",
		type = "shaped",
		recipe = {
			{ "group:wood", "group:bucket_empty", "group:wood" },
			{ "group:wood", "default:steelblock", "group:wood" },
			{ "group:wood", "group:bucket_empty", "group:wood" }
		},
	})

	if core.global_exists('farming') then
		core.clear_craft({
			type = "shapeless",
			recipe = { "farming:wheat", "farming:wheat", "farming:wheat", "farming:wheat" }
		})
		if core.registered_nodes["vessels:glass_bottle"] ~= nil then
			core.register_craft({
				type = "shapeless",
				output = "farming:flour",
				recipe = { "farming:wheat", "farming:wheat", "farming:wheat", "farming:wheat", "openion_yeast:yeast_barrel" },
				replacements = { { "openion_yeast:yeast_barrel", "vessel:glass_bottle" } }
			})
		else
			core.register_craft({
				type = "shapeless",
				output = "farming:flour",
				recipe = { "farming:wheat", "farming:wheat", "farming:wheat", "farming:wheat", "openion_yeast:yeast_barrel" },
			})
		end
	end
end

-- [liquid_pressure]
-- MIT License
-- Copyright (c) 2021 Richard Try

local start_depth = {}
local timer = {}
local timing = 1
local factor = 0.1 -- 1/10, 1 hp per 10 nodes of depth

core.register_globalstep(function(dtime)
    for _, player in ipairs(core:get_connected_players()) do
        local player_pos = player:get_pos()
	    player_pos.y = player_pos.y + player:get_properties().eye_height
	    local node = core.get_node(player_pos)
        if core.get_item_group(node.name, "liquid") > 0 then
            if timer[player] then timer[player] = timer[player] + dtime
            else timer[player] = 0 end
            if timer[player] >= timing then
                if start_depth[player] then
                    local cur_depth = start_depth[player].y - player_pos.y
                    player:set_hp(player:get_hp() - math.max(0, math.floor(factor * cur_depth)))
                else
                    start_depth[player] = player_pos
                end
                timer[player] = 0
            end
        else
            start_depth[player] = nil
            timer[player] = nil
        end
    end
end)
