From 7974aea5a1c491ac44a2ca3648d6357410e0bdeb Mon Sep 17 00:00:00 2001 From: ElPumpo Date: Sat, 8 Dec 2018 13:30:26 +0100 Subject: [PATCH] Implemented processing license --- client/main.lua | 58 +++++++++++++++++++++++++++++++++++++++++++------ client/weed.lua | 48 +++++++++++++++++++++++++++------------- config.lua | 6 +++++ esx_drugs.sql | 4 ++++ locales/en.lua | 7 ++++++ locales/sv.lua | 13 ++++++++--- server/main.lua | 20 +++++++++++++++++ 7 files changed, 131 insertions(+), 25 deletions(-) diff --git a/client/main.lua b/client/main.lua index 54d535c..c7c00ef 100644 --- a/client/main.lua +++ b/client/main.lua @@ -11,7 +11,8 @@ Keys = { } ESX = nil -local shopOpen = false +local menuOpen = false +local wasOpen = false Citizen.CreateThread(function() while ESX == nil do @@ -33,19 +34,20 @@ Citizen.CreateThread(function() local coords = GetEntityCoords(playerPed) if GetDistanceBetweenCoords(coords, Config.CircleZones.DrugDealer.coords, true) < 0.5 then - if not shopOpen then + if not menuOpen then ESX.ShowHelpNotification(_U('dealer_prompt')) if IsControlJustReleased(0, Keys['E']) then + wasOpen = true OpenDrugShop() end else Citizen.Wait(500) end else - if shopOpen then + if wasOpen then + wasOpen = false ESX.UI.Menu.CloseAll() - shopOpen = false end Citizen.Wait(500) @@ -56,7 +58,7 @@ end) function OpenDrugShop() ESX.UI.Menu.CloseAll() local elements = {} - shopOpen = true + menuOpen = true for k, v in pairs(ESX.GetPlayerData().inventory) do local price = Config.DrugDealerItems[v.name] @@ -84,18 +86,60 @@ function OpenDrugShop() TriggerServerEvent('esx_drugs:sellDrug', data.current.name, data.current.value) end, function(data, menu) menu.close() - shopOpen = false + menuOpen = false end) end AddEventHandler('onResourceStop', function(resource) if resource == GetCurrentResourceName() then - if shopOpen then + if menuOpen then ESX.UI.Menu.CloseAll() end end end) +function OpenBuyLicenseMenu(licenseName) + menuOpen = true + local license = Config.LicensePrices[licenseName] + + local elements = { + { + label = _U('license_no'), + value = 'no' + }, + + { + label = ('%s - %s'):format(license.label, _U('dealer_item', ESX.Math.GroupDigits(license.price))), + value = licenseName, + price = license.price, + licenseName = license.label + } + } + + ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'license_shop', { + title = _U('license_title'), + align = 'top-left', + elements = elements + }, function(data, menu) + + if data.current.value ~= 'no' then + ESX.TriggerServerCallback('esx_drugs:buyLicense', function(boughtLicense) + if boughtLicense then + ESX.ShowNotification(_U('license_bought', data.current.licenseName, ESX.Math.GroupDigits(data.current.price))) + else + ESX.ShowNotification(_U('license_bought_fail', data.current.licenseName)) + end + end, data.current.value) + else + menu.close() + end + + end, function(data, menu) + menu.close() + menuOpen = false + end) +end + function CreateBlipCircle(coords, text, radius, color, sprite) local blip = AddBlipForRadius(coords, radius) diff --git a/client/weed.lua b/client/weed.lua index 898581d..2016225 100644 --- a/client/weed.lua +++ b/client/weed.lua @@ -29,23 +29,19 @@ Citizen.CreateThread(function() end if IsControlJustReleased(0, Keys['E']) and not isProcessing then - isProcessing = true - ESX.ShowNotification(_U('weed_processingstarted')) - TriggerServerEvent('esx_drugs:processCannabis') - local timeLeft = Config.Delays.WeedProcessing / 1000 - - while timeLeft > 0 do - Citizen.Wait(1000) - timeLeft = timeLeft - 1 - - if GetDistanceBetweenCoords(GetEntityCoords(playerPed), Config.CircleZones.WeedProcessing.coords, false) > 4 then - ESX.ShowNotification(_U('weed_processingtoofar')) - TriggerServerEvent('esx_drugs:cancelProcessing') - break - end + + if Config.LicenseEnable then + ESX.TriggerServerCallback('esx_license:checkLicense', function(hasProcessingLicense) + if hasProcessingLicense then + ProcessWeed() + else + OpenBuyLicenseMenu('weed_processing') + end + end, GetPlayerServerId(PlayerId()), 'weed_processing') + else + ProcessWeed() end - isProcessing = false end else Citizen.Wait(500) @@ -53,6 +49,28 @@ Citizen.CreateThread(function() end end) +function ProcessWeed() + isProcessing = true + + ESX.ShowNotification(_U('weed_processingstarted')) + TriggerServerEvent('esx_drugs:processCannabis') + local timeLeft = Config.Delays.WeedProcessing / 1000 + local playerPed = PlayerPedId() + + while timeLeft > 0 do + Citizen.Wait(1000) + timeLeft = timeLeft - 1 + + if GetDistanceBetweenCoords(GetEntityCoords(playerPed), Config.CircleZones.WeedProcessing.coords, false) > 4 then + ESX.ShowNotification(_U('weed_processingtoofar')) + TriggerServerEvent('esx_drugs:cancelProcessing') + break + end + end + + isProcessing = false +end + Citizen.CreateThread(function() while true do Citizen.Wait(10) diff --git a/config.lua b/config.lua index 7166134..dfb500a 100644 --- a/config.lua +++ b/config.lua @@ -10,6 +10,12 @@ Config.DrugDealerItems = { marijuana = 91 } +Config.LicenseEnable = false -- enable processing licenses? The player will be required to buy a license in order to process drugs. Requires esx_license + +Config.LicensePrices = { + weed_processing = {label = _U('license_weed'), price = 15000} +} + Config.GiveBlack = true -- give black money? if disabled it'll give regular cash. Config.CircleZones = { diff --git a/esx_drugs.sql b/esx_drugs.sql index 575f57f..6981b9e 100644 --- a/esx_drugs.sql +++ b/esx_drugs.sql @@ -4,3 +4,7 @@ INSERT INTO `items` (`name`, `label`, `limit`, `rare`, `can_remove`) VALUES ('cannabis', 'Cannabis', 40, 0, 1), ('marijuana', 'Marijuana', 14, 0, 1) ; + +INSERT INTO `licenses` (`type`, `label`) VALUES + ('weed_processing', 'Weed Processing License') +; \ No newline at end of file diff --git a/locales/en.lua b/locales/en.lua index 4fcd32e..9d64470 100644 --- a/locales/en.lua +++ b/locales/en.lua @@ -16,6 +16,13 @@ Locales ['en'] = { ['dealer_notenough'] = 'you don\'t have enough of that to sell!', ['dealer_sold'] = 'you\'ve sold ~b~%sx~s~ ~y~%s~s~ for ~g~$%s~s~', + -- license + ['license_title'] = 'you are required to own a processing license to process this product, it can be bought here.', + ['license_no'] = 'no', + ['license_bought'] = 'you bought the ~b~%s~s~ for ~r~$%s~s~', + ['license_bought_fail'] = 'you cannot afford an ~b~%s~s~!', + ['license_weed'] = 'weed Processing License', + -- blips ['blip_weedfield'] = 'weed Field', ['blip_weedprocessing'] = 'weed Processing', diff --git a/locales/sv.lua b/locales/sv.lua index eb973f0..d3f1852 100644 --- a/locales/sv.lua +++ b/locales/sv.lua @@ -3,9 +3,9 @@ Locales ['sv'] = { ['weed_pickupprompt'] = 'tryck ~INPUT_CONTEXT~ för att skörda ~g~Cannabisväxten~s~.', ['weed_inventoryfull'] = 'du har ingen mer plats för ~g~Cannabis~s~.', ['weed_processprompt'] = 'tryck ~INPUT_CONTEXT~ för att börja ~g~Processera Cannabis~s~.', - ['weed_processingstarted'] = 'processera ~g~Cannabis~s~ till ~g~Marijuana~s~...', + ['weed_processingstarted'] = 'processerar ~g~Cannabis~s~ till ~g~Marijuana~s~...', ['weed_processingfull'] = 'processeringen ~r~avbröts~s~ för att utrymmet är slut!', - ['weed_processingenough'] = 'du måste ha ~b~3x~s~ ~g~Cannabis~s~ för att kunna bearbeta den.', + ['weed_processingenough'] = 'du måste ha ~b~3x~s~ ~g~Cannabis~s~ för att kunna bearbeta det.', ['weed_processed'] = 'du har processerat ~b~3x~s~ ~g~Cannabis~s~ till ~b~1x~s~ ~g~Marijuana~s~', ['weed_processingtoofar'] = 'processeringen har ~r~har avbrutit~s~ på grund av att du lämnade området.', @@ -14,7 +14,14 @@ Locales ['sv'] = { ['dealer_title'] = 'drogsäljare', ['dealer_item'] = '%s SEK', ['dealer_notenough'] = 'du har inte tillräckligt med det till salu!', - ['dealer_sold'] = 'duhar sålt ~b~%sx~s~ ~y~%s~s~ för ~g~%s SEK~s~', + ['dealer_sold'] = 'du har sålt ~b~%sx~s~ ~y~%s~s~ för ~g~%s SEK~s~', + + -- license + ['license_title'] = 'du måste ha ett visst processeringslicens för att kunna bearbeta denna produkt, detta kan dock köpas här.', + ['license_no'] = 'nej', + ['license_bought'] = 'du köpte ~b~%s~s~ för ~r~%s SEK~s~', + ['license_bought_fail'] = 'du har inte råd för ~b~%s~s~!', + ['license_weed'] = 'marijuana processeringslicens', -- blips ['blip_weedfield'] = 'marijuana plock', diff --git a/server/main.lua b/server/main.lua index be4bf7e..44eded5 100644 --- a/server/main.lua +++ b/server/main.lua @@ -32,6 +32,26 @@ AddEventHandler('esx_drugs:sellDrug', function(itemName, amount) TriggerClientEvent('esx:showNotification', source, _U('dealer_sold', amount, xItem.label, ESX.Math.GroupDigits(price))) end) +ESX.RegisterServerCallback('esx_drugs:buyLicense', function(source, cb, licenseName) + local xPlayer = ESX.GetPlayerFromId(source) + local license = Config.LicensePrices[licenseName] + + if license == nil then + print(('esx_drugs: %s attempted to buy an invalid license!'):format(xPlayer.identifier)) + cb(false) + end + + if xPlayer.getMoney() >= license.price then + xPlayer.removeMoney(license.price) + + TriggerEvent('esx_license:addLicense', source, licenseName, function() + cb(true) + end) + else + cb(false) + end +end) + RegisterServerEvent('esx_drugs:pickedUpCannabis') AddEventHandler('esx_drugs:pickedUpCannabis', function() local xPlayer = ESX.GetPlayerFromId(source)