diff --git a/client.lua b/client/client.lua similarity index 94% rename from client.lua rename to client/client.lua index 83bf306..f964930 100644 --- a/client.lua +++ b/client/client.lua @@ -26,7 +26,7 @@ local function distanceCheck() lastLocation = GetEntityCoords(vehicle) end - local plate = string.gsub(GetVehicleNumberPlateText(GetVehiclePedIsIn(PlayerPedId(), false)), '^%s*(.-)%s*$', '%1') + local plate = string.gsub(GetVehicleNumberPlateText(GetVehiclePedIsIn(PlayerPedId(), false)), "^%s*(.-)%s*$", "%1") if plate == currentVehPlate and not currentVehOwned and recheckCurrentVeh > 0 then recheckCurrentVeh -= 1000 @@ -64,12 +64,14 @@ local function distanceCheck() if roundedMileage ~= lastUpdatedMileage then Entity(vehicle).state:set("vehicleMileage", roundedMileage) - TriggerServerEvent('jg-vehiclemileage:server:update-mileage', currentVehPlate, roundedMileage) + TriggerServerEvent("jg-vehiclemileage:server:update-mileage", currentVehPlate, roundedMileage) lastUpdatedMileage = roundedMileage end end CreateThread(function() + Wait(2000) + while true do distanceCheck() Wait(1000) diff --git a/config.lua b/config.lua index 74b749c..44d0da6 100644 --- a/config.lua +++ b/config.lua @@ -1,4 +1,6 @@ Config = {} +Config.AutoRunSQL = true + Config.Framework = "auto" -- or "QBCore", "Qbox", "ESX" Config.ShowMileage = true Config.Unit = "miles" -- "miles" or "kilometers" diff --git a/fxmanifest.lua b/fxmanifest.lua index 510862b..1861292 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -4,9 +4,7 @@ lua54 "yes" author "JG Scripts" description "Tracks vehicle mileage with UI" -version "v1.1.1" - -client_script "client.lua" +version "v1.2" shared_scripts { "@ox_lib/init.lua", @@ -14,9 +12,11 @@ shared_scripts { "main.lua" } +client_script "client/*.lua" + server_scripts { "@oxmysql/lib/MySQL.lua", - "server.lua" + "server/*.lua" } ui_page "web/index.html" diff --git a/install/run-esx.sql b/install/run-esx.sql new file mode 100644 index 0000000..2c4bd5f --- /dev/null +++ b/install/run-esx.sql @@ -0,0 +1 @@ +ALTER TABLE owned_vehicles ADD COLUMN IF NOT EXISTS mileage FLOAT DEFAULT 0 NOT NULL \ No newline at end of file diff --git a/install/run-qb.sql b/install/run-qb.sql new file mode 100644 index 0000000..46302cf --- /dev/null +++ b/install/run-qb.sql @@ -0,0 +1 @@ +ALTER TABLE player_vehicles ADD COLUMN IF NOT EXISTS mileage FLOAT DEFAULT 0 NOT NULL \ No newline at end of file diff --git a/main.lua b/main.lua index d5ea262..a1e0b9a 100644 --- a/main.lua +++ b/main.lua @@ -1,8 +1,10 @@ Framework = {} if (Config.Framework == "auto" and (GetResourceState("qbx_core") == "started" or GetResourceState("qb-core") == "started")) or Config.Framework == "Qbox" or Config.Framework == "QBCore" then + Config.Framework = "QBCore" Framework.VehiclesTable = "player_vehicles" elseif (Config.Framework == "auto" and GetResourceState("es_extended") == "started") or Config.Framework == "ESX" then + Config.Framework = "ESX" Framework.VehiclesTable = "owned_vehicles" else error("You haven't set a valid framework. Valid options can be found in main.lua!") diff --git a/run.sql b/run.sql deleted file mode 100644 index afa8351..0000000 --- a/run.sql +++ /dev/null @@ -1,5 +0,0 @@ --- QBCore / QBox -ALTER TABLE player_vehicles ADD mileage FLOAT DEFAULT 0 NOT NULL - --- ESX -ALTER TABLE owned_vehicles ADD mileage FLOAT DEFAULT 0 NOT NULL \ No newline at end of file diff --git a/server.lua b/server/server.lua similarity index 82% rename from server.lua rename to server/server.lua index 8b3fd78..e305a74 100644 --- a/server.lua +++ b/server/server.lua @@ -1,10 +1,10 @@ -lib.callback.register('jg-vehiclemileage:server:get-mileage', function(_, plate) +lib.callback.register("jg-vehiclemileage:server:get-mileage", function(_, plate) local vehicle = MySQL.single.await("SELECT mileage FROM " .. Framework.VehiclesTable .. " WHERE plate = ?", {plate}) if not vehicle then return { error = true } end return { mileage = vehicle.mileage } end) -RegisterNetEvent('jg-vehiclemileage:server:update-mileage', function(plate, mileage) +RegisterNetEvent("jg-vehiclemileage:server:update-mileage", function(plate, mileage) MySQL.update("UPDATE " .. Framework.VehiclesTable .. " SET mileage = ? WHERE plate = ?", {mileage, plate}) end) diff --git a/server/sv-initsql.lua b/server/sv-initsql.lua new file mode 100644 index 0000000..12c4f41 --- /dev/null +++ b/server/sv-initsql.lua @@ -0,0 +1,14 @@ +if Config.AutoRunSQL then + if not pcall(function() + local fileName = (Config.Framework == "QBCore" or Config.Framework == "Qbox") and "run-qb.sql" or "run-esx.sql" + + -- Open & read file + local file = assert(io.open(GetResourcePath(GetCurrentResourceName()) .. "/install/" .. fileName, "rb")) + local sql = file:read("*all") + file:close() + + MySQL.query.await(sql) + end) then + print("^1[SQL ERROR] There was an error while automatically running the required SQL. Don't worry, you just need to run the SQL file for your framework, found in the 'install' folder manually. If you've already ran the SQL code previously, and this error is annoying you, set Config.AutoRunSQL = false^0") + end +end \ No newline at end of file