1
0
Fork 0
forked from Simnation/Main

Update client.lua

This commit is contained in:
Nordi98 2025-07-29 22:35:35 +02:00
parent 179c2e216c
commit d9b50f59c2

View file

@ -1,97 +1,28 @@
local QBCore = exports['qb-core']:GetCoreObject() local QBCore = exports['qb-core']:GetCoreObject()
local nearbyMachine = nil
local displayingText = false
-- Function to initialize targets -- Function to draw 3D text in the world
function InitializeTargets() function DrawText3D(x, y, z, text)
-- Remove existing targets first to avoid duplicates local onScreen, _x, _y = World3dToScreen2d(x, y, z)
exports['qb-target']:RemoveTargetModel(Config.VendingProps) local px, py, pz = table.unpack(GetGameplayCamCoords())
Wait(100) local scale = 0.35
local font = 4
-- Add targets if onScreen then
exports['qb-target']:AddTargetModel(Config.VendingProps, { SetTextScale(scale, scale)
options = { SetTextFont(font)
{ SetTextProportional(1)
type = "client", SetTextColour(255, 255, 255, 215)
event = "vending:client:buyMachine", SetTextOutline()
icon = "fas fa-dollar-sign", SetTextEntry("STRING")
label = "Automaten kaufen ($" .. Config.VendingMachinePrice .. ")", SetTextCentre(1)
canInteract = function(entity) AddTextComponentString(text)
return not isRegisteredMachine(entity) DrawText(_x, _y)
end end
},
{
type = "client",
event = "vending:client:openBuyMenu",
icon = "fas fa-shopping-cart",
label = "Kaufen",
canInteract = function(entity)
return isRegisteredMachine(entity)
end
},
{
type = "client",
event = "vending:client:openOwnerMenu",
icon = "fas fa-cog",
label = "Verwalten",
canInteract = function(entity)
return canManageMachine(entity)
end
},
{
type = "client",
event = "vending:client:startRobbery",
icon = "fas fa-mask",
label = "Aufbrechen",
canInteract = function(entity)
return isRegisteredMachine(entity) and not canManageMachine(entity)
end
}
},
distance = 2.0
})
print("^2[VENDING]^7 Added targets to " .. #Config.VendingProps .. " vending machine props")
end end
-- Add targets to all vending machine props with multiple attempts (Option 1) -- Function to check if machine is registered
CreateThread(function()
-- First attempt
Wait(2000)
InitializeTargets()
-- Second attempt after a delay
Wait(5000)
InitializeTargets()
-- Third attempt after server is fully loaded
Wait(10000)
InitializeTargets()
end)
-- Event-based initialization (Option 2)
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
Wait(1000)
InitializeTargets()
end)
RegisterNetEvent('QBCore:Client:OnPlayerUnload', function()
-- Nothing to do here, but good to have for completeness
end)
-- Listen for resource start/stop events
AddEventHandler('onResourceStart', function(resourceName)
if resourceName == 'qb-target' or resourceName == GetCurrentResourceName() then
Wait(1000)
InitializeTargets()
end
end)
-- Command to manually refresh targets
RegisterCommand('refreshvendingtargets', function()
InitializeTargets()
QBCore.Functions.Notify('Vending machine targets refreshed', 'success')
end, false)
-- Check if machine is registered
function isRegisteredMachine(entity) function isRegisteredMachine(entity)
local coords = GetEntityCoords(entity) local coords = GetEntityCoords(entity)
local isRegistered = false local isRegistered = false
@ -129,6 +60,81 @@ function canManageMachine(entity)
return canManage return canManage
end end
-- Main thread to detect nearby vending machines
CreateThread(function()
while true do
local playerPed = PlayerPedId()
local playerCoords = GetEntityCoords(playerPed)
local wait = 1000
nearbyMachine = nil
-- Check for nearby vending machines
for _, propName in ipairs(Config.VendingProps) do
local hash = GetHashKey(propName)
local objects = GetGamePool('CObject')
for _, obj in ipairs(objects) do
if GetEntityModel(obj) == hash then
local objCoords = GetEntityCoords(obj)
local dist = #(playerCoords - objCoords)
if dist < 2.0 then
wait = 0
nearbyMachine = obj
-- Display interaction options based on conditions
if not displayingText then
displayingText = true
CreateThread(function()
while nearbyMachine == obj and #(GetEntityCoords(PlayerPedId()) - GetEntityCoords(obj)) < 2.0 do
local z = objCoords.z + 1.0
-- Check conditions and display appropriate text
if isRegisteredMachine(obj) then
if canManageMachine(obj) then
DrawText3D(objCoords.x, objCoords.y, z, "~g~E~w~ - Kaufen | ~y~G~w~ - Verwalten")
-- Handle key presses for management
if IsControlJustReleased(0, 38) then -- E key
TriggerEvent('vending:client:openBuyMenu', {entity = obj})
elseif IsControlJustReleased(0, 47) then -- G key
TriggerEvent('vending:client:openOwnerMenu', {entity = obj})
end
else
DrawText3D(objCoords.x, objCoords.y, z, "~g~E~w~ - Kaufen | ~r~G~w~ - Aufbrechen")
-- Handle key presses for buying/robbery
if IsControlJustReleased(0, 38) then -- E key
TriggerEvent('vending:client:openBuyMenu', {entity = obj})
elseif IsControlJustReleased(0, 47) then -- G key
TriggerEvent('vending:client:startRobbery', {entity = obj})
end
end
else
DrawText3D(objCoords.x, objCoords.y, z, "~g~E~w~ - Automaten kaufen ($" .. Config.VendingMachinePrice .. ")")
-- Handle key press for buying machine
if IsControlJustReleased(0, 38) then -- E key
TriggerEvent('vending:client:buyMachine', {entity = obj})
end
end
Wait(0)
end
displayingText = false
end)
end
break
end
end
end
if nearbyMachine then break end
end
Wait(wait)
end
end)
-- Buy vending machine -- Buy vending machine
RegisterNetEvent('vending:client:buyMachine', function(data) RegisterNetEvent('vending:client:buyMachine', function(data)
local entity = data.entity local entity = data.entity
@ -314,7 +320,7 @@ RegisterNetEvent('vending:client:openOwnerMenu', function(data)
end, coords) end, coords)
end) end)
-- Funktion zum Verkaufen des Automaten -- Function to sell the vending machine
function sellVendingMachine(coords, machineId) function sellVendingMachine(coords, machineId)
local input = lib.inputDialog('Automaten verkaufen', { local input = lib.inputDialog('Automaten verkaufen', {
{ {
@ -624,11 +630,6 @@ RegisterNetEvent('vending:client:policeAlert', function(coords, streetName)
QBCore.Functions.Notify('Verkaufsautomat Aufbruch gemeldet: ' .. streetName, 'error', 8000) QBCore.Functions.Notify('Verkaufsautomat Aufbruch gemeldet: ' .. streetName, 'error', 8000)
end) end)
-- Refresh targets (called when new machine is registered)
RegisterNetEvent('vending:client:refreshTargets', function()
InitializeTargets()
end)
-- Management menu (alternative opening method) -- Management menu (alternative opening method)
RegisterNetEvent('vending:client:openManagement', function(machine) RegisterNetEvent('vending:client:openManagement', function(machine)
lib.registerContext({ lib.registerContext({