refactor(esx_weaponshop) - esx_context

replacing esx_menu_default for esx_context
This commit is contained in:
Benzo
2022-10-02 21:46:16 +02:00
parent 68458c228c
commit 8b6d169dcd
5 changed files with 181 additions and 156 deletions
+158 -141
View File
@@ -6,177 +6,194 @@ local CurrentActionData = {}
local ShopOpen = false
function OpenBuyLicenseMenu(zone)
ESX.UI.Menu.CloseAll()
ShopOpen = true
local Elements = {{
icon = "fa-regular fa-money-bill-alt",
unselectable = true,
title = "License Shop"
}, {
icon = "fa-regular fa-id-card",
title = "Buy Weapon License",
description = "Price: $"..Config.LicensePrice,
value = "buylicense"
}, {
icon = "fa-solid fa-xmark",
title = "Cancel",
value = "cancel"
}}
ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'shop_license', {
title = _U('buy_license'),
align = 'top-left',
elements = {
{label = _U('no'), value = 'no'},
{label = _U('yes', ('<span style="color: green;">%s</span>'):format((_U('shop_menu_item', ESX.Math.GroupDigits(Config.LicensePrice))))), value = 'yes'},
}
}, function(data, menu)
if data.current.value == 'yes' then
ESX.OpenContext(Config.MenuPosition, Elements, function(menu, element)
if element.value == "buylicense" then
ESX.TriggerServerCallback('esx_weaponshop:buyLicense', function(bought)
if bought then
menu.close()
OpenShopMenu(zone)
end
end)
if bought then
ESX.CloseContext()
end
end)
end
end, function(data, menu)
menu.close()
end)
if element.value == "cancel" then
ESX.CloseContext()
end
end)
end
function OpenShopMenu(zone)
local elements = {}
ShopOpen = true
ShopOpen = true
local Elements = {{
icon = "fa-solid fa-bullseye",
unselectable = true,
description = "Buy Weapons",
title = "Weapon Shop"
}}
for i = 1, #Config.Zones[zone].Items, 1 do
local item = Config.Zones[zone].Items[i]
item.label = ESX.GetWeaponLabel(item.name)
Elements[#Elements + 1] = {
icon = "fa-solid fa-gun",
title = item.label,
description = "Price: $".. ESX.Math.GroupDigits(item.price),
price = item.price,
weaponName = item.name,
value = "buy"
}
end
for i=1, #Config.Zones[zone].Items, 1 do
local item = Config.Zones[zone].Items[i]
item.label = ESX.GetWeaponLabel(item.name)
ESX.OpenContext(Config.MenuPosition, Elements, function(menu, element)
table.insert(elements, {
label = ('%s - <span style="color: green;">%s</span>'):format(item.label, _U('shop_menu_item', ESX.Math.GroupDigits(item.price))),
price = item.price,
weaponName = item.name
})
end
if element.value == "buy" then
ESX.TriggerServerCallback('esx_weaponshop:buyWeapon', function(bought)
if bought then
DisplayBoughtScaleform(element.weaponName, element.price)
ESX.CloseContext()
else
PlaySoundFrontend(-1, 'ERROR', 'HUD_AMMO_SHOP_SOUNDSET', false)
end
end, element.weaponName, zone)
end
ESX.UI.Menu.CloseAll()
PlaySoundFrontend(-1, 'BACK', 'UD_AMMO_SHOP_SOUNDSET', false)
ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'shop', {
title = _U('shop_menu_title'),
align = 'top-left',
elements = elements
}, function(data, menu)
ESX.TriggerServerCallback('esx_weaponshop:buyWeapon', function(bought)
if bought then
DisplayBoughtScaleform(data.current.weaponName, data.current.price)
else
PlaySoundFrontend(-1, 'ERROR', 'HUD_AMMO_SHOP_SOUNDSET', false)
end
end, data.current.weaponName, zone)
end, function(data, menu)
PlaySoundFrontend(-1, 'BACK', 'HUD_AMMO_SHOP_SOUNDSET', false)
ShopOpen = false
menu.close()
CurrentAction = 'shop_menu'
CurrentActionMsg = _U('shop_menu_prompt')
CurrentActionData = { zone = zone }
end, function(data, menu)
PlaySoundFrontend(-1, 'NAV', 'HUD_AMMO_SHOP_SOUNDSET', false)
end)
end, function(menu)
ShopOpen = false
CurrentAction = 'shop_menu'
CurrentActionMsg = _U('shop_menu_prompt')
CurrentActionData = {
zone = zone
}
end)
end
function DisplayBoughtScaleform(weaponName, price)
local scaleform = ESX.Scaleform.Utils.RequestScaleformMovie('MP_BIG_MESSAGE_FREEMODE')
local sec = 4
local scaleform = ESX.Scaleform.Utils.RequestScaleformMovie('MP_BIG_MESSAGE_FREEMODE')
local sec = 4
BeginScaleformMovieMethod(scaleform, 'SHOW_WEAPON_PURCHASED')
BeginScaleformMovieMethod(scaleform, 'SHOW_WEAPON_PURCHASED')
ScaleformMovieMethodAddParamTextureNameString(_U('weapon_bought', ESX.Math.GroupDigits(price)))
ScaleformMovieMethodAddParamTextureNameString(ESX.GetWeaponLabel(weaponName))
ScaleformMovieMethodAddParamInt(joaat(weaponName))
ScaleformMovieMethodAddParamTextureNameString('')
ScaleformMovieMethodAddParamInt(100)
EndScaleformMovieMethod()
ScaleformMovieMethodAddParamTextureNameString(_U('weapon_bought', ESX.Math.GroupDigits(price)))
ScaleformMovieMethodAddParamTextureNameString(ESX.GetWeaponLabel(weaponName))
ScaleformMovieMethodAddParamInt(joaat(weaponName))
ScaleformMovieMethodAddParamTextureNameString('')
ScaleformMovieMethodAddParamInt(100)
EndScaleformMovieMethod()
PlaySoundFrontend(-1, 'WEAPON_PURCHASE', 'HUD_AMMO_SHOP_SOUNDSET', false)
PlaySoundFrontend(-1, 'WEAPON_PURCHASE', 'HUD_AMMO_SHOP_SOUNDSET', false)
CreateThread(function()
while sec > 0 do
Wait(0)
sec = sec - 0.01
DrawScaleformMovieFullscreen(scaleform, 255, 255, 255, 255)
end
end)
CreateThread(function()
while sec > 0 do
Wait(0)
sec = sec - 0.01
DrawScaleformMovieFullscreen(scaleform, 255, 255, 255, 255)
end
end)
end
AddEventHandler('onResourceStop', function(resource)
if resource == GetCurrentResourceName() then
if ShopOpen then
ESX.UI.Menu.CloseAll()
end
end
if resource == GetCurrentResourceName() then
if ShopOpen then
ESX.CloseContext()
end
end
end)
-- Create Blips
CreateThread(function()
for k,v in pairs(Config.Zones) do
if v.Legal then
for i = 1, #v.Locations, 1 do
local blip = AddBlipForCoord(v.Locations[i])
for k, v in pairs(Config.Zones) do
local blipSettings = v.Blip
if blipSettings.Enabled then
for i = 1, #v.Locations, 1 do
local blip = AddBlipForCoord(v.Locations[i])
SetBlipSprite (blip, 110)
SetBlipDisplay(blip, 4)
SetBlipScale (blip, 0.8)
SetBlipColour (blip, 81)
SetBlipAsShortRange(blip, true)
SetBlipSprite(blip, blipSettings.Sprite)
SetBlipDisplay(blip, blipSettings.Display)
SetBlipScale(blip, blipSettings.Scale)
SetBlipColour(blip, blipSettings.Colour)
SetBlipAsShortRange(blip, blipSettings.ShortRange)
BeginTextCommandSetBlipName("STRING")
AddTextComponentSubstringPlayerName(_U('map_blip'))
EndTextCommandSetBlipName(blip)
end
end
end
BeginTextCommandSetBlipName("STRING")
AddTextComponentSubstringPlayerName(_U('map_blip'))
EndTextCommandSetBlipName(blip)
end
end
end
end)
local TextShown = false
local GetEntityCoords = GetEntityCoords
local CreateThread = CreateThread
local Wait = Wait
local IsControlJustReleased = IsControlJustReleased
-- Display markers
CreateThread(function()
while true do
local Sleep = 1500
local InShop = false
local CurrentShop = nil
local coords = GetEntityCoords(PlayerPedId())
while true do
local Sleep = 1500
local InShop = false
local CurrentShop = nil
local coords = GetEntityCoords(ESX.PlayerData.ped)
for k,v in pairs(Config.Zones) do
for i = 1, #v.Locations, 1 do
if (Config.Type ~= -1 and #(coords - v.Locations[i]) < Config.DrawDistance) then
InShop = true
CurrentShop = v.Locations[i]
Sleep = 0
if #(coords - CurrentShop) < 2.0 then
if not TextShown then
ESX.TextUI(_U('shop_menu_prompt'))
TextShown = true
end
if IsControlJustReleased(0, 38) then
if Config.LicenseEnable and v.Legal then
ESX.TriggerServerCallback('esx_license:checkLicense', function(hasWeaponLicense)
if hasWeaponLicense then
OpenShopMenu(k)
else
OpenBuyLicenseMenu(k)
end
end, GetPlayerServerId(PlayerId()), 'weapon')
else
OpenShopMenu(k)
end
end
end
end
DrawMarker(Config.Type, v.Locations[i], 0.0, 0.0, 0.0, 0, 0.0, 0.0, Config.Size.x, Config.Size.y, Config.Size.z, Config.Color.r, Config.Color.g, Config.Color.b, 100, false, true, 2, false, false, false, false)
end
end
if not CurrentShop and TextShown then
TextShown = false
ESX.HideUI()
end
if not InShop and ShopOpen then
if ShopOpen then
TextShown = false
ESX.HideUI()
ESX.UI.Menu.CloseAll()
ShopOpen = false
end
end
Wait(Sleep)
end
for k, v in pairs(Config.Zones) do
for i = 1, #v.Locations, 1 do
if (Config.Type ~= -1 and #(coords - v.Locations[i]) < Config.DrawDistance) then
InShop = true
CurrentShop = v.Locations[i]
Sleep = 0
if #(coords - CurrentShop) < 2.0 then
if not TextShown then
ESX.TextUI(_U('shop_menu_prompt'))
TextShown = true
end
if IsControlJustReleased(0, 38) then
if Config.LicenseEnable and v.Legal then
ESX.TriggerServerCallback('esx_license:checkLicense', function(hasWeaponLicense)
if hasWeaponLicense then
OpenShopMenu(k)
else
OpenBuyLicenseMenu(k)
end
end, GetPlayerServerId(PlayerId()), 'weapon')
else
OpenShopMenu(k)
end
end
end
end
DrawMarker(Config.Type, v.Locations[i], 0.0, 0.0, 0.0, 0, 0.0, 0.0, Config.Size.x, Config.Size.y,
Config.Size.z, Config.Color.r, Config.Color.g, Config.Color.b, 100, false, true, 2, false, false,
false, false)
end
end
if not CurrentShop and TextShown then
TextShown = false
ESX.HideUI()
end
if not InShop and ShopOpen then
if ShopOpen then
TextShown = false
ESX.HideUI()
ESX.CloseContext()
ShopOpen = false
end
end
Wait(Sleep)
end
end)
+17 -1
View File
@@ -6,7 +6,7 @@ Config.Color = { r = 0, g = 128, b = 255 }
Config.Type = 1
Config.Locale = 'en'
Config.MenuPosition = "right"
Config.OxInventory = ESX.GetConfig().OxInventory
Config.LicenseEnable = false -- only turn this on if you are using esx_license
@@ -16,6 +16,14 @@ Config.Zones = {
GunShop = {
Legal = true,
Blip = {
Enabled = true,
Sprite = 110,
Color = 81,
Display = 4,
Scale = 0.7,
ShortRange = true
},
Items = {
{
name = "WEAPON_PISTOL",
@@ -101,6 +109,14 @@ Config.Zones = {
BlackWeashop = {
Legal = false,
Blip = {
Enabled = false,
Sprite = 110,
Color = 59,
Display = 4,
Scale = 0.7,
ShortRange = true
},
Items = {
{
name = "WEAPON_PISTOL",
@@ -1,5 +1,3 @@
INSERT INTO `licenses` (`type`, `label`) VALUES
('weapon', "Weapon licence")
;
@@ -8,6 +8,5 @@ Locales ['en'] = {
['already_owned'] = 'you already own this weapon!',
['shop_menu_title'] = 'ammu-Nation',
['shop_menu_prompt'] = 'press [E] to access the Ammu-Nation.',
['shop_menu_item'] = '$%s',
['map_blip'] = 'ammu-Nation',
}
+6 -11
View File
@@ -18,7 +18,7 @@ ESX.RegisterServerCallback('esx_weaponshop:buyWeapon', function(source, cb, weap
local xPlayer = ESX.GetPlayerFromId(source)
local price = GetPrice(weaponName, zone)
if price == 0 then
if price <= 0 then
print(('[^3WARNING^7] Player ^5%s^7 attempted to buy Invalid weapon - %s!'):format(source, weaponName))
cb(false)
@@ -53,17 +53,12 @@ ESX.RegisterServerCallback('esx_weaponshop:buyWeapon', function(source, cb, weap
end)
function GetPrice(weaponName, zone)
local weapon = nil
for k,v in pairs(Config.Zones[zone].Items) do
if v.name == weaponName then
weapon = v
break
for i=1, #(Config.Zones[zone].Items) do
if Config.Zones[zone].Items[i].name == weaponName then
local weapon = Config.Zones[zone].Items[i]
return weapon.price
end
end
if weapon then
return weapon.price
else
return 0
end
return -1
end