-- LUALOCALS < ---------------------------------------------------------
local klots, minetest, pairs
    = klots, minetest, pairs
-- LUALOCALS > ---------------------------------------------------------

if klots.editmode then
	minetest.register_tool(":", {
			wield_image = "wieldhand.png",
			on_use = function(_, _, pointed)
				if pointed and pointed.under then
					minetest.remove_node(pointed.under)
				end
			end,
			on_place = function(_, user, pointed)
				if not (user and pointed.under) then return end
				local item = minetest.get_node(pointed.under).name
				local inv = user:get_inventory()
				if inv:contains_item("main", item, false) then return end
				user:get_inventory():add_item("main", item)
			end
		})
else
	local interact = function(player, pointed)
		if not (pointed and pointed.under) then return end
		local node = minetest.get_node(pointed.under)
		local def = minetest.registered_nodes[node.name]
		if not def then return end

		local sndspec = def.sounds and (def.sounds.footstep
			or def.sounds.dig or def.sounds.dug)
		if sndspec then
			local pc = klots.player_control_get(player).param
			if (not pc) or (pc.moveaccel and pc.moveaccel > 0) then
				local t = {}
				for k, v in pairs(sndspec) do t[k] = v end
				t.pos = pointed.under
				minetest.sound_play(t.name, t)
			end
		end

		if def and def.on_actuate then
			def.on_actuate(pointed.under, node, pointed)
		end
	end
	minetest.register_tool(":", {
			wield_image = "wieldhand.png",
			on_use = function(_, player, pointed)
				return interact(player, pointed)
			end,
			on_place = function(_, player, pointed)
				return interact(player, pointed)
			end
		})
end
