Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

Commit

Permalink
1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zf-development committed Aug 3, 2023
1 parent 40d6588 commit 96dc369
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 28 deletions.
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ lua54 'yes'

author 'ZF Labo'
description 'A library for FiveM developers to make their life easier when using QBCore & ESX.'
version '1.2.0'
version '1.3.0'

dependencies {
'/server:5848',
Expand Down
6 changes: 3 additions & 3 deletions modules/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ cache = {

if GetResourceState('qb-core') == 'started' then
zf.core = 'qb-core'
zf.CoreObject = exports['qb-core']:GetCoreObject()
CoreObject = exports['qb-core']:GetCoreObject()
RegisterNetEvent('QBCore:Client:UpdateObject', function()
zf.CoreObject = exports['qb-core']:GetCoreObject()
CoreObject = exports['qb-core']:GetCoreObject()
end)
elseif GetResourceState('es_extended') == 'started' then
zf.core = 'esx'
zf.CoreObject = ESX
CoreObject = ESX
end

if GetResourceState('ox_inventory') == 'started' then
Expand Down
2 changes: 1 addition & 1 deletion modules/items/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ end
function zf.getItemLabel(itemName)
local itemLabel
if zf.core == 'qb-core' then
itemLabel = zf.CoreObject.Shared.Items[itemName].label
itemLabel = CoreObject.Shared.Items[itemName].label
elseif zf.core == 'esx' then
itemLabel = zf.callback.await('zf:getItemLabel', false, itemName)
end
Expand Down
39 changes: 24 additions & 15 deletions modules/items/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ function zf.hasItem(source, item, amount)
local itemData = ox_inventory:GetItem(source, item, nil, true)
if itemData >= amount then return true end
elseif zf.core == "qb-core" then
local Player = zf.CoreObject.Functions.GetPlayer(source)
local Player = CoreObject.Functions.GetPlayer(source)
if not Player then return false end
local itemData = Player.Functions.GetItemByName(item)
if not itemData then return false end
if itemData.amount >= amount then return true end
elseif zf.core == "esx" then
local Player = zf.CoreObject.GetPlayerFromId(source)
local Player = CoreObject.GetPlayerFromId(source)
local itemName, itemCount = Player.hasItem(item)
if itemCount >= amount then return true end
end
Expand All @@ -21,16 +21,25 @@ end

function zf.giveItem(source, item, amount, metadata)
if zf.inventory == 'ox' then
ox_inventory:AddItem(source, item, amount, metadata)
local added, _ = ox_inventory:AddItem(source, item, amount, metadata)
return added
elseif zf.core == "qb-core" then
local Player = zf.CoreObject.Functions.GetPlayer(source)
local Player = CoreObject.Functions.GetPlayer(source)
if not Player then return end
Player.Functions.AddItem(item, amount, false, metadata or {})
TriggerClientEvent("inventory:client:ItemBox", source, zf.CoreObject.Shared.Items[item], "add", amount)
if Player.Functions.AddItem(item, amount, false, metadata or {}) then
TriggerClientEvent("inventory:client:ItemBox", source, CoreObject.Shared.Items[item], "add", amount)
return true
end
elseif zf.core == "esx" then
local Player = zf.CoreObject.GetPlayerFromId(source)
local Player = CoreObject.GetPlayerFromId(source)
local original_amount = Player.getInventoryItem(item)?.count
Player.addInventoryItem(item, amount, metadata or {})
local new_amount = Player.getInventoryItem(item)?.count
if new_amount >= original_amount + amount then
return true
end
end
return false
end

function zf.removeItem(source, item, amount, metadata)
Expand All @@ -41,14 +50,14 @@ function zf.removeItem(source, item, amount, metadata)
return true
end
elseif zf.core == "qb-core" then
local Player = zf.CoreObject.Functions.GetPlayer(source)
local Player = CoreObject.Functions.GetPlayer(source)
if not Player then return end
if Player.Functions.RemoveItem(item, amount) then
TriggerClientEvent("inventory:client:ItemBox", source, zf.CoreObject.Shared.Items[item], "remove", amount)
TriggerClientEvent("inventory:client:ItemBox", source, CoreObject.Shared.Items[item], "remove", amount)
return true
end
elseif zf.core == "esx" and zf.inventory == 'esx' then
local Player = zf.CoreObject.GetPlayerFromId(source)
local Player = CoreObject.GetPlayerFromId(source)
local removedItem = Player.getInventoryItem(item)
if removedItem.count >= amount then
Player.removeInventoryItem(item, amount)
Expand All @@ -61,10 +70,10 @@ end
function zf.createUsableItem(item, cb)
if ConsumableItems[item] then print('[ZF-LIB] The item ' .. item .. ' is already registered as a consumable item. Skipping the registration of this item.') end
if zf.core == "qb-core" then
zf.CoreObject.Functions.CreateUseableItem(item, cb)
CoreObject.Functions.CreateUseableItem(item, cb)
ConsumableItems[item] = cb
elseif zf.core == "esx" and zf.inventory == 'esx' then
zf.CoreObject.RegisterUsableItem(item, cb)
CoreObject.RegisterUsableItem(item, cb)
ConsumableItems[item] = cb
end
end
Expand All @@ -82,12 +91,12 @@ function zf.getItemCount(source, item)
local itemData = ox_inventory:GetItem(source, item, nil, true)
if itemData then return itemData else return 0 end
elseif zf.core == "qb-core" then
local Player = zf.CoreObject.Functions.GetPlayer(source)
local Player = CoreObject.Functions.GetPlayer(source)
if not Player then return 0 end
local itemData = Player.Functions.GetItemByName(item)
if itemData then return itemData.amount else return 0 end
elseif zf.core == "esx" then
local Player = zf.CoreObject.GetPlayerFromId(source)
local Player = CoreObject.GetPlayerFromId(source)
local itemData = Player.getInventoryItem(item)
if itemData then return itemData.count else return 0 end
end
Expand All @@ -105,7 +114,7 @@ zf.callback.register('zf:getItemCount', function(source, item)
end)

zf.callback.register('zf:getItemLabel', function(source, itemName)
local itemLabel = zf.CoreObject.GetItemLabel(itemName)
local itemLabel = CoreObject.GetItemLabel(itemName)
return itemLabel
end)

Expand Down
21 changes: 19 additions & 2 deletions modules/player/client.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
function zf.getPlayerData()
if zf.core == 'qb-core' then
return zf.CoreObject.Functions.GetPlayerData()
return CoreObject.Functions.GetPlayerData()
elseif zf.core == 'esx' then
return zf.CoreObject.GetPlayerData()
return CoreObject.GetPlayerData()
end
end

function zf.getCitizenId()
local citizenid = zf.callback.await('zf-lib:getCitizenid', false)
return citizenid
end

function zf.getPlayerJob()
local job = {}
local playerData = zf.getPlayerData()
Expand Down Expand Up @@ -84,4 +89,16 @@ function zf.getPlayerName()
elseif zf.core == 'esx' then
return playerData.name
end
end

function zf.getLicences()
zf.callback('zf-lib:getlicences', false, function(licenses)
return licenses
end)
end

function zf.getLicence(licenseType)
zf.callback('zf-lib:getlicence', false, function(license)
return license
end, licenseType)
end
88 changes: 83 additions & 5 deletions modules/player/server.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
function zf.getPlayer(source)
if zf.core == 'qb-core' then
return zf.CoreObject.Functions.GetPlayer(source)
return CoreObject.Functions.GetPlayer(source)
elseif zf.core == 'esx' then
return zf.CoreObject.GetPlayerFromId(source)
return CoreObject.GetPlayerFromId(source)
end
end

function zf.getPlayers()
if zf.core == 'qb-core' then
return zf.CoreObject.Functions.GetQBPlayers()
return CoreObject.Functions.GetQBPlayers()
elseif zf.core == 'esx' then
return zf.CoreObject.GetPlayers()
return CoreObject.GetPlayers()
end
end

Expand Down Expand Up @@ -121,4 +121,82 @@ function zf.setMoney(source, moneyType, amount)
moneyType = types[moneyType]['esx']
Player.setAccountMoney(moneyType, amount, reason)
end
end
end

function zf.getLicences(source)
local Player = zf.getPlayer(source)
if zf.core == 'qb-core' then
local licences = Player.PlayerData.metadata['licences']
return licences or false
elseif zf.core == 'esx' then
TriggerEvent('esx_license:getLicenses', source, function(licenses)
return licences or false
end)
end
end

function zf.getLicence(source, licenseType)
local Player = zf.getPlayer(source)
if zf.core == 'qb-core' then
local licences = Player.PlayerData.metadata['licences']
return licences[licenseType] or false
elseif zf.core == 'esx' then
TriggerEvent('esx_license:getLicenses', source, function(licenses)
return licences[licenseType] or false
end)
end
end

function zf.addLicence(source, licenseType)
local Player = zf.getPlayer(source)
if zf.core == 'qb-core' then
local licences = Player.PlayerData.metadata['licences']
licences[licenseType] = true
Player.Functions.SetMetaData('licences', licences)
return true
elseif zf.core == 'esx' then
TriggerEvent('esx_license:addLicense', source, licenseType, function()
return true
end)
end
return false
end

function zf.removeLicence(source, licenseType)
local Player = zf.getPlayer(source)
if zf.core == 'qb-core' then
local licences = Player.PlayerData.metadata['licences']
licences[licenseType] = false
Player.Functions.SetMetaData('licences', licences)
return true
elseif zf.core == 'esx' then
TriggerEvent('esx_license:removeLicense', source, licenseType, function()
return true
end)
end
return false
end

function zf.getCitizenId(source)
local Player = zf.getPlayer(source)
if zf.core == 'qb-core' then
local citizenid = Player.PlayerData.citizenid
return citizenid
elseif zf.core == 'esx' then
local citizenid = Player.license
return citizenid
end
return false
end

zf.callback.register('zf-lib:getlicences', function(source)
return zf.getLicenses(source)
end)

zf.callback.register('zf-lib:getlicence', function(source, licenseType)
return zf.getLicense(source, licenseType)
end)

zf.callback.register('zf-lib:getCitizenid', function(source)
return zf.getCitizenId(source)
end)
2 changes: 1 addition & 1 deletion modules/utils/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function zf.getInventoryName()
end

function zf.getCoreObject()
return zf.CoreObject
return CoreObject
end

function zf.getProgressColor(pourcentage, inverted)
Expand Down
23 changes: 23 additions & 0 deletions modules/vehicle/client.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
function zf.spawnVehicle(model, cb, coords, isnetworked, teleportInto)
local ped = PlayerPedId()
model = type(model) == 'string' and GetHashKey(model) or model
if not IsModelInCdimage(model) then return end
if coords then
coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords
else
coords = GetEntityCoords(ped)
end
isnetworked = isnetworked == nil or isnetworked
QBCore.Functions.LoadModel(model)
local veh = CreateVehicle(model, coords.x, coords.y, coords.z, coords.w, isnetworked, false)
local netid = NetworkGetNetworkIdFromEntity(veh)
SetVehicleHasBeenOwnedByPlayer(veh, true)
SetNetworkIdCanMigrate(netid, true)
SetVehicleNeedsToBeHotwired(veh, false)
SetVehRadioStation(veh, 'OFF')
SetVehicleFuelLevel(veh, 100.0)
SetModelAsNoLongerNeeded(model)
if teleportInto then TaskWarpPedIntoVehicle(PlayerPedId(), veh, -1) end
if cb then cb(veh) end
end

function zf.getPlate(vehicle)
if vehicle == 0 then return end
return zf.trim(GetVehicleNumberPlateText(vehicle))
Expand Down
6 changes: 6 additions & 0 deletions modules/vehicle/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ function zf.spawnVehicle(source, model, coords, warp)
end
while NetworkGetEntityOwner(veh) ~= source do Wait(0) end
return veh
end

function zf.getPlate(netId)
local vehicle = NetworkGetEntityFromNetworkId(netId)
if vehicle == 0 then return end
return zf.trim(GetVehicleNumberPlateText(vehicle))
end

0 comments on commit 96dc369

Please sign in to comment.