local furnitureByGarage = {} local furnitureUsedBy = {} local ALLOWED_FURNITURE_UPDATE_KEYS = { coords = true, rotation = true, lightData = true, } lib.callback.register("garages:furniture:saveFurnitureObject", function(sourcePlayer, garageId, objectData) local playerIdentifier = GetPlayerIdentifier(sourcePlayer) objectData.created = os.time() local newId = db.saveFurnitureObject(playerIdentifier, objectData) objectData.id = newId if not furnitureByGarage[garageId] then furnitureByGarage[garageId] = {} end furnitureByGarage[garageId][#furnitureByGarage[garageId] + 1] = objectData objectData.uniq = tostring("furniture_" .. newId) TriggerClientEvent("garages:furniture:addFurnitureObject", -1, garageId, objectData) Debug("garages:furniture:saveFurnitureObject", "Saved", garageId, "Id", newId, "Data", objectData) return true end) RegisterNetEvent("garages:furniture:updateFurnitureObject") AddEventHandler("garages:furniture:updateFurnitureObject", function(garageId, objectId, updateData) local playerSource = source if not updateData then return Notification(playerSource, i18n.t("furniture.invalid_data"), "error") end local hasInvalidKey = table.find(updateData, function(value, key) return not ALLOWED_FURNITURE_UPDATE_KEYS[key] end) if hasInvalidKey then Notification(playerSource, i18n.t("furniture.invalid_data"), "error") Debug("garages:furniture:updateFurnitureObject unsecured", updateData) return end local foundObject = table.find(furnitureByGarage[garageId], function(obj) return obj.id == objectId end) if not foundObject then return Notification(playerSource, i18n.t("furniture.invalid_object"), "error") end local success = db.updateFurnitureObject(objectId, updateData) if not success then Notification(playerSource, i18n.t("furniture.failed_update"), "error") return end for key, value in pairs(updateData) do if value and (key == "coords" or key == "rotation" or key == "lightData") then updateData[key] = json.decode(value) end foundObject[key] = updateData[key] end TriggerClientEvent("garages:furniture:updateFurnitureObject", -1, garageId, objectId, updateData) Debug("garages:furniture:updateFurnitureObject", "Updated", garageId, "Id", objectId, "data", updateData) end) local function GetFurnitureItemPrice(modelName) for _, item in pairs(Config.customFurnitures) do if item.object == modelName then return item.price end end end RegisterNetEvent("garages:furniture:sellFurnitureObject") AddEventHandler("garages:furniture:sellFurnitureObject", function(garageId, objectId) local playerSource = source local foundObject = table.find(furnitureByGarage[garageId], function(obj) return obj.id == objectId end) if not foundObject then return Notification(playerSource, i18n.t("furniture.invalid_object"), "error") end local success = db.deleteFurnitureObject(objectId) if not success then Notification(playerSource, i18n.t("furniture.failed_sell"), "error") return end local price = GetFurnitureItemPrice(foundObject.modelName) local sellGain = (price or 0) * Config.SellObjectCommision if sellGain > 0 then AddAccountMoney(playerSource, Config.MoneyType, sellGain) end furnitureByGarage[garageId] = table.filter(furnitureByGarage[garageId], function(obj) return obj.id ~= objectId end) TriggerClientEvent("garages:furniture:sellFurnitureObject", -1, garageId, objectId) Notification(playerSource, i18n.t("furniture.sold_furniture", { price = sellGain }), "success") end) local function GetFurnitureObjects(garageId) if not furnitureByGarage[garageId] then local dbObjects = db.getFurnitureObjects(garageId) furnitureByGarage[garageId] = dbObjects or {} end return furnitureByGarage[garageId] or {} end function ClearFurnitures(garageId) furnitureByGarage[garageId] = nil Debug("ClearFurnitures", garageId) end lib.callback.register("garages:furniture:getFurnitureObjects", function(sourcePlayer, garageId) return GetFurnitureObjects(garageId) end) lib.callback.register("garages:furniture:getFurnitureAvailable", function(sourcePlayer, garageId) local usedBy = furnitureUsedBy[garageId] return usedBy or true end) RegisterNetEvent("garages:furniture:updateFurnitureUsedBy") AddEventHandler("garages:furniture:updateFurnitureUsedBy", function(garageId, isUsing) local playerSource = source furnitureUsedBy[garageId] = (isUsing and playerSource) and playerSource or nil Debug("garages:furniture:updateFurnitureUsedBy", garageId, "UsedBy", isUsing) end) AddEventHandler("playerDropped", function(reason) local playerSource = source for garageId, usedByPlayer in pairs(furnitureUsedBy) do if usedByPlayer == playerSource then furnitureUsedBy[garageId] = nil end end end)