Skip to content

Commit

Permalink
Merge branch 'dev' into vehicle-spawn-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mycroft-Studios committed Dec 4, 2024
2 parents 0cac6f5 + dc2256a commit 803508b
Show file tree
Hide file tree
Showing 48 changed files with 1,183 additions and 220 deletions.
7 changes: 7 additions & 0 deletions [SQL]/legacy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,13 @@ ALTER TABLE `licenses`
ALTER TABLE `owned_vehicles`
ADD PRIMARY KEY (`plate`);

--
--
-- Indexes for table `vehicles`
--
ALTER TABLE `vehicles`
ADD PRIMARY KEY (`model`);

--
-- Indexes for table `rented_vehicles`
--
Expand Down
60 changes: 42 additions & 18 deletions [core]/cron/server/main.lua
Original file line number Diff line number Diff line change
@@ -1,51 +1,75 @@
local Jobs = {}
local LastTime = nil
---@class CronJob
---@field h number
---@field m number
---@field cb function|table

---@type CronJob[]
local cronJobs = {}
---@type number|false
local lastTimestamp = false

---@param h number
---@param m number
---@param cb function|table
function RunAt(h, m, cb)
Jobs[#Jobs + 1] = {
cronJobs[#cronJobs + 1] = {
h = h,
m = m,
cb = cb,
}
end

---@return number
function GetUnixTimestamp()
return os.time()
end

function OnTime(time)
for i = 1, #Jobs, 1 do
---@param timestamp number
function OnTime(timestamp)
for i = 1, #cronJobs, 1 do
local scheduledTimestamp = os.time({
hour = Jobs[i].h,
min = Jobs[i].m,
hour = cronJobs[i].h,
min = cronJobs[i].m,
sec = 0, -- Assuming tasks run at the start of the minute
day = os.date("%d", time),
month = os.date("%m", time),
year = os.date("%Y", time),
day = os.date("%d", timestamp),
month = os.date("%m", timestamp),
year = os.date("%Y", timestamp),
})

if time >= scheduledTimestamp and (not LastTime or LastTime < scheduledTimestamp) then
if timestamp >= scheduledTimestamp and (not lastTimestamp or lastTimestamp < scheduledTimestamp) then
local d = os.date('*t', scheduledTimestamp).wday
Jobs[i].cb(d, Jobs[i].h, Jobs[i].m)
cronJobs[i].cb(d, cronJobs[i].h, cronJobs[i].m)
end
end
end

---@return nil
function Tick()
local time = GetUnixTimestamp()
local timestamp = GetUnixTimestamp()

if not LastTime or os.date("%M", time) ~= os.date("%M", LastTime) then
OnTime(time)
LastTime = time
if not lastTimestamp or os.date("%M", timestamp) ~= os.date("%M", lastTimestamp) then
OnTime(timestamp)
lastTimestamp = timestamp
end

SetTimeout(60000, Tick)
end

LastTime = GetUnixTimestamp()

lastTimestamp = GetUnixTimestamp()
Tick()

---@param h number
---@param m number
---@param cb function|table
AddEventHandler("cron:runAt", function(h, m, cb)
local invokingResource = GetInvokingResource() or "Unknown"
local typeH = type(h)
local typeM = type(m)
local typeCb = type(cb)

assert(typeH == "number", ("Expected number for h, got %s. Invoking Resource: '%s'"):format(typeH, invokingResource))
assert(typeM == "number", ("Expected number for m, got %s. Invoking Resource: '%s'"):format(typeM, invokingResource))
assert(typeCb == "function" or (typeCb == "table" and type(getmetatable(cb)?.__call) == "function"), ("Expected function for cb, got %s. Invoking Resource: '%s'"):format(typeCb, invokingResource))

RunAt(h, m, cb)
end)
21 changes: 21 additions & 0 deletions [core]/es_extended/client/imports/class.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local class = {}
class.__index = class

function class:new(...)
local instance = setmetatable({}, self)
if instance.constructor then
local ret = instance:constructor(...)
if type(ret) == 'table' then
return ret
end
end
return instance
end

function Class(body, heritage)
local prototype = body or {}
prototype.__index = prototype
return setmetatable(prototype, heritage or class)
end

return Class
44 changes: 44 additions & 0 deletions [core]/es_extended/client/imports/point.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Point = ESX.Class()

function Point:constructor(properties)
self.coords = properties.coords
self.hidden = properties.hidden or false
self.inside = properties.inside or function() end
self.enter = properties.enter or function() end
self.leave = properties.leave or function() end
self.handle = ESX.CreatePointInternal(properties.coords, properties.distance, properties.hidden, function()
self.nearby = true
if self.enter then
self:enter()
end
if self.inside then
CreateThread(function()
while self.nearby do
local coords = GetEntityCoords(ESX.PlayerData.ped)
self.currDistance = #(coords - self.coords)
self:inside()
Wait(0)
end
end)
end
end, function()
self.nearby = false
if self.leave then
self:leave()
end
end)
end

function Point:delete()
ESX.RemovePointInternal(self.handle)
end

function Point:toggle(hidden)
if hidden == nil then
hidden = not self.hidden
end
self.hidden = hidden
ESX.HidePointInternal(self.handle, hidden)
end

return Point
68 changes: 49 additions & 19 deletions [core]/es_extended/client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ RegisterNetEvent("esx:playerLoaded", function(xPlayer, _, skin)
end

Actions:Init()

StartPointsLoop()
StartServerSyncLoops()
NetworkSetLocalPlayerSyncLookAt(true)
end)

local isFirstSpawn = true
Expand Down Expand Up @@ -286,29 +287,58 @@ end

function StartServerSyncLoops()
if Config.CustomInventory then return end
-- keep track of ammo

local currentWeapon = {
---@type number
---@diagnostic disable-next-line: assign-type-mismatch
hash = `WEAPON_UNARMED`,
ammo = 0,
}

local function updateCurrentWeaponAmmo(weaponName)
local newAmmo = GetAmmoInPedWeapon(ESX.PlayerData.ped, currentWeapon.hash)

if newAmmo ~= currentWeapon.ammo then
currentWeapon.ammo = newAmmo
TriggerServerEvent("esx:updateWeaponAmmo", weaponName, newAmmo)
end
end

CreateThread(function()
local currentWeapon = { Ammo = 0 }
while ESX.PlayerLoaded do
local sleep = 1500
if GetSelectedPedWeapon(ESX.PlayerData.ped) ~= -1569615261 then
sleep = 1000
local _, weaponHash = GetCurrentPedWeapon(ESX.PlayerData.ped, true)
local weapon = ESX.GetWeaponFromHash(weaponHash)
if weapon then
local ammoCount = GetAmmoInPedWeapon(ESX.PlayerData.ped, weaponHash)
if weapon.name ~= currentWeapon.name then
currentWeapon.Ammo = ammoCount
currentWeapon.name = weapon.name
else
if ammoCount ~= currentWeapon.Ammo then
currentWeapon.Ammo = ammoCount
TriggerServerEvent("esx:updateWeaponAmmo", weapon.name, ammoCount)
end
currentWeapon.hash = GetSelectedPedWeapon(ESX.PlayerData.ped)

if currentWeapon.hash ~= `WEAPON_UNARMED` then
local weaponConfig = ESX.GetWeaponFromHash(currentWeapon.hash)

if weaponConfig then
currentWeapon.ammo = GetAmmoInPedWeapon(ESX.PlayerData.ped, currentWeapon.hash)

while GetSelectedPedWeapon(ESX.PlayerData.ped) == currentWeapon.hash do
updateCurrentWeaponAmmo(weaponConfig.name)
Wait(1000)
end

updateCurrentWeaponAmmo(weaponConfig.name)
end
end
Wait(sleep)
Wait(250)
end
end)

CreateThread(function()
local PARACHUTE_OPENING <const> = 1
local PARACHUTE_OPEN <const> = 2

while ESX.PlayerLoaded do
local parachuteState = GetPedParachuteState(ESX.PlayerData.ped)

if parachuteState == PARACHUTE_OPENING or parachuteState == PARACHUTE_OPEN then
TriggerServerEvent("esx:updateWeaponAmmo", "GADGET_PARACHUTE", 0)

while GetPedParachuteState(ESX.PlayerData.ped) ~= -1 do Wait(1000) end
end
Wait(500)
end
end)
end
Expand Down
1 change: 0 additions & 1 deletion [core]/es_extended/client/modules/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ function Actions:PedLoop()
end)
end


function Actions:Init()
self:SlowLoop()
self:PedLoop()
Expand Down
11 changes: 11 additions & 0 deletions [core]/es_extended/client/modules/adjustments.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function Adjustments:RemoveHudComponents()
for i = 1, #Config.RemoveHudComponents do
if Config.RemoveHudComponents[i] then
SetHudComponentSize(i, 0.0, 0.0)
SetHudComponentPosition(i, 900, 900)
end
end
end
Expand Down Expand Up @@ -215,6 +216,15 @@ function Adjustments:WantedLevel()
end
end

function Adjustments:DisableRadio()
if Config.RemoveHudComponents[16] then
AddEventHandler("esx:enteredVehicle", function(vehicle, plate, seat, displayName, netId)
SetVehRadioStation(vehicle,"OFF")
SetUserRadioControlEnabled(false)
end)
end
end

function Adjustments:Load()
self:RemoveHudComponents()
self:DisableAimAssist()
Expand All @@ -228,4 +238,5 @@ function Adjustments:Load()
self:LicensePlates()
self:DiscordPresence()
self:WantedLevel()
self:DisableRadio()
end
Loading

0 comments on commit 803508b

Please sign in to comment.