|
| 1 | +---@diagnostic disable: undefined-global |
| 2 | + |
| 3 | +local ESX = exports["es_extended"]:getSharedObject() |
| 4 | + |
| 5 | +-- ========================= |
| 6 | +-- VARIABILI |
| 7 | +-- ========================= |
| 8 | +local jobBlips = {} |
| 9 | +local shopBlips = {} |
| 10 | +local textUIVisible = false |
| 11 | + |
| 12 | +-- ========================= |
| 13 | +-- MARKER CONFIG |
| 14 | +-- ========================= |
| 15 | +local MARKER_TYPE = 1 |
| 16 | +local MARKER_DRAW_DIST = 10.0 |
| 17 | +local MARKER_SIZE = vec3(0.45, 0.45, 0.45) |
| 18 | + |
| 19 | +local MARKER_COLOR_SHOP = { r = 0, g = 180, b = 255, a = 150 } |
| 20 | +local MARKER_COLOR_STASH = { r = 255, g = 50, b = 50, a = 150 } |
| 21 | + |
| 22 | +-- ========================= |
| 23 | +-- ESX EVENTS |
| 24 | +-- ========================= |
| 25 | +RegisterNetEvent("esx:playerLoaded", function(xPlayer) |
| 26 | + ESX.PlayerData = xPlayer |
| 27 | + ESX.PlayerLoaded = true |
| 28 | + refreshJobBlips() |
| 29 | +end) |
| 30 | + |
| 31 | +RegisterNetEvent("esx:onPlayerLogout", function() |
| 32 | + ESX.PlayerLoaded = false |
| 33 | + ESX.PlayerData = {} |
| 34 | +end) |
| 35 | + |
| 36 | +RegisterNetEvent("esx:setJob", function(job) |
| 37 | + ESX.PlayerData.job = job |
| 38 | + refreshJobBlips() |
| 39 | +end) |
| 40 | + |
| 41 | +-- ========================= |
| 42 | +-- BLIP FUNCTIONS |
| 43 | +-- ========================= |
| 44 | +local function createBlip(data) |
| 45 | + local blip = AddBlipForCoord(data.coords.x, data.coords.y, data.coords.z) |
| 46 | + SetBlipSprite(blip, data.sprite) |
| 47 | + SetBlipDisplay(blip, 4) |
| 48 | + SetBlipScale(blip, data.scale) |
| 49 | + SetBlipColour(blip, data.color) |
| 50 | + SetBlipAsShortRange(blip, true) |
| 51 | + |
| 52 | + BeginTextCommandSetBlipName("STRING") |
| 53 | + AddTextComponentString(data.label) |
| 54 | + EndTextCommandSetBlipName(blip) |
| 55 | + |
| 56 | + return blip |
| 57 | +end |
| 58 | + |
| 59 | +function refreshJobBlips() |
| 60 | + for _, blip in pairs(jobBlips) do |
| 61 | + RemoveBlip(blip) |
| 62 | + end |
| 63 | + jobBlips = {} |
| 64 | + |
| 65 | + if not ESX.PlayerLoaded or not ESX.PlayerData.job then |
| 66 | + return |
| 67 | + end |
| 68 | + |
| 69 | + for shopName, shop in pairs(Config.Shops) do |
| 70 | + if shop.blip.stash.enabled and ESX.PlayerData.job.name == shopName then |
| 71 | + jobBlips[#jobBlips + 1] = createBlip(shop.blip.stash) |
| 72 | + end |
| 73 | + end |
| 74 | +end |
| 75 | + |
| 76 | +-- ========================= |
| 77 | +-- CREATE SHOP BLIPS (PUBLIC) |
| 78 | +-- ========================= |
| 79 | +CreateThread(function() |
| 80 | + for _, shop in pairs(Config.Shops) do |
| 81 | + if shop.blip.shop.enabled then |
| 82 | + shopBlips[#shopBlips + 1] = createBlip(shop.blip.shop) |
| 83 | + end |
| 84 | + end |
| 85 | +end) |
| 86 | + |
| 87 | +-- ========================= |
| 88 | +-- SET PRODUCT PRICE (SECURE) |
| 89 | +-- ========================= |
| 90 | +RegisterNetEvent("bpt_oxshops:setProductPrice", function(shop, slot) |
| 91 | + if not ESX.PlayerLoaded or not ESX.PlayerData.job or ESX.PlayerData.job.name ~= shop then |
| 92 | + print("[ANTICHEAT] Tentativo setProductPrice non autorizzato") |
| 93 | + return |
| 94 | + end |
| 95 | + |
| 96 | + local input = lib.inputDialog(Strings.sell_price, { Strings.amount_input }) |
| 97 | + local price = tonumber(input and input[1]) |
| 98 | + |
| 99 | + if not price or price < 0 then |
| 100 | + return |
| 101 | + end |
| 102 | + |
| 103 | + TriggerEvent("ox_inventory:closeInventory") |
| 104 | + TriggerServerEvent("bpt_oxshops:setData", shop, slot, math.floor(price)) |
| 105 | + |
| 106 | + lib.notify({ |
| 107 | + title = Strings.success, |
| 108 | + description = (Strings.item_stocked_desc):format(price), |
| 109 | + type = "success", |
| 110 | + }) |
| 111 | +end) |
| 112 | + |
| 113 | +-- ========================= |
| 114 | +-- MAIN LOOP (MARKER + UI) |
| 115 | +-- ========================= |
| 116 | +CreateThread(function() |
| 117 | + while true do |
| 118 | + local sleep = 1500 |
| 119 | + local ped = PlayerPedId() |
| 120 | + local coords = GetEntityCoords(ped) |
| 121 | + |
| 122 | + for shopName, shopData in pairs(Config.Shops) do |
| 123 | + local stash = shopData.locations.stash |
| 124 | + local shop = shopData.locations.shop |
| 125 | + |
| 126 | + local distStash = #(coords - stash.coords) |
| 127 | + local distShop = #(coords - shop.coords) |
| 128 | + |
| 129 | + -- ===================== |
| 130 | + -- STASH (JOB LOCKED) |
| 131 | + -- ===================== |
| 132 | + if ESX.PlayerData.job and ESX.PlayerData.job.name == shopName then |
| 133 | + if distStash <= MARKER_DRAW_DIST then |
| 134 | + sleep = 0 |
| 135 | + DrawMarker( |
| 136 | + MARKER_TYPE, |
| 137 | + stash.coords.x, |
| 138 | + stash.coords.y, |
| 139 | + stash.coords.z - 0.95, |
| 140 | + 0.0, |
| 141 | + 0.0, |
| 142 | + 0.0, |
| 143 | + 0.0, |
| 144 | + 0.0, |
| 145 | + 0.0, |
| 146 | + MARKER_SIZE.x, |
| 147 | + MARKER_SIZE.y, |
| 148 | + MARKER_SIZE.z, |
| 149 | + MARKER_COLOR_STASH.r, |
| 150 | + MARKER_COLOR_STASH.g, |
| 151 | + MARKER_COLOR_STASH.b, |
| 152 | + MARKER_COLOR_STASH.a, |
| 153 | + false, |
| 154 | + true, |
| 155 | + 2, |
| 156 | + false, |
| 157 | + nil, |
| 158 | + nil, |
| 159 | + false |
| 160 | + ) |
| 161 | + end |
| 162 | + |
| 163 | + if distStash <= stash.range then |
| 164 | + if not textUIVisible then |
| 165 | + lib.showTextUI(stash.string) |
| 166 | + textUIVisible = true |
| 167 | + end |
| 168 | + |
| 169 | + if IsControlJustReleased(0, 38) then |
| 170 | + exports.ox_inventory:openInventory("stash", shopName) |
| 171 | + end |
| 172 | + end |
| 173 | + end |
| 174 | + |
| 175 | + -- ===================== |
| 176 | + -- SHOP (PUBLIC) |
| 177 | + -- ===================== |
| 178 | + if distShop <= MARKER_DRAW_DIST then |
| 179 | + sleep = 0 |
| 180 | + DrawMarker(MARKER_TYPE, shop.coords.x, shop.coords.y, shop.coords.z - 0.95, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, MARKER_SIZE.x, MARKER_SIZE.y, MARKER_SIZE.z, MARKER_COLOR_SHOP.r, MARKER_COLOR_SHOP.g, MARKER_COLOR_SHOP.b, MARKER_COLOR_SHOP.a, false, true, 2, false, nil, nil, false) |
| 181 | + end |
| 182 | + |
| 183 | + if distShop <= shop.range then |
| 184 | + if not textUIVisible then |
| 185 | + lib.showTextUI(shop.string) |
| 186 | + textUIVisible = true |
| 187 | + end |
| 188 | + |
| 189 | + if IsControlJustReleased(0, 38) then |
| 190 | + exports.ox_inventory:openInventory("shop", { |
| 191 | + type = shopName, |
| 192 | + id = 1, |
| 193 | + }) |
| 194 | + end |
| 195 | + end |
| 196 | + |
| 197 | + -- ===================== |
| 198 | + -- HIDE UI |
| 199 | + -- ===================== |
| 200 | + if distStash > stash.range and distShop > shop.range and textUIVisible then |
| 201 | + lib.hideTextUI() |
| 202 | + textUIVisible = false |
| 203 | + end |
| 204 | + end |
| 205 | + |
| 206 | + Wait(sleep) |
| 207 | + end |
| 208 | +end) |
0 commit comments