Skip to content

Commit

Permalink
refactor(config): add Config.LockpickItems
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Jul 11, 2023
1 parent 9c99e2b commit 01827c6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
4 changes: 2 additions & 2 deletions client/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ CreateThread(function()
icon = 'fas fa-user-lock',
onSelect = pickLock,
canInteract = canPickLock,
items = 'lockpick',
items = Config.LockpickItems,
distance = 1
}
})
Expand All @@ -287,7 +287,7 @@ CreateThread(function()
icon = 'fas fa-user-lock',
action = pickLock,
canInteract = canPickLock,
item = 'lockpick',
item = Config.LockpickItems,
distance = 1
}
}
Expand Down
32 changes: 23 additions & 9 deletions config.lua
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
Config = {}

-- Send a notification when the door is successfully locked/unlocked.
---Send a notification when the door is successfully locked/unlocked.
---@type boolean
Config.Notify = false

-- Draw a persistent notification while in-range of a door, with a prompt to lock/unlock.
---Draw a persistent notification while in-range of a door, with a prompt to lock/unlock.
---@type boolean
Config.DrawTextUI = false

---Set the properties used by [DrawSprite](https://docs.fivem.net/natives/?_0xE7FFAE5EBF23D890).
---@type { [0]: table, [1]: table }
Config.DrawSprite = {
-- Unlocked
[0] = {'mpsafecracking', 'lock_open', 0, 0, 0.018, 0.018, 0, 255, 255, 255, 100},
[0] = { 'mpsafecracking', 'lock_open', 0, 0, 0.018, 0.018, 0, 255, 255, 255, 100 },

-- Locked
[1] = {'mpsafecracking', 'lock_closed', 0, 0, 0.018, 0.018, 0, 255, 255, 255, 100},
[1] = { 'mpsafecracking', 'lock_closed', 0, 0, 0.018, 0.018, 0, 255, 255, 255, 100 },
}

-- Allow the following ACL principal to use 'command.doorlock'.
---Allow the following ACL principal to use 'command.doorlock'.
---@type string
Config.CommandPrincipal = 'group.admin'

-- Allow players with 'command.doorlock' permission to open any doors.
---Allow players with 'command.doorlock' permission to open any doors.
---@type boolean
Config.PlayerAceAuthorised = false

-- Default skill check difficulty when no fields are defined in the Lockpick tab.
---Default skill check difficulty when no fields are defined in the Lockpick tab.
---@type SkillCheckDifficulity | SkillCheckDifficulity[]
Config.LockDifficulty = { 'easy', 'easy', 'medium' }

-- Allow lockpicks to be used to lock an already unlocked door.
Config.CanPickUnlockedDoors = false
---Allow lockpicks to be used to lock an already unlocked door.
---@type boolean?
Config.CanPickUnlockedDoors = false

---Items that function as lockpicks. Needed for ox_target, and authorisation checks.
---@type string[]
Config.LockpickItems = {
'lockpick'
}
26 changes: 14 additions & 12 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -183,34 +183,35 @@ function RemoveItem(playerId, item, slot)
if player then ox_inventory:RemoveItem(playerId, item, 1, nil, slot) end
end

function DoesPlayerHaveItem(player, items)
---@param player table
---@param items string[] | { name: string, remove?: boolean, metadata?: string }[]
---@param alwaysRemove? boolean
---@return string?
function DoesPlayerHaveItem(player, items, alwaysRemove)
local playerId = player.source or player.PlayerData.source

for i = 1, #items do
local item = items[i]
local data = ox_inventory:Search(playerId, 1, item.name, item.metadata)[1]
local itemName = item.name or item
local data = ox_inventory:Search(playerId, 1, itemName, item.metadata)[1]

if data and data.count > 0 then
if item.remove then
ox_inventory:RemoveItem(playerId, item.name, 1, nil, data.slot)
if alwaysRemove or item.remove then
ox_inventory:RemoveItem(playerId, itemName, 1, nil, data.slot)
end

return item.name
return itemName
end
end
end

local lockpickItems = {
{ name = 'lockpick' }
}

local function isAuthorised(playerId, door, lockpick)
local player = GetPlayer(playerId)
local authorised = door.passcode or false --[[@as boolean?]]
local authorised = door.passcode or false --[[@as boolean | string | nil]]

if player then
if lockpick then
return DoesPlayerHaveItem(player, lockpickItems)
return DoesPlayerHaveItem(player, Config.LockpickItems)
end

if door.characters and table.contains(door.characters, GetCharacterId(player)) then
Expand Down Expand Up @@ -343,7 +344,8 @@ RegisterNetEvent('ox_doorlock:editDoorlock', function(id, data)
end)

RegisterNetEvent('ox_doorlock:breakLockpick', function()
RemoveItem(source, 'lockpick')
local player = GetPlayer(source)
return player and DoesPlayerHaveItem(player, Config.LockpickItems, true)
end)

lib.addCommand('doorlock', {
Expand Down

0 comments on commit 01827c6

Please sign in to comment.