-
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
0d424d1
commit bbd536d
Showing
2 changed files
with
65 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,18 @@ | ||
<meta> | ||
<script src="joinquit.lua" type="client" /> | ||
<info name="joinquit" author="Noki" description="Messages in the chat when players join or quit" type="script" /> | ||
<script src="joinquit.lua" type="server" /> | ||
<settings> | ||
<setting name="*showColorCodes" | ||
friendlyname="Show Color Codes" | ||
value="true" | ||
accept="true,false" | ||
desc="Allow hex color codes in nicknames to be shown in the chat" | ||
/> | ||
<setting name="*defaultColor" | ||
friendlyname="Default Color" | ||
value="[[78, 87, 104]]" | ||
accept="" | ||
desc="If show color codes is set to true, this controls what color to output messages in" | ||
/> | ||
</settings> | ||
</meta> |