lib structure overhaul
restructured lib portion to match modules also edited community bridges onplayerunload fire to accept the passed src as it was otherwise nil
This commit is contained in:
1 parent
0b67e4b029
commit
8fd5de2d4e
36 files changed
+459
-210
No files matched your search
@@ -0,0 +1,98 @@
|
||||
ItemsBuilder = ItemsBuilder or {}
|
||||
|
||||
ItemsBuilder = {}
|
||||
|
||||
|
||||
ItemsBuilder.Generate = function(invoking, itemsTable)
|
||||
if not invoking or not itemsTable then return end
|
||||
|
||||
local resourcePath = GetResourcePath(GetCurrentResourceName()) .. "/generateditems/"
|
||||
|
||||
local function fileExists(path)
|
||||
local file = io.open(path, "r")
|
||||
if file then
|
||||
file:close()
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
local qbOldFilePath = resourcePath .. invoking .. "_qb_core_old.lua"
|
||||
local qbNewFilePath = resourcePath .. invoking .. "_qb_core_new.lua"
|
||||
local oxInventoryFilePath = resourcePath .. invoking .. "_ox_inventory.lua"
|
||||
|
||||
if fileExists(qbOldFilePath) or fileExists(qbNewFilePath) or fileExists(oxInventoryFilePath) then return end
|
||||
|
||||
local qbOld = {}
|
||||
for key, item in pairs(itemsTable) do
|
||||
qbOld[key] = string.format(
|
||||
"['%s'] = {label = '%s', weight = %d, stack = %s, close = %s, description = '%s'}",
|
||||
key, item.label, item.weight, tostring(item.stack), tostring(item.close), item.description
|
||||
)
|
||||
end
|
||||
|
||||
local qbNew = {}
|
||||
for key, item in pairs(itemsTable) do
|
||||
qbNew[key] = string.format(
|
||||
"%s = {label = '%s', weight = %d, stack = %s, close = %s, description = '%s'}",
|
||||
key, item.label, item.weight, tostring(item.stack), tostring(item.close), item.description
|
||||
)
|
||||
end
|
||||
|
||||
local oxInventory = {}
|
||||
for key, item in pairs(itemsTable) do
|
||||
oxInventory[key] = {
|
||||
label = item.label,
|
||||
weight = item.weight,
|
||||
stack = item.stack,
|
||||
close = item.close
|
||||
}
|
||||
end
|
||||
|
||||
local function writeInlineFile(fileName, content, useBrackets)
|
||||
local filePath = resourcePath .. fileName
|
||||
local file = io.open(filePath, "w")
|
||||
if file then
|
||||
for key, value in pairs(content) do
|
||||
if useBrackets then
|
||||
-- For qb_core_old with brackets
|
||||
file:write(string.format("['%s'] = %s,\n", key, value))
|
||||
else
|
||||
-- For qb_core_new without brackets
|
||||
file:write(string.format("%s = %s,\n", key, value))
|
||||
end
|
||||
end
|
||||
file:close()
|
||||
print("🌮 Items File Created: " .. filePath)
|
||||
else
|
||||
print("🌮 Something Broke for: " .. filePath)
|
||||
end
|
||||
end
|
||||
|
||||
local function writeSimplifiedFile(fileName, content)
|
||||
local filePath = resourcePath .. fileName
|
||||
local file = io.open(filePath, "w")
|
||||
if file then
|
||||
for key, item in pairs(content) do
|
||||
file:write(string.format('["%s"] = {\n', key))
|
||||
for k, v in pairs(item) do
|
||||
local formattedValue = type(v) == "string" and string.format('"%s"', v) or tostring(v)
|
||||
file:write(string.format(" %s = %s,\n", k, formattedValue))
|
||||
end
|
||||
file:write("},\n")
|
||||
end
|
||||
file:close()
|
||||
print("🌮 Items File Created: " .. filePath)
|
||||
else
|
||||
print("🌮 Something Broke for: " .. filePath)
|
||||
end
|
||||
end
|
||||
|
||||
writeInlineFile(invoking.."_".."qb_core_old.lua", qbOld, true)
|
||||
writeInlineFile(invoking.."_".."qb_core_new.lua", qbNew, false)
|
||||
writeSimplifiedFile(invoking.."_".."ox_inventory.lua", oxInventory)
|
||||
end
|
||||
|
||||
exports('ItemsBuilder', ItemsBuilder)
|
||||
return ItemsBuilder
|
||||
@@ -0,0 +1,74 @@
|
||||
LootTable = LootTable or {}
|
||||
|
||||
local lootTables = {}
|
||||
|
||||
LootTable.Register = function(name, items)
|
||||
local repackedTable = {}
|
||||
for k, v in pairs(items) do
|
||||
table.insert(repackedTable, {
|
||||
name = k,
|
||||
min = v.min,
|
||||
max = v.max,
|
||||
chance = v.chance,
|
||||
tier = v.tier,
|
||||
item = v.item,
|
||||
metadata = v.metadata
|
||||
})
|
||||
end
|
||||
lootTables[name] = repackedTable
|
||||
return lootTables[name]
|
||||
end
|
||||
|
||||
LootTable.Get = function(name)
|
||||
assert(name, "No Name Passed For Loot Table")
|
||||
return lootTables[name] or {}
|
||||
end
|
||||
|
||||
LootTable.GetRandomItem = function(name, tier, randomNumber)
|
||||
assert(name, "No Name Passed For Loot Table")
|
||||
if tier == nil then tier = 1 end
|
||||
local lootTable = lootTables[name] or {}
|
||||
math.randomseed(GetGameTimer())
|
||||
local chance = randomNumber or math.random(1, 100)
|
||||
for _, v in pairs(lootTable) do
|
||||
if v.chance <= chance and tier == v.tier then
|
||||
return { item = v.item, metadata = v.metadata, count = math.random(v.min, v.max), tier = v.tier, chance = v.chance}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
LootTable.GetRandomItems = function(name, tier, randomNumber)
|
||||
assert(name, "No Name Passed For Loot Table")
|
||||
if tier == nil then tier = 1 end
|
||||
local lootTable = LootTable.Get(name)
|
||||
math.randomseed(GetGameTimer())
|
||||
local chance = randomNumber or math.random(1, 100)
|
||||
local items = {}
|
||||
for _, v in pairs(lootTable) do
|
||||
if v.chance <= chance and tier == v.tier then
|
||||
table.insert(items,{item = v.item, metadata = v.metadata, count = math.random(v.min, v.max), tier = v.tier, chance = v.chance})
|
||||
end
|
||||
end
|
||||
return items
|
||||
end
|
||||
|
||||
LootTable.GetRandomItemsWithLimit = function(name, tier, randomNumber)
|
||||
assert(name, "No Name Passed For Loot Table")
|
||||
if tier == nil then tier = 1 end
|
||||
local lootTable = LootTable.Get(name)
|
||||
math.randomseed(GetGameTimer())
|
||||
local chance = randomNumber or math.random(1, 100)
|
||||
local items = {}
|
||||
for k = #lootTable, 1, -1 do
|
||||
local v = lootTable[k]
|
||||
if chance <= v.chance and tier == v.tier then
|
||||
table.insert(items, {v.item, v.metadata, math.random(v.min, v.max), v.tier, v.chance})
|
||||
table.remove(lootTable, k)
|
||||
end
|
||||
end
|
||||
return items
|
||||
end
|
||||
--
|
||||
|
||||
exports('LootTable', LootTable)
|
||||
return LootTable
|
||||
Reference in new issue
Block a user