From 8742676eeb44f42ca26a1aee78a1116804dba6e9 Mon Sep 17 00:00:00 2001 From: bitpredator <67551273+bitpredator@users.noreply.github.com> Date: Fri, 14 Jun 2024 18:16:07 +0200 Subject: [PATCH] convert: esx_boat > bpt_boat --- .../[bpt_addons]/bpt_boat/server/main.lua | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 server-data/resources/[bpt_addons]/bpt_boat/server/main.lua diff --git a/server-data/resources/[bpt_addons]/bpt_boat/server/main.lua b/server-data/resources/[bpt_addons]/bpt_boat/server/main.lua new file mode 100644 index 000000000..432901d50 --- /dev/null +++ b/server-data/resources/[bpt_addons]/bpt_boat/server/main.lua @@ -0,0 +1,109 @@ +function ParkBoats() + MySQL.update("UPDATE owned_vehicles SET `stored` = true WHERE `stored` = false AND type = @type", { + ["@type"] = "boat", + }, function(rowsChanged) + if rowsChanged > 0 then + print(("[^2INFO^7] Stored ^5%s^7 %s !"):format(rowsChanged, rowsChanged > 1 and "boats" or "boat")) + end + end) +end + +MySQL.ready(function() + ParkBoats() +end) + +ESX.RegisterServerCallback("bpt_boat:buyBoat", function(source, cb, vehicleProps) + local xPlayer = ESX.GetPlayerFromId(source) + local price = getPriceFromModel(vehicleProps.model) + + -- vehicle model not found + if price == 0 then + print(("[^2INFO^7] Player ^5%s^7 Attempted To Exploit Shop"):format(xPlayer.source)) + cb(false) + else + if xPlayer.getMoney() >= price then + xPlayer.removeMoney(price, "Boat Purchase") + + MySQL.update("INSERT INTO owned_vehicles (owner, plate, vehicle, type, `stored`) VALUES (@owner, @plate, @vehicle, @type, @stored)", { + ["@owner"] = xPlayer.identifier, + ["@plate"] = vehicleProps.plate, + ["@vehicle"] = json.encode(vehicleProps), + ["@type"] = "boat", + ["@stored"] = true, + }, function(rowsChanged) + cb(true) + end) + else + cb(false) + end + end +end) + +RegisterServerEvent("bpt_boat:takeOutVehicle") +AddEventHandler("bpt_boat:takeOutVehicle", function(plate) + local xPlayer = ESX.GetPlayerFromId(source) + + MySQL.update("UPDATE owned_vehicles SET `stored` = @stored WHERE owner = @owner AND plate = @plate", { + ["@stored"] = false, + ["@owner"] = xPlayer.identifier, + ["@plate"] = plate, + }, function(rowsChanged) + if rowsChanged == 0 then + print(("[^2INFO^7] Player ^5%s^7 Attempted To Exploit Garage"):format(xPlayer.source)) + end + end) +end) + +ESX.RegisterServerCallback("bpt_boat:storeVehicle", function(source, cb, plate) + local xPlayer = ESX.GetPlayerFromId(source) + + MySQL.update("UPDATE owned_vehicles SET `stored` = @stored WHERE owner = @owner AND plate = @plate", { + ["@stored"] = true, + ["@owner"] = xPlayer.identifier, + ["@plate"] = plate, + }, function(rowsChanged) + cb(rowsChanged) + end) +end) + +ESX.RegisterServerCallback("bpt_boat:getGarage", function(source, cb) + local xPlayer = ESX.GetPlayerFromId(source) + + MySQL.query("SELECT vehicle FROM owned_vehicles WHERE owner = @owner AND type = @type AND `stored` = @stored", { + ["@owner"] = xPlayer.identifier, + ["@type"] = "boat", + ["@stored"] = true, + }, function(result) + local vehicles = {} + + for i = 1, #result, 1 do + table.insert(vehicles, result[i].vehicle) + end + + cb(vehicles) + end) +end) + +ESX.RegisterServerCallback("bpt_boat:buyBoatLicense", function(source, cb) + local xPlayer = ESX.GetPlayerFromId(source) + + if xPlayer.getMoney() >= Config.LicensePrice then + xPlayer.removeMoney(Config.LicensePrice, "Boat License Purchase") + + TriggerEvent("esx_license:addLicense", source, "boat", function() + cb(true) + end) + else + cb(false) + end +end) + +function getPriceFromModel(model) + for k, v in ipairs(Config.Vehicles) do + if joaat(v.model) == model then + return v.price + end + end + + return 0 +end