From 00b5a75d4f5e413516e4ae0084cc956a1e08ce78 Mon Sep 17 00:00:00 2001 From: ElPumpo Date: Sat, 4 Jan 2020 12:56:46 +0100 Subject: [PATCH] Minor server sided code improvement, fixed #14 --- README.md | 3 ++- server/main.lua | 36 +++++++++++++++++------------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index c1fd14aa..ae32faa2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # esx_joblisting + Simple job listing script, you can specify what jobs you want to be whitelisted in db. ## Download & Installation @@ -30,7 +31,7 @@ start esx_joblisting ### License esx_joblisting - job listing script -Copyright (C) 2015-2018 Jérémie N'gadi +Copyright (C) 2015-2020 Jérémie N'gadi This program Is free software: you can redistribute it And/Or modify it under the terms Of the GNU General Public License As published by the Free Software Foundation, either version 3 Of the License, Or (at your option) any later version. diff --git a/server/main.lua b/server/main.lua index 3802ae40..b7a2d509 100644 --- a/server/main.lua +++ b/server/main.lua @@ -1,37 +1,35 @@ ESX = nil +local availableJobs = {} + TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end) -ESX.RegisterServerCallback('esx_joblisting:getJobsList', function(source, cb) +MySQL.ready(function() MySQL.Async.fetchAll('SELECT * FROM jobs WHERE whitelisted = @whitelisted', { ['@whitelisted'] = false }, function(result) - local data = {} - for i=1, #result, 1 do - table.insert(data, { - job = result[i].name, + table.insert(availableJobs, { + job = result[i].name, label = result[i].label }) end - - cb(data) end) end) +ESX.RegisterServerCallback('esx_joblisting:getJobsList', function(source, cb) + cb(availableJobs) +end) + RegisterServerEvent('esx_joblisting:setJob') AddEventHandler('esx_joblisting:setJob', function(job) - local _source = source - local xPlayer = ESX.GetPlayerFromId(_source) + local xPlayer = ESX.GetPlayerFromId(source) - MySQL.Async.fetchAll('SELECT whitelisted FROM jobs WHERE name = @name', { - ['@name'] = job, - }, function(result) - if not result[1].whitelisted then - xPlayer.setJob(job, 0) - else - print(('esx_joblisting: %s attempted to set a whitelisted job! (lua injector)'):format(xPlayer.identifier)) + if xPlayer then + for k,v in ipairs(availableJobs) do + if v.job == job then + xPlayer.setJob(job, 0) + break + end end - - end) - + end end)