Save and restore last used player colors (#3)

Clean up and re-format the code.
This commit is contained in:
Maksim 2019-12-30 21:01:28 +01:00 committed by SmallJoker
parent a39f4f9482
commit 2739649c47
4 changed files with 52 additions and 54 deletions

100
init.lua
View File

@ -1,13 +1,13 @@
local colors = {
green = {r = 0, g = 255, b = 0},
blue = {r = 0, g = 0, b = 255},
red = {r = 255, g = 0, b = 0},
purple = {r = 200, g = 0, b = 200},
yellow = {r = 255, g = 255, b = 0},
cyan = {r = 0, g = 255, b = 255},
white = {r = 255, g = 255, b = 255},
orange = {r = 255, g = 165, b = 0},
black = {r = 0, g = 0, b = 0},
green = {r = 0, g = 255, b = 0},
blue = {r = 0, g = 0, b = 255},
red = {r = 255, g = 0, b = 0},
purple = {r = 200, g = 0, b = 200},
yellow = {r = 255, g = 255, b = 0},
cyan = {r = 0, g = 255, b = 255},
white = {r = 255, g = 255, b = 255},
orange = {r = 255, g = 165, b = 0},
black = {r = 0, g = 0, b = 0}
}
-- Register command to allow players to colour their nametag
@ -17,61 +17,59 @@ minetest.register_chatcommand("nametag",{
privs = {shout = true},
func = function(user, param)
param = param:lower()
local found, _, redValue, greenValue, blueValue = param:find("^([^%s]+)%s+(.+)%s+(.+)$")
if param == "" then
minetest.after(1, minetest.show_formspec, user, "name-colours",
minetest.after(0, minetest.show_formspec, user, "nametag",
"size[8.5,4.5]"..
"label[0.25,0.5;Choose a color:]"..
"button_exit[0.25,1.5;2.5,0.5;green;Green]"..
"button_exit[3,1.5;2.5,0.5;blue;Blue]"..
"button_exit[5.75,1.5;2.5,0.5;red;Red]"..
"button_exit[0.25,2.5;2.5,0.5;purple;Purple]"..
"button_exit[3,2.5;2.5,0.5;yellow;Yellow]"..
"button_exit[5.75,2.5;2.5,0.5;cyan;Cyan]"..
"button_exit[0.25,3.5;2.5,0.5;white;White]"..
"button_exit[3,3.5;2.5,0.5;orange;Orange]"..
"label[0.25,0.5;Choose a color:]" ..
"button_exit[0.25,1.5;2.5,0.5;green;Green]" ..
"button_exit[3,1.5;2.5,0.5;blue;Blue]" ..
"button_exit[5.75,1.5;2.5,0.5;red;Red]" ..
"button_exit[0.25,2.5;2.5,0.5;purple;Purple]" ..
"button_exit[3,2.5;2.5,0.5;yellow;Yellow]" ..
"button_exit[5.75,2.5;2.5,0.5;cyan;Cyan]" ..
"button_exit[0.25,3.5;2.5,0.5;white;White]" ..
"button_exit[3,3.5;2.5,0.5;orange;Orange]" ..
"button_exit[5.75,3.5;2.5,0.5;black;Black]"
)
elseif colors[param] then
local player = minetest.get_player_by_name(user)
player:set_nametag_attributes({
color = colors[param]
})
return true, "Your nametag color has been set to: " .. param
end
return false, "Invalid usage, see /help nametag"
end
})
player:set_nametag_attributes({color = colors[param]})
player:set_attribute("nametag_color", minetest.serialize(colors[param]))
-- Customized RGB-format color
minetest.register_chatcommand("custom-nametag", {
params = "<red> <green> <blue>",
description = "Customizable-nametag color",
privs = {nametag = true},
func = function(user, param)
local found, _, redValue, greenValue, blueValue = param:find("^([^%s]+)%s+(.+)%s+(.+)$")
if not found then
return false, "Invalid usage, see /help custom-nametag"
end
local player = minetest.get_player_by_name(user)
player:set_nametag_attributes({
color = {r = redValue, g = greenValue, b = blueValue}
})
return true, "Your nametag color has been set to " .. param:gsub("^%l", string.upper)
elseif found then
local player = minetest.get_player_by_name(user)
local color = {r = redValue, g = greenValue, b = blueValue}
player:set_nametag_attributes({color = color})
player:set_attribute("nametag_color", minetest.serialize(color))
return true, "Your nametag color has been set to the value of: " ..
redValue .. ", " .. greenValue .. ", " .. blueValue
return true, "Your nametag color has been set to the value of: " ..
redValue .. ", " .. greenValue .. ", " .. blueValue
else
return false, "Incorrect Color! If you don't know what colours are avalible, just use /nametag without an option after it."
end
end
})
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "name-colours" then return end
for key, value in pairs(colors) do
if fields[key] then
player:set_nametag_attributes({
color = colors[key]
})
if formname == "nametag" then
for key, value in pairs(colors) do
if fields[key] then
player:set_nametag_attributes({color = colors[key]})
player:set_attribute("nametag_color", minetest.serialize(colors[key]))
minetest.chat_send_player(player:get_player_name(), "Your nametag color has been set to " .. key:gsub("^%l", string.upper))
end
end
end
end
end)
minetest.register_on_joinplayer(function(player)
local color = minetest.deserialize(player:get_attribute("nametag_color"))
if color then
player:set_nametag_attributes({color = color})
end
end)
-- Log

View File

@ -1,2 +1,2 @@
name = coloured_nametag
description = Allows players to change the colour of their nametags.
description = Allows players to change the colour of their nametags.

View File

@ -57,5 +57,5 @@ Special thanks to Chanku/Sapein.
* Maybe make a second page of colours?
## License:
Copyright (C) Amaz1 2015-2016.
See [`license.txt`](/license.txt) for more information.
Copyright (C) Amaz1 2015-2016.
See [`license.txt`](/license.txt) for more information.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 82 KiB

After

Width:  |  Height:  |  Size: 67 KiB