1
0
Fork 0
forked from Simnation/Main
This commit is contained in:
Nordi98 2025-06-12 22:07:42 +02:00
parent 9789a8fd12
commit bd65997839
2 changed files with 79 additions and 38 deletions

View file

@ -9,16 +9,18 @@ version '1.3.0'
dependencies { dependencies {
'ox_lib', 'ox_lib',
'/assetpacks', '/assetpacks',
'mysql-async' -- oder 'oxmysql', je nachdem was du verwendest
} }
shared_scripts { shared_scripts {
'@ox_lib/init.lua', '@ox_lib/init.lua',
"shared/variables.lua", -- Add this line "shared/variables.lua",
"shared/*.lua", "shared/*.lua",
"config.lua", "config.lua",
} }
server_scripts { server_scripts {
'@mysql-async/lib/MySQL.lua', -- oder '@mysql-async/lib/MySQL.lua'
'version.lua', 'version.lua',
"server/events.lua", "server/events.lua",
"server/functions.lua", "server/functions.lua",

View file

@ -1,42 +1,62 @@
local positions = {} local positions = {}
local presets = {} local presets = {}
local positionsFile = 'positions.json'
local presetsFile = 'presets.json'
-- Load saved positions from JSON file -- Load positions from database
local function LoadPositions() local function LoadPositionsFromDB()
local file = LoadResourceFile(GetCurrentResourceName(), 'json/' .. positionsFile) local result = MySQL.query.await('SELECT * FROM weapon_positions')
if file then if result then
positions = json.decode(file) or {} for _, data in ipairs(result) do
local weaponData = {
coords = vector3(data.position_x, data.position_y, data.position_z),
rot = vector3(data.rotation_x, data.rotation_y, data.rotation_z),
boneId = data.bone_id
}
if data.is_preset == 1 then
presets[data.weapon_name] = weaponData
else
if not positions[data.identifier] then
positions[data.identifier] = {}
end
positions[data.identifier][data.weapon_name] = weaponData
end
end
end end
end end
-- Load saved presets from JSON file -- Save position to database
local function LoadPresets() local function SavePositionToDB(identifier, weaponName, position, rotation, boneId, isPreset)
local file = LoadResourceFile(GetCurrentResourceName(), 'json/' .. presetsFile) MySQL.query.await('INSERT INTO weapon_positions (identifier, weapon_name, position_x, position_y, position_z, rotation_x, rotation_y, rotation_z, bone_id, is_preset) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE position_x = VALUES(position_x), position_y = VALUES(position_y), position_z = VALUES(position_z), rotation_x = VALUES(rotation_x), rotation_y = VALUES(rotation_y), rotation_z = VALUES(rotation_z), bone_id = VALUES(bone_id)', {
if file then identifier,
presets = json.decode(file) or {} weaponName,
end position.x,
position.y,
position.z,
rotation.x,
rotation.y,
rotation.z,
boneId,
isPreset and 1 or 0
})
end end
-- Save positions to JSON file -- Delete position from database
local function SavePositions() local function DeletePositionFromDB(identifier, weaponName)
SaveResourceFile(GetCurrentResourceName(), 'json/' .. positionsFile, json.encode(positions), -1) MySQL.query.await('DELETE FROM weapon_positions WHERE identifier = ? AND weapon_name = ?', {
end identifier,
weaponName
-- Save presets to JSON file })
local function SavePresets()
SaveResourceFile(GetCurrentResourceName(), 'json/' .. presetsFile, json.encode(presets), -1)
end end
-- Load data when resource starts -- Load data when resource starts
CreateThread(function() CreateThread(function()
LoadPositions() LoadPositionsFromDB()
LoadPresets()
end) end)
lib.callback.register('force-sling:callback:getCachedPositions', function(source) lib.callback.register('force-sling:callback:getCachedPositions', function(source)
return positions local src = source
local identifier = GetPlayerIdentifier(src)
return positions[identifier] or {}
end) end)
lib.callback.register('force-sling:callback:getCachedPresets', function(source) lib.callback.register('force-sling:callback:getCachedPresets', function(source)
@ -46,27 +66,31 @@ end)
lib.callback.register('force-sling:callback:isPlayerAdmin', function(source) lib.callback.register('force-sling:callback:isPlayerAdmin', function(source)
local src = source local src = source
-- Add your admin check logic here -- Add your admin check logic here
-- Example for QBCore:
-- local Player = QBCore.Functions.GetPlayer(src)
-- return Player.PlayerData.admin or false
-- For testing, returning true
return {isAdmin = true} return {isAdmin = true}
end) end)
lib.callback.register('force-sling:callback:resetWeaponPositions', function(source, weapon) lib.callback.register('force-sling:callback:resetWeaponPositions', function(source, weapon)
local src = source local src = source
local identifier = GetPlayerIdentifier(src)
if weapon then if weapon then
positions[weapon] = nil if positions[identifier] then
positions[identifier][weapon] = nil
DeletePositionFromDB(identifier, weapon)
end
else else
positions = {} positions[identifier] = {}
MySQL.query.await('DELETE FROM weapon_positions WHERE identifier = ? AND is_preset = 0', {identifier})
end end
SavePositions()
return positions return positions[identifier] or {}
end) end)
RegisterNetEvent('force-sling:server:saveWeaponPosition', function(position, rotation, weapon, weaponName, boneId, isPreset) RegisterNetEvent('force-sling:server:saveWeaponPosition')
AddEventHandler('force-sling:server:saveWeaponPosition', function(position, rotation, weapon, weaponName, boneId, isPreset)
local src = source local src = source
local identifier = GetPlayerIdentifier(src)
local data = { local data = {
coords = position, coords = position,
rot = rotation, rot = rotation,
@ -75,10 +99,25 @@ RegisterNetEvent('force-sling:server:saveWeaponPosition', function(position, rot
if isPreset then if isPreset then
presets[weaponName] = data presets[weaponName] = data
SavePresets() SavePositionToDB('preset', weaponName, position, rotation, boneId, true)
else else
positions[weaponName] = data if not positions[identifier] then
SavePositions() positions[identifier] = {}
end
positions[identifier][weaponName] = data
SavePositionToDB(identifier, weaponName, position, rotation, boneId, false)
end end
end) end)
-- Helper function to get player identifier
function GetPlayerIdentifier(source)
local identifiers = GetPlayerIdentifiers(source)
-- Modify this based on your framework (e.g., for QBCore or ESX)
for _, identifier in pairs(identifiers) do
if string.find(identifier, 'license:') then
return identifier
end
end
return nil
end