Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to be used with oxmysql and latest ESX legacy #39

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Let cops jail people!
4. Select language in `config.lua`
5. (Optional) See below on how to jail via `esx_policejob`

# Dependencies
oxmysql (I removed async since it was crap) - [oxmysql](https://github.com/overextended/oxmysql)

# How to jail

- Use the `esx_jail:sendToJail(source, jailTime)` server side trigger
Expand All @@ -33,18 +36,23 @@ Let cops jail people!
# Based off
- [Original script](https://forum.fivem.net/t/release-fx-jailer-1-1-0-0/41963)
- [dbjailer](https://github.com/SSPU1W/dbjailer)
- [oxmysql](https://github.com/overextended/oxmysql)

# Add to menu

Example in `esx_policejob: client/main.lua`:

```lua
{label = _U('fine'), value = 'fine'},
{label = _U('jail'), value = 'jail'}
{icon = "fas fa-idkyet", title = TranslateCap('fine'), value = 'fine'},
{icon = "fas fa-idkyet", title = TranslateCap('jail'), value = 'jail'},
{icon = "fas fa-idkyet", title = TranslateCap('unpaid_bills'), value = 'unpaid_bills'}


if data2.current.value == 'jail' then
JailPlayer(GetPlayerServerId(closestPlayer))
elseif action == 'license' then
ShowPlayerLicense(closestPlayer)
elseif action == 'jail' then
JailPlayer(GetPlayerServerId(closestPlayer))
elseif action == 'unpaid_bills' then
end

---
Expand All @@ -65,3 +73,9 @@ function JailPlayer(player)
end)
end
```

This is for English local only for esx_policejob. Add this line below to (esx_policejob/locales/en.lua) this is needed so that it says in the interact menu (Jail). This is the same concept for all other locales BUT make sure you edit the line ({label = _U('blah blah'), value = 'blah blah'}) so that it matches your lang. So whatever word you put in Jail you need to match this in locales (['blah blah'] = 'blah blah',)
```
['jail'] = 'jail',
```

42 changes: 23 additions & 19 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
local isInJail, unjail = false, false
local jailTime, fastTimer = 0, 0
ESX = nil

Citizen.CreateThread(function()
while ESX == nil do
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
Citizen.Wait(0)
end
end)
-- Initialize ESX
ESX = exports["es_extended"]:getSharedObject()

RegisterNetEvent('esx_jail:jailPlayer')
AddEventHandler('esx_jail:jailPlayer', function(_jailTime)
jailTime = _jailTime
fastTimer = jailTime

local playerPed = PlayerPedId()

-- Apply prison uniform
TriggerEvent('skinchanger:getSkin', function(skin)
if skin.sex == 0 then
TriggerEvent('skinchanger:loadClothes', skin, Config.Uniforms.prison_wear.male)
Expand All @@ -27,6 +24,7 @@ AddEventHandler('esx_jail:jailPlayer', function(_jailTime)
ESX.Game.Teleport(playerPed, Config.JailLocation)
isInJail, unjail = true, false

-- Main jail loop
while not unjail do
playerPed = PlayerPedId()

Expand All @@ -35,36 +33,40 @@ AddEventHandler('esx_jail:jailPlayer', function(_jailTime)
ClearPedTasksImmediately(playerPed)
end

Citizen.Wait(20000)
Citizen.Wait(20000) -- Wait 20 seconds before checking escape attempt

-- Is the player trying to escape?
if #(GetEntityCoords(playerPed) - Config.JailLocation) > 10 then
local distance = #(GetEntityCoords(playerPed) - vector3(Config.JailLocation.x, Config.JailLocation.y, Config.JailLocation.z))
if distance > 10.0 then
ESX.Game.Teleport(playerPed, Config.JailLocation)
TriggerEvent('chat:addMessage', {args = {_U('judge'), _U('escape_attempt')}, color = {147, 196, 109}})
end
end

-- Release player from jail
ESX.Game.Teleport(playerPed, Config.JailBlip)
isInJail = false

-- Restore player skin
ESX.TriggerServerCallback('esx_skin:getPlayerSkin', function(skin)
TriggerEvent('skinchanger:loadSkin', skin)
end)
end)

-- Jail timer thread
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)

if jailTime > 0 and isInJail then
if fastTimer < 0 then
fastTimer = jailTime
end
Citizen.Wait(1000) -- Update every second

fastTimer = fastTimer - 1
draw2dText(_U('remaining_msg', fastTimer), 0.175, 0.955)

draw2dText(_U('remaining_msg', ESX.Math.Round(fastTimer)), 0.175, 0.955)
fastTimer = fastTimer - 0.01
if fastTimer <= 0 then
TriggerEvent('esx_jail:unjailPlayer')
end
else
Citizen.Wait(500)
Citizen.Wait(5000) -- No need to run the loop frequently if not in jail
end
end
end)
Expand All @@ -80,11 +82,12 @@ AddEventHandler('playerSpawned', function(spawn)
end
end)

-- Create jail blip on the map
Citizen.CreateThread(function()
local blip = AddBlipForCoord(Config.JailBlip)

SetBlipSprite(blip, 188)
SetBlipScale (blip, 1.9)
SetBlipScale(blip, 1.9)
SetBlipColour(blip, 6)
SetBlipAsShortRange(blip, true)

Expand All @@ -93,6 +96,7 @@ Citizen.CreateThread(function()
EndTextCommandSetBlipName(blip)
end)

-- Function to draw 2D text on screen
function draw2dText(text, x, y)
SetTextFont(4)
SetTextScale(0.45, 0.45)
Expand All @@ -104,4 +108,4 @@ function draw2dText(text, x, y)
BeginTextCommandDisplayText('STRING')
AddTextComponentSubstringPlayerName(text)
EndTextCommandDisplayText(x, y)
end
end
9 changes: 5 additions & 4 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
fx_version 'adamant'
fx_version 'cerulean'

game 'gta5'

description 'ESX Jail'

version '1.1.0'

shared_script '@es_extended/imports.lua'

server_scripts {
'@async/async.lua',
'@mysql-async/lib/MySQL.lua',
'@oxmysql/lib/MySQL.lua',
'@es_extended/locale.lua',
'locales/en.lua',
'locales/br.lua',
Expand All @@ -30,5 +31,5 @@ client_scripts {

dependencies {
'es_extended',
'async'
'oxmysql'
}
50 changes: 14 additions & 36 deletions server/main.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
ESX = nil
local playersInJail = {}

TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

AddEventHandler('esx:playerLoaded', function(playerId, xPlayer)
MySQL.Async.fetchAll('SELECT jail_time FROM users WHERE identifier = @identifier', {
['@identifier'] = xPlayer.identifier
}, function(result)
exports.oxmysql:execute('SELECT jail_time FROM users WHERE identifier = ?', { xPlayer.identifier }, function(result)
if result[1] and result[1].jail_time > 0 then
TriggerEvent('esx_jail:sendToJail', xPlayer.source, result[1].jail_time, true)
end
Expand All @@ -17,17 +12,16 @@ AddEventHandler('esx:playerDropped', function(playerId, reason)
playersInJail[playerId] = nil
end)

MySQL.ready(function()
-- Use 'AddEventHandler' to listen for the 'oxmysql:ready' event
AddEventHandler('oxmysql:ready', function()
Citizen.Wait(2000)
local xPlayers = ESX.GetPlayers()

for i=1, #xPlayers do
Citizen.Wait(100)
local xPlayer = ESX.GetPlayerFromId(xPlayers[i])

MySQL.Async.fetchAll('SELECT jail_time FROM users WHERE identifier = @identifier', {
['@identifier'] = xPlayer.identifier
}, function(result)
exports.oxmysql:execute('SELECT jail_time FROM users WHERE identifier = ?', { xPlayer.identifier }, function(result)
if result[1] and result[1].jail_time > 0 then
TriggerEvent('esx_jail:sendToJail', xPlayer.source, result[1].jail_time, true)
end
Expand All @@ -54,19 +48,15 @@ AddEventHandler('esx_jail:sendToJail', function(playerId, jailTime, quiet)

if xPlayer then
if not playersInJail[playerId] then
MySQL.Async.execute('UPDATE users SET jail_time = @jail_time WHERE identifier = @identifier', {
['@identifier'] = xPlayer.identifier,
['@jail_time'] = jailTime
}, function(rowsChanged)
exports.oxmysql:execute('UPDATE users SET jail_time = ? WHERE identifier = ?', { jailTime, xPlayer.identifier }, function(rowsChanged)
xPlayer.triggerEvent('esx_policejob:unrestrain')
xPlayer.triggerEvent('esx_jail:jailPlayer', jailTime)
playersInJail[playerId] = {timeRemaining = jailTime, identifier = xPlayer.getIdentifier()}

if not quiet then
TriggerClientEvent('chat:addMessage', -1, {args = {_U('judge'), _U('jailed_msg', xPlayer.getName(), ESX.Math.Round(jailTime / 60))}, color = {147, 196, 109}})
TriggerClientEvent('chat:addMessage', -1, {args = {'Judge', ('%s has been jailed for %s minutes.'):format(xPlayer.getName(), ESX.Math.Round(jailTime / 60))}, color = {147, 196, 109}})
end
end)

end
end
end)
Expand All @@ -76,10 +66,8 @@ function unjailPlayer(playerId)

if xPlayer then
if playersInJail[playerId] then
MySQL.Async.execute('UPDATE users SET jail_time = 0 WHERE identifier = @identifier', {
['@identifier'] = xPlayer.identifier
}, function(rowsChanged)
TriggerClientEvent('chat:addMessage', -1, {args = {_U('judge'), _U('unjailed', xPlayer.getName())}, color = {147, 196, 109}})
exports.oxmysql:execute('UPDATE users SET jail_time = 0 WHERE identifier = ?', { xPlayer.identifier }, function(rowsChanged)
TriggerClientEvent('chat:addMessage', -1, {args = {'Judge', ('%s has been released from jail.'):format(xPlayer.getName())}, color = {147, 196, 109}})
playersInJail[playerId] = nil
xPlayer.triggerEvent('esx_jail:unjailPlayer')
end)
Expand All @@ -91,34 +79,24 @@ Citizen.CreateThread(function()
while true do
Citizen.Wait(1000)

for playerId,data in pairs(playersInJail) do
for playerId, data in pairs(playersInJail) do
playersInJail[playerId].timeRemaining = data.timeRemaining - 1

if data.timeRemaining < 1 then
unjailPlayer(playerId, false)
unjailPlayer(playerId)
end
end
end
end)

-- Updated thread without 'Async.parallelLimit'
Citizen.CreateThread(function()
while true do
Citizen.Wait(Config.JailTimeSyncInterval)
local tasks = {}

for playerId,data in pairs(playersInJail) do
local task = function(cb)
MySQL.Async.execute('UPDATE users SET jail_time = @time_remaining WHERE identifier = @identifier', {
['@identifier'] = data.identifier,
['@time_remaining'] = data.timeRemaining
}, function(rowsChanged)
cb(rowsChanged)
end)
end

table.insert(tasks, task)
for playerId, data in pairs(playersInJail) do
-- Update jail time for each player
exports.oxmysql:execute('UPDATE users SET jail_time = ? WHERE identifier = ?', { data.timeRemaining, data.identifier })
end

Async.parallelLimit(tasks, 4, function(results) end)
end
end)