Skip to content

Commit

Permalink
Update 1.0.1 - Added support for ox_core and formatted code for bette…
Browse files Browse the repository at this point in the history
…r readability
  • Loading branch information
Muhaddil committed Dec 26, 2024
1 parent a4085d5 commit 6b891b8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 34 deletions.
29 changes: 23 additions & 6 deletions client.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
local buying = false -- Variable to prevent multiple purchases
local LastWaterCoolerUse = 0 -- Variable to prevent multiple uses of the water cooler
local buying = false -- Variable to prevent multiple purchases
local LastWaterCoolerUse = 0 -- Variable to prevent multiple uses of the water cooler
local TimeoutDuration = Config.WaterCoolerTimeout * 1000 -- Timeout duration for water coolers in milliseconds
local DrinkCount = 0 -- Variable to count the number of drinks
local DrinkCount = 0 -- Variable to count the number of drinks

if Config.Framework == "esx" then
ESX = exports['es_extended']:getSharedObject()
elseif Config.Framework == "qb" then
QBCore = exports['qb-core']:GetCoreObject()
elseif Config.Framework == "ox" then
Ox = require '@ox_core.lib.init'
else
ESX = exports['es_extended']:getSharedObject()
end
Expand Down Expand Up @@ -38,6 +40,21 @@ function Notify(msgtitle, msg, time, type2) -- Notification function
QBCore.Functions.Notify(msg, type2, time)
elseif Config.Framework == 'esx' then
TriggerEvent('esx:showNotification', msg, type2, time)
elseif Config.Framework == 'ox' then
lib.notify({
title = msgtitle,
description = msg,
showDuration = true,
type = type2,
style = {
backgroundColor = 'rgba(0, 0, 0, 0.75)',
color = 'rgba(255, 255, 255, 1)',
['.description'] = {
color = '#909296',
backgroundColor = 'transparent'
}
}
})
end
end
end
Expand All @@ -47,7 +64,6 @@ AddEventHandler("muhaddil-machines:Notify", function(msgtitle, msg, time, type)
Notify(msgtitle, msg, time, type)
end)


local function loadAnimDict(animDict)
RequestAnimDict(animDict)
while not HasAnimDictLoaded(animDict) do
Expand Down Expand Up @@ -339,7 +355,8 @@ local function newsAnimation(entity)
Citizen.Wait(1000)

loadAnimDict(Config.Animations.newsSellers[1])
TaskPlayAnim(ped, Config.Animations.newsSellers[1], Config.Animations.newsSellers[2], 8.0, 5.0, -1, 1, 1, false, false, false)
TaskPlayAnim(ped, Config.Animations.newsSellers[1], Config.Animations.newsSellers[2], 8.0, 5.0, -1, 1, 1, false,
false, false)
Citizen.Wait(2500)
ClearPedTasks(ped)
RemoveAnimDict(Config.Animations.newsSellers[1])
Expand Down Expand Up @@ -397,7 +414,7 @@ local function newsMenu(newsName, entity, items)
lib.showContext('news_menu_' .. newsName)
end

local function setupTargeting(targetSystem)
local function setupTargeting()
for vendingMachineName, data in pairs(Config.machines) do
local options = {
{
Expand Down
2 changes: 1 addition & 1 deletion config.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Config = Config or {}

Config.DebugMode = true -- Enable debug mode
Config.Framework = 'esx' -- 'esx' or 'qb'
Config.Framework = 'ox' -- 'esx', 'qb' or 'ox'
Config.UseOXNotifications = true -- Use OX Notifications or framework notifications
Config.Inventory = 'ox'-- 'qs', 'ox' or leave blank
Config.NewQBInventory = false -- If you're using the new QB Inventory
Expand Down
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ lua54 'yes'

author 'Muhaddil'
description 'FiveM script that adds vending machines'
version 'v1.0.0'
version 'v1.0.1'

shared_scripts {
'config.lua',
Expand Down
72 changes: 46 additions & 26 deletions server/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ if Config.Framework == "esx" then
ESX = exports['es_extended']:getSharedObject()
elseif Config.Framework == "qb" then
QBCore = exports['qb-core']:GetCoreObject()
elseif Config.Framework == "ox" then
Ox = require '@ox_core.lib.init'
else
ESX = exports['es_extended']:getSharedObject()
end
Expand All @@ -11,6 +13,8 @@ local function getPlayerObject(src) -- Get the player object
return QBCore.Functions.GetPlayer(src)
elseif Config.Framework == 'esx' then
return ESX.GetPlayerFromId(src)
elseif Config.Framework == 'ox' then
return Ox.GetPlayer(src)
end
end

Expand All @@ -37,6 +41,11 @@ local function TakeMoney(playerObject, method, amount) -- Take money from the pl
return true
end
end
elseif Config.Framework == 'ox' then
if exports.ox_inventory:GetItemCount(source, 'money') >= amount then
exports.ox_inventory:RemoveItem(source, 'money', amount)
return true
end
end

return false
Expand All @@ -63,6 +72,8 @@ local function giveItem(src, playerObject, item, amount) -- Give the item to the
else
playerObject.addInventoryItem(item.name, amount)
end
elseif Config.Framework == 'ox' then
exports.ox_inventory:AddItem(source, item.name, amount)
end
end

Expand Down Expand Up @@ -91,53 +102,62 @@ local function findItemInSource(sourceData, itemName) -- Find the item in the so
return nil
end

RegisterNetEvent('muhaddil-machines:buy', function(sourceType, sourceName, itemName, cantidad) -- Event for buying the item
local src = source
local player = getPlayerObject(src)

local sourceData
if sourceType == 'machine' then
sourceData = Config.machines[sourceName]
elseif sourceType == 'stand' then
sourceData = Config.Stands[sourceName]
elseif sourceType == 'news' then
sourceData = Config.NewsSellers[sourceName]
else
TriggerClientEvent('muhaddil-machines:Notify', src, '', 'El tipo de origen no es válido.', 'error')
return
end
RegisterNetEvent('muhaddil-machines:buy',
function(sourceType, sourceName, itemName, cantidad) -- Event for buying the item
local src = source
local player = getPlayerObject(src)

local item = findItemInSource(sourceData, itemName)
if item then
local totalPrice = item.price * cantidad
handlePurchase(src, player, item, sourceName, totalPrice, cantidad)
else
TriggerClientEvent('muhaddil-machines:Notify', src, '', 'El artículo no está disponible', 'error')
end
end)
local sourceData
if sourceType == 'machine' then
sourceData = Config.machines[sourceName]
elseif sourceType == 'stand' then
sourceData = Config.Stands[sourceName]
elseif sourceType == 'news' then
sourceData = Config.NewsSellers[sourceName]
else
TriggerClientEvent('muhaddil-machines:Notify', src, '', 'El tipo de origen no es válido.', 'error')
return
end

local item = findItemInSource(sourceData, itemName)
if item then
local totalPrice = item.price * cantidad
handlePurchase(src, player, item, sourceName, totalPrice, cantidad)
else
TriggerClientEvent('muhaddil-machines:Notify', src, '', 'El artículo no está disponible', 'error')
end
end)

RegisterServerEvent('muhaddil-machines:RemoveThirst') -- Event for the watercoolers to remove thirst
AddEventHandler('muhaddil-machines:RemoveThirst', function()
local src = source

if Config.Framework == 'qb' then
local player = QBCore.Functions.GetPlayer(src)
if player then
local currentThirst = player.PlayerData.metadata['thirst'] or 0
if currentThirst < 100 then
local newThirst = math.min(currentThirst + Config.ThirstRemoval, 100)
player.Functions.SetMetaData('thirst', newThirst)

TriggerClientEvent('hud:client:UpdateNeeds', src, player.PlayerData.metadata.hunger or 50, newThirst)
else
print("[Info] El jugador " .. src .. " ya tiene la sed máxima (100).")
end
else
print("[Error] No se pudo obtener el jugador para src: " .. tostring(src))
end

elseif Config.Framework == 'esx' then
TriggerClientEvent('esx_status:add', src, 'thirst', Config.ThirstRemoval)
elseif Config.Framework == 'ox' then
local player = Ox.GetPlayer(src)
local beforeStatus = player.getStatus('thirst')
player.removeStatus('thirst', Config.ThirstRemoval)
local afterStatus = player.getStatus('thirst')
DebugPrint("[Info] El jugador " ..
src .. " tenía " .. beforeStatus .. " de sed y ahora tiene " .. afterStatus .. ".")
local statuses = player.getStatuses()
DebugPrint("[Info] Los estados del jugador " .. src .. " son: " .. json.encode(statuses))
else
print("[Error] Configuración de framework no válida.")
end
Expand Down

0 comments on commit 6b891b8

Please sign in to comment.