Skip to content

Commit 81be82f

Browse files
authored
Merge pull request #2 from esx-framework/esx_lib
2 parents 0f4b808 + 79423f1 commit 81be82f

File tree

5 files changed

+310
-104
lines changed

5 files changed

+310
-104
lines changed
Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,28 @@
1-
--All client-side functions outsourced from the Core to the lib will be stored here for compatability, e.g:
1+
--All client-side functions outsourced from the Core to the lib will be stored here for compatability, e.g:
2+
3+
ESX.Game.GetShapeTestResultSync = xLib.Raycast.GetShapeTestResult
4+
ESX.Game.RaycastScreen = xLib.Raycast.FromScreen
5+
ESX.Game.StartRaycasting = xLib.Raycast.Start
6+
---@param raycast table The raycast object returned from ESX.Game.StartRaycasting
7+
ESX.Game.StopRaycasting = function(raycast)
8+
if raycast and raycast.active then
9+
raycast:Stop()
10+
end
11+
end
12+
---@param raycast table The raycast object returned from ESX.Game.StartRaycasting
13+
ESX.Game.IsRaycastActive = function(raycast)
14+
if raycast and raycast.active then
15+
return raycast:IsActive()
16+
end
17+
return false
18+
end
19+
---@param raycast table The raycast object returned from ESX.Game.StartRaycasting
20+
ESX.Game.GetRaycastResult = function(raycast)
21+
if raycast and raycast.active then
22+
return raycast.result
23+
end
24+
return nil
25+
end
26+
ESX.Game.GetClosestEntity = xLib.entity.closest
27+
EnumerateEntitiesWithinDistance = xLib.entity.EnumerateWithinDistance
28+
ESX.Game.Teleport = xLib.entity.Teleport

[core]/es_extended/client/functions.lua

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -473,26 +473,6 @@ function ESX.Game.GetPedMugshot(ped, transparent)
473473
return mugshot, GetPedheadshotTxdString(mugshot)
474474
end
475475

476-
---@param entity integer The entity to get the coords of
477-
---@param coords table | vector3 | vector4 The coords to teleport the entity to
478-
---@param cb? function The callback function
479-
function ESX.Game.Teleport(entity, coords, cb)
480-
481-
if DoesEntityExist(entity) then
482-
RequestCollisionAtCoord(coords.x, coords.y, coords.z)
483-
while not HasCollisionLoadedAroundEntity(entity) do
484-
Wait(0)
485-
end
486-
487-
SetEntityCoords(entity, coords.x, coords.y, coords.z, false, false, false, false)
488-
SetEntityHeading(entity, coords.w or coords.heading or 0.0)
489-
end
490-
491-
if cb then
492-
cb()
493-
end
494-
end
495-
496476
---@param object integer | string The object to spawn
497477
---@param coords table | vector3 The coords to spawn the object at
498478
---@param cb? function The callback function
@@ -689,32 +669,6 @@ function ESX.Game.GetClosestVehicle(coords, modelFilter)
689669
return ESX.Game.GetClosestEntity(ESX.Game.GetVehicles(), false, coords, modelFilter)
690670
end
691671

692-
---@param entities table The entities to search through
693-
---@param isPlayerEntities boolean Whether the entities are players
694-
---@param coords table | vector3 The coords to search from
695-
---@param maxDistance number The max distance to search within
696-
---@return table
697-
local function EnumerateEntitiesWithinDistance(entities, isPlayerEntities, coords, maxDistance)
698-
local nearbyEntities = {}
699-
700-
if coords then
701-
coords = vector3(coords.x, coords.y, coords.z)
702-
else
703-
local playerPed = ESX.PlayerData.ped
704-
coords = GetEntityCoords(playerPed)
705-
end
706-
707-
for k, entity in pairs(entities) do
708-
local distance = #(coords - GetEntityCoords(entity))
709-
710-
if distance <= maxDistance then
711-
nearbyEntities[#nearbyEntities + 1] = isPlayerEntities and k or entity
712-
end
713-
end
714-
715-
return nearbyEntities
716-
end
717-
718672
---@param coords table | vector3 The coords to search from
719673
---@param maxDistance number The max distance to search within
720674
---@return table
@@ -736,63 +690,6 @@ function ESX.Game.IsSpawnPointClear(coords, maxDistance)
736690
return #ESX.Game.GetVehiclesInArea(coords, maxDistance) == 0
737691
end
738692

739-
---@param shape integer The shape to get the test result from
740-
---@return boolean, table, table, integer, integer
741-
function ESX.Game.GetShapeTestResultSync(shape)
742-
local handle, hit, coords, normal, material, entity
743-
repeat
744-
handle, hit, coords, normal, material, entity = GetShapeTestResultIncludingMaterial(shape)
745-
Wait(0)
746-
until handle ~= 1
747-
return hit, coords, normal, material, entity
748-
end
749-
750-
---@param depth number The depth to raycast
751-
---@vararg any The arguments to pass to the shape test
752-
---@return table, boolean, table, table, integer, integer
753-
function ESX.Game.RaycastScreen(depth, ...)
754-
local world, normal = GetWorldCoordFromScreenCoord(.5, .5)
755-
local origin = world + normal
756-
local target = world + normal * depth
757-
return target, ESX.Game.GetShapeTestResultSync(StartShapeTestLosProbe(origin.x, origin.y, origin.z, target.x, target.y, target.z, ...))
758-
end
759-
760-
---@param entities table The entities to search through
761-
---@param isPlayerEntities boolean Whether the entities are players
762-
---@param coords? table | vector3 The coords to search from
763-
---@param modelFilter? table The model filter
764-
---@return integer, integer
765-
function ESX.Game.GetClosestEntity(entities, isPlayerEntities, coords, modelFilter)
766-
local closestEntity, closestEntityDistance, filteredEntities = -1, -1, nil
767-
768-
if coords then
769-
coords = vector3(coords.x, coords.y, coords.z)
770-
else
771-
local playerPed = ESX.PlayerData.ped
772-
coords = GetEntityCoords(playerPed)
773-
end
774-
775-
if modelFilter then
776-
filteredEntities = {}
777-
778-
for currentEntityIndex = 1, #entities do
779-
if modelFilter[GetEntityModel(entities[currentEntityIndex])] then
780-
filteredEntities[#filteredEntities + 1] = entities[currentEntityIndex]
781-
end
782-
end
783-
end
784-
785-
for k, entity in pairs(filteredEntities or entities) do
786-
local distance = #(coords - GetEntityCoords(entity))
787-
788-
if closestEntityDistance == -1 or distance < closestEntityDistance then
789-
closestEntity, closestEntityDistance = isPlayerEntities and k or entity, distance
790-
end
791-
end
792-
793-
return closestEntity, closestEntityDistance
794-
end
795-
796693
---@return integer | nil, vector3 | nil
797694
function ESX.Game.GetVehicleInDirection()
798695
local _, hit, coords, _, _, entity = ESX.Game.RaycastScreen(5, 10, ESX.PlayerData.ped)
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
--[[
2+
https://github.com/overextended/ox_lib
3+
4+
This file is licensed under LGPL-3.0 or higher <https://www.gnu.org/licenses/lgpl-3.0.en.html>
5+
6+
Copyright © 2025 Linden <https://github.com/thelindat>
7+
]]
8+
9+
---@class DuiProperties
10+
---@field url string
11+
---@field width number
12+
---@field height number
13+
---@field debug? boolean
14+
15+
---@class Dui
16+
---@field private_id string
17+
---@field private_debug boolean
18+
---@field url string
19+
---@field duiObject number
20+
---@field duiHandle string
21+
---@field runtimeTxd number
22+
---@field txdObject number
23+
---@field dictName string
24+
---@field txtName string
25+
xLib.dui = xLib.class()
26+
27+
local resource<const> = GetCurrentResourceName()
28+
29+
---@type table<string, Dui>
30+
local Duis = {}
31+
32+
local currentId = 0
33+
34+
---@param data DuiProperties
35+
function xLib.dui:constructor(data)
36+
local time = GetGameTimer()
37+
local id = ("%s_%s_%s"):format(resource, time, currentId)
38+
currentId = currentId + 1
39+
local dictName = ("%s_dui_dict_%s"):format(resource, id)
40+
local txtName = ("%s_lib_dui_txt_%s"):format(resource, id)
41+
local duiObject = CreateDui(data.url, data.width, data.height)
42+
local duiHandle = GetDuiHandle(duiObject)
43+
local runtimeTxd = CreateRuntimeTxd(dictName)
44+
local txdObject = CreateRuntimeTextureFromDuiHandle(runtimeTxd, txtName, duiHandle)
45+
self.private_id = id
46+
self.private_debug = data.debug or false
47+
self.url = data.url
48+
self.duiObject = duiObject
49+
self.duiHandle = duiHandle
50+
self.runtimeTxd = runtimeTxd
51+
self.txdObject = txdObject
52+
self.dictName = dictName
53+
self.txtName = txtName
54+
Duis[id] = self
55+
56+
if self.private_debug then
57+
print(("Dui %s created"):format(id))
58+
end
59+
end
60+
61+
function xLib.dui:remove()
62+
SetDuiUrl(self.duiObject, "about:blank")
63+
DestroyDui(self.duiObject)
64+
Duis[self.private_id] = nil
65+
66+
if self.private_debug then
67+
print(("Dui %s removed"):format(self.private_id))
68+
end
69+
end
70+
71+
---@param url string
72+
function xLib.dui:setUrl(url)
73+
self.url = url
74+
SetDuiUrl(self.duiObject, url)
75+
76+
if self.private_debug then
77+
print(("Dui %s url set to %s"):format(self.private_id, url))
78+
end
79+
end
80+
81+
---@param message table
82+
function xLib.dui:sendMessage(message)
83+
SendDuiMessage(self.duiObject, json.encode(message))
84+
85+
if self.private_debug then
86+
print(("Dui %s message sent with data :"):format(self.private_id), json.encode(message, { indent = true }))
87+
end
88+
end
89+
90+
---@param x number
91+
---@param y number
92+
function xLib.dui:sendMouseMove(x, y)
93+
SendDuiMouseMove(self.duiObject, x, y)
94+
end
95+
96+
---@param button "left" | "middle" | "right"
97+
function xLib.dui:sendMouseDown(button)
98+
SendDuiMouseDown(self.duiObject, button)
99+
end
100+
101+
---@param button "left" | "middle" | "right"
102+
function xLib.dui:sendMouseUp(button)
103+
SendDuiMouseUp(self.duiObject, button)
104+
end
105+
106+
---@param deltaX number
107+
---@param deltaY number
108+
function xLib.dui:sendMouseWheel(deltaX, deltaY)
109+
SendDuiMouseWheel(self.duiObject, deltaY, deltaX)
110+
end
111+
112+
AddEventHandler("onResourceStop", function(resourceName)
113+
if resource ~= resourceName then return end
114+
115+
for id in next, Duis do
116+
Duis[id]:remove()
117+
end
118+
end)
119+
120+
return xLib.dui
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
xLib.entity = {}
2+
3+
---@param entities table The entities to search through
4+
---@param isPlayerEntities boolean Whether the entities are players
5+
---@param coords? table | vector3 The coords to search from
6+
---@param modelFilter? table The model filter
7+
---@return integer, integer
8+
function xLib.entity.closest(entities, isPlayerEntities, coords, modelFilter)
9+
local closestEntity, closestEntityDistance, filteredEntities = -1, -1, nil
10+
11+
if coords then
12+
coords = vector3(coords.x, coords.y, coords.z)
13+
else
14+
local playerPed = PlayerPedId()
15+
coords = GetEntityCoords(playerPed)
16+
end
17+
18+
if modelFilter then
19+
filteredEntities = {}
20+
21+
for currentEntityIndex = 1, #entities do
22+
if modelFilter[GetEntityModel(entities[currentEntityIndex])] then
23+
filteredEntities[#filteredEntities + 1] = entities[currentEntityIndex]
24+
end
25+
end
26+
end
27+
28+
for k, entity in pairs(filteredEntities or entities) do
29+
local distance = #(coords - GetEntityCoords(entity))
30+
31+
if closestEntityDistance == -1 or distance < closestEntityDistance then
32+
closestEntity, closestEntityDistance = isPlayerEntities and k or entity, distance
33+
end
34+
end
35+
36+
return closestEntity, closestEntityDistance
37+
end
38+
39+
---@param entities table The entities to search through
40+
---@param isPlayerEntities boolean Whether the entities are players
41+
---@param coords? table | vector3 The coords to search from
42+
---@param maxDistance number The maximum distance
43+
---@return table
44+
function xLib.entity.EnumerateWithinDistance(entities, isPlayerEntities, coords, maxDistance)
45+
local nearbyEntities = {}
46+
47+
if coords then
48+
coords = vector3(coords.x, coords.y, coords.z)
49+
else
50+
local playerPed = PlayerPedId()
51+
coords = GetEntityCoords(playerPed)
52+
end
53+
54+
for k, entity in pairs(entities) do
55+
local distance = #(coords - GetEntityCoords(entity))
56+
57+
if distance <= maxDistance then
58+
nearbyEntities[#nearbyEntities + 1] = isPlayerEntities and k or entity
59+
end
60+
end
61+
62+
return nearbyEntities
63+
end
64+
65+
---@param entity integer The entity to get the coords of
66+
---@param coords table | vector3 | vector4 The coords to teleport the entity to
67+
---@param cb? function The callback function
68+
function xLib.entity.Teleport(entity, coords, cb)
69+
70+
if DoesEntityExist(entity) then
71+
RequestCollisionAtCoord(coords.x, coords.y, coords.z)
72+
while not HasCollisionLoadedAroundEntity(entity) do
73+
Wait(0)
74+
end
75+
76+
SetEntityCoords(entity, coords.x, coords.y, coords.z, false, false, false, false)
77+
SetEntityHeading(entity, coords.w or coords.heading or 0.0)
78+
end
79+
80+
if cb then
81+
cb()
82+
end
83+
end
84+
85+
return xLib.entity

0 commit comments

Comments
 (0)