This commit is contained in:
Nordi98 2025-08-02 13:51:31 +02:00
parent d734800a74
commit a700c6d796
3 changed files with 124 additions and 142 deletions

View file

@ -8,29 +8,27 @@ RegisterNetEvent('pet-bowls:client:loadBowls', function(bowls)
local coords = json.decode(bowl.coords)
local bowlCoords = vector3(coords.x, coords.y, coords.z)
-- Create the bowl object
local hash = GetHashKey(bowl.model)
RequestModel(hash)
while not HasModelLoaded(hash) do
Wait(10)
-- Add target to existing bowl objects
local bowlObjects = GetGamePool('CObject')
for _, object in pairs(bowlObjects) do
local objectCoords = GetEntityCoords(object)
local distance = #(objectCoords - bowlCoords)
if distance < 0.5 and GetEntityModel(object) == GetHashKey(bowl.model) then
-- Found the bowl object
placedBowls[bowl.bowl_id] = {
object = object,
id = bowl.bowl_id,
model = bowl.model,
type = bowl.type,
fillLevel = bowl.fill_level
}
-- Add target
AddTargetToBowl(object, bowl.bowl_id, bowl.type)
break
end
end
local bowlObject = CreateObject(hash, bowlCoords.x, bowlCoords.y, bowlCoords.z, false, false, false)
SetEntityHeading(bowlObject, coords.w)
FreezeEntityPosition(bowlObject, true)
SetEntityAsMissionEntity(bowlObject, true, true)
-- Store in local table
placedBowls[bowl.bowl_id] = {
object = bowlObject,
id = bowl.bowl_id,
model = bowl.model,
type = bowl.type,
fillLevel = bowl.fill_level
}
-- Add target
AddTargetToBowl(bowlObject, bowl.bowl_id, bowl.type)
end
end)
@ -59,17 +57,6 @@ function AddTargetToBowl(bowlObject, bowlId, bowlType)
canInteract = function()
return true
end,
},
{
type = "client",
icon = "fas fa-trash",
label = "Pick Up Bowl",
action = function()
PickUpBowl(bowlId)
end,
canInteract = function()
return true
end,
}
},
distance = 2.0
@ -134,101 +121,6 @@ function FillBowl(bowlId, itemName, fillAmount)
end
end
-- Function to pick up a bowl
function PickUpBowl(bowlId)
local bowl = placedBowls[bowlId]
if not bowl then return end
-- Delete the object and remove from server
if DoesEntityExist(bowl.object) then
DeleteEntity(bowl.object)
end
TriggerServerEvent('pet-bowls:server:removeBowl', bowlId)
placedBowls[bowlId] = nil
end
-- Command to place a bowl
RegisterCommand('placebowl', function()
OpenPlaceBowlMenu()
end)
-- Function to open place bowl menu
function OpenPlaceBowlMenu()
local options = {}
for _, bowl in pairs(Config.BowlProps) do
table.insert(options, {
title = bowl.label,
description = 'Type: ' .. (bowl.type == 'food' and 'Food Bowl' or 'Water Bowl'),
onSelect = function()
PlaceBowl(bowl)
end
})
end
lib.registerContext({
id = 'place_bowl_menu',
title = 'Place Bowl',
options = options
})
lib.showContext('place_bowl_menu')
end
-- Function to place a bowl
function PlaceBowl(bowlConfig)
local playerPed = PlayerPedId()
local coords = GetEntityCoords(playerPed)
local heading = GetEntityHeading(playerPed)
-- Create the bowl object
local hash = GetHashKey(bowlConfig.model)
RequestModel(hash)
while not HasModelLoaded(hash) do
Wait(10)
end
local forward = GetEntityForwardVector(playerPed)
local placementCoords = vector3(
coords.x + forward.x * 0.5,
coords.y + forward.y * 0.5,
coords.z - 0.5
)
local bowlObject = CreateObject(hash, placementCoords.x, placementCoords.y, placementCoords.z, true, false, false)
SetEntityHeading(bowlObject, heading)
PlaceObjectOnGroundProperly(bowlObject)
FreezeEntityPosition(bowlObject, true)
SetEntityAsMissionEntity(bowlObject, true, true)
-- Generate a unique ID for this bowl
local bowlId = 'bowl_' .. math.random(100000, 999999) .. '_' .. GetGameTimer()
-- Save to server
local finalCoords = GetEntityCoords(bowlObject)
TriggerServerEvent('pet-bowls:server:placeBowl', bowlId, bowlConfig.model, bowlConfig.type, {
x = finalCoords.x,
y = finalCoords.y,
z = finalCoords.z,
w = heading
})
-- Store locally
placedBowls[bowlId] = {
object = bowlObject,
id = bowlId,
model = bowlConfig.model,
type = bowlConfig.type,
fillLevel = 0
}
-- Add target
AddTargetToBowl(bowlObject, bowlId, bowlConfig.type)
lib.notify(Config.Notifications.bowlPlaced)
end
-- Update bowl fill level
RegisterNetEvent('pet-bowls:client:updateBowlLevel', function(bowlId, newLevel)
if placedBowls[bowlId] then
@ -236,8 +128,99 @@ RegisterNetEvent('pet-bowls:client:updateBowlLevel', function(bowlId, newLevel)
end
end)
-- Register a new bowl (called from ProPlacer)
RegisterNetEvent('pet-bowls:client:registerBowl', function(objectNetId, bowlType)
local bowlObject = NetworkGetEntityFromNetworkId(objectNetId)
if not DoesEntityExist(bowlObject) then return end
local model = GetEntityModel(bowlObject)
local modelName = nil
-- Find the model name from the hash
for _, bowlConfig in pairs(Config.BowlProps) do
if GetHashKey(bowlConfig.model) == model then
modelName = bowlConfig.model
break
end
end
if not modelName then return end
-- Generate a unique ID for this bowl
local bowlId = 'bowl_' .. math.random(100000, 999999) .. '_' .. GetGameTimer()
-- Save to server
local coords = GetEntityCoords(bowlObject)
local heading = GetEntityHeading(bowlObject)
TriggerServerEvent('pet-bowls:server:placeBowl', bowlId, modelName, bowlType, {
x = coords.x,
y = coords.y,
z = coords.z,
w = heading
})
-- Store locally
placedBowls[bowlId] = {
object = bowlObject,
id = bowlId,
model = modelName,
type = bowlType,
fillLevel = 0
}
-- Add target
AddTargetToBowl(bowlObject, bowlId, bowlType)
lib.notify(Config.Notifications.bowlPlaced)
end)
-- Initialize
Citizen.CreateThread(function()
-- Request all placed bowls from server
TriggerServerEvent('pet-bowls:server:requestBowls')
-- Add target to all valid bowl props in the world
Citizen.Wait(2000) -- Wait for world to load
local validModels = {}
for _, bowlConfig in pairs(Config.BowlProps) do
validModels[GetHashKey(bowlConfig.model)] = bowlConfig.type
end
local objects = GetGamePool('CObject')
for _, object in pairs(objects) do
local model = GetEntityModel(object)
if validModels[model] then
-- This is a valid bowl model, check if it's already registered
local isRegistered = false
for id, bowl in pairs(placedBowls) do
if bowl.object == object then
isRegistered = true
break
end
end
if not isRegistered then
-- This bowl isn't registered yet, add interaction without database entry
exports['qb-target']:AddTargetEntity(object, {
options = {
{
type = "client",
icon = "fas fa-plus",
label = "Register as Bowl",
action = function()
local bowlType = validModels[model]
TriggerEvent('pet-bowls:client:registerBowl', NetworkGetNetworkIdFromEntity(object), bowlType)
end,
canInteract = function()
return true
end,
}
},
distance = 2.0
})
end
end
end
end)