146 lines
4.6 KiB
Lua
146 lines
4.6 KiB
Lua
local function ValidateFurnitureObject(item, categoryName)
|
|
Debug("isObjectValid", item, categoryName)
|
|
|
|
local modelHash = joaat(item.object)
|
|
if not IsModelValid(modelHash) then
|
|
Notification(i18n.t("furniture_creator.object_is_not_valid", { object = item.object }), "error")
|
|
return false
|
|
end
|
|
|
|
if categoryName then
|
|
local category = Config.Furniture[categoryName]
|
|
if category then
|
|
local existingItem = table.find(category.items, function(existing)
|
|
return existing.object == item.object
|
|
end)
|
|
if existingItem and existingItem.id ~= item.id then
|
|
Notification(i18n.t("furniture_creator.object_already_in_category", {
|
|
object = item.object,
|
|
category = categoryName
|
|
}), "error")
|
|
return false
|
|
end
|
|
end
|
|
end
|
|
|
|
return true
|
|
end
|
|
|
|
RegisterNUICallback("create_furniture_item", function(data, cb)
|
|
if not ValidateFurnitureObject(data.item, data.category) then
|
|
return cb(false)
|
|
end
|
|
|
|
Debug("create_furniture_item", data)
|
|
|
|
local success = lib.callback.await("garages:createFurnitureItem", false, data)
|
|
if success then
|
|
Notification(i18n.t("furniture_creator.item_created"), "success")
|
|
else
|
|
Notification(i18n.t("furniture_creator.item_create_failed"), "error")
|
|
end
|
|
cb(success)
|
|
end)
|
|
|
|
RegisterNUICallback("update_furniture_item", function(data, cb)
|
|
if not ValidateFurnitureObject(data.item, data.category) then
|
|
return cb(false)
|
|
end
|
|
|
|
Debug("update_furniture_item", data)
|
|
|
|
local success = lib.callback.await("garages:updateFurnitureItem", false, data)
|
|
if success then
|
|
Notification(i18n.t("furniture_creator.item_updated"), "success")
|
|
else
|
|
Notification(i18n.t("furniture_creator.item_update_failed"), "error")
|
|
end
|
|
cb(success)
|
|
end)
|
|
|
|
RegisterNUICallback("remove_furniture_item", function(data, cb)
|
|
Debug("remove_furniture_item", data)
|
|
|
|
if not data.id then
|
|
Notification(i18n.t("furniture_creator.cannot_delete_config_item"), "error")
|
|
return cb(false)
|
|
end
|
|
|
|
local success = lib.callback.await("garages:removeFurnitureItem", false, data)
|
|
if success then
|
|
Notification(i18n.t("furniture_creator.item_removed"), "success")
|
|
furnitureCreator:updateUI()
|
|
else
|
|
Notification(i18n.t("furniture_creator.item_remove_failed"), "error")
|
|
end
|
|
cb(success)
|
|
end)
|
|
|
|
RegisterNUICallback("create_furniture_category", function(data, cb)
|
|
Debug("create_furniture_category", data)
|
|
|
|
local success = lib.callback.await("garages:createFurnitureCategory", false, data)
|
|
if success then
|
|
Notification(i18n.t("furniture_creator.category_created"), "success")
|
|
end
|
|
cb(success)
|
|
end)
|
|
|
|
RegisterNetEvent("garages:syncFurnitureItem")
|
|
AddEventHandler("garages:syncFurnitureItem", function(action, categoryName, itemData)
|
|
local objectName = itemData.object or itemData
|
|
Debug("garages:syncFurnitureItem", action, categoryName, objectName)
|
|
|
|
if action == "create" then
|
|
local category = Config.Furniture[categoryName]
|
|
if not category then
|
|
Error("garages:syncFurnitureItem", "Category not found", categoryName)
|
|
return
|
|
end
|
|
table.insert(category.items, itemData)
|
|
|
|
elseif action == "update" then
|
|
local category = Config.Furniture[categoryName]
|
|
if category then
|
|
for index, existingItem in pairs(category.items) do
|
|
if existingItem.id == itemData.id then
|
|
category.items[index] = itemData
|
|
break
|
|
end
|
|
end
|
|
end
|
|
|
|
elseif action == "delete" then
|
|
local category = Config.Furniture[categoryName]
|
|
if category then
|
|
category.items = table.filter(category.items, function(item)
|
|
return item.id ~= itemData.id
|
|
end)
|
|
end
|
|
end
|
|
|
|
furnitureCreator:updateUI()
|
|
InitializeFurnitures()
|
|
|
|
Debug("Config.Furniture updated:", action, categoryName, objectName)
|
|
end)
|
|
|
|
RegisterNUICallback("furniture_take_screenshot", function(data, cb)
|
|
if not data then
|
|
Notification(i18n.t("furniture_creator.object_required"), "error")
|
|
furnitureCreator:open()
|
|
return cb(nil)
|
|
end
|
|
|
|
local modelHash = joaat(data)
|
|
if not IsModelValid(modelHash) then
|
|
Notification(i18n.t("furniture_creator.object_is_not_valid", { object = data }), "error")
|
|
furnitureCreator:open()
|
|
return cb(nil)
|
|
end
|
|
|
|
local screenshotUrl = furnitureCreator:takeObjectScreenshot(data)
|
|
furnitureCreator:open()
|
|
cb(screenshotUrl)
|
|
end)
|