Skip to content

Commit 6b891b8

Browse files
committed
Update 1.0.1 - Added support for ox_core and formatted code for better readability
1 parent a4085d5 commit 6b891b8

File tree

4 files changed

+71
-34
lines changed

4 files changed

+71
-34
lines changed

client.lua

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
local buying = false -- Variable to prevent multiple purchases
2-
local LastWaterCoolerUse = 0 -- Variable to prevent multiple uses of the water cooler
1+
local buying = false -- Variable to prevent multiple purchases
2+
local LastWaterCoolerUse = 0 -- Variable to prevent multiple uses of the water cooler
33
local TimeoutDuration = Config.WaterCoolerTimeout * 1000 -- Timeout duration for water coolers in milliseconds
4-
local DrinkCount = 0 -- Variable to count the number of drinks
4+
local DrinkCount = 0 -- Variable to count the number of drinks
55

66
if Config.Framework == "esx" then
77
ESX = exports['es_extended']:getSharedObject()
88
elseif Config.Framework == "qb" then
99
QBCore = exports['qb-core']:GetCoreObject()
10+
elseif Config.Framework == "ox" then
11+
Ox = require '@ox_core.lib.init'
1012
else
1113
ESX = exports['es_extended']:getSharedObject()
1214
end
@@ -38,6 +40,21 @@ function Notify(msgtitle, msg, time, type2) -- Notification function
3840
QBCore.Functions.Notify(msg, type2, time)
3941
elseif Config.Framework == 'esx' then
4042
TriggerEvent('esx:showNotification', msg, type2, time)
43+
elseif Config.Framework == 'ox' then
44+
lib.notify({
45+
title = msgtitle,
46+
description = msg,
47+
showDuration = true,
48+
type = type2,
49+
style = {
50+
backgroundColor = 'rgba(0, 0, 0, 0.75)',
51+
color = 'rgba(255, 255, 255, 1)',
52+
['.description'] = {
53+
color = '#909296',
54+
backgroundColor = 'transparent'
55+
}
56+
}
57+
})
4158
end
4259
end
4360
end
@@ -47,7 +64,6 @@ AddEventHandler("muhaddil-machines:Notify", function(msgtitle, msg, time, type)
4764
Notify(msgtitle, msg, time, type)
4865
end)
4966

50-
5167
local function loadAnimDict(animDict)
5268
RequestAnimDict(animDict)
5369
while not HasAnimDictLoaded(animDict) do
@@ -339,7 +355,8 @@ local function newsAnimation(entity)
339355
Citizen.Wait(1000)
340356

341357
loadAnimDict(Config.Animations.newsSellers[1])
342-
TaskPlayAnim(ped, Config.Animations.newsSellers[1], Config.Animations.newsSellers[2], 8.0, 5.0, -1, 1, 1, false, false, false)
358+
TaskPlayAnim(ped, Config.Animations.newsSellers[1], Config.Animations.newsSellers[2], 8.0, 5.0, -1, 1, 1, false,
359+
false, false)
343360
Citizen.Wait(2500)
344361
ClearPedTasks(ped)
345362
RemoveAnimDict(Config.Animations.newsSellers[1])
@@ -397,7 +414,7 @@ local function newsMenu(newsName, entity, items)
397414
lib.showContext('news_menu_' .. newsName)
398415
end
399416

400-
local function setupTargeting(targetSystem)
417+
local function setupTargeting()
401418
for vendingMachineName, data in pairs(Config.machines) do
402419
local options = {
403420
{

config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Config = Config or {}
22

33
Config.DebugMode = true -- Enable debug mode
4-
Config.Framework = 'esx' -- 'esx' or 'qb'
4+
Config.Framework = 'ox' -- 'esx', 'qb' or 'ox'
55
Config.UseOXNotifications = true -- Use OX Notifications or framework notifications
66
Config.Inventory = 'ox'-- 'qs', 'ox' or leave blank
77
Config.NewQBInventory = false -- If you're using the new QB Inventory

fxmanifest.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ lua54 'yes'
44

55
author 'Muhaddil'
66
description 'FiveM script that adds vending machines'
7-
version 'v1.0.0'
7+
version 'v1.0.1'
88

99
shared_scripts {
1010
'config.lua',

server/server.lua

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ if Config.Framework == "esx" then
22
ESX = exports['es_extended']:getSharedObject()
33
elseif Config.Framework == "qb" then
44
QBCore = exports['qb-core']:GetCoreObject()
5+
elseif Config.Framework == "ox" then
6+
Ox = require '@ox_core.lib.init'
57
else
68
ESX = exports['es_extended']:getSharedObject()
79
end
@@ -11,6 +13,8 @@ local function getPlayerObject(src) -- Get the player object
1113
return QBCore.Functions.GetPlayer(src)
1214
elseif Config.Framework == 'esx' then
1315
return ESX.GetPlayerFromId(src)
16+
elseif Config.Framework == 'ox' then
17+
return Ox.GetPlayer(src)
1418
end
1519
end
1620

@@ -37,6 +41,11 @@ local function TakeMoney(playerObject, method, amount) -- Take money from the pl
3741
return true
3842
end
3943
end
44+
elseif Config.Framework == 'ox' then
45+
if exports.ox_inventory:GetItemCount(source, 'money') >= amount then
46+
exports.ox_inventory:RemoveItem(source, 'money', amount)
47+
return true
48+
end
4049
end
4150

4251
return false
@@ -63,6 +72,8 @@ local function giveItem(src, playerObject, item, amount) -- Give the item to the
6372
else
6473
playerObject.addInventoryItem(item.name, amount)
6574
end
75+
elseif Config.Framework == 'ox' then
76+
exports.ox_inventory:AddItem(source, item.name, amount)
6677
end
6778
end
6879

@@ -91,53 +102,62 @@ local function findItemInSource(sourceData, itemName) -- Find the item in the so
91102
return nil
92103
end
93104

94-
RegisterNetEvent('muhaddil-machines:buy', function(sourceType, sourceName, itemName, cantidad) -- Event for buying the item
95-
local src = source
96-
local player = getPlayerObject(src)
97-
98-
local sourceData
99-
if sourceType == 'machine' then
100-
sourceData = Config.machines[sourceName]
101-
elseif sourceType == 'stand' then
102-
sourceData = Config.Stands[sourceName]
103-
elseif sourceType == 'news' then
104-
sourceData = Config.NewsSellers[sourceName]
105-
else
106-
TriggerClientEvent('muhaddil-machines:Notify', src, '', 'El tipo de origen no es válido.', 'error')
107-
return
108-
end
105+
RegisterNetEvent('muhaddil-machines:buy',
106+
function(sourceType, sourceName, itemName, cantidad) -- Event for buying the item
107+
local src = source
108+
local player = getPlayerObject(src)
109109

110-
local item = findItemInSource(sourceData, itemName)
111-
if item then
112-
local totalPrice = item.price * cantidad
113-
handlePurchase(src, player, item, sourceName, totalPrice, cantidad)
114-
else
115-
TriggerClientEvent('muhaddil-machines:Notify', src, '', 'El artículo no está disponible', 'error')
116-
end
117-
end)
110+
local sourceData
111+
if sourceType == 'machine' then
112+
sourceData = Config.machines[sourceName]
113+
elseif sourceType == 'stand' then
114+
sourceData = Config.Stands[sourceName]
115+
elseif sourceType == 'news' then
116+
sourceData = Config.NewsSellers[sourceName]
117+
else
118+
TriggerClientEvent('muhaddil-machines:Notify', src, '', 'El tipo de origen no es válido.', 'error')
119+
return
120+
end
121+
122+
local item = findItemInSource(sourceData, itemName)
123+
if item then
124+
local totalPrice = item.price * cantidad
125+
handlePurchase(src, player, item, sourceName, totalPrice, cantidad)
126+
else
127+
TriggerClientEvent('muhaddil-machines:Notify', src, '', 'El artículo no está disponible', 'error')
128+
end
129+
end)
118130

119131
RegisterServerEvent('muhaddil-machines:RemoveThirst') -- Event for the watercoolers to remove thirst
120132
AddEventHandler('muhaddil-machines:RemoveThirst', function()
121133
local src = source
122-
134+
123135
if Config.Framework == 'qb' then
124136
local player = QBCore.Functions.GetPlayer(src)
125137
if player then
126138
local currentThirst = player.PlayerData.metadata['thirst'] or 0
127139
if currentThirst < 100 then
128140
local newThirst = math.min(currentThirst + Config.ThirstRemoval, 100)
129141
player.Functions.SetMetaData('thirst', newThirst)
130-
142+
131143
TriggerClientEvent('hud:client:UpdateNeeds', src, player.PlayerData.metadata.hunger or 50, newThirst)
132144
else
133145
print("[Info] El jugador " .. src .. " ya tiene la sed máxima (100).")
134146
end
135147
else
136148
print("[Error] No se pudo obtener el jugador para src: " .. tostring(src))
137149
end
138-
139150
elseif Config.Framework == 'esx' then
140151
TriggerClientEvent('esx_status:add', src, 'thirst', Config.ThirstRemoval)
152+
elseif Config.Framework == 'ox' then
153+
local player = Ox.GetPlayer(src)
154+
local beforeStatus = player.getStatus('thirst')
155+
player.removeStatus('thirst', Config.ThirstRemoval)
156+
local afterStatus = player.getStatus('thirst')
157+
DebugPrint("[Info] El jugador " ..
158+
src .. " tenía " .. beforeStatus .. " de sed y ahora tiene " .. afterStatus .. ".")
159+
local statuses = player.getStatuses()
160+
DebugPrint("[Info] Los estados del jugador " .. src .. " son: " .. json.encode(statuses))
141161
else
142162
print("[Error] Configuración de framework no válida.")
143163
end

0 commit comments

Comments
 (0)