aboutsummaryrefslogtreecommitdiff
path: root/init.lua
blob: 0d9d6a4c1d6922b35f34b6cf3b5cb236457bc8d0 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
local modstorage = core.get_mod_storage()

local modpath = minetest.get_modpath("colour_chat")

dofile(modpath .. "/colournames.lua")

local register_on_message = core.register_on_sending_chat_message
if core.register_on_sending_chat_messages then
	register_on_message = core.register_on_sending_chat_messages
end

local function safe(func)
    -- wrap a function w/ logic to avoid crashing the game
    local f = function(...)
        local status, out = pcall(func, ...)
        if status then
            return out
        else
            log('warning', 'Error (func):  ' .. out)
            return nil
        end
    end
    return f
end

local function paramsplit(str)
   -- minetest.display_chat_message("paramsplit 1")
   local s = str:gsub("^%s*(.-)%s*$", "%1")
   local p1 = string.find(s," ",1,true)
   if not p1 then return nil end
   -- minetest.display_chat_message("paramsplit 2")
   local p2 = string.find(s," ",p1+1,true)
   local s1 = str:sub(1,p1-1)
   if not p2 then return {s1,s1,str:sub(p1+1,512)}
 end
   -- minetest.display_chat_message("paramsplit 3")
   local s2 = str:sub(p1+1,p2-1)
   local s3 = str:sub(p2+1,512)
   return {s1,s2,s3}
end

local function rgb_to_hex(rgb)
	local hexadecimal = '#'

	for key, value in pairs(rgb) do
		local hex = ''

		while(value > 0)do
			local index = math.fmod(value, 16) + 1
			value = math.floor(value / 16)
			hex = string.sub('0123456789ABCDEF', index, index) .. hex			
		end

		if(string.len(hex) == 0)then
			hex = '00'

		elseif(string.len(hex) == 1)then
			hex = '0' .. hex
		end

		hexadecimal = hexadecimal .. hex
	end

	return hexadecimal
end

local function color_from_hue(hue)
	local h = hue / 60
	local c = 255
	local x = (1 - math.abs(h%2 - 1)) * 255

  	local i = math.floor(h);
  	if (i == 0) then
		return rgb_to_hex({c, x, 0})
  	elseif (i == 1) then 
		return rgb_to_hex({x, c, 0})
  	elseif (i == 2) then 
		return rgb_to_hex({0, c, x})
	elseif (i == 3) then
		return rgb_to_hex({0, x, c});
	elseif (i == 4) then
		return rgb_to_hex({x, 0, c});
	else 
		return rgb_to_hex({c, 0, x});
	end
end

local function pastel_from_hue(hue)
	local h = hue / 60
	local c = 255
   local d = 192
	local x = (1 - math.abs(h%2 - 1)) * 255

  	local i = math.floor(h);
  	if (i == 0) then
		return rgb_to_hex({c, x, d})
  	elseif (i == 1) then 
		return rgb_to_hex({x, c, d})
  	elseif (i == 2) then 
		return rgb_to_hex({d, c, x})
	elseif (i == 3) then
		return rgb_to_hex({d, x, c});
	elseif (i == 4) then
		return rgb_to_hex({x, d, c});
	else 
		return rgb_to_hex({c, d, x});
	end
end

function hex2rgb(hex)  -- added - found on github
    hex = hex:gsub("#","")
    return {tonumber("0x"..hex:sub(1,2)), tonumber("0x"..hex:sub(3,4)), tonumber("0x"..hex:sub(5,6))}
end

local function color_step(pos, col1, col2)  -- added - gets a color between colors
   --   if not col1 then col1 = "#888888" end
   -- if not col2 then col2 = "#888888" end
	local rgb1 = hex2rgb(col1)
	local rgb2 = hex2rgb(col2)
	local rgb = {}
	for i, v in pairs(rgb1) do
		rgb[i] = rgb1[i]+(rgb2[i]-rgb1[i])*(pos/255)
	end
	return rgb_to_hex(rgb)
end

local function canTalk()
	if core.get_privilege_list then
		return core.get_privilege_list().shout
	else
		return true
	end
end

local function say(message)
	if not canTalk() then
		minetest.display_chat_message("You need 'shout' in order to talk")
		return
	end
	minetest.send_chat_message(message)
	if minetest.get_server_info().protocol_version < 29 then
		local name = minetest.localplayer:get_name()
		minetest.display_chat_message("<"..name.."> " .. message)
	end
end

function fade(parameter)
   local p = paramsplit(parameter)
   if not p then return end
   -- local col1 = string.sub(parameter, 1, 7)
   -- local col2 = string.sub(parameter, 9, 15)
   -- local message = string.sub(parameter, 17)

   local col1 = colourname2hex(p[1])
   -- minetest.display_chat_message("col1.a: "..col1)
   -- minetest.display_chat_message("col1.b: "..core.get_color_escape_sequence(col1):sub(2,-1))
   -- minetest.display_chat_message("col1.c: "..minetest.colorize(col1):sub(2,-1))
   local col2 = colourname2hex(p[2])
   -- minetest.display_chat_message("col2.a: "..p[2])
   -- minetest.display_chat_message("col2.b: "..col2)
   local message = p[3]
   -- minetest.display_chat_message("msg: "..message)

   -- if true then return end

   if not canTalk() then
      return false, "You need 'shout' in order to use this command"
   end
   if col1 == col2 then
      -- minetest.display_chat_message("col1=col2")
      say(minetest.get_color_escape_sequence(col1) .. message)
      -- minetest.display_chat_message("col1=col2 b")
      return true
   end

   -- minetest.display_chat_message("pos4")

   local step = 255 / (message:len()/2)
   local pos = 0
   local output = ""
   for i = 1, message:len() do
      local char = message:sub(i,i)
      if char:match("%s") then
	 output = output .. char
      else
	 output = output .. minetest.get_color_escape_sequence(color_step(pos, col1, col2)) .. char 
      end
      pos = pos + step
   end
   say(output)
   return true
end

register_on_message(function(message)
	if message:sub(1,1) == "/" or modstorage:get_string("colour") == "" or modstorage:get_string("colour") == "white" then
		return false
	end

	fade(modstorage:get_string("colour") .. " " .. message)
	return true
end)

core.register_chatcommand("set_colour", {
	description = core.gettext("Change chat colour"),
	func = function(colour)
		  local c = paramsplit(colour)
		  if not c then 		     
		     local c1 = colourname2hex(colour)
		     if c1 and c1 ~= "" then 
			modstorage:set_string("colour", colour.." "..colour)
			return true, "Chat colour changed."
		     else
			return true,"'"..colour.."' is not a colour"
		     end
		  end
		  c1  = colourname2hex(c[1])
		  c2  = colourname2hex(c[3])
		  if not c1 or c1 == "" then 
		     return true,"'"..c[1].."' is not a colour"
		  end
		  if not c2 or c2 == "" then 
		     return true,"'"..c[2].."' is not a colour"
		  end
		  modstorage:set_string("colour", c1.." "..c2)
		  return true, "Chat colours changed."
	       end,
})

core.register_chatcommand("get_colour", {
        description = core.gettext("Display chat colour"),
	func = function()
		  local cc = modstorage:get_string("colour")
		  return true, "Current chat colour: " .. cc
	       end,
     })
			     

core.register_chatcommand("rainbow", {
	description = core.gettext("rainbow text"),
	func = function(param)
		if not canTalk() then
			return false, "You need 'shout' in order to use this command"
		end
		local step = 360 / param:len()
 		local hue = 0
     		 -- iterate the whole 360 degrees
		local output = ""
      		for i = 1, param:len() do
			local char = param:sub(i,i)
			if char:match("%s") then
				output = output .. char
			else
        			output = output  .. core.get_color_escape_sequence(colour_from_hue(hue)) .. char 
			end
        		hue = hue + step
		end
		say(output)
		return true
end,
})

core.register_chatcommand("fade", {  -- added - fades between any two colors
			     description = core.gettext("fade message between two colors"),
			     func = fade,
			  })

core.register_chatcommand("pastel", {
	description = core.gettext("pastel rainbow text"),
	func = function(param)
		if not canTalk() then
			return false, "You need 'shout' in order to use this command"
		end
		local step = 360 / param:len()
 		local hue = 0
     		 -- iterate the whole 360 degrees
		local output = ""
      		for i = 1, param:len() do
			local char = param:sub(i,i)
			if char:match("%s") then
				output = output .. char
			else
        			output = output  .. core.get_color_escape_sequence(pastel_from_hue(hue)) .. char 
			end
        		hue = hue + step
		end
		say(output)
		return true
end,
})

core.register_chatcommand("mw", {
        description = core.gettext("moderator warning"),
        func = function(param)
                if not canTalk() then
                        return false, "You need 'shout' in order to use this comand"
                end
		minetest.run_server_chatcommand('mw', param)
                -- local output = ""
                -- output = core.get_color_escape_sequence("#ffdf00") .. "MODERATOR WARNING: "
                -- output = output .. core.get_color_escape_sequence("#ff7fff") ..param
                -- say(output)
                return true
end,
})


core.register_chatcommand("mi", {
        description = core.gettext("moderator info"),
        func = function(param)
                if not canTalk() then
                        return false, "You need 'shout' in order to use this command"
                end
		minetest.run_server_chatcommand('mi', param)
                -- local output = ""
		-- output = core.get_color_escape_sequence("#d0ff00") .. "MODERATOR INFO: " 
		-- output = output .. core.get_color_escape_sequence("#dfdfff") .. param
    		-- say(output)
		return true
end,
})



core.register_chatcommand("rainbow", {
	description = core.gettext("rainbow text"),
	func = function(param)
		if not canTalk() then
			return false, "You need 'shout' in order to use this command"
		end
		local step = 360 / param:len()
 		local hue = 0
     		 -- iterate the whole 360 degrees
		local output = ""
      		for i = 1, param:len() do
			local char = param:sub(i,i)
			if char:match("%s") then
				output = output .. char
			else
        			output = output  .. core.get_color_escape_sequence(color_from_hue(hue)) .. char 
			end
        		hue = hue + step
		end
		say(output)
		return true
end,
})

core.register_chatcommand("say", {
	description = core.gettext("Send text without applying colour to it"),
	func = function(text)
		say(text)
		return true
	end,
})