Qbox-project / qbx_core

http://qbox-project.github.io
Other
64 stars 136 forks source link

You can have more than the amount of jobs set via qbx:max_jobs_per_player #400

Closed gamenew09 closed 7 months ago

gamenew09 commented 7 months ago

Summary

You can have more than the amount of jobs set via qbx:max_jobs_per_player.

Reproduction

  1. Set qbx:max_jobs_per_player to 1
  2. Add the first job via a custom command (taxi with grade 0 for instance)
  3. Add the second job via a custom command (police with grade 0 for instance).

Command in question:

---@type table<string, Job>
local Jobs = exports.qbx_core:GetJobs()

RegisterNetEvent("qbx_core:client:onJobUpdate", function (jobName, job)
    Jobs[jobName] = job
end)

lib.addCommand("addjob", {
    help = "(Admin Only) Adds a job to a specified player",
    restricted = "group.admin",
    params = {
        {
            name = "playerId",
            help = "The player to add a job to",
            optional = false,
            type = 'playerId'
        },
        {
            name = "jobName",
            help = "The job to add.",
            optional = false,
            type = 'string'
        },
        {
            name = 'jobGrade',
            help = "The grade of the job",
            optional = true,
            type = 'number'
        },
        {
            name = 'setAsJob',
            help = "If true, then the player will also become the job.",
            optional = true,
            type = 'string'
        }
    }
}, function (source, args, raw)
    local playerId = args.playerId
    local jobName = args.jobName
    local jobGrade = args.jobGrade or 0

    ---@type Player|nil
    local player = exports.qbx_core:GetPlayer(playerId)
    if not player then 
        exports.qbx_core:Notify(source, "Player is not logged in or doesn't exist.", "error")
        return
    end

    if not Jobs[jobName] then
        exports.qbx_core:Notify(source, ("%s is not a valid job."):format(jobName), "error")
        return
    end

    local job = Jobs[jobName]
    if not job.grades[jobGrade] then
        exports.qbx_core:Notify(source, ("Grade %i does not exist in %s."):format(jobGrade, job.label), "error")
        return
    end

    local s, r = pcall(function ()
        exports.qbx_core:AddPlayerToJob(player.PlayerData.citizenid, jobName, jobGrade)
    end)
    if s then
        exports.qbx_core:Notify(source, ("Successfully added job %s (grade %i) to %s %s."):format(
            job.label, 
            jobGrade,
            player.PlayerData?.charinfo?.firstname or "unknown", 
            player.PlayerData?.charinfo?.lastname or "unknown"
        ), "success")
        if args.setAsJob == "true" then
            local s,r = pcall(function ()
                exports.qbx_core:SetPlayerPrimaryJob(player.PlayerData.citizenid, jobName)
            end)
            if s then
                exports.qbx_core:Notify(source, ("Successfully set job for %s %s."):format(
                    player.PlayerData?.charinfo?.firstname or "unknown", 
                    player.PlayerData?.charinfo?.lastname or "unknown"
                ), "success")
            else
                exports.qbx_core:Notify(source, r, "error")
            end
        end
    else
        exports.qbx_core:Notify(source, r, "error")
    end
end)

Expected behavior

For the second job addition to give me back a "player already has maximum amount of jobs allowed" error.

Actual behavior

The second job was added.

Additional context

My guess is that the issue is related to this line of code https://github.com/Qbox-project/qbx_core/blob/594f4e630a70d9c9729ed1b007b0fa3281cfebdb/server/player.lua#L132 Counting a table using # only works on numerical indexed tables that are properly ordered (1, 2, 3, etc).

Current Version

1.7.1

Custom Resources

gmn_qboxmultijob