From cbb1f00ed28a99915642f3926d64025a19b6e0d6 Mon Sep 17 00:00:00 2001 From: bitpredator <67551273+bitpredator@users.noreply.github.com> Date: Wed, 11 Sep 2024 18:56:42 +0200 Subject: [PATCH] feat: [esx_addons]\esx_givevehicle --- .../[esx_addons]/esx_givevehicle/README.md | 44 +++++ .../esx_givevehicle/client/main.lua | 114 +++++++++++ .../[esx_addons]/esx_givevehicle/config.lua | 9 + .../esx_givevehicle/fxmanifest.lua | 27 +++ .../esx_givevehicle/locales/de.lua | 8 + .../esx_givevehicle/locales/en.lua | 8 + .../esx_givevehicle/locales/tw.lua | 8 + .../esx_givevehicle/server/main.lua | 177 ++++++++++++++++++ 8 files changed, 395 insertions(+) create mode 100644 server-data/resources/[esx_addons]/esx_givevehicle/README.md create mode 100644 server-data/resources/[esx_addons]/esx_givevehicle/client/main.lua create mode 100644 server-data/resources/[esx_addons]/esx_givevehicle/config.lua create mode 100644 server-data/resources/[esx_addons]/esx_givevehicle/fxmanifest.lua create mode 100644 server-data/resources/[esx_addons]/esx_givevehicle/locales/de.lua create mode 100644 server-data/resources/[esx_addons]/esx_givevehicle/locales/en.lua create mode 100644 server-data/resources/[esx_addons]/esx_givevehicle/locales/tw.lua create mode 100644 server-data/resources/[esx_addons]/esx_givevehicle/server/main.lua diff --git a/server-data/resources/[esx_addons]/esx_givevehicle/README.md b/server-data/resources/[esx_addons]/esx_givevehicle/README.md new file mode 100644 index 000000000..05fa25081 --- /dev/null +++ b/server-data/resources/[esx_addons]/esx_givevehicle/README.md @@ -0,0 +1,44 @@ +# **Depiction:** +This plugin you can give vehicle with custom or random plate number into player's garage with command +also you can delete a vehicle by plate + +# **Commands:** +**In game:** (give permission in config) + +Give a car to the target player: ``/givecar [playerID] [vehicle] `` +Give a plane to the target player: ``/giveplane [playerID] [vehicle] `` +Give a boat to the target player: ``/giveboat [playerID] [vehicle] `` +Give a helicopter to the target player: ``/giveheli [playerID] [vehicle] `` +*Note: If plate is none will randomly generate a new plate* + +Delete a owned car by plate: ``/delcarplate [plate]`` + +**On console:** (prefix need change to "_") +``` +_givecar [playerID] [car] +_giveplane [playerID] [car] +_giveboat [playerID] [car] +_giveheli [playerID] [car] +_delcarplate [plate] +``` + +# **Config:** +``` +Config = {} +Config.Locale = 'en' + +Config.ReceiveMsg = true + +-- Allow below identifier player to execute commands +Config.AuthorizedRanks = { + 'superadmin', +-- 'admin' +} +``` + +# **Requirements:** +* es_extended +* esx_vehicleshop + +FiveM Forum Release:https://forum.cfx.re/t/release-esx-give-or-delete-car-to-owned-vehicle-database-plate-can-custom-or-random-spawn/1050821 + diff --git a/server-data/resources/[esx_addons]/esx_givevehicle/client/main.lua b/server-data/resources/[esx_addons]/esx_givevehicle/client/main.lua new file mode 100644 index 000000000..ce75fcfaa --- /dev/null +++ b/server-data/resources/[esx_addons]/esx_givevehicle/client/main.lua @@ -0,0 +1,114 @@ +ESX = exports["es_extended"]:getSharedObject() + +TriggerEvent("chat:addSuggestion", "/givecar", "Give a car to player", { + { name = "id", help = "The ID of the player" }, + { name = "vehicle", help = "Vehicle model" }, + { name = "", help = "Vehicle plate, skip if you want random generate plate number" }, +}) + +TriggerEvent("chat:addSuggestion", "/giveplane", "Give an airplane to player", { + { name = "id", help = "The ID of the player" }, + { name = "vehicle", help = "Vehicle model" }, + { name = "", help = "Vehicle plate, skip if you want random generate plate number" }, +}) + +TriggerEvent("chat:addSuggestion", "/giveboat", "Give a boat to player", { + { name = "id", help = "The ID of the player" }, + { name = "vehicle", help = "Vehicle model" }, + { name = "", help = "Vehicle plate, skip if you want random generate plate number" }, +}) + +TriggerEvent("chat:addSuggestion", "/giveheli", "Give a helicopter to player", { + { name = "id", help = "The ID of the player" }, + { name = "vehicle", help = "Vehicle model" }, + { name = "", help = "Vehicle plate, skip if you want random generate plate number" }, +}) + +TriggerEvent("chat:addSuggestion", "/delcarplate", "Delete a owned vehicle by plate number", { + { name = "plate", help = "Vehicle's plate number" }, +}) + +RegisterNetEvent("esx_giveownedcar:spawnVehicle") +AddEventHandler("esx_giveownedcar:spawnVehicle", function(playerID, model, playerName, type, vehicleType) + local playerPed = GetPlayerPed(-1) + local coords = GetEntityCoords(playerPed) + local carExist = false + + ESX.Game.SpawnVehicle(model, coords, 0.0, function(vehicle) --get vehicle info + if DoesEntityExist(vehicle) then + carExist = true + SetEntityVisible(vehicle, false, false) + SetEntityCollision(vehicle, false) + + local newPlate = exports.esx_vehicleshop:GeneratePlate() + local vehicleProps = ESX.Game.GetVehicleProperties(vehicle) + vehicleProps.plate = newPlate + TriggerServerEvent("esx_giveownedcar:setVehicle", vehicleProps, playerID, vehicleType) + ESX.Game.DeleteVehicle(vehicle) + if type ~= "console" then + ESX.ShowNotification(_U("gived_car", model, newPlate, playerName)) + else + local msg = ("addCar: " .. model .. ", plate: " .. newPlate .. ", toPlayer: " .. playerName) + TriggerServerEvent("esx_giveownedcar:printToConsole", msg) + end + end + end) + + Wait(2000) + if not carExist then + if type ~= "console" then + ESX.ShowNotification(_U("unknown_car", model)) + else + TriggerServerEvent("esx_giveownedcar:printToConsole", "ERROR: " .. model .. " is an unknown vehicle model") + end + end +end) + +RegisterNetEvent("esx_giveownedcar:spawnVehiclePlate") +AddEventHandler("esx_giveownedcar:spawnVehiclePlate", function(playerID, model, plate, playerName, type, vehicleType) + local playerPed = GetPlayerPed(-1) + local coords = GetEntityCoords(playerPed) + local generatedPlate = string.upper(plate) + local carExist = false + + ESX.TriggerServerCallback("esx_vehicleshop:isPlateTaken", function(isPlateTaken) + if not isPlateTaken then + ESX.Game.SpawnVehicle(model, coords, 0.0, function(vehicle) --get vehicle info + if DoesEntityExist(vehicle) then + carExist = true + SetEntityVisible(vehicle, false, false) + SetEntityCollision(vehicle, false) + + local newPlate = string.upper(plate) + local vehicleProps = ESX.Game.GetVehicleProperties(vehicle) + vehicleProps.plate = newPlate + TriggerServerEvent("esx_giveownedcar:setVehicle", vehicleProps, playerID, vehicleType) + ESX.Game.DeleteVehicle(vehicle) + if type ~= "console" then + ESX.ShowNotification(_U("gived_car", model, newPlate, playerName)) + else + local msg = ("addCar: " .. model .. ", plate: " .. newPlate .. ", toPlayer: " .. playerName) + TriggerServerEvent("esx_giveownedcar:printToConsole", msg) + end + end + end) + else + carExist = true + if type ~= "console" then + ESX.ShowNotification(_U("plate_already_have")) + else + local msg = "ERROR: this plate is already been used on another vehicle" + TriggerServerEvent("esx_giveownedcar:printToConsole", msg) + end + end + end, generatedPlate) + + Wait(2000) + if not carExist then + if type ~= "console" then + ESX.ShowNotification(_U("unknown_car", model)) + else + TriggerServerEvent("esx_giveownedcar:printToConsole", "ERROR: " .. model .. " is an unknown vehicle model") + end + end +end) diff --git a/server-data/resources/[esx_addons]/esx_givevehicle/config.lua b/server-data/resources/[esx_addons]/esx_givevehicle/config.lua new file mode 100644 index 000000000..e6a3c3e6b --- /dev/null +++ b/server-data/resources/[esx_addons]/esx_givevehicle/config.lua @@ -0,0 +1,9 @@ +Config = {} +Config.Locale = "en" + +Config.ReceiveMsg = true + +-- Allow below identifier player to execute commands +Config.AuthorizedRanks = { + "admin", +} diff --git a/server-data/resources/[esx_addons]/esx_givevehicle/fxmanifest.lua b/server-data/resources/[esx_addons]/esx_givevehicle/fxmanifest.lua new file mode 100644 index 000000000..ffa0d0d93 --- /dev/null +++ b/server-data/resources/[esx_addons]/esx_givevehicle/fxmanifest.lua @@ -0,0 +1,27 @@ +fx_version("cerulean") +game("gta5") + +author("MEENO") +description("assign or remove the vehicle to a player via chat") + +version("1.0.2") +server_scripts({ + "@oxmysql/lib/MySQL.lua", + "server/main.lua", +}) + +shared_scripts({ + "@es_extended/imports.lua", + "@es_extended/locale.lua", + "locales/*.lua", + "config.lua", +}) + +client_scripts({ + "client/main.lua", +}) + +dependency({ + "es_extended", + "esx_vehicleshop", +}) diff --git a/server-data/resources/[esx_addons]/esx_givevehicle/locales/de.lua b/server-data/resources/[esx_addons]/esx_givevehicle/locales/de.lua new file mode 100644 index 000000000..4cc18caec --- /dev/null +++ b/server-data/resources/[esx_addons]/esx_givevehicle/locales/de.lua @@ -0,0 +1,8 @@ +Locales["de"] = { + ["gived_car"] = "Fahrzeug ~y~%s~s~ mit dem Nummernschild ~y~%s~s~ wurde in ~g~%s~s~'s Garage gestellt.", + ["received_car"] = "Dein Fahrzeug mit dem Nummernschild ~y~%s~s~ wurde in Deine Garage gestellt.", + ["del_car"] = "Fahrzeug mit dem Kennzeichen ~y~%s~s~ gelöscht.", + ["none_plate"] = "Nummernschild Angabe fehlt.", + ["unknown_car"] = "Unbekanntes Fahrzeug", + ["plate_already_have"] = "Nummernschild bereits vergeben.", +} diff --git a/server-data/resources/[esx_addons]/esx_givevehicle/locales/en.lua b/server-data/resources/[esx_addons]/esx_givevehicle/locales/en.lua new file mode 100644 index 000000000..fb022f582 --- /dev/null +++ b/server-data/resources/[esx_addons]/esx_givevehicle/locales/en.lua @@ -0,0 +1,8 @@ +Locales["en"] = { + ["gived_car"] = "Vehicle ~y~%s ~s~with plate number ~y~ %s ~s~has been park into ~g~%s~s~'s garage", + ["received_car"] = "You received a vehicle with plate number ~y~%s", + ["del_car"] = "Has deleted a vehicle with plate number ~y~%s", + ["del_car_error"] = "Can't find vehicle with plate is ~y~%s", + ["unknown_car"] = "~r~Unknown vehicle model ~y~%s", + ["plate_already_have"] = "~r~This plate is already been used on another vehicle", +} diff --git a/server-data/resources/[esx_addons]/esx_givevehicle/locales/tw.lua b/server-data/resources/[esx_addons]/esx_givevehicle/locales/tw.lua new file mode 100644 index 000000000..caa25d1b8 --- /dev/null +++ b/server-data/resources/[esx_addons]/esx_givevehicle/locales/tw.lua @@ -0,0 +1,8 @@ +Locales["tw"] = { + ["gived_car"] = "已將 ~y~%s ~s~車牌為~y~ %s ~s~的車分配給 ~g~%s", + ["received_car"] = "你已收到一台車牌為 ~y~%s ~s~的車, 快去車庫看看吧!", + ["del_car"] = "已刪除一台車牌為 ~y~%s ~s~的車", + ["del_car_error"] = "~r~找不到車牌為 ~y~%s ~r~的車", + ["unknown_car"] = "~r~未知的車輛", + ["plate_already_have"] = "~r~此車牌已經有車輛擁有了, 請換一個車牌", +} diff --git a/server-data/resources/[esx_addons]/esx_givevehicle/server/main.lua b/server-data/resources/[esx_addons]/esx_givevehicle/server/main.lua new file mode 100644 index 000000000..be17a5224 --- /dev/null +++ b/server-data/resources/[esx_addons]/esx_givevehicle/server/main.lua @@ -0,0 +1,177 @@ +ESX = exports["es_extended"]:getSharedObject() + +--give car with a random plate- 1: playerID 2: carModel (3: plate) +RegisterCommand("givecar", function(source, args) + givevehicle(source, args, "car") +end) + +--give car with a random plate- 1: playerID 2: carModel (3: plate) +RegisterCommand("giveplane", function(source, args) + givevehicle(source, args, "airplane") +end) + +--give car with a random plate- 1: playerID 2: carModel (3: plate) +RegisterCommand("giveboat", function(source, args) + givevehicle(source, args, "boat") +end) + +--give car with a random plate- 1: playerID 2: carModel (3: plate) +RegisterCommand("giveheli", function(source, args) + givevehicle(source, args, "helicopter") +end) + +function givevehicle(_source, _args, vehicleType) + if havePermission(_source) then + if _args[1] == nil or _args[2] == nil then + TriggerClientEvent("esx:showNotification", _source, "~r~/givevehicle playerID carModel [plate]") + elseif _args[3] ~= nil then + local playerName = GetPlayerName(_args[1]) + local plate = _args[3] + if #_args > 3 then + for i = 4, #_args do + plate = plate .. " " .. _args[i] + end + end + plate = string.upper(plate) + TriggerClientEvent("esx_giveownedcar:spawnVehiclePlate", _source, _args[1], _args[2], plate, playerName, "player", vehicleType) + else + local playerName = GetPlayerName(_args[1]) + TriggerClientEvent("esx_giveownedcar:spawnVehicle", _source, _args[1], _args[2], playerName, "player", vehicleType) + end + else + TriggerClientEvent("esx:showNotification", _source, "~r~You don't have permission to do this command!") + end +end + +RegisterCommand("_givecar", function(source, args) + _givevehicle(source, args, "car") +end) + +RegisterCommand("_giveplane", function(source, args) + _givevehicle(source, args, "airplane") +end) + +RegisterCommand("_giveboat", function(source, args) + _givevehicle(source, args, "boat") +end) + +RegisterCommand("_giveheli", function(source, args) + _givevehicle(source, args, "helicopter") +end) + +function _givevehicle(_source, _args, vehicleType) + if _source == 0 then + local sourceID = _args[1] + if _args[1] == nil or _args[2] == nil then + print("SYNTAX ERROR: _givevehicle [plate]") + elseif _args[3] ~= nil then + local playerName = GetPlayerName(sourceID) + local plate = _args[3] + if #_args > 3 then + for i = 4, #_args do + plate = plate .. " " .. _args[i] + end + end + plate = string.upper(plate) + TriggerClientEvent("esx_giveownedcar:spawnVehiclePlate", sourceID, _args[1], _args[2], plate, playerName, "console", vehicleType) + else + local playerName = GetPlayerName(_args[1]) + TriggerClientEvent("esx_giveownedcar:spawnVehicle", sourceID, _args[1], _args[2], playerName, "console", vehicleType) + end + end +end + +RegisterCommand("delcarplate", function(source, args) + if havePermission(source) then + if args[1] == nil then + TriggerClientEvent("esx:showNotification", source, "~r~/delcarplate ") + else + local plate = args[1] + if #args > 1 then + for i = 2, #args do + plate = plate .. " " .. args[i] + end + end + plate = string.upper(plate) + + local result = MySQL.Sync.execute("DELETE FROM owned_vehicles WHERE plate = @plate", { + ["@plate"] = plate, + }) + if result == 1 then + TriggerClientEvent("esx:showNotification", source, _U("del_car", plate)) + elseif result == 0 then + TriggerClientEvent("esx:showNotification", source, _U("del_car_error", plate)) + end + end + else + TriggerClientEvent("esx:showNotification", source, "~r~You don't have permission to do this command!") + end +end) + +RegisterCommand("_delcarplate", function(source, args) + if source == 0 then + if args[1] == nil then + print("SYNTAX ERROR: _delcarplate ") + else + local plate = args[1] + if #args > 1 then + for i = 2, #args do + plate = plate .. " " .. args[i] + end + end + plate = string.upper(plate) + + local result = MySQL.Sync.execute("DELETE FROM owned_vehicles WHERE plate = @plate", { + ["@plate"] = plate, + }) + if result == 1 then + print("Deleted car plate: " .. plate) + elseif result == 0 then + print("Can't find car with plate is " .. plate) + end + end + end +end) + +--functions-- + +RegisterServerEvent("esx_giveownedcar:setVehicle") +AddEventHandler("esx_giveownedcar:setVehicle", function(vehicleProps, playerID, vehicleType, jobType) + local _source = playerID + local xPlayer = ESX.GetPlayerFromId(_source) + + MySQL.Async.execute("INSERT INTO owned_vehicles (owner, plate, vehicle, stored, type) VALUES (@owner, @plate, @vehicle, @stored, @type)", { + ["@owner"] = xPlayer.identifier, + ["@plate"] = vehicleProps.plate, + ["@vehicle"] = json.encode(vehicleProps), + ["@stored"] = 1, + ["type"] = vehicleType, + }, function() + if Config.ReceiveMsg then + TriggerClientEvent("esx:showNotification", _source, _U("received_car", string.upper(vehicleProps.plate))) + end + end) +end) + +RegisterServerEvent("esx_giveownedcar:printToConsole") +AddEventHandler("esx_giveownedcar:printToConsole", function(msg) + print(msg) +end) + +function havePermission(_source) + local xPlayer = ESX.GetPlayerFromId(_source) + local playerGroup = xPlayer.getGroup() + local isAdmin = false + for k, v in pairs(Config.AuthorizedRanks) do + if v == playerGroup then + isAdmin = true + break + end + end + + if IsPlayerAceAllowed(_source, "giveownedcar.command") then + isAdmin = true + end + + return isAdmin +end