1
0
Fork 0
forked from Simnation/Main
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 coords = json.decode(bowl.coords)
local bowlCoords = vector3(coords.x, coords.y, coords.z) local bowlCoords = vector3(coords.x, coords.y, coords.z)
-- Create the bowl object -- Add target to existing bowl objects
local hash = GetHashKey(bowl.model) local bowlObjects = GetGamePool('CObject')
RequestModel(hash) for _, object in pairs(bowlObjects) do
while not HasModelLoaded(hash) do local objectCoords = GetEntityCoords(object)
Wait(10) 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 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
end) end)
@ -59,17 +57,6 @@ function AddTargetToBowl(bowlObject, bowlId, bowlType)
canInteract = function() canInteract = function()
return true return true
end, end,
},
{
type = "client",
icon = "fas fa-trash",
label = "Pick Up Bowl",
action = function()
PickUpBowl(bowlId)
end,
canInteract = function()
return true
end,
} }
}, },
distance = 2.0 distance = 2.0
@ -134,101 +121,6 @@ function FillBowl(bowlId, itemName, fillAmount)
end end
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 -- Update bowl fill level
RegisterNetEvent('pet-bowls:client:updateBowlLevel', function(bowlId, newLevel) RegisterNetEvent('pet-bowls:client:updateBowlLevel', function(bowlId, newLevel)
if placedBowls[bowlId] then if placedBowls[bowlId] then
@ -236,8 +128,99 @@ RegisterNetEvent('pet-bowls:client:updateBowlLevel', function(bowlId, newLevel)
end end
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 -- Initialize
Citizen.CreateThread(function() Citizen.CreateThread(function()
-- Request all placed bowls from server -- Request all placed bowls from server
TriggerServerEvent('pet-bowls:server:requestBowls') 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) end)

View file

@ -87,7 +87,7 @@ Config.ProgressBar = {
}, },
filling = { filling = {
duration = 2000, duration = 2000,
label = 'Filling Bowl...', label = 'Napf füllen...',
position = 'bottom', position = 'bottom',
useWhileDead = false, useWhileDead = false,
canCancel = true, canCancel = true,
@ -108,7 +108,7 @@ Config.ProgressBar = {
Config.Notifications = { Config.Notifications = {
bowlPlaced = { bowlPlaced = {
title = 'Bowl System', title = 'Bowl System',
description = 'You placed a bowl!', description = 'Bowl registered successfully!',
type = 'success' type = 'success'
}, },
bowlFilled = { bowlFilled = {

View file

@ -33,7 +33,7 @@ RegisterNetEvent('pet-bowls:server:requestBowls', function()
TriggerClientEvent('pet-bowls:client:loadBowls', src, bowls) TriggerClientEvent('pet-bowls:client:loadBowls', src, bowls)
end) end)
-- Place a new bowl -- Place a new bowl (register an existing prop as a bowl)
RegisterNetEvent('pet-bowls:server:placeBowl', function(bowlId, model, bowlType, coords) RegisterNetEvent('pet-bowls:server:placeBowl', function(bowlId, model, bowlType, coords)
local src = source local src = source
local Player = QBCore.Functions.GetPlayer(src) local Player = QBCore.Functions.GetPlayer(src)
@ -69,16 +69,17 @@ RegisterNetEvent('pet-bowls:server:fillBowl', function(bowlId, itemName, fillAmo
if not Player then return end if not Player then return end
-- Check if player has the item -- Check if player has the item using standard QBCore function
local hasItem = exports["tgiann-inventory"]:GetItemByName(src, itemName) local item = Player.Functions.GetItemByName(itemName)
if not hasItem or hasItem.amount < 1 then if not item or item.amount < 1 then
TriggerClientEvent('ox_lib:notify', src, Config.Notifications.noItem) TriggerClientEvent('ox_lib:notify', src, Config.Notifications.noItem)
return return
end end
-- Remove the item -- Remove the item using standard QBCore function
exports["tgiann-inventory"]:RemoveItem(src, itemName, 1) Player.Functions.RemoveItem(itemName, 1)
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[itemName], "remove")
-- Update bowl fill level -- Update bowl fill level
local bowl = bowls[bowlId] local bowl = bowls[bowlId]
@ -163,7 +164,7 @@ RegisterNetEvent('pet-bowls:server:consumeBowl', function(bowlId)
TriggerClientEvent('ox_lib:notify', src, Config.Notifications.consumed) TriggerClientEvent('ox_lib:notify', src, Config.Notifications.consumed)
end) end)
-- Remove a bowl -- Remove a bowl from database (if needed)
RegisterNetEvent('pet-bowls:server:removeBowl', function(bowlId) RegisterNetEvent('pet-bowls:server:removeBowl', function(bowlId)
local src = source local src = source
local Player = QBCore.Functions.GetPlayer(src) local Player = QBCore.Functions.GetPlayer(src)
@ -175,9 +176,7 @@ RegisterNetEvent('pet-bowls:server:removeBowl', function(bowlId)
-- Remove from memory -- Remove from memory
bowls[bowlId] = nil bowls[bowlId] = nil
end)
-- Notify all clients
-- Register command to place bowls TriggerClientEvent('pet-bowls:client:bowlRemoved', -1, bowlId)
QBCore.Commands.Add('placebowl', 'Place a pet bowl', {}, false, function(source, args)
TriggerClientEvent('pet-bowls:client:openPlaceBowlMenu', source)
end) end)