From 94275ae188a6e6d50c160b5b783b96b0ee5af983 Mon Sep 17 00:00:00 2001 From: Lewis Watson Date: Tue, 2 Nov 2021 18:19:58 +1000 Subject: [PATCH 1/2] 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 --- [gameplay]/joinquit/joinquit.lua | 92 +++++++++++++++++--------------- [gameplay]/joinquit/meta.xml | 18 ++++++- 2 files changed, 66 insertions(+), 44 deletions(-) diff --git a/[gameplay]/joinquit/joinquit.lua b/[gameplay]/joinquit/joinquit.lua index a7878004d..383526457 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 = (Resource.getThis()).name +local settingPrefix = ("*%s."):format(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 (settingName:find(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(player:getNametagColor())..nick +end +function joinMessage() + if (showColorCodes) then + outputChatBox(getDefaultColor().."* "..getHexFriendlyNick(source, source.name)..getDefaultColor().." has joined the game", root, 255, 100, 100, true) + else + outputChatBox("* "..source.name.." 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, source.name)..getDefaultColor().." has left the game ["..reason.."]", root, 255, 100, 100, true) + else + outputChatBox("* "..source.name.." 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..5b2a44aed 100644 --- a/[gameplay]/joinquit/meta.xml +++ b/[gameplay]/joinquit/meta.xml @@ -1,3 +1,19 @@ -