-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.lua
More file actions
95 lines (85 loc) · 2.84 KB
/
init.lua
File metadata and controls
95 lines (85 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
local color_reset = "\x1b(c@#FFF)"
local c_pattern = "\x1b%(c@#?[0-9a-fA-F]+%)"
local c_namepat = "[A-z0-9-_]+"
core.register_on_receiving_chat_message(function(line)
local myname_l = "~[CAPS£"
if core.localplayer then
myname_l = core.localplayer:get_name():lower()
end
-- Detect color to still do the name mentioning effect
local color, line_nc = line:match("^(" .. c_pattern .. ")(.*)")
line = line_nc or line
local prefix -- Anything that comes before the player name
local is_chat_msg = false -- Whether to add "<", ">" around the name
local message_separator = " " -- What to add between the player name and the message
-- Chat message where the color starts after "<Name>" (no space)
local name, color_end, message = line:match("^%<(" .. c_namepat .. ")%>%s*(" .. c_pattern .. ")%s*(.*)")
if not message then
name, message = line:match("^%<(" .. c_namepat .. ")%> (.*)")
if name then
name = name:gsub(c_pattern, "")
end
is_chat_msg = (message ~= nil)
end
if not message then
-- Translated server messages, actions
prefix, name, message = line:match("^(.*\x1bF)(".. c_namepat .. ")(\x1bE.*)")
if message then
message_separator = ""
end
end
if not message then
-- Server messages, actions
prefix, name, message = line:match("^(%*+ )(" .. c_namepat .. ") (.*)")
end
if not message then
-- Colored prefix
prefix, name, message = line:match("^(.* )%<(" .. c_namepat .. ")%> (.*)")
if color and message and #prefix > 0 then
prefix = color .. prefix .. color_reset
color = nil
end
is_chat_msg = (message ~= nil)
end
if not message then
-- "Name: Message", or IRC notation (seen on some servers)
name, message = line:match("^(" .. c_namepat .. "): (.*)")
prefix = nil
is_chat_msg = (message ~= nil)
end
if not message then
-- Skip unknown chat line: Do not manipulate.
return
end
prefix = prefix or ""
local name_wrap = name
-- No color yet? We need color.
if not color then
local color = core.sha1(name, true)
local R = color:byte( 1) % 0x10
local G = color:byte(10) % 0x10
local B = color:byte(20) % 0x10
if R + G + B < 24 then
R = 15 - R
G = 15 - G
B = 15 - B
end
if is_chat_msg then
name_wrap = "<" .. name .. ">"
end
name_wrap = core.colorize(string.format("#%X%X%X", R, G, B), name_wrap)
elseif is_chat_msg then
name_wrap = "<" .. name .. ">"
end
-- Highlight messages that mention the current player name
if (is_chat_msg or prefix == "* ") and name:lower() ~= myname_l
and message:lower():find(myname_l) then
prefix = core.colorize("#F33", "[!] ") .. prefix
end
return core.display_chat_message(prefix .. (color or "")
.. name_wrap .. (color_end or "") .. message_separator .. message)
end)
if not ("").split then
core.log("action", "[colored_names] Luanti 5.15.2 detected. Using compatibility code.")
getmetatable("").__index = string
end