├── README.md ├── esplugin_mysql.sql ├── fxmanifest.lua ├── migrations ├── 0001_create_user.cs └── 0002_add_roles.cs └── server.lua /README.md: -------------------------------------------------------------------------------- 1 | # esplugin_mysql 2 | 3 | An MySQL plugin for [EssentialMode](https://github.com/kanersps/essentialmode). 4 | 5 | ## Installation 6 | 7 | - Download [esplugin_mysql](https://github.com/kanersps/esplugin_mysql/archive/master.zip) 8 | - Make sure you have [mysql-async](https://github.com/brouznouf/fivem-mysql-async) installed and working 9 | - Import the SQL file provided with this project, `esplugin_mysql.sql` (You can also use FXMigrant by uncommenting lines in fxmanifest.lua) 10 | - Add the following convar in your server configuration file: `set es_enableCustomData 1`. Make sure you put it directly under `mysql_connection_string` 11 | - Make your load order is correct. Here's how it should look: 12 | 13 | ```bash 14 | start mysql-async 15 | start essentialmode 16 | start esplugin_mysql 17 | ``` 18 | -------------------------------------------------------------------------------- /esplugin_mysql.sql: -------------------------------------------------------------------------------- 1 | -- -------------------------------------------------------- 2 | -- Host: 127.0.0.1 3 | -- Server version: 10.1.22-MariaDB - mariadb.org binary distribution 4 | -- Server OS: Win64 5 | -- HeidiSQL Version: 9.4.0.5125 6 | -- -------------------------------------------------------- 7 | 8 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 9 | /*!40101 SET NAMES utf8 */; 10 | /*!50503 SET NAMES utf8mb4 */; 11 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 12 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 13 | 14 | 15 | -- Dumping database structure for essentialmode 16 | CREATE DATABASE IF NOT EXISTS `essentialmode` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin */; 17 | USE `essentialmode`; 18 | 19 | -- Dumping structure for table essentialmode.users 20 | CREATE TABLE IF NOT EXISTS `users` ( 21 | `identifier` varchar(50) COLLATE utf8mb4_bin NOT NULL, 22 | `license` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL, 23 | `money` int(11) DEFAULT NULL, 24 | `bank` int(11) DEFAULT NULL, 25 | `permission_level` int(11) DEFAULT NULL, 26 | `group` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL, 27 | PRIMARY KEY (`identifier`) 28 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; 29 | 30 | -- Data exporting was unselected. 31 | /*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */; 32 | /*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */; 33 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 34 | -------------------------------------------------------------------------------- /fxmanifest.lua: -------------------------------------------------------------------------------- 1 | fx_version 'bodacious' 2 | games { 'rdr3', 'gta5' } 3 | 4 | author 'Kanersps' 5 | description 'A MySQL plugin for EssentialMode' 6 | 7 | --[[migration_files { 8 | 'migrations/0001_create_user.cs', 9 | 'migrations/0002_add_roles.cs' 10 | }]] 11 | 12 | server_scripts { 13 | '@mysql-async/lib/MySQL.lua', 14 | --'@fxmigrant/helper.lua', 15 | 'server.lua' 16 | } 17 | 18 | dependencies { 19 | 'essentialmode', 20 | 'mysql-async' 21 | --'fxmigrant' 22 | } 23 | -------------------------------------------------------------------------------- /migrations/0001_create_user.cs: -------------------------------------------------------------------------------- 1 | using FluentMigrator; 2 | 3 | [Migration(1)] 4 | public class CreateUser : Migration 5 | { 6 | public override void Up() 7 | { 8 | Create.Table("users") 9 | .WithColumn("identifier").AsString(50).PrimaryKey().NotNullable() 10 | .WithColumn("license").AsString(50).NotNullable() 11 | .WithColumn("bank").AsInt32().NotNullable().WithDefaultValue(-1) 12 | .WithColumn("permission_level").AsInt32().NotNullable().WithDefaultValue(0) 13 | .WithColumn("money").AsInt32().NotNullable().WithDefaultValue(0) 14 | .WithColumn("group").AsString(500).NotNullable(); 15 | } 16 | 17 | public override void Down() 18 | { 19 | Delete.Table("users"); 20 | } 21 | } -------------------------------------------------------------------------------- /migrations/0002_add_roles.cs: -------------------------------------------------------------------------------- 1 | using FluentMigrator; 2 | 3 | [Migration(2)] 4 | public class AddRoles : Migration 5 | { 6 | public override void Up() 7 | { 8 | Create.Column("roles") 9 | .OnTable("users") 10 | .AsString(500) 11 | .Nullable() 12 | .WithDefaultValue(""); 13 | } 14 | 15 | public override void Down() 16 | { 17 | Delete.Column("roles").FromTable("users"); 18 | } 19 | } -------------------------------------------------------------------------------- /server.lua: -------------------------------------------------------------------------------- 1 | -- Make sure you set this convar: 2 | -- set es_enableCustomData 1 3 | 4 | AddEventHandler('es_db:doesUserExist', function(identifier, callback) 5 | MySQL.Async.fetchScalar('SELECT COUNT(1) FROM users WHERE identifier = @identifier', { ['@identifier'] = identifier }, function(users) 6 | if users > 0 then 7 | callback(true) 8 | else 9 | callback(false) 10 | end 11 | end) 12 | end) 13 | 14 | AddEventHandler('es_db:retrieveUser', function(identifier, callback) 15 | local Callback = callback 16 | MySQL.Async.fetchAll('SELECT * FROM users WHERE `identifier`=@identifier;', {identifier = identifier}, function(users) 17 | if users[1] then 18 | Callback(users[1]) 19 | else 20 | Callback(false) 21 | end 22 | end) 23 | end) 24 | 25 | AddEventHandler('es_db:createUser', function(identifier, license, cash, bank, callback) 26 | local user = { 27 | identifier = identifier, 28 | money = cash or 0, 29 | bank = bank or 0, 30 | license = license, 31 | group = 'user', 32 | permission_level = 0 33 | } 34 | 35 | MySQL.Async.execute('INSERT INTO users (`identifier`, `money`, `bank`, `group`, `permission_level`, `license`) VALUES (@identifier, @money, @bank, @group, @permission_level, @license);', 36 | { 37 | identifier = user.identifier, 38 | money = user.money, 39 | bank = user.bank, 40 | permission_level = user.permission_level, 41 | group = user.group, 42 | license = user.license 43 | }, function(rowsChanged) 44 | callback() 45 | end) 46 | end) 47 | 48 | AddEventHandler('es_db:retrieveLicensedUser', function(license, callback) 49 | MySQL.Async.fetchAll('SELECT * FROM users WHERE `license`=@license;', {license = license}, function(users) 50 | if users[1] then 51 | callback(users[1]) 52 | else 53 | callback(false) 54 | end 55 | end) 56 | end) 57 | 58 | AddEventHandler('es_db:doesLicensedUserExist', function(license, callback) 59 | MySQL.Async.fetchScalar('SELECT COUNT(1) FROM users WHERE license = @license', { ['@license'] = license }, function(users) 60 | if users > 0 then 61 | callback(true) 62 | else 63 | callback(false) 64 | end 65 | end) 66 | end) 67 | 68 | AddEventHandler('es_db:updateUser', function(identifier, new, callback) 69 | Citizen.CreateThread(function() 70 | --if(#new ~= 0)then 71 | local updateString = '' 72 | local params = {identifier = identifier} 73 | 74 | local length = tLength(new) 75 | local cLength = 1 76 | for k,v in pairs(new) do 77 | if (type(k) == 'string') then 78 | updateString = updateString .. '`' .. k .. '`=@' .. k 79 | params[k] = v 80 | if cLength < length then 81 | updateString = updateString .. ', ' 82 | end 83 | end 84 | cLength = cLength + 1 85 | end 86 | 87 | MySQL.Async.execute('UPDATE users SET ' .. updateString .. ' WHERE `identifier`=@identifier', params, function(rowsChanged) 88 | if callback then 89 | callback(true) 90 | end 91 | end) 92 | --end 93 | end) 94 | end) 95 | 96 | function tLength(t) 97 | local l = 0 98 | for k,v in pairs(t)do 99 | l = l + 1 100 | end 101 | return l 102 | end 103 | --------------------------------------------------------------------------------