181 lines
5.8 KiB
Lua
181 lines
5.8 KiB
Lua
local lightSets = {}
|
|
local playerAllowed = false
|
|
|
|
-- ============================================================
|
|
-- REQUEST PERMISSION IF ACL IS ENABLED
|
|
-- ============================================================
|
|
CreateThread(function()
|
|
if Config.ACL.UseACL then
|
|
TriggerServerEvent("weigh_lights:checkPermission")
|
|
else
|
|
playerAllowed = true -- bypass permission
|
|
end
|
|
end)
|
|
|
|
RegisterNetEvent("weigh_lights:permissionResult", function(allowed)
|
|
playerAllowed = allowed
|
|
end)
|
|
|
|
-- ============================================================
|
|
-- LOAD CONFIGURED AMBER LIGHT SETS
|
|
-- ============================================================
|
|
CreateThread(function()
|
|
for _, cfg in ipairs(Config.RoadSigns.LightSets) do
|
|
table.insert(lightSets, {
|
|
name = cfg.name,
|
|
model = cfg.model,
|
|
coordsA = cfg.coordsA,
|
|
coordsB = cfg.coordsB,
|
|
rotA = cfg.rotA or vector3(0.0, 0.0, 0.0),
|
|
rotB = cfg.rotB or vector3(0.0, 0.0, 0.0),
|
|
control = cfg.control,
|
|
distance = cfg.distance or 2.0,
|
|
propA = nil,
|
|
propB = nil,
|
|
flashing = false,
|
|
pointCreated = false
|
|
})
|
|
end
|
|
end)
|
|
|
|
-- ============================================================
|
|
-- REQUEST SYNC EVERY 10 SECONDS
|
|
-- ============================================================
|
|
CreateThread(function()
|
|
while true do
|
|
TriggerServerEvent("weigh_lights:requestSync")
|
|
Wait(10000)
|
|
end
|
|
end)
|
|
|
|
-- ============================================================
|
|
-- SYNC ALL STATES
|
|
-- ============================================================
|
|
RegisterNetEvent("weigh_lights:syncAll", function(states)
|
|
for _, set in ipairs(lightSets) do
|
|
if states[set.name] ~= nil then
|
|
local desired = states[set.name]
|
|
|
|
if desired and not set.flashing then
|
|
StartFlashing(set)
|
|
set.flashing = true
|
|
elseif not desired and set.flashing then
|
|
StopFlashing(set)
|
|
set.flashing = false
|
|
end
|
|
end
|
|
end
|
|
end)
|
|
|
|
-- ============================================================
|
|
-- SYNC A SINGLE SET
|
|
-- ============================================================
|
|
RegisterNetEvent("weigh_lights:syncSingle", function(setName, state)
|
|
for _, set in ipairs(lightSets) do
|
|
if set.name == setName then
|
|
if state and not set.flashing then
|
|
StartFlashing(set)
|
|
set.flashing = true
|
|
elseif not state and set.flashing then
|
|
StopFlashing(set)
|
|
set.flashing = false
|
|
end
|
|
break
|
|
end
|
|
end
|
|
end)
|
|
|
|
-- ============================================================
|
|
-- FLASHING LOGIC
|
|
-- ============================================================
|
|
function StartFlashing(set)
|
|
if DoesEntityExist(set.propA) then DeleteEntity(set.propA) end
|
|
if DoesEntityExist(set.propB) then DeleteEntity(set.propB) end
|
|
|
|
if not HasModelLoaded(set.model) then
|
|
RequestModel(set.model)
|
|
while not HasModelLoaded(set.model) do Wait(10) end
|
|
end
|
|
|
|
local zOffset = -0.09
|
|
local posA = set.coordsA + vector3(0.0, 0.0, zOffset)
|
|
local posB = set.coordsB + vector3(0.0, 0.0, zOffset)
|
|
|
|
set.propA = CreateObject(set.model, posA.x, posA.y, posA.z, false, false, false)
|
|
set.propB = CreateObject(set.model, posB.x, posB.y, posB.z, false, false, false)
|
|
|
|
SetEntityRotation(set.propA, set.rotA.x, set.rotA.y, set.rotA.z, 2, true)
|
|
SetEntityRotation(set.propB, set.rotB.x, set.rotB.y, set.rotB.z, 2, true)
|
|
|
|
SetEntityVisible(set.propA, false, false)
|
|
SetEntityVisible(set.propB, false, false)
|
|
|
|
SetEntityAsMissionEntity(set.propA, true, true)
|
|
SetEntityAsMissionEntity(set.propB, true, true)
|
|
|
|
CreateThread(function()
|
|
local toggle = true
|
|
while set.flashing do
|
|
if toggle then
|
|
SetEntityVisible(set.propA, true, false)
|
|
SetEntityVisible(set.propB, false, false)
|
|
else
|
|
SetEntityVisible(set.propA, false, false)
|
|
SetEntityVisible(set.propB, true, false)
|
|
end
|
|
toggle = not toggle
|
|
Wait(500)
|
|
end
|
|
|
|
if DoesEntityExist(set.propA) then DeleteEntity(set.propA) end
|
|
if DoesEntityExist(set.propB) then DeleteEntity(set.propB) end
|
|
set.propA = nil
|
|
set.propB = nil
|
|
end)
|
|
end
|
|
|
|
function StopFlashing(set)
|
|
set.flashing = false
|
|
end
|
|
|
|
-- ============================================================
|
|
-- OX_LIB E-KEY INTERACTION (with ACL toggle)
|
|
-- ============================================================
|
|
CreateThread(function()
|
|
while #lightSets == 0 do Wait(100) end
|
|
|
|
-- wait until permission is determined
|
|
if Config.ACL.UseACL then
|
|
while playerAllowed == false do Wait(100) end
|
|
end
|
|
|
|
for _, set in ipairs(lightSets) do
|
|
if set.control and not set.pointCreated then
|
|
local point = lib.points.new(set.control, set.distance)
|
|
set.pointCreated = true
|
|
|
|
function point:nearby()
|
|
-- bypass or check permission
|
|
if Config.ACL.UseACL and not playerAllowed then
|
|
lib.hideTextUI()
|
|
return
|
|
end
|
|
|
|
if self.currentDistance < set.distance then
|
|
lib.showTextUI("[E] Toggle Amber Light: " .. set.name)
|
|
|
|
if IsControlJustPressed(0, 38) then
|
|
TriggerServerEvent("weigh_lights:toggleLight", set.name, not set.flashing)
|
|
end
|
|
else
|
|
lib.hideTextUI()
|
|
end
|
|
end
|
|
|
|
function point:onExit()
|
|
lib.hideTextUI()
|
|
end
|
|
end
|
|
end
|
|
end)
|