diff --git a/client/main.lua b/client/main.lua index d1eab3d..f7d1b3b 100644 --- a/client/main.lua +++ b/client/main.lua @@ -5,18 +5,17 @@ local currentGear = { enabled = false } -local isWearingSuit = false local oxygenLevel = 0 -RegisterNetEvent("qb-diving:client:setoxygenlevel", function() - if oxygenLevel == 0 then - oxygenLevel = Config.OxygenLevel - exports.qbx_core:Notify(Lang:t("success.tube_filled"), 'success') - TriggerServerEvent('qb-diving:server:removeItemAfterFill') - return +lib.callback.register('qbx_divegear:client:fillTank', function() + if oxygenLevel ~= 0 then + exports.qbx_core:Notify(Lang:t("error.oxygenlevel", {oxygenlevel = oxygenLevel}), 'error') + return false end - exports.qbx_core:Notify(Lang:t("error.oxygenlevel", {oxygenlevel = oxygenLevel}), 'error') + oxygenLevel = Config.OxygenLevel + exports.qbx_core:Notify(Lang:t("success.tube_filled"), 'success') + return true end) local function deleteGear() @@ -33,112 +32,99 @@ local function deleteGear() end end -RegisterNetEvent('qb-diving:client:UseGear', function() - if isWearingSuit then - if lib.progressBar({ - duration = 5000, - label = Lang:t("info.pullout_suit"), - useWhileDead = false, - canCancel = true, - anim = { - dict = "clothingshirt", - clip = "try_shirt_positive_d", - blendIn = 8.0 - } - }) then - SetEnableScuba(cache.ped, false) - SetPedMaxTimeUnderwater(cache.ped, 50.00) - currentGear.enabled = false - deleteGear() - exports.qbx_core:Notify(Lang:t("success.took_out")) - TriggerServerEvent("InteractSound_SV:PlayOnSource", nil, 0.25) - isWearingSuit = false - oxygenLevel = oxygenLevel - end - - ClearPedTasks(cache.ped) - else - if oxygenLevel <= 0 then - exports.qbx_core:Notify(Lang:t("error.need_otube"), 'error') - return - end - - isWearingSuit = true - - if IsPedSwimming(cache.ped) or cache.vehicle then - exports.qbx_core:Notify(Lang:t("error.not_standing_up"), 'error') - return - end - - if lib.progressBar({ - duration = 5000, - label = Lang:t("info.put_suit"), - useWhileDead = false, - canCancel = true, - anim = { - dict = "clothingshirt", - clip = "try_shirt_positive_d", - blendIn = 8.0 - } - }) then - deleteGear() - local maskModel = `p_d_scuba_mask_s` - local tankModel = `p_s_scuba_tank_s` - lib.requestModel(maskModel) - lib.requestModel(tankModel) - currentGear.tank = CreateObject(tankModel, 1.0, 1.0, 1.0, true, true, false) - local bone1 = GetPedBoneIndex(cache.ped, 24818) - AttachEntityToEntity(currentGear.tank, cache.ped, bone1, -0.25, -0.25, 0.0, 180.0, 90.0, 0.0, true, true, false, false, 2, true) - currentGear.mask = CreateObject(maskModel, 1.0, 1.0, 1.0, true, true, false) - local bone2 = GetPedBoneIndex(cache.ped, 12844) - AttachEntityToEntity(currentGear.mask, cache.ped, bone2, 0.0, 0.0, 0.0, 180.0, 90.0, 0.0, true, true, false, false, 2, true) - SetEnableScuba(cache.ped, true) - SetPedMaxTimeUnderwater(cache.ped, 2000.00) - currentGear.enabled = true - TriggerServerEvent("InteractSound_SV:PlayOnSource", "breathdivingsuit", 0.25) - CreateThread(function() - while currentGear.enabled do - if IsPedSwimmingUnderWater(cache.ped) and oxygenLevel > 0 then - oxygenLevel -= 1 - if oxygenLevel % 10 == 0 and oxygenLevel ~= Config.OxygenLevel then - TriggerServerEvent("InteractSound_SV:PlayOnSource", "breathdivingsuit", 0.25) - elseif oxygenLevel == 0 then - SetEnableScuba(cache.ped, false) - SetPedMaxTimeUnderwater(cache.ped, 1.00) - currentGear.enabled = false - isWearingSuit = false - TriggerServerEvent("InteractSound_SV:PlayOnSource", nil, 0.25) - end - end - Wait(1000) - end - end) - end - - ClearPedTasks(cache.ped) +local function takeOffSuit() + if lib.progressBar({ + duration = 5000, + label = Lang:t("info.pullout_suit"), + useWhileDead = false, + canCancel = true, + anim = { + dict = "clothingshirt", + clip = "try_shirt_positive_d", + blendIn = 8.0 + } + }) then + SetEnableScuba(cache.ped, false) + SetPedMaxTimeUnderwater(cache.ped, 50.00) + currentGear.enabled = false + deleteGear() + exports.qbx_core:Notify(Lang:t("success.took_out")) + TriggerServerEvent("InteractSound_SV:PlayOnSource", nil, 0.25) end -end) -local function DrawText(text) - SetTextFont(4) - SetTextProportional(true) - SetTextScale(0.0, 0.45) - SetTextDropshadow(1, 0, 0, 0, 255) - SetTextEdge(1, 0, 0, 0, 255) - SetTextDropShadow() - SetTextOutline() - BeginTextCommandDisplayText("STRING") - AddTextComponentSubstringPlayerName(text) - EndTextCommandDisplayText(0.45, 0.90) + ClearPedTasks(cache.ped) end -CreateThread(function() - while true do - if currentGear.enabled and isWearingSuit then - if IsPedSwimmingUnderWater(cache.ped) then - DrawText(oxygenLevel..'⏱') - end - end - Wait(0) +local function putOnSuit() + if oxygenLevel <= 0 then + exports.qbx_core:Notify(Lang:t("error.need_otube"), 'error') + return end -end) + + if IsPedSwimming(cache.ped) or cache.vehicle then + exports.qbx_core:Notify(Lang:t("error.not_standing_up"), 'error') + return + end + + if lib.progressBar({ + duration = 5000, + label = Lang:t("info.put_suit"), + useWhileDead = false, + canCancel = true, + anim = { + dict = "clothingshirt", + clip = "try_shirt_positive_d", + blendIn = 8.0 + } + }) then + deleteGear() + local maskModel = `p_d_scuba_mask_s` + local tankModel = `p_s_scuba_tank_s` + lib.requestModel(maskModel) + lib.requestModel(tankModel) + currentGear.tank = CreateObject(tankModel, 1.0, 1.0, 1.0, true, true, false) + local bone1 = GetPedBoneIndex(cache.ped, 24818) + AttachEntityToEntity(currentGear.tank, cache.ped, bone1, -0.25, -0.25, 0.0, 180.0, 90.0, 0.0, true, true, false, false, 2, true) + currentGear.mask = CreateObject(maskModel, 1.0, 1.0, 1.0, true, true, false) + local bone2 = GetPedBoneIndex(cache.ped, 12844) + AttachEntityToEntity(currentGear.mask, cache.ped, bone2, 0.0, 0.0, 0.0, 180.0, 90.0, 0.0, true, true, false, false, 2, true) + SetEnableScuba(cache.ped, true) + SetPedMaxTimeUnderwater(cache.ped, 2000.00) + currentGear.enabled = true + TriggerServerEvent("InteractSound_SV:PlayOnSource", "breathdivingsuit", 0.25) + CreateThread(function() + while currentGear.enabled do + if IsPedSwimmingUnderWater(cache.ped) and oxygenLevel > 0 then + oxygenLevel -= 1 + if oxygenLevel % 10 == 0 and oxygenLevel ~= Config.OxygenLevel then + TriggerServerEvent("InteractSound_SV:PlayOnSource", "breathdivingsuit", 0.25) + elseif oxygenLevel == 0 then + SetEnableScuba(cache.ped, false) + SetPedMaxTimeUnderwater(cache.ped, 1.00) + TriggerServerEvent("InteractSound_SV:PlayOnSource", nil, 0.25) + break + end + end + Wait(1000) + end + end) + CreateThread(function() + while currentGear.enabled do + if IsPedSwimmingUnderWater(cache.ped) then + DrawText2D(oxygenLevel..'⏱', vec2(0.45, 0.90), 1.0, 1.0, 0.45) + end + Wait(0) + end + end) + end + + ClearPedTasks(cache.ped) +end + +RegisterNetEvent('qbx_divegear:client:useGear', function() + if currentGear.enabled then + takeOffSuit() + else + putOnSuit() + end +end) \ No newline at end of file diff --git a/fxmanifest.lua b/fxmanifest.lua index 51742a7..c4cb9d2 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -10,6 +10,8 @@ shared_script { 'locales/en.lua', 'locales/*.lua', 'config.lua', + '@qbx_core/import.lua', + 'qbx_core:utils', } server_scripts { diff --git a/server/main.lua b/server/main.lua index 524db63..1f2ad92 100644 --- a/server/main.lua +++ b/server/main.lua @@ -1,11 +1,10 @@ exports.qbx_core:CreateUseableItem("diving_gear", function(source) - TriggerClientEvent("qb-diving:client:UseGear", source) + TriggerClientEvent('qbx_divegear:client:useGear', source) end) exports.qbx_core:CreateUseableItem("diving_fill", function(source) - TriggerClientEvent("qb-diving:client:setoxygenlevel", source) -end) - -RegisterNetEvent('qb-diving:server:removeItemAfterFill', function() - exports.ox_inventory:RemoveItem(source, 'diving_fill', 1) + local success = lib.callback.await('qbx_divegear:client:fillTank', source) + if success then + exports.ox_inventory:RemoveItem(source, 'diving_fill', 1) + end end) \ No newline at end of file