From 15e69fe40bae9904f6bb60ecda36c1e67fda0bb4 Mon Sep 17 00:00:00 2001
From: Mahan Moulaei <75180223+mahanmoulaei@users.noreply.github.com>
Date: Wed, 13 Apr 2022 17:32:31 -0400
Subject: [PATCH] feat(Config): Add config to let players customize how their
name is displayed on the radio list
---
Client/Client.lua | 13 ++++--
Config.lua | 10 ++--
README.md | 3 ++
Server/Server.lua | 115 +++++++++++++++++++++++++++++++---------------
4 files changed, 96 insertions(+), 45 deletions(-)
diff --git a/Client/Client.lua b/Client/Client.lua
index e00d488..d74a1f9 100644
--- a/Client/Client.lua
+++ b/Client/Client.lua
@@ -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)
@@ -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)
@@ -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
diff --git a/Config.lua b/Config.lua
index 7debeff..adf11ef 100644
--- a/Config.lua
+++ b/Config.lua
@@ -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",
diff --git a/README.md b/README.md
index 74175ad..5e7c347 100644
--- a/README.md
+++ b/README.md
@@ -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
## 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
diff --git a/Server/Server.lua b/Server/Server.lua
index d6b32cd..0a12171 100644
--- a/Server/Server.lua
+++ b/Server/Server.lua
@@ -1,3 +1,4 @@
+local PMA = exports['pma-voice']
local Framework = nil
local Core = nil
@@ -14,7 +15,7 @@ if Config.UseRPName then
end
end
-
+local CustomNames = {}
local PlayersInCurrentRadioChannel = {}
--TODO : Check The Bug In playerDropped Event
AddEventHandler("playerDropped", function()
@@ -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
@@ -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
\ No newline at end of file