Skip to content

Commit

Permalink
refactor(client): Move logic to separate lua files (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheiLLeniumStudios authored Apr 27, 2023
1 parent def920a commit cf1f7a6
Show file tree
Hide file tree
Showing 10 changed files with 490 additions and 478 deletions.
485 changes: 7 additions & 478 deletions client/client.lua

Large diffs are not rendered by default.

83 changes: 83 additions & 0 deletions client/common.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
function CheckDuty()
return not Config.OnDutyOnlyClothingRooms or (Config.OnDutyOnlyClothingRooms and client.job.onduty)
end

function IsPlayerAllowedForOutfitRoom(outfitRoom)
local isAllowed = false
local count = #outfitRoom.citizenIDs
for i = 1, count, 1 do
if Framework.IsPlayerAllowed(outfitRoom.citizenIDs[i]) then
isAllowed = true
break
end
end
return isAllowed or not outfitRoom.citizenIDs or count == 0
end

function GetPlayerJobOutfits(clothingRoom)
local outfits = {}
local gender = Framework.GetGender()
local gradeLevel = clothingRoom.job and Framework.GetJobGrade() or Framework.GetGangGrade()
local jobName = clothingRoom.job and client.job.name or client.gang.name

if Config.BossManagedOutfits then
local mType = clothingRoom.job and "Job" or "Gang"
local result = lib.callback.await("illenium-appearance:server:getManagementOutfits", false, mType, gender)
for i = 1, #result, 1 do
outfits[#outfits + 1] = {
type = mType,
model = result[i].model,
components = result[i].components,
props = result[i].props,
disableSave = true,
name = result[i].name
}
end
else
for i = 1, #Config.Outfits[jobName][gender], 1 do
for _, v in pairs(Config.Outfits[jobName][gender][i].grades) do
if v == gradeLevel then
outfits[#outfits + 1] = Config.Outfits[jobName][gender][i]
outfits[#outfits].gender = gender
outfits[#outfits].jobName = jobName
end
end
end
end

return outfits
end

function OpenOutfitRoom(outfitRoom)
local isAllowed = IsPlayerAllowedForOutfitRoom(outfitRoom)
if isAllowed then
OpenMenu(nil, "outfit")
end
end

function OpenBarberShop()
local config = GetDefaultConfig()
config.headOverlays = true
OpenShop(config, false, "barber")
end

function OpenTattooShop()
local config = GetDefaultConfig()
config.tattoos = true
OpenShop(config, false, "tattoo")
end

function OpenSurgeonShop()
local config = GetDefaultConfig()
config.headBlend = true
config.faceFeatures = true
OpenShop(config, false, "surgeon")
end

AddEventHandler("onResourceStop", function(resource)
if resource == GetCurrentResourceName() then
if Config.BossManagedOutfits then
Management.RemoveItems()
end
end
end)
2 changes: 2 additions & 0 deletions client/radial/ox.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
if not Config.UseRadialMenu then return end

if not Radial.IsOX() then return end

function Radial.Add(title, event)
Expand Down
2 changes: 2 additions & 0 deletions client/radial/qb.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
if not Config.UseRadialMenu then return end

if not Radial.IsQB() then return end

function Radial.Add(title, event)
Expand Down
10 changes: 10 additions & 0 deletions client/radial/radial.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
if not Config.UseRadialMenu then return end

Radial = {}

Radial.MenuID = "open_clothing_menu"
Expand Down Expand Up @@ -42,3 +44,11 @@ function Radial.RemoveOption()
radialOptionAdded = false
end
end

AddEventHandler("onResourceStop", function(resource)
if resource == GetCurrentResourceName() then
if Config.UseOxRadial and GetResourceState("ox_lib") == "started" or GetResourceState("qb-radialmenu") == "started" then
Radial.RemoveOption()
end
end
end)
2 changes: 2 additions & 0 deletions client/target/ox.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
if not Config.UseTarget then return end

if not Target.IsOX() then return end

local ZoneIDMap = {}
Expand Down
2 changes: 2 additions & 0 deletions client/target/qb.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
if not Config.UseTarget then return end

if not Target.IsQB() then return end

function Target.RemoveZone(zone)
Expand Down
183 changes: 183 additions & 0 deletions client/target/target.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
if not Config.UseTarget then return end

local TargetPeds = {
Store = {},
ClothingRoom = {},
PlayerOutfitRoom = {}
}

Target = {}

function Target.IsOX()
Expand All @@ -7,3 +15,178 @@ end
function Target.IsQB()
return GetResourceState("qb-target") ~= "missing"
end

local function RemoveTargetPeds(peds)
for i = 1, #peds, 1 do
DeletePed(peds[i])
end
end

local function RemoveTargets()
if Config.EnablePedsForShops then
RemoveTargetPeds(TargetPeds.Store)
else
for k, v in pairs(Config.Stores) do
Target.RemoveZone(v.type .. k)
end
end

if Config.EnablePedsForClothingRooms then
RemoveTargetPeds(TargetPeds.ClothingRoom)
else
for k, v in pairs(Config.ClothingRooms) do
Target.RemoveZone("clothing_" .. (v.job or v.gang) .. k)
end
end

if Config.EnablePedsForPlayerOutfitRooms then
RemoveTargetPeds(TargetPeds.PlayerOutfitRoom)
else
for k in pairs(Config.PlayerOutfitRooms) do
Target.RemoveZone("playeroutfitroom_" .. k)
end
end
end

AddEventHandler("onResourceStop", function(resource)
if resource == GetCurrentResourceName() then
if Target.IsTargetStarted() then
RemoveTargets()
end
end
end)

local function CreatePedAtCoords(pedModel, coords, scenario)
pedModel = type(pedModel) == "string" and joaat(pedModel) or pedModel
lib.requestModel(pedModel)
local ped = CreatePed(0, pedModel, coords.x, coords.y, coords.z - 0.98, coords.w, false, false)
TaskStartScenarioInPlace(ped, scenario, true)
FreezeEntityPosition(ped, true)
SetEntityVisible(ped, true)
SetEntityInvincible(ped, true)
PlaceObjectOnGroundProperly(ped)
SetBlockingOfNonTemporaryEvents(ped, true)
return ped
end

local function SetupStoreTarget(targetConfig, action, k, v)
local parameters = {
options = {{
type = "client",
action = action,
icon = targetConfig.icon,
label = targetConfig.label
}},
distance = targetConfig.distance,
rotation = v.rotation
}

if Config.EnablePedsForShops then
TargetPeds.Store[k] = CreatePedAtCoords(v.targetModel or targetConfig.model, v.coords, v.targetScenario or targetConfig.scenario)
Target.AddTargetEntity(TargetPeds.Store[k], parameters)
elseif v.usePoly then
Target.AddPolyZone(v.type .. k, v.points, parameters)
else
Target.AddBoxZone(v.type .. k, v.coords, v.size, parameters)
end
end

local function SetupStoreTargets()
for k, v in pairs(Config.Stores) do
local targetConfig = Config.TargetConfig[v.type]
local action

if v.type == "barber" then
action = OpenBarberShop
elseif v.type == "clothing" then
action = function()
TriggerEvent("illenium-appearance:client:openClothingShopMenu")
end
elseif v.type == "tattoo" then
action = OpenTattooShop
elseif v.type == "surgeon" then
action = OpenSurgeonShop
end

if not (Config.RCoreTattoosCompatibility and v.type == "tattoo") then
SetupStoreTarget(targetConfig, action, k, v)
end
end
end

local function SetupClothingRoomTargets()
for k, v in pairs(Config.ClothingRooms) do
local targetConfig = Config.TargetConfig["clothingroom"]
local action = function()
local outfits = GetPlayerJobOutfits(v)
TriggerEvent("illenium-appearance:client:openJobOutfitsMenu", outfits)
end

local parameters = {
options = {{
type = "client",
action = action,
icon = targetConfig.icon,
label = targetConfig.label,
canInteract = v.job and CheckDuty or nil,
job = v.job,
gang = v.gang
}},
distance = targetConfig.distance,
rotation = v.rotation
}

local key = "clothing_" .. (v.job or v.gang) .. k
if Config.EnablePedsForClothingRooms then
TargetPeds.ClothingRoom[k] = CreatePedAtCoords(v.targetModel or targetConfig.model, v.coords, v.targetScenario or targetConfig.scenario)
Target.AddTargetEntity(TargetPeds.ClothingRoom[k], parameters)
elseif v.usePoly then
Target.AddPolyZone(key, v.points, parameters)
else
Target.AddBoxZone(key, v.coords, v.size, parameters)
end
end
end

local function SetupPlayerOutfitRoomTargets()
for k, v in pairs(Config.PlayerOutfitRooms) do
local targetConfig = Config.TargetConfig["playeroutfitroom"]

local parameters = {
options = {{
type = "client",
action = function()
OpenOutfitRoom(v)
end,
icon = targetConfig.icon,
label = targetConfig.label,
canInteract = function()
return IsPlayerAllowedForOutfitRoom(v)
end
}},
distance = targetConfig.distance,
rotation = v.rotation
}

if Config.EnablePedsForPlayerOutfitRooms then
TargetPeds.PlayerOutfitRoom[k] = CreatePedAtCoords(v.targetModel or targetConfig.model, v.coords, v.targetScenario or targetConfig.scenario)
Target.AddTargetEntity(TargetPeds.PlayerOutfitRoom[k], parameters)
elseif v.usePoly then
Target.AddPolyZone("playeroutfitroom_" .. k, v.points, parameters)
else
Target.AddBoxZone("playeroutfitroom_" .. k, v.coords, v.size, parameters)
end
end
end

local function SetupTargets()
SetupStoreTargets()
SetupClothingRoomTargets()
SetupPlayerOutfitRoomTargets()
end

CreateThread(function()
if Config.UseTarget then
SetupTargets()
end
end)
Loading

0 comments on commit cf1f7a6

Please sign in to comment.