From 065a8df46dab432ad5be0ff7d132aab2b9deb835 Mon Sep 17 00:00:00 2001 From: Madd Date: Tue, 8 Apr 2025 12:27:11 +0930 Subject: [PATCH] Add ability to have multiple smelter locations --- client/smelting.lua | 95 ++++++++++++++++++++++++++++----------------- config/shared.lua | 4 +- server/smelting.lua | 8 ++-- 3 files changed, 67 insertions(+), 40 deletions(-) diff --git a/client/smelting.lua b/client/smelting.lua index 55a0021..8905c3b 100644 --- a/client/smelting.lua +++ b/client/smelting.lua @@ -9,6 +9,12 @@ local mining = exports.lation_mining -- Initialize table to store smelting menu local menu = {} +-- Initialize table to store smelter zones +local smelters = {} + +-- Initialize variable to track if player is inside smelting zone +local inside = nil + -- Initialize boolean to track smelting state local smelting = false @@ -106,7 +112,7 @@ local function startSmelting(ingotId, count) end smelted += 1 - TriggerServerEvent('lation_mining:completesmelt', ingotId) + TriggerServerEvent('lation_mining:completesmelt', ingotId, inside) end ClearPedTasks(cache.ped) @@ -136,41 +142,58 @@ end) -- Setup on player loaded AddEventHandler('lation_mining:onPlayerLoaded', function() - lib.zones.sphere({ - coords = shared.smelting.coords, - radius = 200, - onEnter = function() - AddCircleZone({ - coords = shared.smelting.coords, - name = 'smelt-zone', - radius = 3, - debug = shared.setup.debug, - options = { - { - name = 'smelt-zone', - label = locale('target.smelt-ore'), - icon = icons.smelt, - iconColor = icons.smelt_color, - distance = 2, - canInteract = function() - if smelting then return false end - return true - end, - onSelect = function() - lib.showContext('smelt-menu') - end, - action = function() - lib.showContext('smelt-menu') - end + for smelterId, coords in pairs(shared.smelting.coords) do + local zone = lib.zones.sphere({ + coords = coords, + radius = 15, + onEnter = function() + inside = smelterId + AddCircleZone({ + coords = coords, + name = 'smelt-zone'..smelterId, + radius = 3, + debug = shared.setup.debug, + options = { + { + name = 'smelt-zone'..smelterId, + label = locale('target.smelt-ore'), + icon = icons.smelt, + iconColor = icons.smelt_color, + distance = 2, + canInteract = function() + if smelting then return false end + return true + end, + onSelect = function() + lib.showContext('smelt-menu') + end, + action = function() + lib.showContext('smelt-menu') + end + } } - } - }) - end, - onExit = function() - RemoveCircleZone('smelt-zone') - end, - debug = shared.setup.debug - }) + }) + end, + onExit = function() + inside = nil + RemoveCircleZone('smelt-zone'..smelterId) + end, + debug = shared.setup.debug + }) + smelters[smelterId] = zone + createBlip(coords, shared.smelting.blip) + end buildMenu() - createBlip(shared.smelting.coords, shared.smelting.blip) +end) + +-- Cleanup on resource stop +--- @param resourceName string +AddEventHandler('onResourceStop', function(resourceName) + if GetCurrentResourceName() ~= resourceName then return end + for smelterId, zone in pairs(smelters) do + if zone then + zone:remove() + end + smelters[smelterId] = nil + end end) \ No newline at end of file diff --git a/config/shared.lua b/config/shared.lua index a7aa80c..295cce6 100644 --- a/config/shared.lua +++ b/config/shared.lua @@ -285,7 +285,9 @@ return { smelting = { -- Where do you want the smelter to be? - coords = vec3(1087.6827, -2002.1394, 31.4841), + coords = { + vec3(1087.6827, -2002.1394, 31.4841), + }, -- The types of ingots that can be smelted from ores ingots = { [1] = { diff --git a/server/smelting.lua b/server/smelting.lua index c7674d6..10c691d 100644 --- a/server/smelting.lua +++ b/server/smelting.lua @@ -10,13 +10,15 @@ local players = {} -- Complete smelting action --- @param ingotId number -RegisterNetEvent('lation_mining:completesmelt', function(ingotId) - if not source or not ingotId then return end +RegisterNetEvent('lation_mining:completesmelt', function(ingotId, inside) + if not source or not ingotId or not inside then return end local source = source local ingot = shared.smelting.ingots[ingotId] if not ingot then return end + if not shared.smelting.coords[inside] then return end + local hasItems = true for _, req in pairs(ingot.required) do if GetItemCount(source, req.item) < req.quantity then @@ -29,7 +31,7 @@ RegisterNetEvent('lation_mining:completesmelt', function(ingotId) return end - local dist = #(shared.smelting.coords - GetEntityCoords(GetPlayerPed(source))) <= 15 + local dist = #(shared.smelting.coords[inside] - GetEntityCoords(GetPlayerPed(source))) <= 15 if not dist then return end local canCarry = true