forked from Simnation/Main
sling
This commit is contained in:
parent
9789a8fd12
commit
bd65997839
2 changed files with 79 additions and 38 deletions
|
@ -9,16 +9,18 @@ version '1.3.0'
|
|||
dependencies {
|
||||
'ox_lib',
|
||||
'/assetpacks',
|
||||
'mysql-async' -- oder 'oxmysql', je nachdem was du verwendest
|
||||
}
|
||||
|
||||
shared_scripts {
|
||||
'@ox_lib/init.lua',
|
||||
"shared/variables.lua", -- Add this line
|
||||
"shared/variables.lua",
|
||||
"shared/*.lua",
|
||||
"config.lua",
|
||||
}
|
||||
|
||||
server_scripts {
|
||||
'@mysql-async/lib/MySQL.lua', -- oder '@mysql-async/lib/MySQL.lua'
|
||||
'version.lua',
|
||||
"server/events.lua",
|
||||
"server/functions.lua",
|
||||
|
|
|
@ -1,42 +1,62 @@
|
|||
local positions = {}
|
||||
local presets = {}
|
||||
local positionsFile = 'positions.json'
|
||||
local presetsFile = 'presets.json'
|
||||
|
||||
-- Load saved positions from JSON file
|
||||
local function LoadPositions()
|
||||
local file = LoadResourceFile(GetCurrentResourceName(), 'json/' .. positionsFile)
|
||||
if file then
|
||||
positions = json.decode(file) or {}
|
||||
-- Load positions from database
|
||||
local function LoadPositionsFromDB()
|
||||
local result = MySQL.query.await('SELECT * FROM weapon_positions')
|
||||
if result then
|
||||
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
|
||||
|
||||
-- Load saved presets from JSON file
|
||||
local function LoadPresets()
|
||||
local file = LoadResourceFile(GetCurrentResourceName(), 'json/' .. presetsFile)
|
||||
if file then
|
||||
presets = json.decode(file) or {}
|
||||
end
|
||||
-- Save position to database
|
||||
local function SavePositionToDB(identifier, weaponName, position, rotation, boneId, isPreset)
|
||||
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)', {
|
||||
identifier,
|
||||
weaponName,
|
||||
position.x,
|
||||
position.y,
|
||||
position.z,
|
||||
rotation.x,
|
||||
rotation.y,
|
||||
rotation.z,
|
||||
boneId,
|
||||
isPreset and 1 or 0
|
||||
})
|
||||
end
|
||||
|
||||
-- Save positions to JSON file
|
||||
local function SavePositions()
|
||||
SaveResourceFile(GetCurrentResourceName(), 'json/' .. positionsFile, json.encode(positions), -1)
|
||||
end
|
||||
|
||||
-- Save presets to JSON file
|
||||
local function SavePresets()
|
||||
SaveResourceFile(GetCurrentResourceName(), 'json/' .. presetsFile, json.encode(presets), -1)
|
||||
-- Delete position from database
|
||||
local function DeletePositionFromDB(identifier, weaponName)
|
||||
MySQL.query.await('DELETE FROM weapon_positions WHERE identifier = ? AND weapon_name = ?', {
|
||||
identifier,
|
||||
weaponName
|
||||
})
|
||||
end
|
||||
|
||||
-- Load data when resource starts
|
||||
CreateThread(function()
|
||||
LoadPositions()
|
||||
LoadPresets()
|
||||
LoadPositionsFromDB()
|
||||
end)
|
||||
|
||||
lib.callback.register('force-sling:callback:getCachedPositions', function(source)
|
||||
return positions
|
||||
local src = source
|
||||
local identifier = GetPlayerIdentifier(src)
|
||||
return positions[identifier] or {}
|
||||
end)
|
||||
|
||||
lib.callback.register('force-sling:callback:getCachedPresets', function(source)
|
||||
|
@ -46,27 +66,31 @@ end)
|
|||
lib.callback.register('force-sling:callback:isPlayerAdmin', function(source)
|
||||
local src = source
|
||||
-- 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}
|
||||
end)
|
||||
|
||||
lib.callback.register('force-sling:callback:resetWeaponPositions', function(source, weapon)
|
||||
local src = source
|
||||
local identifier = GetPlayerIdentifier(src)
|
||||
|
||||
if weapon then
|
||||
positions[weapon] = nil
|
||||
if positions[identifier] then
|
||||
positions[identifier][weapon] = nil
|
||||
DeletePositionFromDB(identifier, weapon)
|
||||
end
|
||||
else
|
||||
positions = {}
|
||||
positions[identifier] = {}
|
||||
MySQL.query.await('DELETE FROM weapon_positions WHERE identifier = ? AND is_preset = 0', {identifier})
|
||||
end
|
||||
SavePositions()
|
||||
return positions
|
||||
|
||||
return positions[identifier] or {}
|
||||
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 identifier = GetPlayerIdentifier(src)
|
||||
|
||||
local data = {
|
||||
coords = position,
|
||||
rot = rotation,
|
||||
|
@ -75,10 +99,25 @@ RegisterNetEvent('force-sling:server:saveWeaponPosition', function(position, rot
|
|||
|
||||
if isPreset then
|
||||
presets[weaponName] = data
|
||||
SavePresets()
|
||||
SavePositionToDB('preset', weaponName, position, rotation, boneId, true)
|
||||
else
|
||||
positions[weaponName] = data
|
||||
SavePositions()
|
||||
if not positions[identifier] then
|
||||
positions[identifier] = {}
|
||||
end
|
||||
positions[identifier][weaponName] = data
|
||||
SavePositionToDB(identifier, weaponName, position, rotation, boneId, false)
|
||||
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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue