Skip to content

Feat: Money as Item #1152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ QBConfig.StatusInterval = 5000 -- how often to check hu

QBConfig.Money = {}
QBConfig.Money.MoneyTypes = { cash = 500, bank = 5000, crypto = 0 } -- type = startamount - Add or remove money types for your server (for ex. blackmoney = 0), remember once added it will not be removed from the database!
QBConfig.Money.MoneyAsItems = false -- If true the currencies set below will be handled as items, if false money will be handled as normal data in playerdata
QBConfig.Money.MoneyItems = { cash = 'cash' } -- type = item name - Add or remove money items for your server (for ex. blackmoney = 'blackmoney'). Items must be defined in items.lua
QBConfig.Money.DontAllowMinus = { 'cash', 'crypto' } -- Money that is not allowed going in minus
QBConfig.Money.MinusLimit = -5000 -- The maximum amount you can be negative
QBConfig.Money.MinusLimit = -5000 -- The maximum amount you can be negative
QBConfig.Money.PayCheckTimeOut = 10 -- The time in minutes that it will give the paycheck
QBConfig.Money.PayCheckSociety = false -- If true paycheck will come from the society account that the player is employed at, requires qb-management

Expand Down
33 changes: 33 additions & 0 deletions server/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ function QBCore.Player.CreatePlayer(PlayerData, Offline)
if not key or type(key) ~= 'string' then return end
self.PlayerData[key] = val
self.Functions.UpdatePlayerData()

if not QBConfig.Money.MoneyAsItems or key ~= "items" then return end

for money, _ in pairs(QBConfig.Money.MoneyItems) do
self.Functions.UpdateMoney(money, true)
end
end

function self.Functions.SetMetaData(meta, val)
Expand Down Expand Up @@ -310,6 +316,26 @@ function QBCore.Player.CreatePlayer(PlayerData, Offline)
return self.PlayerData.metadata['rep'][rep] or 0
end

function self.Functions.UpdateMoney(moneytype, prioritizeItem)
if not QBConfig.Money.MoneyItems[moneytype] or not QBConfig.Money.MoneyAsItems then return end

local currentMoneyItem = exports['qb-inventory']:GetItemCount(self.PlayerData.source, QBConfig.Money.MoneyItems[moneytype])
local diff = self.PlayerData.money[moneytype] - currentMoneyItem
if diff == 0 then return end

if prioritizeItem then
self.PlayerData.money[moneytype] = currentMoneyItem
self.Functions.UpdatePlayerData()
return
end

if diff > 0 then
self.Functions.AddItem(QBConfig.Money.MoneyItems[moneytype], diff)
else
self.Functions.RemoveItem(QBConfig.Money.MoneyItems[moneytype], -diff)
end
end

function self.Functions.AddMoney(moneytype, amount, reason)
reason = reason or 'unknown'
moneytype = moneytype:lower()
Expand All @@ -318,6 +344,8 @@ function QBCore.Player.CreatePlayer(PlayerData, Offline)
if not self.PlayerData.money[moneytype] then return false end
self.PlayerData.money[moneytype] = self.PlayerData.money[moneytype] + amount

self.Functions.UpdateMoney(moneytype)

if not self.Offline then
self.Functions.UpdatePlayerData()
if amount > 100000 then
Expand Down Expand Up @@ -346,9 +374,12 @@ function QBCore.Player.CreatePlayer(PlayerData, Offline)
end
end
end

if self.PlayerData.money[moneytype] - amount < QBCore.Config.Money.MinusLimit then return false end
self.PlayerData.money[moneytype] = self.PlayerData.money[moneytype] - amount

self.Functions.UpdateMoney(moneytype)

if not self.Offline then
self.Functions.UpdatePlayerData()
if amount > 100000 then
Expand Down Expand Up @@ -376,6 +407,8 @@ function QBCore.Player.CreatePlayer(PlayerData, Offline)
local difference = amount - self.PlayerData.money[moneytype]
self.PlayerData.money[moneytype] = amount

self.Functions.UpdateMoney(moneytype)

if not self.Offline then
self.Functions.UpdatePlayerData()
TriggerEvent('qb-log:server:CreateLog', 'playermoney', 'SetMoney', 'green', '**' .. GetPlayerName(self.PlayerData.source) .. ' (citizenid: ' .. self.PlayerData.citizenid .. ' | id: ' .. self.PlayerData.source .. ')** $' .. amount .. ' (' .. moneytype .. ') set, new ' .. moneytype .. ' balance: ' .. self.PlayerData.money[moneytype] .. ' reason: ' .. reason)
Expand Down
8 changes: 5 additions & 3 deletions shared/items.lua
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,11 @@ QBShared.Items = {
newscam = { name = 'newscam', label = 'News Camera', weight = 100, type = 'item', image = 'newscam.png', unique = true, useable = true, shouldClose = true, description = 'A camera for the news' },
newsmic = { name = 'newsmic', label = 'News Microphone', weight = 100, type = 'item', image = 'newsmic.png', unique = true, useable = true, shouldClose = true, description = 'A microphone for the news' },
newsbmic = { name = 'newsbmic', label = 'Boom Microphone', weight = 100, type = 'item', image = 'newsbmic.png', unique = true, useable = true, shouldClose = true, description = 'A Useable BoomMic' },


-- Cash
-- cash = { name = 'cash', label = 'Money', weight = 0, type = 'item', image = 'money.png', unique = true, useable = false, shouldClose = false, description = 'A piece of paper that is useful' },

-- Crafting table's
item_bench = {name = "item_bench", label = "Workbench", weight = 15000, type = "item", image = "workbench.png", unique = true, useable = true, shouldClose = false, combinable = nil, description = "A workbench to craft items."},
attachment_bench = {name = "attachment_bench", label = "Attachment Workbench", weight = 15000, type = "item", image = "attworkbench.png", unique = true, useable = true, shouldClose = false, combinable = nil, description = "A workbench for crafting attachments."},

}
}
Loading