From bbd536d18abb27850bb4ec0895031558a2c90501 Mon Sep 17 00:00:00 2001 From: Lewis Watson Date: Tue, 23 Nov 2021 07:24:27 +1000 Subject: [PATCH] enhancement: joinquit settings support + code refactor (#364) * Moved script serverside and added support for settings - Added OOP - Use RGB instead of hex codes for ease of use - No more anonymous functions - Listen for settings changes * Use procedural code --- [gameplay]/joinquit/joinquit.lua | 92 +++++++++++++++++--------------- [gameplay]/joinquit/meta.xml | 17 +++++- 2 files changed, 65 insertions(+), 44 deletions(-) diff --git a/[gameplay]/joinquit/joinquit.lua b/[gameplay]/joinquit/joinquit.lua index a7878004d..b04beac57 100644 --- a/[gameplay]/joinquit/joinquit.lua +++ b/[gameplay]/joinquit/joinquit.lua @@ -1,59 +1,65 @@ -local showColorCodes = true; -- Shows player's names colorcoded if set to true, and if set to false it doesn't -local defaultHexCode = "#4E5768"; -- Hex code for what color to output messages in (only used if showColorCodes is true) - +local resourceName = getResourceName(getThisResource()) +local settingPrefix = string.format("*%s.", resourceName) + +local showColorCodes = get("showColorCodes") == "true" -- Shows player"s names colorcoded if set to true, and if set to false it doesn"t +local defaultColor = get("defaultColor") -- Hex code for what color to output messages in (only used if showColorCodes is true) +local fallbackHexCode = "#4E5768" -- Fallback hex code for incorrectly input settings values + +function reloadSettings(settingName) + -- Setting change affects this resource + if (string.find(settingName, settingPrefix, 1, true)) then + showColorCodes = get("showColorCodes") == "true" + defaultColor = get("defaultColor") + end +end +addEventHandler("onSettingChange", root, reloadSettings) -- This function converts RGB colors to colorcodes like #ffffff -function RGBToHex(red, green, blue, alpha) - +function RGBToHex(red, green, blue) -- Make sure RGB values passed to this function are correct - if( ( red < 0 or red > 255 or green < 0 or green > 255 or blue < 0 or blue > 255 ) or ( alpha and ( alpha < 0 or alpha > 255 ) ) ) then + if (not red or not green or not blue or (red < 0 or red > 255 or green < 0 or green > 255 or blue < 0 or blue > 255)) then return nil end - -- Alpha check - if alpha then - return string.format("#%.2X%.2X%.2X%.2X", red, green, blue, alpha) - else - return string.format("#%.2X%.2X%.2X", red, green, blue) - end - + return string.format("#%.2X%.2X%.2X", red, green, blue) end - -addEventHandler('onClientPlayerJoin', root, - function() - - if showColorCodes then - outputChatBox(defaultHexCode .. '* ' .. RGBToHex(getPlayerNametagColor(source)) .. getPlayerName(source) .. defaultHexCode .. ' has joined the game', 255, 100, 100, true) - else - outputChatBox('* ' .. getPlayerName(source) .. ' has joined the game', 255, 100, 100) - end - +function getDefaultColor() + local hexColor = RGBToHex(defaultColor[1], defaultColor[2], defaultColor[3]) + if (not hexColor) then + return fallbackHexCode end -) - -addEventHandler('onClientPlayerChangeNick', root, - function(oldNick, newNick) + return hexColor +end - if showColorCodes then - outputChatBox(defaultHexCode .. '* ' .. RGBToHex(getPlayerNametagColor(source)) .. oldNick .. defaultHexCode .. ' is now known as ' .. RGBToHex(getPlayerNametagColor(source)) .. newNick, 255, 100, 100, true) - else - outputChatBox('* ' .. oldNick .. ' is now known as ' .. newNick, 255, 100, 100) - end +function getHexFriendlyNick(player, nick) + return RGBToHex(getPlayerNametagColor(player))..nick +end +function joinMessage() + if (showColorCodes) then + outputChatBox(getDefaultColor().."* "..getHexFriendlyNick(source, getPlayerName(source))..getDefaultColor().." has joined the game", root, 255, 100, 100, true) + else + outputChatBox("* "..getPlayerName(source).." has joined the game", root, 255, 100, 100) end -) - - -addEventHandler('onClientPlayerQuit', root, - function(reason) +end +addEventHandler("onPlayerJoin", root, joinMessage) - if showColorCodes then - outputChatBox(defaultHexCode .. '* ' .. RGBToHex(getPlayerNametagColor(source)) .. getPlayerName(source) .. defaultHexCode .. ' has left the game [' .. reason .. ']', 255, 100, 100, true) - else - outputChatBox('* ' .. getPlayerName(source) .. ' has left the game [' .. reason .. ']', 255, 100, 100) - end +function nickChangeMessage(oldNick, newNick) + if (showColorCodes) then + outputChatBox(getDefaultColor().."* "..getHexFriendlyNick(source, oldNick)..getDefaultColor().." is now known as "..getHexFriendlyNick(source, newNick), root, 255, 100, 100, true) + else + outputChatBox("* "..oldNick.." is now known as "..newNick, root, 255, 100, 100) + end +end +addEventHandler("onPlayerChangeNick", root, nickChangeMessage) +function leftMessage(reason) + if (showColorCodes) then + outputChatBox(getDefaultColor().."* "..getHexFriendlyNick(source, getPlayerName(source))..getDefaultColor().." has left the game ["..reason.."]", root, 255, 100, 100, true) + else + outputChatBox("* "..getPlayerName(source).." has left the game ["..reason.."]", root, 255, 100, 100) end -) +end +addEventHandler("onPlayerQuit", root, leftMessage) diff --git a/[gameplay]/joinquit/meta.xml b/[gameplay]/joinquit/meta.xml index aae400526..e40f72880 100644 --- a/[gameplay]/joinquit/meta.xml +++ b/[gameplay]/joinquit/meta.xml @@ -1,3 +1,18 @@ -