Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 56 additions & 6 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ local function startTargeting()
end
end


if newOptions then
if hasTarget == 1 and (totalOptions - hidden) > 1 then
hasTarget = true
Expand All @@ -312,10 +313,45 @@ local function startTargeting()
})
end

-- Filter options with showIf (only when showing the menu)
local function filterShowIf(tbl)
if not tbl then return {} end
local filtered = {}
for i = 1, #tbl do
local v = tbl[i]
if type(v) == 'table' then
-- Assign a unique id if not present
if not v.id then
v.id = v.name or (v.label .. '_' .. tostring(i))
end
local include = true
if v.showIf then
local ok, res = pcall(v.showIf, currentTarget.entity, currentTarget.distance, currentTarget.coords, v.name)
if not ok or not res then include = false end
end
if include then
table.insert(filtered, v)
end
end
end
return filtered
end

local filteredOptions = {}
for k, v in pairs(options) do
filteredOptions[k] = filterShowIf(v)
end

local filteredZones = {}
for i = 1, #zones do
filteredZones[i] = filterShowIf(zones[i])
end

-- Send filtered options with their ids to NUI
SendNuiMessage(json.encode({
event = 'setTarget',
options = options,
zones = zones,
options = filteredOptions,
zones = filteredZones,
}, { sort_keys = true }))
end

Expand Down Expand Up @@ -406,9 +442,23 @@ RegisterNUICallback('select', function(data, cb)
cb(1)

local zone = data[3] and nearbyZones[data[3]]
local optionId = data[4] -- expects NUI to send the option id as the 4th element

local function findOptionById(tbl, id)
for i = 1, #tbl do
if tbl[i] and tbl[i].id == id then
return tbl[i]
end
end
end

---@type OxTargetOption?
local option = zone and zone.options[data[2]] or options[data[1]][data[2]]
local option = nil
if zone then
option = findOptionById(zone.options, optionId)
else
option = findOptionById(options[data[1]], optionId)
end

if option then
if option.openMenu then
Expand All @@ -433,12 +483,12 @@ RegisterNUICallback('select', function(data, cb)
state.setNuiFocus(false)
end

currentTarget.zone = zone?.id
currentTarget.zone = zone and zone.id or nil

if option.onSelect then
option.onSelect(option.qtarget and currentTarget.entity or getResponse(option))
elseif option.export then
exports[option.resource or zone.resource][option.export](nil, getResponse(option))
exports[option.resource or (zone and zone.resource)][option.export](nil, getResponse(option))
elseif option.event then
TriggerEvent(option.event, getResponse(option))
elseif option.serverEvent then
Expand All @@ -450,7 +500,7 @@ RegisterNUICallback('select', function(data, cb)
if option.menuName == 'home' then return end
end

if not option?.openMenu and IsNuiFocused() then
if not (option and option.openMenu) and IsNuiFocused() then
state.setActive(false)
end
end)
6 changes: 4 additions & 2 deletions web/js/createOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ function onClick() {
// when nuifocus is disabled after a click, the hover event is never released
this.style.pointerEvents = "none";

fetchNui("select", [this.targetType, this.targetId, this.zoneId]);
// Send the unique id as the 4th element
fetchNui("select", [this.targetType, this.targetId, this.zoneId, this.optionId]);
// is there a better way to handle this? probably
setTimeout(() => (this.style.pointerEvents = "auto"), 100);
}
Expand All @@ -24,7 +25,8 @@ export function createOptions(type, data, id, zoneId) {
option.targetType = type;
option.targetId = id;
option.zoneId = zoneId;
option.optionId = data.id; // Pass the unique id from Lua

option.addEventListener("click", onClick);
optionsWrapper.appendChild(option);
}
}