Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Commit

Permalink
feat(Config): Add config to let players customize how their name is d…
Browse files Browse the repository at this point in the history
…isplayed on the radio list
  • Loading branch information
mahanmoulaei committed Apr 13, 2022
1 parent 6c96283 commit 15e69fe
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 45 deletions.
13 changes: 9 additions & 4 deletions Client/Client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ local PlayersInRadio = {}
local firstTimeEventGetsTriggered = true
local RadioChannelsName = {--[[Will Be Automatically Filled With Channels' Name => e.g. every frequency between 0 and 1 will be named to Admin Radio]]}

RegisterNetEvent('JolbakLifeRP-RadioList:Client:SyncRadioChannelPlayers')
AddEventHandler('JolbakLifeRP-RadioList:Client:SyncRadioChannelPlayers', function(src, RadioChannelToJoin, PlayersInRadioChannel)
RegisterNetEvent('JLRP-RadioList:Client:SyncRadioChannelPlayers')
AddEventHandler('JLRP-RadioList:Client:SyncRadioChannelPlayers', function(src, RadioChannelToJoin, PlayersInRadioChannel)
if firstTimeEventGetsTriggered then
for i, v in pairs(Config.RadioChannelsWithName) do
local frequency = tonumber(i)
Expand Down Expand Up @@ -78,8 +78,8 @@ AddEventHandler('pma-voice:radioActive', function(talkingState)
SendNUIMessage({ radioId = PlayerServerID, radioTalking = talkingState }) -- Set player talking in radio list
end)

RegisterNetEvent('JolbakLifeRP-RadioList:Client:DisconnectPlayerCurrentChannel')
AddEventHandler('JolbakLifeRP-RadioList:Client:DisconnectPlayerCurrentChannel', function()
RegisterNetEvent('JLRP-RadioList:Client:DisconnectPlayerCurrentChannel')
AddEventHandler('JLRP-RadioList:Client:DisconnectPlayerCurrentChannel', function()
ResetTheRadioList() -- Delete the PlayersInRadio contents so it opens up memory
HideTheRadioList()
end)
Expand All @@ -100,4 +100,9 @@ if Config.LetPlayersChangeVisibilityOfRadioList then
visibility = not visibility
SendNUIMessage({ changeVisibility = true, visible = visibility })
end)
TriggerEvent("chat:addSuggestion", "/"..Config.RadioListVisibilityCommand, "Show/Hide Radio List")
end

if Config.LetPlayersSetTheirOwnNameInRadio then
TriggerEvent("chat:addSuggestion", "/"..Config.RadioListChangeNameCommand, "Customize your name to be shown in radio list", { { name = 'customized name', help = "Enter your desired name to be shown in radio list" } })
end
10 changes: 7 additions & 3 deletions Config.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
Config = {}

Config.UseRPName = true
Config.UseRPName = true -- If set to true, it uses either esx-legacy or qb-core built-in function to get players' RP name

Config.LetPlayersChangeVisibilityOfRadioList = true
Config.RadioListVisibilityCommand = "radiolist" --Only works if Config.LetPlayersChangeVisibilityOfRadioList is set to true
Config.LetPlayersChangeVisibilityOfRadioList = true -- Let players to toggle visibility of the list
Config.RadioListVisibilityCommand = "radiolist" -- Only works if Config.LetPlayersChangeVisibilityOfRadioList is set to true

Config.LetPlayersSetTheirOwnNameInRadio = true -- Let players to customize how their name is displayed on the list
Config.ResetPlayersCustomizedNameOnExit = true -- Only works if Config.LetPlayersSetTheirOwnNameInRadio is set to true - Removes customized name players set for themselves on their server exit
Config.RadioListChangeNameCommand = "nameinradio" -- Only works if Config.LetPlayersSetTheirOwnNameInRadio is set to true

Config.RadioChannelsWithName = {
["0"] = "Admin",
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
* Config to show players’ RP name (needs framework integration - works with ESX-Legacy & QB-Core by default)
* Changes the color of each player when they talk on radio
* Ability to name radio channels in config (e.g. all frequencies between 0 and 1 called "Admin Radio", between 1 and 2 called "Police Radio" & etc)
* Ability to toggle visibility of the list for self
* Ability to customize self name on the radio list
* Super easy to integrate other frameworks compatibility

<hr>

## Changelog
* V1.4 : Add config to let players customize how their name is displayed on the radio list
* V1.3 : Add config to let players hide/show the list
* V1.2 : Add config to use RP name for QB-Core
* V1.1 : Add config to use RP name for ESX-Legacy
Expand Down
115 changes: 77 additions & 38 deletions Server/Server.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local PMA = exports['pma-voice']
local Framework = nil
local Core = nil

Expand All @@ -14,7 +15,7 @@ if Config.UseRPName then
end
end


local CustomNames = {}
local PlayersInCurrentRadioChannel = {}
--TODO : Check The Bug In playerDropped Event
AddEventHandler("playerDropped", function()
Expand All @@ -23,12 +24,19 @@ AddEventHandler("playerDropped", function()
local currentRadioChannel = Player(src).state.radioChannel

local playersInCurrentRadioChannel = CreateFullRadioListOfChannel(currentRadioChannel)
for index, player in pairs(playersInCurrentRadioChannel) do
for _, player in pairs(playersInCurrentRadioChannel) do
if player.Source ~= src then
TriggerClientEvent("JolbakLifeRP-RadioList:Client:SyncRadioChannelPlayers", player.Source, src, 0, playersInCurrentRadioChannel)
TriggerClientEvent("JLRP-RadioList:Client:SyncRadioChannelPlayers", player.Source, src, 0, playersInCurrentRadioChannel)
end
end
playersInCurrentRadioChannel = {}

if Config.LetPlayersSetTheirOwnNameInRadio and Config.ResetPlayersCustomizedNameOnExit then
local playerIdentifier = GetIdentifier(src)
if CustomNames[playerIdentifier] and CustomNames[playerIdentifier] ~= nil then
CustomNames[playerIdentifier] = nil
end
end
end)

--This event is located on pma-voice/server/module/radio.lua
Expand All @@ -52,59 +60,90 @@ function Connect(src, currentRadioChannel, radioChannelToJoin)
Wait(1000) -- Wait for pma-voice to finilize setting the player radio channel

local playersInCurrentRadioChannel = CreateFullRadioListOfChannel(radioChannelToJoin)
for index, player in pairs(playersInCurrentRadioChannel) do
TriggerClientEvent("JolbakLifeRP-RadioList:Client:SyncRadioChannelPlayers", player.Source, src, radioChannelToJoin, playersInCurrentRadioChannel)
for _, player in pairs(playersInCurrentRadioChannel) do
TriggerClientEvent("JLRP-RadioList:Client:SyncRadioChannelPlayers", player.Source, src, radioChannelToJoin, playersInCurrentRadioChannel)
end
playersInCurrentRadioChannel = {}
end

function Disconnect(src, currentRadioChannel)
local playersInCurrentRadioChannel = CreateFullRadioListOfChannel(currentRadioChannel)
TriggerClientEvent("JolbakLifeRP-RadioList:Client:SyncRadioChannelPlayers", src, src, 0, playersInCurrentRadioChannel)
for index, player in pairs(playersInCurrentRadioChannel) do
TriggerClientEvent("JolbakLifeRP-RadioList:Client:SyncRadioChannelPlayers", player.Source, src, 0, playersInCurrentRadioChannel)
TriggerClientEvent("JLRP-RadioList:Client:SyncRadioChannelPlayers", src, src, 0, playersInCurrentRadioChannel)
for _, player in pairs(playersInCurrentRadioChannel) do
TriggerClientEvent("JLRP-RadioList:Client:SyncRadioChannelPlayers", player.Source, src, 0, playersInCurrentRadioChannel)
end
playersInCurrentRadioChannel = {}
end

function CreateFullRadioListOfChannel(RadioChannel)
local playersInRadio = exports['pma-voice']:getPlayersInRadioChannel(RadioChannel)
local playersInRadio = PMA:getPlayersInRadioChannel(RadioChannel)
for player, isTalking in pairs(playersInRadio) do
playersInRadio[player] = {}
playersInRadio[player].Source = player

playersInRadio[player].Source = player
playersInRadio[player].Name = GetPlayerNameForRadio(player)
end

return playersInRadio
end

function GetPlayerNameForRadio(source)
if Config.LetPlayersSetTheirOwnNameInRadio then
local playerIdentifier = GetIdentifier(source)
if CustomNames[playerIdentifier] and CustomNames[playerIdentifier] ~= nil then
return CustomNames[playerIdentifier]
end
end

if Config.UseRPName then
local name = nil

if Config.UseRPName then
if Framework == 'ESX' then
local xPlayer = Core.GetPlayerFromId(player)
if xPlayer then
name = xPlayer.getName()
end
elseif Framework == 'QB' then
local xPlayer = Core.Functions.GetPlayer(player)
if xPlayer then
name = xPlayer.PlayerData.charinfo.firstname..' '..xPlayer.PlayerData.charinfo.lastname
end
elseif Framework == 'JLRP' then
local xPlayer = Core.GetPlayerFromId(player)
if xPlayer then
name = xPlayer.getName()
else --extra check to make sure player sends a name to client
name = GetPlayerName(player)
end
if Framework == 'ESX' then
local xPlayer = Core.GetPlayerFromId(source)
if xPlayer then
name = xPlayer.getName()
end
elseif Framework == 'QB' then
local xPlayer = Core.Functions.GetPlayer(source)
if xPlayer then
name = xPlayer.PlayerData.charinfo.firstname..' '..xPlayer.PlayerData.charinfo.lastname
end

if name == nil then --extra check to make sure player sends a name to client
name = GetPlayerName(player)
elseif Framework == 'JLRP' then
local xPlayer = Core.GetPlayerFromId(source)
if xPlayer then
name = xPlayer.getName()
end
else
name = GetPlayerName(player)
end
if name == nil then --extra check to make sure player sends a name to client
name = GetPlayerName(source)
end
playersInRadio[player].Name = name
return name
else
return GetPlayerName(source)
end

return playersInRadio
end

if Config.LetPlayersSetTheirOwnNameInRadio then
local commandLength = string.len(Config.RadioListChangeNameCommand)
local argumentStartIndex = commandLength + 2
RegisterCommand(Config.RadioListChangeNameCommand, function(source, args, rawCommand)
local _source = source
if _source > 0 then
local customizedName = rawCommand:sub(argumentStartIndex)
if customizedName ~= "" and customizedName ~= " " and customizedName ~= nil then
CustomNames[GetIdentifier(_source)] = customizedName
local currentRadioChannel = Player(_source).state.radioChannel
if currentRadioChannel > 0 then
Connect(_source, currentRadioChannel, currentRadioChannel)
end
end
end
end)
end

function GetIdentifier(source)
for _, v in ipairs(GetPlayerIdentifiers(source)) do
if string.match(v, 'license:') then
local identifier = string.gsub(v, 'license:', '')
return identifier
end
end
end

0 comments on commit 15e69fe

Please sign in to comment.