2022-04-13 20:01:09 +01:00
local QBCore = exports [ ' qb-core ' ] : GetCoreObject ( )
2022-08-24 09:23:43 +01:00
for k , v in pairs ( Config.Consumables ) do
QBCore.Functions . CreateUseableItem ( k , function ( source , item ) TriggerClientEvent ( ' jim-consumables:Consume ' , source , item.name ) end )
2023-02-14 14:40:09 +00:00
if not QBCore.Shared . Items [ k ] then print ( " ^1Debug^7: ^2Item check ^7- '^1 " .. k .. " ^7' ^2not found in the shared lua^7 " ) end
if not Config.Emotes [ v.emote ] then print ( " ^1Debug^7: ^2Emote check ^7- '^1 " .. k .. " ^7' ^2requested emote ^7'^6 " .. v.emote .. " ^7' - ^2not found in config^7.^2lua^7 " ) end
2022-08-24 09:23:43 +01:00
end
RegisterNetEvent ( ' jim-consumables:server:toggleItem ' , function ( give , item , amount )
local src = source
if give == 0 or give == false then
if QBCore.Functions . GetPlayer ( src ) . Functions.RemoveItem ( item , amount or 1 ) then
TriggerClientEvent ( ' inventory:client:ItemBox ' , src , QBCore.Shared . Items [ item ] , " remove " , amount or 1 )
end
else
if QBCore.Functions . GetPlayer ( src ) . Functions.AddItem ( item , amount or 1 ) then
TriggerClientEvent ( ' inventory:client:ItemBox ' , src , QBCore.Shared . Items [ item ] , " add " , amount or 1 )
end
2022-08-15 10:06:30 +01:00
end
2022-08-26 06:10:20 +06:00
end )
2023-02-14 14:52:57 +00:00
RegisterNetEvent ( " jim-consumables:server:DupeWarn " , function ( item , newsrc )
local src = newsrc or source
local P = QBCore.Functions . GetPlayer ( src )
print ( " ^5DupeWarn: ^1 " .. P.PlayerData . charinfo.firstname .. " " .. P.PlayerData . charinfo.lastname .. " ^7(^1 " .. tostring ( src ) .. " ^7) ^2Tried to remove item ^7('^3 " .. item .. " ^7')^2 but it wasn't there^7 " )
if not Config.Debug then DropPlayer ( src , " Kicked for attempting to duplicate items " ) end
print ( " ^5DupeWarn: ^1 " .. P.PlayerData . charinfo.firstname .. " " .. P.PlayerData . charinfo.lastname .. " ^7(^1 " .. tostring ( src ) .. " ^7) ^2Dropped from server for item duplicating^7 " )
end )
RegisterNetEvent ( ' jim-consumables:server:toggleItem ' , function ( give , item , amount , newsrc )
local src = newsrc or source
local amount = amount or 1
local remamount = amount
if not give then
if HasItem ( src , item , amount ) then -- check if you still have the item
if QBCore.Functions . GetPlayer ( src ) . Functions.GetItemByName ( item ) . unique then -- If unique item, keep removing until gone
while remamount > 0 do
QBCore.Functions . GetPlayer ( src ) . Functions.RemoveItem ( item , 1 )
remamount -= 1
end
TriggerClientEvent ( ' inventory:client:ItemBox ' , src , QBCore.Shared . Items [ item ] , " remove " , amount ) -- Show removal item box when all are removed
return
end
if QBCore.Functions . GetPlayer ( src ) . Functions.RemoveItem ( item , amount ) then
if Config.Debug then print ( " ^5Debug^7: ^1Removing ^2from Player^7(^2 " .. src .. " ^7) '^6 " .. QBCore.Shared . Items [ item ] . label .. " ^7(^2x^6 " .. ( amount or " 1 " ) .. " ^7)' " ) end
TriggerClientEvent ( ' inventory:client:ItemBox ' , src , QBCore.Shared . Items [ item ] , " remove " , amount )
end
else TriggerEvent ( " jim-consumables:server:DupeWarn " , item , src ) end -- if not boot the player
elseif give then
if QBCore.Functions . GetPlayer ( src ) . Functions.AddItem ( item , amount ) then
if Config.Debug then print ( " ^5Debug^7: ^4Giving ^2Player^7(^2 " .. src .. " ^7) '^6 " .. QBCore.Shared . Items [ item ] . label .. " ^7(^2x^6 " .. ( amount or " 1 " ) .. " ^7)' " ) end
TriggerClientEvent ( ' inventory:client:ItemBox ' , src , QBCore.Shared . Items [ item ] , " add " , amount )
end
end
end )
2022-08-26 06:10:20 +06:00
RegisterNetEvent ( ' jim-consumables:server:addThirst ' , function ( amount )
local Player = QBCore.Functions . GetPlayer ( source )
if not Player then return end
Player.Functions . SetMetaData ( ' thirst ' , amount )
TriggerClientEvent ( ' hud:client:UpdateNeeds ' , source , Player.PlayerData . metadata.hunger , amount )
end )
RegisterNetEvent ( ' jim-consumables:server:addHunger ' , function ( amount )
local Player = QBCore.Functions . GetPlayer ( source )
if not Player then return end
Player.Functions . SetMetaData ( ' hunger ' , amount )
TriggerClientEvent ( ' hud:client:UpdateNeeds ' , source , amount , Player.PlayerData . metadata.thirst )
2022-12-15 22:30:42 -05:00
end )
2023-02-14 14:40:09 +00:00
2023-02-14 14:52:57 +00:00
function HasItem ( source , items , amount )
local amount = amount or 1
local Player = QBCore.Functions . GetPlayer ( source )
if not Player then return false end
local count = 0
if Config.Debug then print ( " ^5Debug^7: ^3HasItem^7: ^2Checking if player has required item^7 '^3 " .. tostring ( items ) .. " ^7' " ) end
for _ , itemData in pairs ( Player.PlayerData . items ) do
if itemData and ( itemData.name == items ) then
if Config.Debug then print ( " ^5Debug^7: ^3HasItem^7: ^2Item^7: '^3 " .. tostring ( items ) .. " ^7' ^2Slot^7: ^3 " .. itemData.slot .. " ^7x(^3 " .. tostring ( itemData.amount ) .. " ^7) " ) end
count += itemData.amount
end
end
if count >= amount then
if Config.Debug then print ( " ^5Debug^7: ^3HasItem^7: ^2Items ^5FOUND^7 x^3 " .. count .. " ^7 " ) end
return true
end
if Config.Debug then print ( " ^5Debug^7: ^3HasItem^7: ^2Items ^1NOT FOUND^7 " ) end
return false
end
2023-02-14 14:40:09 +00:00
--Export Import System--
RegisterNetEvent ( " jim-consumables:getSync " , function ( ) local src = source TriggerClientEvent ( " jim-consumables:syncItems " , source , Config.Consumables , Config.Emotes ) end )
local function importEmote ( emoteName , eInfo )
if Config.Debug then print ( " ^5Debug^7: ^2Importing Item ^7'^6 " .. emoteName .. " ^7' " ) end
Config.Emotes [ emoteName ] = eInfo
TriggerClientEvent ( " jim-consumables:syncItems " , - 1 , nil , Config.Emotes )
end
local function importConsumable ( itemName , conInfo )
if Config.Debug then print ( " ^5Debug^7: ^2Importing Item ^7'^6 " .. itemName .. " ^7' " ) end
if not QBCore.Shared . Items [ itemName ] then print ( " ^1Debug^7: ^2Item check ^7- '^1 " .. k .. " ^7' ^2not found in the shared lua^7 " ) end
Config.Consumables [ itemName ] = conInfo
QBCore.Functions . CreateUseableItem ( itemName , function ( source , item ) TriggerClientEvent ( ' jim-consumables:Consume ' , source , itemName ) end )
TriggerClientEvent ( " jim-consumables:syncItems " , - 1 , Config.Consumables , nil )
end
exports ( " importEmote " , importEmote )
exports ( " importConsumable " , importConsumable )