Skip to content

Commit

Permalink
Speedzones and permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeSzymkowicz committed Jun 3, 2022
1 parent d751628 commit 72cbad3
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 58 deletions.
1 change: 0 additions & 1 deletion client/events.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
RegisterNetEvent('ps-objectspawner:client:containers')
AddEventHandler('ps-objectspawner:client:containers', function(data)
local objectData = data
print(objectData.id)
TriggerServerEvent("inventory:server:OpenInventory", "stash", "container_"..objectData.id, {maxweight = 1000000, slots = 10})
TriggerEvent("inventory:client:SetCurrentStash", "container_"..objectData.id)
end)
128 changes: 87 additions & 41 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local QBCore = exports['qb-core']:GetCoreObject()
local ObjectList = {} -- Object, Model, Coords, IsRendered, SpawnRange

local PlacingObject = false
Expand All @@ -7,7 +8,8 @@ local CurrentObject = nil
local CurrentObjectType = nil
local CurrentSpawnRange = nil
local CurrentCoords = nil
local CurrentDirection = "x"

local group = 'user'

local ObjectTypes = {
"none",
Expand All @@ -18,25 +20,71 @@ local ObjectParams = {
["container"] = {event = "ps-objectspawner:client:containers", icon = "fas fa-question", label = "Container", SpawnRange = 200},
}

--Functions
local function openMenu()
SetNuiFocus(true, true)
if LoadedObjects then
SendNUIMessage({ action = "open"})
else
LoadedObjects = true
SendNUIMessage({ action = "load", objects = Objects, objectTypes = ObjectTypes })
end
end

AddEventHandler('onResourceStart', function(resourceName)
if GetCurrentResourceName() == resourceName then
QBCore.Functions.TriggerCallback('ps-objectspawner:server:RequestObjects', function(incObjectList)
ObjectList = incObjectList
end)

QBCore.Functions.TriggerCallback('qb-afkkick:server:GetPermissions', function(UserGroup)
group = UserGroup
if group == 'god' then
RegisterCommand('object', function()
openMenu()
end)

RegisterCommand('+CancelObject', function()
CancelPlacement()
end)

RegisterKeyMapping("+CancelObject", "Cancel Placing Object", "keyboard", "")
end
end)
end
end)

AddEventHandler('onResourceStop', function(resourceName)
if GetCurrentResourceName() == resourceName then
for k, v in pairs(ObjectList) do
if v["IsRendered"] then
RemoveRoadNodeSpeedZone(v["speedzone"])
DeleteObject(v["object"])
end
end
end
end)

local function openMenu()
SetNuiFocus(true, true)
if LoadedObjects then
SendNUIMessage({ action = "open"})
else
LoadedObjects = true
SendNUIMessage({ action = "load", objects = Objects, objectTypes = ObjectTypes })
end
end
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
QBCore.Functions.TriggerCallback('ps-objectspawner:server:RequestObjects', function(incObjectList)
ObjectList = incObjectList
end)

QBCore.Functions.TriggerCallback('qb-afkkick:server:GetPermissions', function(UserGroup)
group = UserGroup
if group == 'god' then
RegisterCommand('object', function()
openMenu()
end)

RegisterCommand('+CancelObject', function()
CancelPlacement()
end)

RegisterKeyMapping("+CancelObject", "Cancel Placing Object", "keyboard", "")
end
end)
end)

local function ButtonMessage(text)
BeginTextCommandScaleformString("STRING")
Expand Down Expand Up @@ -70,6 +118,13 @@ local function setupScaleform(scaleform)
ButtonMessage("Place object")
PopScaleformMovieFunctionVoid()

PushScaleformMovieFunction(scaleform, "SET_DATA_SLOT")
PushScaleformMovieFunctionParameterInt(1)
Button(GetControlInstructionalButton(2, 190, true))
Button(GetControlInstructionalButton(2, 189, true))
ButtonMessage("Rotate object")
PopScaleformMovieFunctionVoid()

PushScaleformMovieFunction(scaleform, "DRAW_INSTRUCTIONAL_BUTTONS")
PopScaleformMovieFunctionVoid()

Expand Down Expand Up @@ -122,14 +177,13 @@ local function RayCastGamePlayCamera(distance)
end

local function PlaceSpawnedObject(heading)
print(heading)
local ObjectType = 'prop' --will be replaced with inputted prop type later, which will determine options/events
local Options = { SpawnRange = tonumber(CurrentSpawnRange) }
if ObjectParams[CurrentObjectType] ~= nil then
Options = { event = ObjectParams[CurrentObjectType].event, icon = ObjectParams[CurrentObjectType].icon, label = ObjectParams[CurrentObjectType].label, SpawnRange = ObjectParams[CurrentObjectType].SpawnRange} --will be replaced with config of options later
end
local finalCoords = vector4(CurrentCoords.x, CurrentCoords.y, CurrentCoords.z, heading)
TriggerServerEvent("objects:CreateNewObject", CurrentModel, finalCoords, CurrentObjectType, Options)
TriggerServerEvent("ps-objectspawner:server:CreateNewObject", CurrentModel, finalCoords, CurrentObjectType, Options)
DeleteObject(CurrentObject)
PlacingObject = false
CurrentObject = nil
Expand Down Expand Up @@ -188,8 +242,6 @@ local function CreateSpawnedObject(data)
end)
end



local function CancelPlacement()
DeleteObject(CurrentObject)
SetNuiFocus(true, true)
Expand All @@ -210,19 +262,9 @@ RegisterNUICallback('spawn', function(data)
CreateSpawnedObject(data)
end)

RegisterNetEvent("objects:UpdateObjectList", function(NewObjectList)
RegisterNetEvent("ps-objectspawner:client:UpdateObjectList", function(NewObjectList)
ObjectList = NewObjectList
print("Object List Updated")
end)

RegisterCommand('object', function()
openMenu()
end)

RegisterCommand('+CancelObject', function()
CancelPlacement()
end)
RegisterKeyMapping("+CancelObject", "Cancel Placing Object", "keyboard", "")

CreateThread(function()
while true do
Expand All @@ -241,27 +283,29 @@ CreateThread(function()
v["IsRendered"] = true
v["object"] = object

--local model = GetEntityModel(object)
--local min, max = GetModelDimensions(model) --TODO: get max model dimensions to generate the SpeedZone radius
v["speedzone"] = AddRoadNodeSpeedZone(objectCoords["x"], objectCoords["y"], objectCoords["z"], 10.0, 0, false)

for i = 0, 255, 51 do
Wait(50)
SetEntityAlpha(v["object"], i, false)
end

print(v.type, v.type)
if ObjectParams[v.type] ~= nil and ObjectParams[v.type].event ~= nil then
print("HELLO?")
-- exports.qtarget:AddTargetEntity(object, {
-- --debugPoly=true,
-- options = {
-- {
-- name = "object_spawner_"..object,
-- event = ObjectParams[v.type].event,
-- icon = ObjectParams[v.type].icon,
-- label = ObjectParams[v.type].label,
-- id = v.id
-- },
-- },
-- distance = ObjectParams[data.SpawnRange]
-- })
exports.qtarget:AddTargetEntity(object, {
--debugPoly=true,
options = {
{
name = "object_spawner_"..object,
event = ObjectParams[v.type].event,
icon = ObjectParams[v.type].icon,
label = ObjectParams[v.type].label,
id = v.id
},
},
distance = ObjectParams[data.SpawnRange]
})
end
end

Expand All @@ -272,6 +316,8 @@ CreateThread(function()
SetEntityAlpha(v["object"], i, false)
end
DeleteObject(v["object"])

RemoveRoadNodeSpeedZone(v["speedzone"])
v["object"] = nil
v["IsRendered"] = nil
end
Expand All @@ -281,6 +327,6 @@ CreateThread(function()
end
end)

RegisterNetEvent("objects:AddObject", function(object)
RegisterNetEvent("ps-objectspawner:client:AddObject", function(object)
ObjectList[#ObjectList+1] = object
end)
28 changes: 12 additions & 16 deletions server/main.lua
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
local QBCore = exports["qb-core"]:GetCoreObject()
local ServerObjects = {}

RegisterNetEvent("objects:CreateNewObject", function(model, coords, objecttype, options)
print(objecttype)
RegisterNetEvent("ps-objectspawner:server:CreateNewObject", function(model, coords, objecttype, options)
local src = source
print(model)
print(coords)
print(objecttype)
if model and coords then
local data = MySQL.query.await("INSERT INTO objects (model, coords, type, options) VALUES (?, ?, ?, ?)", { model, json.encode(coords), objecttype, json.encode(options) })
ServerObjects[#ServerObjects+1] = {id = data.insertId, model = model, coords = json.encode(coords), type = objecttype, options = json.encode(options)}
TriggerClientEvent("objects:AddObject", -1, {id = data.insertId, model = model, coords = json.encode(coords), type = objecttype, options = json.encode(options)})
TriggerClientEvent("ps-objectspawner:client:AddObject", -1, {id = data.insertId, model = model, coords = json.encode(coords), type = objecttype, options = json.encode(options)})
else
print("[P-OBJECTS]: Object or coords was invalid")
print("[PS-OBJECTSPAWNER]: Object or coords was invalid")
end
end)

function CreateDataObject(mode, coords, type, options)
MySQL.query.await("INSERT INTO objects (model, coords, type, options) VALUES (?, ?, ?, ?)", { model, json.encode(coords), type, json.encode(options) })
end
exports("CreateDataObject", CreateDataObject)

CreateThread(function()
-- MySQL.Async.execute('DELETE FROM objects')
ServerObjects = MySQL.query.await('SELECT * FROM objects', {})
Wait(5000)
TriggerClientEvent("objects:UpdateObjectList", -1, ServerObjects)
--Wait(5000)
--TriggerClientEvent("ps-objectspawner:client:UpdateObjectList", -1, ServerObjects)
end)

QBCore.Functions.CreateCallback("RequestObjects", function(source, cb)
QBCore.Functions.CreateCallback("ps-objectspawner:server:RequestObjects", function(source, cb)
cb(ServerObjects)
end)

local function CreateDataObject(mode, coords, type, options)
MySQL.query.await("INSERT INTO objects (model, coords, type, options) VALUES (?, ?, ?, ?)", { model, json.encode(coords), type, json.encode(options) })
end

exports("CreateDataObject", CreateDataObject)

0 comments on commit 72cbad3

Please sign in to comment.