Skip to content

Commit

Permalink
Enhancements and Fixes: Improved Aging Logic, Menu UI, Database Updat…
Browse files Browse the repository at this point in the history
…es, and More (#97)

## Changelog

### Fixed:
- **Aging**: Animals will now only age once their condition has been reached.
- **Ranch State Update**: Fixed issue where ranch was being updated incorrectly after a resource restart; `is_any_animals_out` now correctly updated.
- **Server Events**: Removed duplicate server events.

### Improvements:
- **VORP Utils**: Removed unnecessary VORP utilities.
- **BCCUtils RPC**: Replaced with `bccutils` RPC for better performance and flexibility.
- **Database Updates**: Added `dbupdater` to automatically insert new data into the database.
- **MySQL Queries**: Updated to follow the latest oxmysql documentation.
- **Versioning**: Added versioning to track updates and changes.
- **Versioner Check**: Replaced release check with a check file for version control.
- **Menu UI**: Improved menu elements for better visual presentation.
- **Inventory Docs**: Updated to the latest VORP inventory documentation.

### TODO:
- Finish implementing RPC.
- Add additional checks for each task.
- Finalize API integration.
  • Loading branch information
iseeyoucopy authored Dec 1, 2024
1 parent 82e8897 commit ce644b4
Show file tree
Hide file tree
Showing 35 changed files with 1,467 additions and 643 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- Ranch Managment menu for admins to delete ranch's, rename, and change thier radius!
- Hire employees to work at your ranch!
- Harvest eggs from chickens, and milk cows!
- Animals will only age if their max condition has reached, if the maximum not achieve aging will not start

# How it works
- Admins can make a ranch by entering the command, they will be greeted with a menu to name the ranch, insert the ranches radius, and the owners static id!
Expand Down
23 changes: 14 additions & 9 deletions client/helpers/functions.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
-- Pulling Essentials Here As Global Variables So No Need To Pull Them In Every File
VORPcore = {}
TriggerEvent("getCore", function(core)
VORPcore = core
end)
VORPutils = {}
TriggerEvent("getUtils", function(utils)
VORPutils = utils
end)
VORPcore = exports.vorp_core:GetCore()
BccUtils = exports['bcc-utils'].initiate()

if Config.devMode then
-- Helper function for debugging
function devPrint(message)
print("^1[DEV MODE] ^4" .. message.."^0")
end
else
-- Define devPrint as a no-op function if DevMode is not enabled
function devPrint(message)
end
end

FeatherMenu = exports['feather-menu'].initiate()
BccUtils = exports['bcc-utils'].initiate()
MiniGame = exports['bcc-minigames'].initiate()
Expand Down
35 changes: 19 additions & 16 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,37 @@ AddEventHandler('vorp:SelectedCharacter', function()
end)

RegisterCommand(Config.commands.devModeCommand, function()
if Config.devMode then
TriggerServerEvent('bcc-ranch:AdminCheck')
local player = GetPlayerServerId(tonumber(PlayerId()))
TriggerServerEvent("bcc-ranch:StoreAllPlayers", player)
TriggerServerEvent('bcc-ranch:CheckIfPlayerOwnsARanch')
TriggerServerEvent('bcc-ranch:CheckIfPlayerIsEmployee')
end
if Config.devMode then
TriggerServerEvent('bcc-ranch:AdminCheck')
local player = GetPlayerServerId(tonumber(PlayerId()))
TriggerServerEvent("bcc-ranch:StoreAllPlayers", player)
TriggerServerEvent('bcc-ranch:CheckIfPlayerOwnsARanch')
TriggerServerEvent('bcc-ranch:CheckIfPlayerIsEmployee')
end
end)

-- Handling Initial Ranch Startup
---@param ranchData table
---@param isOwnerOfRanch boolean
RegisterNetEvent("bcc-ranch:PlayerOwnsARanch", function(ranchData, isOwnerOfRanch)
-- Handle ranch ownership notification
BccUtils.RPC:Register("bcc-ranch:PlayerOwnsARanch", function(params)
local ranchData = params.ranchData
local isOwnerOfRanch = params.isOwnerOfRanch

RanchData = ranchData
if isOwnerOfRanch then
IsOwnerOfRanch = true
end

-- Having to setup the coords to work, as they are encoded when recieved
-- Decode ranch coordinates
RanchData.ranchcoords = json.decode(RanchData.ranchcoords)
RanchData.ranchcoordsVector3 = vector3(RanchData.ranchcoords.x, RanchData.ranchcoords.y, RanchData.ranchcoords.z)

-- Create ranch blip
ranchBlip = BccUtils.Blip:SetBlip(RanchData.ranchname, Config.ranchSetup.ranchBlip, 0.2, RanchData.ranchcoords.x, RanchData.ranchcoords.y, RanchData.ranchcoords.z)

local promptGroup = VORPutils.Prompts:SetupPromptGroup()
-- Set up ranch management prompt
local promptGroup = BccUtils.Prompts:SetupPromptGroup()
local firstprompt = promptGroup:RegisterPrompt(_U("manage"), Config.ranchSetup.manageRanchKey, 1, 1, true, 'hold', {timedeventhash = "MEDIUM_TIMED_EVENT"})

-- Command for managing the ranch
if Config.commands.manageMyRanchCommand then
RegisterCommand(Config.commands.manageMyRanchCommandName, function()
local dist = #(RanchData.ranchcoordsVector3 - GetEntityCoords(PlayerPedId()))
Expand Down Expand Up @@ -82,12 +86,10 @@ RegisterNetEvent("bcc-ranch:PlayerOwnsARanch", function(ranchData, isOwnerOfRanc
end
end)

---@param admin boolean
RegisterNetEvent("bcc-ranch:IsAdminClientReceiver", function(admin)
IsAdmin = admin
end)

---@param ranchData table
RegisterNetEvent('bcc-ranch:UpdateRanchData', function (ranchData)
if not IsInMission then
IsInMission = true -- This is to stop the player from being able to manage their ranch while the Ranchdata is updating
Expand Down Expand Up @@ -115,5 +117,6 @@ AddEventHandler('onResourceStop', function(resourceName)
if ranchBlip then
ranchBlip:Remove()
end
BCCRanchMenu:Close()
end
end)
end)
16 changes: 13 additions & 3 deletions client/services/adminmenu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,22 @@ function RanchSelected(ranchTable)
})
end

CreateThread(function ()
CreateThread(function()
if Config.commands.manageRanches then
RegisterCommand(Config.commands.manageRanches, function(source, args, rawCommand)
-- Check for admin permissions (or use whatever method you need)
if IsAdmin then
TriggerServerEvent('bcc-ranch:GetAllRanches')
-- Calling the server-side RPC
BccUtils.RPC:Call("bcc-ranch:GetAllRanches", {}, function(success, ranches)
if success then
TriggerEvent('bcc-ranch:ShowAllRanchesMenu', ranches)
else
VORPcore.NotifyRightTip(source, _U("FailedToFetchRanches"), 4000)
end
end)
else
VORPcore.NotifyRightTip(source, _U("NoPermission"), 4000)
end
end)
end
end)
end)
91 changes: 23 additions & 68 deletions client/services/aging.lua
Original file line number Diff line number Diff line change
@@ -1,80 +1,35 @@
local agingActive = false
---@param firstIteration boolean
RegisterNetEvent('bcc-ranch:AgingTriggered', function(firstIteration, resetAging)
if not firstIteration then
if not agingActive then
agingActive = true
else
agingActive = false
end
else --done as on first iteration it will always stop aging and restart it so we make a dif if for it
if not agingActive then
agingActive = true
end
end
if resetAging then --done so we can allow the RanchData time to update before we start aging again (Used to sync when we buy new animals)
agingActive = false
Wait(1000)
agingActive = true
end
end)
RanchData = {}

-- Cow Thread
CreateThread(function()
while true do
if not agingActive or RanchData.cows ~= "true" or IsInMission then
-- Check if aging is active and if we're not in a mission
if not agingActive or IsInMission then
Wait(2000)
else
Wait(Config.animalSetup.cows.ageTimer)
TriggerServerEvent('bcc-ranch:IncreaseAnimalAge', RanchData.ranchid, 'cows', Config.animalSetup.cows.ageIncrease)
end
end
end)

-- Pig Thread
CreateThread(function()
while true do
if not agingActive or RanchData.pigs ~= 'true' or IsInMission then
Wait(2000)
else
Wait(Config.animalSetup.pigs.ageTimer)
TriggerServerEvent('bcc-ranch:IncreaseAnimalAge', RanchData.ranchid, 'pigs', Config.animalSetup.pigs.ageIncrease)
end
end
end)
-- Loop through all animal types (cows, pigs, sheep, goats, chickens)
for animalType, config in pairs(Config.animalSetup) do
if RanchData[animalType] == "true" then
devPrint(animalType .. " aging thread: Waiting for ageTimer")
Wait(config.ageTimer) -- Wait for the specified timer for the current animal

-- Sheep Thread
CreateThread(function()
while true do
if not agingActive or RanchData.sheeps ~= "true" or IsInMission then
Wait(2000)
else
Wait(Config.animalSetup.sheeps.ageTimer)
TriggerServerEvent('bcc-ranch:IncreaseAnimalAge', RanchData.ranchid, 'sheeps', Config.animalSetup.sheeps.ageIncrease)
-- Trigger the RPC to increase the animal age
BccUtils.RPC:Call("bcc-ranch:IncreaseAnimalAge", {
ranchId = RanchData.ranchid,
animalType = animalType,
incAmount = config.ageIncrease
}, function(success)
if success then
devPrint("Successfully increased age for " .. animalType .. " in ranch: " .. RanchData.ranchid)
else
devPrint("Failed to increase age for " .. animalType .. " in ranch: " .. RanchData.ranchid)
end
end)
end
end
end
end)

-- Goat Thread
CreateThread(function()
while true do
if not agingActive or RanchData.goats ~= "true" or IsInMission then
Wait(2000)
else
Wait(Config.animalSetup.goats.ageTimer)
TriggerServerEvent('bcc-ranch:IncreaseAnimalAge', RanchData.ranchid, 'goats', Config.animalSetup.goats.ageIncrease)
end
-- Wait before checking again
Wait(1000) -- Check again every second for any changes
end
end)

-- Chicken Thread
CreateThread(function()
while true do
if not agingActive or RanchData.chickens ~= "true" or IsInMission then
Wait(2000)
else
Wait(Config.animalSetup.chickens.ageTimer)
TriggerServerEvent('bcc-ranch:IncreaseAnimalAge', RanchData.ranchid, 'chickens', Config.animalSetup.chickens.ageIncrease)
end
end
end)
51 changes: 41 additions & 10 deletions client/services/animalshelper/butcheringanimals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,65 @@ local createdPed = nil
function ButcherAnimals(animalType)
local model, tables, spawnCoords
BCCRanchMenu:Close()

local selectAnimalFuncts = {
['cows'] = function()
if tonumber(RanchData.cows_age) < Config.animalSetup.cows.AnimalGrownAge then
VORPcore.NotifyRightTip(_U("tooYoung"), 4000) return
VORPcore.NotifyRightTip(_U("tooYoung"), 4000)
return
end
tables = Config.animalSetup.cows
model = 'a_c_cow'
spawnCoords = json.decode(RanchData.cow_coords)
end,
['pigs'] = function()
if tonumber(RanchData.pigs_age) < Config.animalSetup.pigs.AnimalGrownAge then
VORPcore.NotifyRightTip(_U("tooYoung"), 4000) return
VORPcore.NotifyRightTip(_U("tooYoung"), 4000)
return
end
tables = Config.animalSetup.pigs
model = 'a_c_pig_01'
spawnCoords = json.decode(RanchData.pig_coords)
end,
['sheeps'] = function()
if tonumber(RanchData.sheeps_age) < Config.animalSetup.sheeps.AnimalGrownAge then
VORPcore.NotifyRightTip(_U("tooYoung"), 4000) return
VORPcore.NotifyRightTip(_U("tooYoung"), 4000)
return
end
tables = Config.animalSetup.sheeps
model = 'a_c_sheep_01'
spawnCoords = json.decode(RanchData.sheep_coords)
end,
['goats'] = function()
if tonumber(RanchData.goats_age) < Config.animalSetup.goats.AnimalGrownAge then
VORPcore.NotifyRightTip(_U("tooYoung"), 4000) return
VORPcore.NotifyRightTip(_U("tooYoung"), 4000)
return
end
tables = Config.animalSetup.goats
model = 'a_c_goat_01'
spawnCoords = json.decode(RanchData.goat_coords)
end,
['chickens'] = function()
if tonumber(RanchData.chickens_age) < Config.animalSetup.chickens.AnimalGrownAge then
VORPcore.NotifyRightTip(_U("tooYoung"), 4000) return
VORPcore.NotifyRightTip(_U("tooYoung"), 4000)
return
end
tables = Config.animalSetup.chickens
model = 'a_c_chicken_01'
spawnCoords = json.decode(RanchData.chicken_coords)
end
}

if selectAnimalFuncts[animalType] then
selectAnimalFuncts[animalType]()
end

-- Check if spawnCoords is nil, notify the player if true
if not spawnCoords or not spawnCoords.x or not spawnCoords.y or not spawnCoords.z then
VORPcore.NotifyRightTip(_U("noCoordsSet"), 4000)
return
end

IsInMission = true

createdPed = BccUtils.Ped.CreatePed(model, spawnCoords.x, spawnCoords.y, spawnCoords.z, true, true, false)
Expand All @@ -56,26 +69,44 @@ function ButcherAnimals(animalType)
Citizen.InvokeNative(0x9587913B9E772D29, createdPed, true)
FreezeEntityPosition(createdPed, true)
VORPcore.NotifyRightTip(_U("killAnimal"), 4000)

while true do
Wait(5)
if IsEntityDead(PlayerPedId()) then break end
if IsEntityDead(createdPed) then
VORPcore.NotifyRightTip(_U("skinAnimal"), 4000) break
VORPcore.NotifyRightTip(_U("skinAnimal"), 4000)
break
end
end

local PromptGroup = VORPutils.Prompts:SetupPromptGroup()
local firstprompt = PromptGroup:RegisterPrompt(_U("skinAnimal"), 0x760A9C6F, 1, 1, true, 'hold', {timedeventhash = "MEDIUM_TIMED_EVENT"})
local PromptGroup = BccUtils.Prompts:SetupPromptGroup()
local firstprompt = PromptGroup:RegisterPrompt(_U("skinAnimal"), 0x760A9C6F, 1, 1, true, 'hold', { timedeventhash = "MEDIUM_TIMED_EVENT" })
while true do
Wait(5)
if IsEntityDead(PlayerPedId()) then break end
if #(GetEntityCoords(PlayerPedId()) - GetEntityCoords(createdPed)) < 3 then
PromptGroup:ShowGroup('')
if firstprompt:HasCompleted() then
-- Player performs a crouch action, and the animal is butchered.
BccUtils.Ped.ScenarioInPlace(PlayerPedId(), 'WORLD_HUMAN_CROUCH_INSPECT', 5000)
DeletePed(createdPed)
VORPcore.NotifyRightTip(_U("animalKilled"), 4000)
TriggerServerEvent('bcc-ranch:ButcherAnimalHandler', animalType, RanchData.ranchid, tables) break

-- Prepare the parameters
local params = {
animalType = animalType,
ranchId = RanchData.ranchid,
table = tables
}

BccUtils.RPC:Call("bcc-ranch:ButcherAnimalHandler", params, function(success)
if success then
devPrint("Animal butchered successfully and items added.")
else
devPrint("Failed to butcher the animal.")
end
end)
break
end
end
end
Expand All @@ -92,4 +123,4 @@ AddEventHandler('onResourceStop', function(resource)
DeletePed(createdPed)
createdPed = nil
end
end)
end)
Loading

0 comments on commit ce644b4

Please sign in to comment.