cfg_functions.lua

local Locales = require("config.cfg_locales")

--- Sends a notification to the client.
--- @param data {title: string, description: string, type: string, duration: number, position?: string}
local function ClientNotify(data)
	lib.notify({
		title = data.title,
		description = data.description,
		type = data.type,
		duration = data.duration,

		position = data.position or "top-left",
		style = { backgroundColor = "#1E1E1E" },
	})
end

--- Sends a notification from the server to a specific client.
--- @param source number Player source ID
--- @param data {title: string, description: string, type: string, duration: number, position?: string}
local function ServerNotify(source, data)
	TriggerClientEvent("ox_lib:notify", source, {
		title = data.title,
		description = data.description,
		type = data.type,
		duration = data.duration,

		position = data.position or "top-left",
		style = { backgroundColor = "#1E1E1E" },
	})
end

--- Sends an announcement notification to the client.
--- @param data {title: string, description: string, type: string, duration: number, position?: string}
local function ClientAnnounce(data)
	lib.notify({
		title = data.title,
		description = data.description,
		type = data.type,
		duration = data.duration,

		position = data.position or "top-left",
		style = { backgroundColor = "#1E1E1E" },
	})
end

--- Toggles the HUD visibility.
--- @param state boolean Whether to enable or disable the HUD
local function ToggleHud(state)
	DisplayHud(state)
	DisplayRadar(state)
end

--- Displays a help text.
--- @param msg string The message to display
local function HelpText(msg)
	AddTextEntry("textUI", msg)
	DisplayHelpTextThisFrame("textUI", false)
end

--- Displays floating help text near an entity or specific coordinates.
--- @param entity? number The entity ID (optional)
--- @param coords? vector3 The coordinates (required if entity is nil)
--- @param msg string The message to display
local function FloatingHelpText(entity, coords, msg)
	AddTextEntry("FloatingHelpText", msg)
	SetFloatingHelpTextStyle(1, 1, 2, -1, 3, 0)
	BeginTextCommandDisplayHelp("FloatingHelpText")
	EndTextCommandDisplayHelp(2, false, false, -1)

	if entity then
		local entityCoords = GetEntityCoords(entity)
		SetFloatingHelpTextWorldPosition(1, entityCoords.x, entityCoords.y, entityCoords.z + 0.85)
	elseif coords then
		SetFloatingHelpTextWorldPosition(1, coords.x, coords.y, coords.z)
	end
end

--- Creates a target zone for interacting with a garage.
--- @param location string The garage location key
--- @param data {Coords: vector4, Interaction: {Target: {BoxZoneSize: vector3, DrawSprite: boolean, Distance: number}}}
local function AddGarageTarget(location, data)
	exports.ox_target:addBoxZone({
		coords = data.Coords,
		size = data.Interaction.Target.BoxZoneSize,
		drawSprite = data.Interaction.Target.DrawSprite,
		options = {
			icon = Locales.Menu.Garage.Target.Icon,
			label = Locales.Menu.Garage.Target.Label,
			onSelect = function()
				Interaction.OpenMenu(location)
			end,
			distance = data.Interaction.Target.Distance,
			canInteract = function()
				return not IsPedInAnyVehicle(cache.ped, false)
			end,
		},
	})
end

--- Creates a global target for vehicle interactions.
--- @param options table Options for the vehicle target
local function AddVehicleTarget(options)
	exports.ox_target:addGlobalVehicle(options)
end

--- Assigns vehicle keys to the player.
--- @param vehicle number The vehicle entity ID
--- @param vehPlate string The vehicle plate number
local function GiveVehicleKeys(vehicle, vehPlate)
	Entity(vehicle).state.owner = cache.serverId
end

--- Sets vehicle fuel of the specified vehicle
--- @param vehicle number The vehicle entity ID
--- @param vehFuel number The new vehicle fuel level to set
local function SetVehicleFuel(vehicle, vehFuel)
	Entity(vehicle).state.fuel = vehFuel
end

return {
	Notify = {
		Client = ClientNotify,
		Server = ServerNotify,
	},
	Announce = {
		Client = ClientAnnounce,
	},
	Interact = {
		ToggleHud = ToggleHud,
		HelpText = HelpText,
		FloatingHelpText = FloatingHelpText,
		Target = {
			Garage = AddGarageTarget,
			Vehicle = AddVehicleTarget,
		},
	},
	Vehicle = {
		GiveKeys = GiveVehicleKeys,
		SetFuel = SetVehicleFuel,
	},
}

Last updated