Skip to content

Commit bbd536d

Browse files
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
1 parent 0d424d1 commit bbd536d

File tree

2 files changed

+65
-44
lines changed

2 files changed

+65
-44
lines changed

[gameplay]/joinquit/joinquit.lua

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,65 @@
1-
local showColorCodes = true; -- Shows player's names colorcoded if set to true, and if set to false it doesn't
2-
local defaultHexCode = "#4E5768"; -- Hex code for what color to output messages in (only used if showColorCodes is true)
3-
1+
local resourceName = getResourceName(getThisResource())
2+
local settingPrefix = string.format("*%s.", resourceName)
3+
4+
local showColorCodes = get("showColorCodes") == "true" -- Shows player"s names colorcoded if set to true, and if set to false it doesn"t
5+
local defaultColor = get("defaultColor") -- Hex code for what color to output messages in (only used if showColorCodes is true)
6+
local fallbackHexCode = "#4E5768" -- Fallback hex code for incorrectly input settings values
7+
8+
function reloadSettings(settingName)
9+
-- Setting change affects this resource
10+
if (string.find(settingName, settingPrefix, 1, true)) then
11+
showColorCodes = get("showColorCodes") == "true"
12+
defaultColor = get("defaultColor")
13+
end
14+
end
15+
addEventHandler("onSettingChange", root, reloadSettings)
416

517
-- This function converts RGB colors to colorcodes like #ffffff
6-
function RGBToHex(red, green, blue, alpha)
7-
18+
function RGBToHex(red, green, blue)
819
-- Make sure RGB values passed to this function are correct
9-
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
20+
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
1021
return nil
1122
end
1223

13-
-- Alpha check
14-
if alpha then
15-
return string.format("#%.2X%.2X%.2X%.2X", red, green, blue, alpha)
16-
else
17-
return string.format("#%.2X%.2X%.2X", red, green, blue)
18-
end
19-
24+
return string.format("#%.2X%.2X%.2X", red, green, blue)
2025
end
2126

22-
23-
addEventHandler('onClientPlayerJoin', root,
24-
function()
25-
26-
if showColorCodes then
27-
outputChatBox(defaultHexCode .. '* ' .. RGBToHex(getPlayerNametagColor(source)) .. getPlayerName(source) .. defaultHexCode .. ' has joined the game', 255, 100, 100, true)
28-
else
29-
outputChatBox('* ' .. getPlayerName(source) .. ' has joined the game', 255, 100, 100)
30-
end
31-
27+
function getDefaultColor()
28+
local hexColor = RGBToHex(defaultColor[1], defaultColor[2], defaultColor[3])
29+
if (not hexColor) then
30+
return fallbackHexCode
3231
end
33-
)
34-
3532

36-
addEventHandler('onClientPlayerChangeNick', root,
37-
function(oldNick, newNick)
33+
return hexColor
34+
end
3835

39-
if showColorCodes then
40-
outputChatBox(defaultHexCode .. '* ' .. RGBToHex(getPlayerNametagColor(source)) .. oldNick .. defaultHexCode .. ' is now known as ' .. RGBToHex(getPlayerNametagColor(source)) .. newNick, 255, 100, 100, true)
41-
else
42-
outputChatBox('* ' .. oldNick .. ' is now known as ' .. newNick, 255, 100, 100)
43-
end
36+
function getHexFriendlyNick(player, nick)
37+
return RGBToHex(getPlayerNametagColor(player))..nick
38+
end
4439

40+
function joinMessage()
41+
if (showColorCodes) then
42+
outputChatBox(getDefaultColor().."* "..getHexFriendlyNick(source, getPlayerName(source))..getDefaultColor().." has joined the game", root, 255, 100, 100, true)
43+
else
44+
outputChatBox("* "..getPlayerName(source).." has joined the game", root, 255, 100, 100)
4545
end
46-
)
47-
48-
49-
addEventHandler('onClientPlayerQuit', root,
50-
function(reason)
46+
end
47+
addEventHandler("onPlayerJoin", root, joinMessage)
5148

52-
if showColorCodes then
53-
outputChatBox(defaultHexCode .. '* ' .. RGBToHex(getPlayerNametagColor(source)) .. getPlayerName(source) .. defaultHexCode .. ' has left the game [' .. reason .. ']', 255, 100, 100, true)
54-
else
55-
outputChatBox('* ' .. getPlayerName(source) .. ' has left the game [' .. reason .. ']', 255, 100, 100)
56-
end
49+
function nickChangeMessage(oldNick, newNick)
50+
if (showColorCodes) then
51+
outputChatBox(getDefaultColor().."* "..getHexFriendlyNick(source, oldNick)..getDefaultColor().." is now known as "..getHexFriendlyNick(source, newNick), root, 255, 100, 100, true)
52+
else
53+
outputChatBox("* "..oldNick.." is now known as "..newNick, root, 255, 100, 100)
54+
end
55+
end
56+
addEventHandler("onPlayerChangeNick", root, nickChangeMessage)
5757

58+
function leftMessage(reason)
59+
if (showColorCodes) then
60+
outputChatBox(getDefaultColor().."* "..getHexFriendlyNick(source, getPlayerName(source))..getDefaultColor().." has left the game ["..reason.."]", root, 255, 100, 100, true)
61+
else
62+
outputChatBox("* "..getPlayerName(source).." has left the game ["..reason.."]", root, 255, 100, 100)
5863
end
59-
)
64+
end
65+
addEventHandler("onPlayerQuit", root, leftMessage)

[gameplay]/joinquit/meta.xml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
<meta>
2-
<script src="joinquit.lua" type="client" />
2+
<info name="joinquit" author="Noki" description="Messages in the chat when players join or quit" type="script" />
3+
<script src="joinquit.lua" type="server" />
4+
<settings>
5+
<setting name="*showColorCodes"
6+
friendlyname="Show Color Codes"
7+
value="true"
8+
accept="true,false"
9+
desc="Allow hex color codes in nicknames to be shown in the chat"
10+
/>
11+
<setting name="*defaultColor"
12+
friendlyname="Default Color"
13+
value="[[78, 87, 104]]"
14+
accept=""
15+
desc="If show color codes is set to true, this controls what color to output messages in"
16+
/>
17+
</settings>
318
</meta>

0 commit comments

Comments
 (0)