--my first mod!!!! if there is any messy or weird code DO send suggestions i want this to be better

local get_connected_players = minetest.get_connected_players
local hurtwait = {}
local floor = math.floor

local function blockwithinrange(xs, ys, zs, xe, ye, ze, name)
	for x = floor(xs), floor(xe) do
		for y = floor(ys), floor(ye) do
			for z = floor(zs), floor(ze) do
				if minetest.get_node({x=x, y=y, z=z}).name == name then
					return true
				end
			end
		end
	end
	return false
end

local function blocktouching(player, name)
	local pos = player:get_pos()
	local spdy = player:get_velocity().y
	-- only check for cactus above the player if the player is jumping
	local yoff = 0.0
	if spdy ~= 0.0 then
	    yoff = 0.4
	end
	return blockwithinrange(pos.x + 0.15, pos.y + 0.2, pos.z + 0.15, pos.x + 0.85, pos.y + 2 + yoff, pos.z + 0.85, name)
end

minetest.register_globalstep(function(dtime)
	for _, player in pairs(get_connected_players()) do
		local pname = player:get_player_name()
		if not hurtwait[pname] then
			if blocktouching(player, "default:cactus") then
				player:set_hp(player:get_hp() - 1, {type = "node_damage"})
				hurtwait[pname] = true
				minetest.after(0.5, function() hurtwait[pname] = nil end)
			end
		end
	end
end)

--[[minetest.register_chatcommand("pos", {
	params = "",
	description = "Position",
	privs = { server = true },
	func = function(name, param)
		minetest.chat_send_player(name, minetest.colorize("#ffffff", minetest.get_player_by_name(name):get_pos()))
	end,
})]]
