]> git.lizzy.rs Git - crafter_client.git/blob - colored_names/colored_names.lua
Update README.md
[crafter_client.git] / colored_names / colored_names.lua
1 -- Backwards compatibility for 0.4.x
2 if not core.register_on_receiving_chat_message then
3         core.register_on_receiving_chat_message = core.register_on_receiving_chat_messages
4 end
5
6 local color_reset = "\x1b(c@#FFF)"
7 local c_pattern = "\x1b%(c@#[0-9a-fA-F]+%)"
8
9 core.register_on_receiving_chat_message(function(line)
10         local myname_l = "~[CAPSĀ£"
11         if core.localplayer then
12                 myname_l = core.localplayer:get_name():lower()
13         end
14
15         -- Detect color to still do the name mentioning effect
16         local color, line_nc = line:match("^(" .. c_pattern .. ")(.*)")
17         line = line_nc or line
18
19         local prefix
20         local chat_line = false
21
22         local name, color_end, message = line:match("^%<(%S+)%>%s*(" .. c_pattern .. ")%s*(.*)")
23         if not message then
24                 name, message = line:match("^%<(%S+)%> (.*)")
25                 if name then
26                         name = name:gsub(c_pattern, "")
27                 end
28         end
29
30         if message then
31                 -- To keep the <Name> notation
32                 chat_line = true
33         else
34                 -- Server messages, actions
35                 prefix, name, message = line:match("^(%*+ )(%S+) (.*)")
36         end
37         if not message then
38                 -- Colored prefix
39                 prefix, name, message = line:match("^(.* )%<(%S+)%> (.*)")
40                 if color and message and prefix:len() > 0 then
41                         prefix = color .. prefix .. color_reset
42                         color = nil
43                 end
44                 chat_line = true
45         end
46         if not message then
47                 -- Skip unknown chat line
48                 return
49         end
50
51         prefix = prefix or ""
52         local name_wrap = name
53
54         -- No color yet? We need color.
55         if not color then
56                 local color = core.sha1(name, true)
57                 local R = color:byte( 1) % 0x10
58                 local G = color:byte(10) % 0x10
59                 local B = color:byte(20) % 0x10
60                 if R + G + B < 24 then
61                         R = 15 - R
62                         G = 15 - G
63                         B = 15 - B
64                 end
65                 if chat_line then
66                         name_wrap = "<" .. name .. ">"
67                 end
68                 name_wrap = minetest.colorize(string.format("#%X%X%X", R, G, B), name_wrap)
69         elseif chat_line then
70                 name_wrap = "<" .. name .. ">"
71         end
72
73         if (chat_line or prefix == "* ") and name:lower() ~= myname_l
74                         and message:lower():find(myname_l) then
75                 prefix = minetest.colorize("#F33", "[!] ") .. prefix
76         end
77
78         return minetest.display_chat_message(prefix .. (color or "")
79                 .. name_wrap .. (color_end or "") .. " " .. message)
80 end)