This commit is contained in:
Nordi98 2025-07-20 17:07:46 +02:00
parent 5cb18574a7
commit 491ceff534
4 changed files with 186 additions and 152 deletions

View file

@ -1,7 +1,9 @@
local QBCore = exports['qb-core']:GetCoreObject()
local isRobbing = false
local currentPoint = nil
local currentZone = nil
local containerBlip = nil
local inZone = false
local currentZoneId = nil

-- Debug function
local function Debug(msg)
@ -10,7 +12,7 @@ local function Debug(msg)
end
end

-- Completely rewritten DrawText3D function
-- Function to draw 3D text
function DrawText3D(x, y, z, text)
-- Set the text properties
SetTextScale(0.35, 0.35)
@ -27,22 +29,36 @@ function DrawText3D(x, y, z, text)
ClearDrawOrigin()
end

-- Function to find the nearest container point
local function GetNearestContainerPoint()
-- Function to check if a point is inside a polygon
local function IsPointInPolygon(point, polygon)
local x, y = point.x, point.y
local inside = false
local j = #polygon
for i = 1, #polygon do
if (polygon[i].y > y) ~= (polygon[j].y > y) and
x < (polygon[j].x - polygon[i].x) * (y - polygon[i].y) / (polygon[j].y - polygon[i].y) + polygon[i].x then
inside = not inside
end
j = i
end
return inside
end

-- Function to check if player is in any container zone
local function GetCurrentZone()
local playerPed = PlayerPedId()
local playerCoords = GetEntityCoords(playerPed)
local closestPoint = nil
local minDistance = 3.0 -- Maximum interaction distance
for _, point in pairs(Config.ContainerPoints) do
local distance = #(playerCoords - point.coords)
if distance < minDistance then
minDistance = distance
closestPoint = point
for _, zone in pairs(Config.ContainerZones) do
if IsPointInPolygon(playerCoords, zone.points) and
playerCoords.z >= zone.minZ and playerCoords.z <= zone.maxZ then
return zone
end
end
return closestPoint, minDistance
return nil
end

-- Function to create a blip at the robbery location
@ -117,18 +133,18 @@ local function PlayRobberyAnimation(containerType)
end

-- Function to start container robbery
local function StartContainerRobbery(point)
local function StartContainerRobbery(zone)
if isRobbing then return end
isRobbing = true
currentPoint = point
currentZone = zone
-- Get container type from point
local containerType = Config.ContainerTypes[point.type]
-- Get container type from zone
local containerType = Config.ContainerTypes[zone.type]
if not containerType then
QBCore.Functions.Notify("Invalid container type!", "error")
isRobbing = false
currentPoint = nil
currentZone = nil
return
end
@ -137,7 +153,7 @@ local function StartContainerRobbery(point)
if not hasTools then
QBCore.Functions.Notify(Config.Notifications.noTools, "error")
isRobbing = false
currentPoint = nil
currentZone = nil
return
end
@ -146,7 +162,7 @@ local function StartContainerRobbery(point)
if not cooldownCheck.success then
QBCore.Functions.Notify(cooldownCheck.message, "error")
isRobbing = false
currentPoint = nil
currentZone = nil
return
end
@ -155,18 +171,15 @@ local function StartContainerRobbery(point)
if policeCount < Config.PoliceRequired then
QBCore.Functions.Notify(Config.Notifications.notEnoughPolice, "error")
isRobbing = false
currentPoint = nil
currentZone = nil
return
end
-- Position player for animation
SetEntityCoords(PlayerPedId(), point.coords.x, point.coords.y, point.coords.z)
SetEntityHeading(PlayerPedId(), point.heading)
-- Alert police if configured
if containerType.policeAlert then
local streetName = GetStreetNameFromHashKey(GetStreetNameAtCoord(point.coords.x, point.coords.y, point.coords.z))
TriggerServerEvent('container_heist:server:alertPolice', point.coords, streetName, containerType.label)
local playerCoords = GetEntityCoords(PlayerPedId())
local streetName = GetStreetNameFromHashKey(GetStreetNameAtCoord(playerCoords.x, playerCoords.y, playerCoords.z))
TriggerServerEvent('container_heist:server:alertPolice', playerCoords, streetName, containerType.label)
end
-- Start robbery progress bar
@ -179,31 +192,31 @@ local function StartContainerRobbery(point)
disableCombat = true,
}, {}, {}, {}, function() -- Done
-- Success
TriggerServerEvent('container_heist:server:finishRobbery', point.id, point.type)
TriggerServerEvent('container_heist:server:finishRobbery', zone.id, zone.type)
QBCore.Functions.Notify(Config.Notifications.success, "success")
isRobbing = false
currentPoint = nil
currentZone = nil
end, function() -- Cancel
-- Cancelled
QBCore.Functions.Notify(Config.Notifications.failed, "error")
isRobbing = false
currentPoint = nil
currentZone = nil
end)
end)
end, point.id)
end, zone.id)
end)
end

-- Command to start container robbery
RegisterCommand('robcontainer', function()
local point, distance = GetNearestContainerPoint()
-- Register usable item
RegisterNetEvent('container_heist:client:useFlexItem', function()
local zone = GetCurrentZone()
if point then
StartContainerRobbery(point)
if zone then
StartContainerRobbery(zone)
else
QBCore.Functions.Notify("No container nearby!", "error")
QBCore.Functions.Notify(Config.Notifications.notInZone, "error")
end
end, false)
end)

-- Command to toggle debug mode
RegisterCommand('containerdebug', function()
@ -216,52 +229,87 @@ RegisterCommand('containerdebug', function()
end
end, false)

-- Main thread for checking nearby container points and drawing text
-- Main thread for checking if player is in a zone
CreateThread(function()
while true do
local sleep = 1000
local playerPed = PlayerPedId()
local playerCoords = GetEntityCoords(playerPed)
local zone = GetCurrentZone()
for _, point in pairs(Config.ContainerPoints) do
local distance = #(playerCoords - point.coords)
if distance < 10.0 then
sleep = 0
-- Draw marker at the container point
DrawMarker(1, point.coords.x, point.coords.y, point.coords.z - 1.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
1.0, 1.0, 0.2, 0, 255, 0, 100, false, true, 2, false, nil, nil, false)
-- Draw 3D text above the marker when close enough
if distance < 3.0 then
DrawText3D(point.coords.x, point.coords.y, point.coords.z + 0.5, point.label .. " [E]")
-- Check for interaction
if IsControlJustReleased(0, 38) and distance < 1.5 then -- E key
StartContainerRobbery(point)
end
end
if zone then
if not inZone or currentZoneId ~= zone.id then
inZone = true
currentZoneId = zone.id
QBCore.Functions.Notify("You entered " .. zone.label .. ". Use your flex tool to break in.", "primary")
end
end
-- Debug mode - show all container points
if Config.Debug then
for _, point in pairs(Config.ContainerPoints) do
DrawMarker(1, point.coords.x, point.coords.y, point.coords.z - 1.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
1.0, 1.0, 0.2, 255, 0, 0, 100, false, true, 2, false, nil, nil, false)
DrawText3D(point.coords.x, point.coords.y, point.coords.z + 1.0,
point.id .. " (" .. point.type .. ")")
sleep = 500
else
if inZone then
inZone = false
currentZoneId = nil
end
sleep = 0
end
Wait(sleep)
end
end)

-- Debug thread for visualizing zones
CreateThread(function()
while true do
Wait(0)
if Config.Debug then
local playerPed = PlayerPedId()
local playerCoords = GetEntityCoords(playerPed)
for _, zone in pairs(Config.ContainerZones) do
-- Draw lines connecting the points to visualize the zone
for i = 1, #zone.points do
local j = i % #zone.points + 1
local point1 = zone.points[i]
local point2 = zone.points[j]
-- Draw line at ground level
DrawLine(
point1.x, point1.y, zone.minZ,
point2.x, point2.y, zone.minZ,
255, 0, 0, 255
)
-- Draw line at max height
DrawLine(
point1.x, point1.y, zone.maxZ,
point2.x, point2.y, zone.maxZ,
255, 0, 0, 255
)
-- Connect min and max height
DrawLine(
point1.x, point1.y, zone.minZ,
point1.x, point1.y, zone.maxZ,
255, 0, 0, 255
)
}
-- Calculate center of zone for label
local centerX, centerY = 0, 0
for _, point in ipairs(zone.points) do
centerX = centerX + point.x
centerY = centerY + point.y
end
centerX = centerX / #zone.points
centerY = centerY / #zone.points
-- Draw zone label
local centerZ = (zone.minZ + zone.maxZ) / 2
DrawText3D(centerX, centerY, centerZ, zone.id .. " (" .. zone.type .. ")")
end
else
Wait(1000)
end
end
end)

-- Event to show police alert
RegisterNetEvent('container_heist:client:policeAlert', function(coords, streetName, containerLabel)
local playerJob = QBCore.Functions.GetPlayerData().job.name