
local S = mobs.intllib

-- Bee by KrupnoPavel (.b3d model by sirrobzeroone)

minetest.register_craftitem("mob_bee:honey", {
	description="Honey",
	inventory_image = "mobs_honey_inv.png^[transformFX",
	on_use=minetest.item_eat(5),
	groups={heal=5}
})

-- Sorry for the misnomer, I changed my mind (again)
minetest.register_craftitem("mob_bee:honey_cake", {
	description="Caramel apple.",
	inventory_image = "caramel_apple.png",
	on_use=minetest.item_eat(7),
	groups={heal=7}
})

minetest.register_craft {
	output="mob_bee:honey_cake",
	type="shapeless",
	recipe={ "mob_bee:honey", "default:apple" },
}

-- beehive (when placed spawns bee)
minetest.register_node(":mobs:beehive", {
	description = "Artificial Hive.\nProduces honey.\nPlace a butterfly egg (catch a butterfly with a net) inside.\nPlace some flowers next to it.",
	tiles = {"xdecor_hive_top.png", "xdecor_hive_top.png",
		 "xdecor_hive_side.png", "xdecor_hive_side.png",
		 "xdecor_hive_side.png", "xdecor_hive_front.png"},
	paramtype = "light",
	groups = {oddly_breakable_by_hand = 2, flammable = 1, protected=1, nopicker=1},
	sounds = default.node_sound_defaults(),

	paramtype2="facedir",
	on_place=minetest.rotate_node,

	on_construct = function(pos)

		local meta = minetest.get_meta(pos)

		meta:set_string("formspec", "size[8,6]"
			..default.gui_bg..default.gui_bg_img..default.gui_slots
--			.. "image[3,0.8;0.8,0.8;mobs_bee_inv.png]"
			.. "list[context;beehive;4,0.5;1,1;]"
			.. "list[current_player;main;0,2.35;8,4;]"
			.. "listring[]")

		meta:get_inventory():set_size("beehive", 1)
	end,

	after_place_node = function(pos, placer, itemstack)
		local meta = minetest.get_meta(pos)
		if placer and placer:is_player() then
			minetest.set_node(pos, {name = "mobs:beehive", param2 = 1})
			meta:set_string("owner", placer:get_player_name() or "")
			meta:set_string("infotext", "Owned by ".. meta:get_string("owner"))
		end
	end,

	allow_metadata_inventory_put = function(pos, listname, index, stack, player)
		if not default.can_interact_with_node(player, pos) then return 0 end
		if listname == "beehive" and stack:get_name()~= "mobs_butterfly:butterfly_set" then
			return 0
		end
		return 1
	end,

	allow_metadata_inventory_take = function(pos, listname, index, stack, player)
		if not default.can_interact_with_node(player, pos) then
			return 0
		end
		return 2
	end,

	can_dig = function(pos,player)

		local meta = minetest.get_meta(pos)

		-- only dig beehive if no honey inside
		return meta:get_inventory():is_empty("beehive")
	end,

})

minetest.register_craft({
	output = "mobs:beehive",
	type="shapeless",
	recipe = {
		"default:chest_locked","mobs_butterfly:butterfly_set",
	}
})

-- beehive workings
minetest.register_abm({
	nodenames = {"mobs:beehive"},
	interval = 1801, -- a prime for 30 minutes.
	chance = 2,
	catch_up = true,
	action = function(pos, node)

		-- is hive full?
		local meta = minetest.get_meta(pos)
		if not meta then return end -- for older beehives
		local inv = meta:get_inventory()
		local item = inv:get_stack("beehive", 1)
		
		if item:get_name() ~= "mobs_butterfly:butterfly_set" then return end
		
		inv:remove_item("beehive", "mobs_butterfly:butterfly_set")
		inv:add_item("beehive", "mob_bee:honey")
	end
})
