Skip to content

Commit bcce86c

Browse files
committed
Don't import whole core
1 parent 8d1cffa commit bcce86c

2 files changed

Lines changed: 58 additions & 38 deletions

File tree

client/main.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- Variables
2-
local QBCore = exports['qb-core']:GetCoreObject()
2+
local QBCore = exports['qb-core']:GetCoreObject({ 'Functions', 'Shared' })
33
local PlayerData = QBCore.Functions.GetPlayerData()
44
local CurrentWeaponData, CanShoot, MultiplierAmount, currentWeapon = {}, true, 0, nil
55

@@ -98,7 +98,7 @@ RegisterNetEvent('qb-weapons:client:AddAmmo', function(ammoType, amount, itemDat
9898
disableCarMovement = false,
9999
disableMouse = false,
100100
disableCombat = true,
101-
}, {}, {}, {}, function() -- Done
101+
}, {}, {}, {}, function() -- Done
102102
weapon = GetSelectedPedWeapon(ped) -- Get weapon at time of completion
103103

104104
if QBCore.Shared.Weapons[weapon]?.ammotype ~= ammoType then

server/main.lua

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
1-
local QBCore = exports['qb-core']:GetCoreObject()
1+
local QBCore = exports['qb-core']:GetCoreObject({ 'Functions', 'Commands' })
2+
local sharedItems = exports['qb-core']:GetShared('Items')
3+
local sharedWeapons = exports['qb-core']:GetShared('Weapons')
4+
5+
local function SplitStr(str, delimiter)
6+
local result = {}
7+
local from = 1
8+
local delim_from, delim_to = string.find(str, delimiter, from)
9+
while delim_from do
10+
result[#result + 1] = string.sub(str, from, delim_from - 1)
11+
from = delim_to + 1
12+
delim_from, delim_to = string.find(str, delimiter, from)
13+
end
14+
result[#result + 1] = string.sub(str, from)
15+
return result
16+
end
17+
18+
local function Round(value, numDecimalPlaces)
19+
local mult = 10 ^ (numDecimalPlaces or 0)
20+
return math.floor(value * mult + 0.5) / mult
21+
end
222

323
-- Functions
424

@@ -20,11 +40,11 @@ QBCore.Functions.CreateCallback('qb-weapons:server:GetConfig', function(_, cb)
2040
end)
2141

2242
QBCore.Functions.CreateCallback('weapon:server:GetWeaponAmmo', function(source, cb, WeaponData)
23-
local Player = QBCore.Functions.GetPlayer(source)
43+
local Player = exports['qb-core']:GetPlayer(source)
2444
local retval = 0
2545
if WeaponData then
2646
if Player then
27-
local ItemData = Player.Functions.GetItemBySlot(WeaponData.slot)
47+
local ItemData = Player.GetItemBySlot(WeaponData.slot)
2848
if ItemData then
2949
retval = ItemData.info.ammo and ItemData.info.ammo or 0
3050
end
@@ -35,11 +55,11 @@ end)
3555

3656
QBCore.Functions.CreateCallback('qb-weapons:server:RepairWeapon', function(source, cb, RepairPoint, data)
3757
local src = source
38-
local Player = QBCore.Functions.GetPlayer(src)
58+
local Player = exports['qb-core']:GetPlayer(src)
3959
local minute = 60 * 1000
4060
local Timeout = math.random(5 * minute, 10 * minute)
41-
local WeaponData = QBCore.Shared.Weapons[GetHashKey(data.name)]
42-
local WeaponClass = (QBCore.Shared.SplitStr(WeaponData.ammotype, '_')[2]):lower()
61+
local WeaponData = sharedWeapons[GetHashKey(data.name)]
62+
local WeaponClass = (SplitStr(WeaponData.ammotype, '_')[2]):lower()
4363

4464
if not Player then
4565
cb(false)
@@ -59,7 +79,7 @@ QBCore.Functions.CreateCallback('qb-weapons:server:RepairWeapon', function(sourc
5979
return
6080
end
6181

62-
if not Player.Functions.RemoveMoney('cash', Config.WeaponRepairCosts[WeaponClass]) then
82+
if not Player.RemoveMoney('cash', Config.WeaponRepairCosts[WeaponClass]) then
6383
cb(false)
6484
return
6585
end
@@ -72,11 +92,11 @@ QBCore.Functions.CreateCallback('qb-weapons:server:RepairWeapon', function(sourc
7292
}
7393

7494
if not exports['qb-inventory']:RemoveItem(src, data.name, 1, data.slot, 'qb-weapons:server:RepairWeapon') then
75-
Player.Functions.AddMoney('cash', Config.WeaponRepairCosts[WeaponClass], 'qb-weapons:server:RepairWeapon')
95+
Player.AddMoney('cash', Config.WeaponRepairCosts[WeaponClass], 'qb-weapons:server:RepairWeapon')
7696
return
7797
end
7898

79-
TriggerClientEvent('qb-inventory:client:ItemBox', src, QBCore.Shared.Items[data.name], 'remove')
99+
TriggerClientEvent('qb-inventory:client:ItemBox', src, sharedItems[data.name], 'remove')
80100
TriggerClientEvent('qb-inventory:client:CheckWeapon', src, data.name)
81101
TriggerClientEvent('qb-weapons:client:SyncRepairShops', -1, Config.WeaponRepairPoints[RepairPoint], RepairPoint)
82102

@@ -103,11 +123,11 @@ QBCore.Functions.CreateCallback('qb-weapons:server:RepairWeapon', function(sourc
103123
end)
104124

105125
QBCore.Functions.CreateCallback('prison:server:checkThrowable', function(source, cb, weapon)
106-
local Player = QBCore.Functions.GetPlayer(source)
126+
local Player = exports['qb-core']:GetPlayer(source)
107127
if not Player then return cb(false) end
108128
local throwable = false
109129
for _, v in pairs(Config.Throwables) do
110-
if QBCore.Shared.Weapons[weapon].name == 'weapon_' .. v then
130+
if sharedWeapons[weapon].name == 'weapon_' .. v then
111131
if not exports['qb-inventory']:RemoveItem(source, 'weapon_' .. v, 1, false, 'prison:server:checkThrowable') then return cb(false) end
112132
throwable = true
113133
break
@@ -120,51 +140,51 @@ end)
120140

121141
RegisterNetEvent('qb-weapons:server:UpdateWeaponAmmo', function(CurrentWeaponData, amount)
122142
local src = source
123-
local Player = QBCore.Functions.GetPlayer(src)
143+
local Player = exports['qb-core']:GetPlayer(src)
124144
if not Player then return end
125145
amount = tonumber(amount)
126146
if CurrentWeaponData then
127147
if Player.PlayerData.items[CurrentWeaponData.slot] then
128148
Player.PlayerData.items[CurrentWeaponData.slot].info.ammo = amount
129149
end
130-
Player.Functions.SetInventory(Player.PlayerData.items, true)
150+
Player.SetInventory(Player.PlayerData.items, true)
131151
end
132152
end)
133153

134154
RegisterNetEvent('qb-weapons:server:TakeBackWeapon', function(k)
135155
local src = source
136-
local Player = QBCore.Functions.GetPlayer(src)
156+
local Player = exports['qb-core']:GetPlayer(src)
137157
if not Player then return end
138158
local itemdata = Config.WeaponRepairPoints[k].RepairingData.WeaponData
139159
itemdata.info.quality = 100
140160
exports['qb-inventory']:AddItem(src, itemdata.name, 1, false, itemdata.info, 'qb-weapons:server:TakeBackWeapon')
141-
TriggerClientEvent('qb-inventory:client:ItemBox', src, QBCore.Shared.Items[itemdata.name], 'add')
161+
TriggerClientEvent('qb-inventory:client:ItemBox', src, sharedItems[itemdata.name], 'add')
142162
Config.WeaponRepairPoints[k].IsRepairing = false
143163
Config.WeaponRepairPoints[k].RepairingData = {}
144164
TriggerClientEvent('qb-weapons:client:SyncRepairShops', -1, Config.WeaponRepairPoints[k], k)
145165
end)
146166

147167
RegisterNetEvent('qb-weapons:server:SetWeaponQuality', function(data, hp)
148168
local src = source
149-
local Player = QBCore.Functions.GetPlayer(src)
169+
local Player = exports['qb-core']:GetPlayer(src)
150170
if not Player then return end
151171
local WeaponSlot = Player.PlayerData.items[data.slot]
152172
WeaponSlot.info.quality = hp
153-
Player.Functions.SetInventory(Player.PlayerData.items, true)
173+
Player.SetInventory(Player.PlayerData.items, true)
154174
end)
155175

156176
RegisterNetEvent('qb-weapons:server:UpdateWeaponQuality', function(data, RepeatAmount)
157177
local src = source
158-
local Player = QBCore.Functions.GetPlayer(src)
159-
local WeaponData = QBCore.Shared.Weapons[GetHashKey(data.name)]
178+
local Player = exports['qb-core']:GetPlayer(src)
179+
local WeaponData = sharedWeapons[GetHashKey(data.name)]
160180
local WeaponSlot = Player.PlayerData.items[data.slot]
161181
local DecreaseAmount = Config.DurabilityMultiplier[data.name]
162182
if WeaponSlot then
163183
if not IsWeaponBlocked(WeaponData.name) then
164184
if WeaponSlot.info.quality then
165185
for _ = 1, RepeatAmount, 1 do
166186
if WeaponSlot.info.quality - DecreaseAmount > 0 then
167-
WeaponSlot.info.quality = QBCore.Shared.Round(WeaponSlot.info.quality - DecreaseAmount, 2)
187+
WeaponSlot.info.quality = Round(WeaponSlot.info.quality - DecreaseAmount, 2)
168188
else
169189
WeaponSlot.info.quality = 0
170190
TriggerClientEvent('qb-weapons:client:UseWeapon', src, data, false)
@@ -176,7 +196,7 @@ RegisterNetEvent('qb-weapons:server:UpdateWeaponQuality', function(data, RepeatA
176196
WeaponSlot.info.quality = 100
177197
for _ = 1, RepeatAmount, 1 do
178198
if WeaponSlot.info.quality - DecreaseAmount > 0 then
179-
WeaponSlot.info.quality = QBCore.Shared.Round(WeaponSlot.info.quality - DecreaseAmount, 2)
199+
WeaponSlot.info.quality = Round(WeaponSlot.info.quality - DecreaseAmount, 2)
180200
else
181201
WeaponSlot.info.quality = 0
182202
TriggerClientEvent('qb-weapons:client:UseWeapon', src, data, false)
@@ -187,11 +207,11 @@ RegisterNetEvent('qb-weapons:server:UpdateWeaponQuality', function(data, RepeatA
187207
end
188208
end
189209
end
190-
Player.Functions.SetInventory(Player.PlayerData.items, true)
210+
Player.SetInventory(Player.PlayerData.items, true)
191211
end)
192212

193213
RegisterNetEvent('qb-weapons:server:removeWeaponAmmoItem', function(item)
194-
local Player = QBCore.Functions.GetPlayer(source)
214+
local Player = exports['qb-core']:GetPlayer(source)
195215
if not Player or type(item) ~= 'table' or not item.name or not item.slot then return end
196216
exports['qb-inventory']:RemoveItem(source, item.name, 1, item.slot, 'qb-weapons:server:removeWeaponAmmoItem')
197217
end)
@@ -223,12 +243,12 @@ local function GetWeaponSlotByName(items, weaponName)
223243
end
224244

225245
local function IsMK2Weapon(weaponHash)
226-
local weaponName = QBCore.Shared.Weapons[weaponHash]['name']
246+
local weaponName = sharedWeapons[weaponHash]['name']
227247
return string.find(weaponName, 'mk2') ~= nil
228248
end
229249

230250
local function EquipWeaponTint(source, tintIndex, item, isMK2)
231-
local Player = QBCore.Functions.GetPlayer(source)
251+
local Player = exports['qb-core']:GetPlayer(source)
232252
if not Player then return end
233253

234254
local ped = GetPlayerPed(source)
@@ -239,7 +259,7 @@ local function EquipWeaponTint(source, tintIndex, item, isMK2)
239259
return
240260
end
241261

242-
local weaponName = QBCore.Shared.Weapons[selectedWeaponHash].name
262+
local weaponName = sharedWeapons[selectedWeaponHash].name
243263
if not weaponName then return end
244264

245265
if isMK2 and not IsMK2Weapon(selectedWeaponHash) then
@@ -257,9 +277,9 @@ local function EquipWeaponTint(source, tintIndex, item, isMK2)
257277

258278
weaponSlot.info.tint = tintIndex
259279
Player.PlayerData.items[weaponSlotIndex] = weaponSlot
260-
Player.Functions.SetInventory(Player.PlayerData.items, true)
280+
Player.SetInventory(Player.PlayerData.items, true)
261281
exports['qb-inventory']:RemoveItem(source, item, 1, false, 'qb-weapon:EquipWeaponTint')
262-
TriggerClientEvent('qb-inventory:client:ItemBox', source, QBCore.Shared.Items[item], 'remove')
282+
TriggerClientEvent('qb-inventory:client:ItemBox', source, sharedItems[item], 'remove')
263283
TriggerClientEvent('qb-weapons:client:EquipTint', source, selectedWeaponHash, tintIndex)
264284
end
265285

@@ -298,14 +318,14 @@ local function EquipWeaponAttachment(src, item)
298318
local ped = GetPlayerPed(src)
299319
local selectedWeaponHash = GetSelectedPedWeapon(ped)
300320
if selectedWeaponHash == `WEAPON_UNARMED` then return end
301-
local weaponName = QBCore.Shared.Weapons[selectedWeaponHash].name
321+
local weaponName = sharedWeapons[selectedWeaponHash].name
302322
if not weaponName then return end
303323
local attachmentComponent = DoesWeaponTakeWeaponComponent(item, weaponName)
304324
if not attachmentComponent then
305325
TriggerClientEvent('QBCore:Notify', src, 'This attachment is not valid for the selected weapon.', 'error')
306326
return
307327
end
308-
local Player = QBCore.Functions.GetPlayer(src)
328+
local Player = exports['qb-core']:GetPlayer(src)
309329
if not Player then return end
310330
local weaponSlot, weaponSlotIndex = GetWeaponSlotByName(Player.PlayerData.items, weaponName)
311331
if not weaponSlot then return end
@@ -322,10 +342,10 @@ local function EquipWeaponAttachment(src, item)
322342
shouldRemove = true
323343
end
324344
Player.PlayerData.items[weaponSlotIndex] = weaponSlot
325-
Player.Functions.SetInventory(Player.PlayerData.items, true)
345+
Player.SetInventory(Player.PlayerData.items, true)
326346
if shouldRemove then
327347
exports['qb-inventory']:RemoveItem(src, item, 1, false, 'qb-weapons:EquipWeaponAttachment')
328-
TriggerClientEvent('qb-inventory:client:ItemBox', src, QBCore.Shared.Items[item], 'remove')
348+
TriggerClientEvent('qb-inventory:client:ItemBox', src, sharedItems[item], 'remove')
329349
end
330350
end
331351

@@ -337,7 +357,7 @@ end
337357

338358
QBCore.Functions.CreateCallback('qb-weapons:server:RemoveAttachment', function(source, cb, AttachmentData, WeaponData)
339359
local src = source
340-
local Player = QBCore.Functions.GetPlayer(src)
360+
local Player = exports['qb-core']:GetPlayer(src)
341361
local Inventory = Player.PlayerData.items
342362
local allAttachments = WeaponAttachments
343363
local AttachmentComponent = allAttachments[AttachmentData.attachment][WeaponData.name]
@@ -346,10 +366,10 @@ QBCore.Functions.CreateCallback('qb-weapons:server:RemoveAttachment', function(s
346366
local HasAttach, key = HasAttachment(AttachmentComponent, Inventory[WeaponData.slot].info.attachments)
347367
if HasAttach then
348368
table.remove(Inventory[WeaponData.slot].info.attachments, key)
349-
Player.Functions.SetInventory(Player.PlayerData.items, true)
369+
Player.SetInventory(Player.PlayerData.items, true)
350370
exports['qb-inventory']:AddItem(src, AttachmentData.attachment, 1, false, false, 'qb-weapons:server:RemoveAttachment')
351-
TriggerClientEvent('qb-inventory:client:ItemBox', src, QBCore.Shared.Items[AttachmentData.attachment], 'add')
352-
TriggerClientEvent('QBCore:Notify', src, Lang:t('info.removed_attachment', { value = QBCore.Shared.Items[AttachmentData.attachment].label }), 'error')
371+
TriggerClientEvent('qb-inventory:client:ItemBox', src, sharedItems[AttachmentData.attachment], 'add')
372+
TriggerClientEvent('QBCore:Notify', src, Lang:t('info.removed_attachment', { value = sharedItems[AttachmentData.attachment].label }), 'error')
353373
cb(Inventory[WeaponData.slot].info.attachments)
354374
else
355375
cb(false)

0 commit comments

Comments
 (0)