Use proper deferarals, endless improvements

- 4 tab indents
- Commands reworked
   - now supports source 0 (console)
   - toLower, and duplicate checks in db
- Fixed error messages
    - added seperate error message when `WhiteList` table is empty
    - fixed steam id check
This commit is contained in:
ElPumpo
2018-08-28 19:33:20 +02:00
parent 421826abcf
commit 29f64dd928
8 changed files with 161 additions and 74 deletions
+35 -8
View File
@@ -1,11 +1,38 @@
# ESX Whitelist
# esx_whitelist
## Download & Installation
### Using [fvm](https://github.com/qlaffont/fvm-installer)
```
fvm install --save --folder=esx esx-org/esx_whitelist
```
### Using Git
```
cd resources
git clone https://github.com/ESX-Org/esx_whitelist [esx]/esx_whitelist
```
### Manually
- Download https://github.com/ESX-Org/esx_whitelist/archive/master.zip
- Put it in the `[esx]` directory
## Installation
- Import `esx_whitelist.sql` to your database
- Add this in your `server.cfg`:
1. CD in your resources/[esx] folder.
2. Clone the repository.
```bash
git clone https://github.com/FXServer-ESX/fxserver-esx_whitelist.git esx_whitelist
```
3. Import esx_whitelist.sql in your database.
4. Add `start esx_whitelist` in your server.cfg.
```
start esx_whitelist
```
# Legal
### License
esx_whitelist - Whitelist script
Copyright (C) 2015-2018 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.
This program Is distributed In the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty Of MERCHANTABILITY Or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License For more details.
You should have received a copy Of the GNU General Public License along with this program. If Not, see http://www.gnu.org/licenses/.
+11 -8
View File
@@ -1,13 +1,16 @@
resource_manifest_version '44febabe-d386-4d18-afbe-5e627f4af937'
description 'ESX Whitelist'
version '1.0.2'
version '1.1.0'
server_scripts {
'@mysql-async/lib/MySQL.lua',
'@es_extended/locale.lua',
'config.lua',
'locales/en.lua',
'locales/fr.lua',
'server/main.lua',
'server/commands.lua',
'@mysql-async/lib/MySQL.lua',
'@es_extended/locale.lua',
'config.lua',
'locales/en.lua',
'locales/fr.lua',
'locales/sv.lua',
'server/main.lua',
'server/commands.lua'
}
+3 -2
View File
@@ -1,6 +1,7 @@
USE `essentialmode`;
CREATE TABLE `whitelist` (
`identifier` varchar(60) NOT NULL,
PRIMARY KEY (`identifier`)
`identifier` varchar(60) NOT NULL,
PRIMARY KEY (`identifier`)
);
+3 -1
View File
@@ -1,6 +1,8 @@
Locales['en'] = {
['whitelist_check'] = 'making sure you\'re whitelisted on this server . . .',
['not_whitelisted'] = 'you are not whitelisted on this server',
['steamid_error'] = 'there seems to be a problem with your SteamID',
['steamid_error'] = 'your Steam ID was not found, is Steam running?',
['whitelist_empty'] = 'the whitelist hasn\'t been loaded yet, or alternatively no one has been whitelisted!',
['help_whitelist_add'] = 'add someone to the whitelist',
['help_whitelist_load'] = 'reload the whitelist',
}
+2
View File
@@ -1,6 +1,8 @@
Locales['fr'] = {
['whitelist_check'] = 'making sure you\'re whitelisted on this server . . .',
['not_whitelisted'] = 'ceci est un serveur avec whitelist',
['steamid_error'] = 'nous n\'arrivons pas à lire votre SteamID',
['whitelist_empty'] = 'the whitelist hasn\'t been loaded yet, or alternatively no one has been whitelisted!',
['help_whitelist_add'] = 'ajouter quelqu\'un dans la Whitelist',
['help_whitelist_load'] = 'recharger la Whitelist',
}
+8
View File
@@ -0,0 +1,8 @@
Locales['sv'] = {
['whitelist_check'] = 'making sure you\'re whitelisted on this server . . .',
['not_whitelisted'] = 'du är inte whitelistad på denna server!',
['steamid_error'] = 'ditt Steam ID kunde ej hittas, är Steam påslaget?',
['whitelist_empty'] = 'whitelisten har ännu inte laddats in, eller så är ingen whitelitad!',
['help_whitelist_add'] = 'lägg till någon till whitelisten',
['help_whitelist_load'] = 'ladda om whitelist',
}
+37 -18
View File
@@ -1,23 +1,42 @@
function displayPermissionIssue ()
TriggerClientEvent('chatMessage', source, 'SYSTEM', { 255, 0, 0 }, 'Insufficienct permissions!')
end
TriggerEvent('es:addGroupCommand', 'whitelist:load', 'admin', function (source, args, user)
loadWhiteList()
TriggerEvent('es:addGroupCommand', 'wlrefresh', 'admin', function (source, args, user)
loadWhiteList(function()
TriggerEvent('esx_whitelist:sendMessage', source, 'Whitelist', 'Whitelist reloaded')
end)
end, function (source, args, user)
displayPermissionIssue(source)
TriggerClientEvent('chat:addMessage', source, { args = { '^1SYSTEM', 'Insufficienct permissions!' } })
end, { help = _U('help_whitelist_load') })
TriggerEvent('es:addGroupCommand', 'whitelist:add', 'admin', function (source, args, user)
local steamID = 'steam:' .. args[1]
TriggerEvent('es:addGroupCommand', 'wladd', 'admin', function (source, args, user)
local steamID = 'steam:' .. args[1]:lower()
MySQL.Async.execute(
'INSERT INTO whitelist (identifier) VALUES (@identifier)',
{ ['@identifier'] = tostring(steamID) },
function ()
loadWhiteList()
end
)
if string.len(steamID) ~= 21 then
TriggerEvent('esx_whitelist:sendMessage', source, '^1SYSTEM', 'Invalid steam ID length!')
return
end
MySQL.Async.fetchAll('SELECT * FROM whitelist WHERE identifier = @identifier', {
['@identifier'] = steamID
}, function(result)
if result[1] ~= nil then
TriggerEvent('esx_whitelist:sendMessage', source, '^1SYSTEM', 'The player is already whitelisted on this server!')
else
MySQL.Async.execute('INSERT INTO whitelist (identifier) VALUES (@identifier)', {
['@identifier'] = steamID
}, function (rowsChanged)
table.insert(WhiteList, steamID)
TriggerEvent('esx_whitelist:sendMessage', source, 'Whitelist', 'The player has been whitelisted!')
end)
end
end)
end, function (source, args, user)
displayPermissionIssue(source)
end, { help = _U('help_whitelist_add'), params = { steam = 'SteamID', help = 'SteamID formated to hex' }})
TriggerClientEvent('chat:addMessage', source, { args = { '^1SYSTEM', 'Insufficienct permissions!' } })
end, { help = _U('help_whitelist_add'), params = { steam = 'SteamID', help = 'SteamID formated to hex, begins with 11' }})
-- console / rcon can also utilize es:command events, but breaks since the source isn't a connected player, ending up in error messages
AddEventHandler('esx_whitelist:sendMessage', function(source, title, message)
if source ~= 0 then
TriggerClientEvent('chat:addMessage', source, { args = { title, message } })
else
print('esx_whitelist: ' .. message)
end
end)
+62 -37
View File
@@ -1,45 +1,70 @@
WhiteList = {}
WhiteList = {}
local hasSqlRun = false
function loadWhiteList ()
MySQL.Async.fetchAll(
'SELECT * FROM whitelist',
{},
function (identifiers)
Whitelist = {}
function loadWhiteList(cb)
Whitelist = {}
for i=1, #identifiers, 1 do
table.insert(WhiteList, tostring(identifiers[i].identifier))
end
end
)
MySQL.Async.fetchAll('SELECT * FROM whitelist', {}, function (identifiers)
for i=1, #identifiers, 1 do
table.insert(WhiteList, tostring(identifiers[i].identifier):lower())
end
hasSqlRun = true
if cb ~= nil then
cb()
end
end)
end
MySQL.ready(function ()
loadWhiteList()
AddEventHandler('onMySQLReady', function()
loadWhiteList()
end)
AddEventHandler('playerConnecting', function (playerName, setKickReason)
if (WhiteList == {}) then
Citizen.Wait(1000)
end
Citizen.CreateThread(function()
Citizen.Wait(10000)
local whitelisted = false
local steamID = GetPlayerIdentifiers(source)[1] or false
if steamID == false then
setKickReason(_U('steamid_error'))
CancelEvent()
end
for i = 1, #WhiteList, 1 do
if (tostring(WhiteList[i]) == tostring(steamID)) then
whitelisted = true
break
end
end
if whitelisted == false then
setKickReason(_U('not_whitelisted'))
CancelEvent()
end
if not hasSqlRun then
loadWhiteList()
end
end)
AddEventHandler('playerConnecting', function(name, setCallback, deferrals)
-- Mark this connection as deferred, this is to prevent problems while checking player identifiers.
deferrals.defer()
local _source = source
-- Letting the user know what's going on.
deferrals.update(_U('whitelist_check'))
-- Needed, not sure why.
Citizen.Wait(100)
local whitelisted, kickReason, steamID = false, nil, GetPlayerIdentifiers(_source)[1]
if #WhiteList == 0 then
kickReason = _U('whitelist_empty')
elseif not string.match(steamID, 'steam:1') then
kickReason = _U('steamid_error')
else
for i = 1, #WhiteList, 1 do
if tostring(WhiteList[i]) == tostring(steamID) then
whitelisted = true
break
end
end
if not whitelisted then
kickReason = _U('not_whitelisted')
end
end
if whitelisted then
deferrals.done()
else
deferrals.done(kickReason)
end
end)