forked from Simnation/Main
ed
This commit is contained in:
parent
d2b98317d3
commit
b2c41ba5eb
4 changed files with 214 additions and 380 deletions
|
@ -1,85 +1,131 @@
|
|||
local QBCore = exports['qb-core']:GetCoreObject()
|
||||
local vehicles = {}
|
||||
|
||||
-- Debug Funktion
|
||||
local function Debug(msg)
|
||||
if Config.Debug then
|
||||
print("[AntiDespawn] " .. msg)
|
||||
end
|
||||
end
|
||||
|
||||
-- Erstelle Tabelle bei Serverstart
|
||||
CreateThread(function()
|
||||
MySQL.query([[
|
||||
CREATE TABLE IF NOT EXISTS `vehicle_positions` (
|
||||
CREATE TABLE IF NOT EXISTS `vehicle_antidespawn` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`plate` varchar(50) NOT NULL,
|
||||
`citizenid` varchar(50) NOT NULL,
|
||||
`model` varchar(50) NOT NULL,
|
||||
`position` longtext NOT NULL,
|
||||
`rotation` longtext NOT NULL,
|
||||
`engine_health` float DEFAULT 1000.0,
|
||||
`body_health` float DEFAULT 1000.0,
|
||||
`coords` longtext NOT NULL,
|
||||
`heading` float NOT NULL,
|
||||
`fuel` int(11) DEFAULT 100,
|
||||
`mods` longtext DEFAULT NULL,
|
||||
`last_updated` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `plate` (`plate`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
]])
|
||||
end)
|
||||
|
||||
-- Speichere Fahrzeugposition
|
||||
RegisterNetEvent('vehicle-persistence:server:saveVehiclePosition', function(vehicleData)
|
||||
local src = source
|
||||
local Player = QBCore.Functions.GetPlayer(src)
|
||||
|
||||
if not Player then return end
|
||||
Debug("Datenbank initialisiert")
|
||||
|
||||
local query = [[
|
||||
INSERT INTO vehicle_positions (plate, citizenid, model, position, rotation, engine_health, body_health, fuel, mods)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
position = VALUES(position),
|
||||
rotation = VALUES(rotation),
|
||||
engine_health = VALUES(engine_health),
|
||||
body_health = VALUES(body_health),
|
||||
fuel = VALUES(fuel),
|
||||
mods = VALUES(mods),
|
||||
last_updated = CURRENT_TIMESTAMP
|
||||
]]
|
||||
|
||||
MySQL.insert(query, {
|
||||
vehicleData.plate,
|
||||
Player.PlayerData.citizenid,
|
||||
vehicleData.model,
|
||||
json.encode(vehicleData.position),
|
||||
json.encode(vehicleData.rotation),
|
||||
vehicleData.engineHealth,
|
||||
vehicleData.bodyHealth,
|
||||
vehicleData.fuel,
|
||||
json.encode(vehicleData.mods)
|
||||
})
|
||||
|
||||
if Config.Debug then
|
||||
print(string.format("Saved vehicle position for plate: %s", vehicleData.plate))
|
||||
end
|
||||
end)
|
||||
|
||||
-- Lade gespeicherte Fahrzeuge beim Serverstart
|
||||
RegisterNetEvent('vehicle-persistence:server:loadVehicles', function()
|
||||
local src = source
|
||||
local Player = QBCore.Functions.GetPlayer(src)
|
||||
|
||||
if not Player then return end
|
||||
|
||||
MySQL.query('SELECT * FROM vehicle_positions WHERE citizenid = ?', {
|
||||
Player.PlayerData.citizenid
|
||||
}, function(result)
|
||||
if result and #result > 0 then
|
||||
TriggerClientEvent('vehicle-persistence:client:spawnSavedVehicles', src, result)
|
||||
-- Lade alle Fahrzeuge aus der Datenbank
|
||||
MySQL.query("SELECT * FROM vehicle_antidespawn", {}, function(results)
|
||||
if results and #results > 0 then
|
||||
Debug("Lade " .. #results .. " Fahrzeuge aus der Datenbank")
|
||||
|
||||
for _, vehicle in pairs(results) do
|
||||
vehicles[vehicle.plate] = {
|
||||
model = vehicle.model,
|
||||
coords = json.decode(vehicle.coords),
|
||||
heading = vehicle.heading,
|
||||
fuel = vehicle.fuel,
|
||||
last_updated = vehicle.last_updated
|
||||
}
|
||||
end
|
||||
end
|
||||
end)
|
||||
end)
|
||||
|
||||
-- Entferne Fahrzeug aus Datenbank
|
||||
RegisterNetEvent('vehicle-persistence:server:removeVehicle', function(plate)
|
||||
MySQL.execute('DELETE FROM vehicle_positions WHERE plate = ?', {plate})
|
||||
-- Registriere ein Fahrzeug
|
||||
RegisterNetEvent('antidespawn:server:registerVehicle', function(plate, model, coords, heading)
|
||||
local src = source
|
||||
|
||||
if Config.Debug then
|
||||
print(string.format("Removed vehicle from persistence: %s", plate))
|
||||
if not plate or not model or not coords then
|
||||
Debug("Ungültige Daten beim Registrieren eines Fahrzeugs")
|
||||
return
|
||||
end
|
||||
|
||||
vehicles[plate] = {
|
||||
model = model,
|
||||
coords = coords,
|
||||
heading = heading,
|
||||
fuel = 100,
|
||||
last_updated = os.time()
|
||||
}
|
||||
|
||||
MySQL.query("INSERT INTO vehicle_antidespawn (plate, model, coords, heading, fuel) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE coords = VALUES(coords), heading = VALUES(heading), last_updated = CURRENT_TIMESTAMP", {
|
||||
plate,
|
||||
model,
|
||||
json.encode(coords),
|
||||
heading,
|
||||
100
|
||||
})
|
||||
|
||||
Debug("Fahrzeug registriert: " .. plate)
|
||||
end)
|
||||
|
||||
-- Aktualisiere ein Fahrzeug
|
||||
RegisterNetEvent('antidespawn:server:updateVehicle', function(plate, coords, heading)
|
||||
if not vehicles[plate] then return end
|
||||
|
||||
vehicles[plate].coords = coords
|
||||
vehicles[plate].heading = heading
|
||||
vehicles[plate].last_updated = os.time()
|
||||
|
||||
MySQL.query("UPDATE vehicle_antidespawn SET coords = ?, heading = ?, last_updated = CURRENT_TIMESTAMP WHERE plate = ?", {
|
||||
json.encode(coords),
|
||||
heading,
|
||||
plate
|
||||
})
|
||||
|
||||
Debug("Fahrzeug aktualisiert: " .. plate)
|
||||
end)
|
||||
|
||||
-- Entferne ein Fahrzeug
|
||||
RegisterNetEvent('antidespawn:server:removeVehicle', function(plate)
|
||||
if not vehicles[plate] then return end
|
||||
|
||||
vehicles[plate] = nil
|
||||
|
||||
MySQL.query("DELETE FROM vehicle_antidespawn WHERE plate = ?", {
|
||||
plate
|
||||
})
|
||||
|
||||
Debug("Fahrzeug entfernt: " .. plate)
|
||||
end)
|
||||
|
||||
-- Lade Fahrzeuge für einen Spieler
|
||||
RegisterNetEvent('antidespawn:server:loadVehicles', function()
|
||||
local src = source
|
||||
local Player = QBCore.Functions.GetPlayer(src)
|
||||
|
||||
if not Player then return end
|
||||
|
||||
local playerCoords = GetEntityCoords(GetPlayerPed(src))
|
||||
|
||||
-- Lade nur Fahrzeuge in der Nähe des Spielers
|
||||
for plate, vehicle in pairs(vehicles) do
|
||||
local distance = #(playerCoords - vector3(vehicle.coords.x, vehicle.coords.y, vehicle.coords.z))
|
||||
|
||||
if distance < 100.0 then
|
||||
TriggerClientEvent('antidespawn:client:spawnVehicle', src, {
|
||||
plate = plate,
|
||||
model = vehicle.model,
|
||||
coords = vehicle.coords,
|
||||
heading = vehicle.heading,
|
||||
fuel = vehicle.fuel
|
||||
})
|
||||
|
||||
Debug("Fahrzeug für Spieler geladen: " .. plate)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
|
@ -87,30 +133,7 @@ end)
|
|||
CreateThread(function()
|
||||
while true do
|
||||
Wait(3600000) -- 1 Stunde
|
||||
MySQL.execute('DELETE FROM vehicle_positions WHERE last_updated < DATE_SUB(NOW(), INTERVAL 24 HOUR)')
|
||||
if Config.Debug then
|
||||
print("Cleaned up old vehicle positions")
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
-- jg-advanced-garage Events
|
||||
RegisterNetEvent('jg-advancedgarages:server:vehicle-stored', function(data)
|
||||
-- Entferne Fahrzeug aus Persistence wenn es in Garage gespeichert wird
|
||||
if data and data.plate then
|
||||
TriggerEvent('vehicle-persistence:server:removeVehicle', data.plate)
|
||||
if Config.Debug then
|
||||
print(string.format("Vehicle stored in garage, removed from persistence: %s", data.plate))
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
RegisterNetEvent('jg-advancedgarages:server:vehicle-spawned', function(data)
|
||||
-- Entferne aus Persistence da Fahrzeug jetzt über Garage gespawnt wurde
|
||||
if data and data.plate then
|
||||
TriggerEvent('vehicle-persistence:server:removeVehicle', data.plate)
|
||||
if Config.Debug then
|
||||
print(string.format("Vehicle spawned from garage, removed from persistence: %s", data.plate))
|
||||
end
|
||||
MySQL.query("DELETE FROM vehicle_antidespawn WHERE last_updated < DATE_SUB(NOW(), INTERVAL 24 HOUR)")
|
||||
Debug("Alte Fahrzeugeinträge bereinigt")
|
||||
end
|
||||
end)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue