2021-03-08 16:15:52 +01:00
|
|
|
local C = minetest.colorize
|
|
|
|
local has_mcl_colors = minetest.get_modpath("mcl_colors")
|
|
|
|
|
|
|
|
local huds_idx = {}
|
|
|
|
huds_idx.title = {}
|
|
|
|
huds_idx.subtitle = {}
|
|
|
|
huds_idx.actionbar = {}
|
|
|
|
|
|
|
|
mcl_title = {}
|
|
|
|
mcl_title.defaults = {fadein = 10, stay = 70, fadeout = 20}
|
|
|
|
mcl_title.layout = {}
|
|
|
|
mcl_title.layout.title = {position = {x = 0.5, y = 0.5}, alignment = {x = 0, y = -1.3}, size = 5}
|
|
|
|
mcl_title.layout.subtitle = {position = {x = 0.5, y = 0.5}, alignment = {x = 0, y = 1.3}, size = 2}
|
|
|
|
mcl_title.layout.actionbar = {position = {x = 0.5, y = 1}, alignment = {x = 0, y = -10}, size = 1}
|
|
|
|
|
|
|
|
local function gametick_to_secondes(gametick)
|
|
|
|
return gametick / 20
|
|
|
|
end
|
|
|
|
|
|
|
|
function mcl_title.set(playername, type, data)
|
|
|
|
if not data.color then
|
|
|
|
data.color = "white"
|
|
|
|
end
|
|
|
|
local _, hex_color = mcl_util.get_color(data.color)
|
|
|
|
if not hex_color then
|
|
|
|
return false, "Invalid color: " .. data.color
|
|
|
|
end
|
|
|
|
local player = minetest.get_player_by_name(playername)
|
|
|
|
if player and data then
|
|
|
|
if huds_idx[type][playername] then
|
|
|
|
player:hud_remove(huds_idx[type][playername])
|
|
|
|
end
|
|
|
|
local stay = player:get_meta():get_int("mcl_title:stay") or 3
|
|
|
|
huds_idx[type][playername] = player:hud_add({
|
|
|
|
hud_elem_type = "text",
|
|
|
|
position = mcl_title.layout[type].position,
|
|
|
|
alignment = mcl_title.layout[type].alignment,
|
|
|
|
text = data.text,
|
|
|
|
size = {x = mcl_title.layout[type].size},
|
|
|
|
number = hex_color,
|
|
|
|
z_index = 1100
|
|
|
|
})
|
|
|
|
minetest.after(stay, function()
|
|
|
|
if huds_idx[type][playername] then
|
|
|
|
player:hud_remove(huds_idx[type][playername])
|
|
|
|
end
|
|
|
|
huds_idx[type][playername] = nil
|
|
|
|
end)
|
|
|
|
return true, "Title command executed successfuly"
|
|
|
|
else
|
|
|
|
return false, "Player doesn't exist or json failed to parse"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function mcl_title.remove(playername, type)
|
|
|
|
local player = minetest.get_player_by_name(playername)
|
|
|
|
if player then
|
|
|
|
if huds_idx[type][playername] then
|
|
|
|
player:hud_remove(huds_idx[type][playername])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
huds_idx[type][playername] = nil
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2021-03-09 19:04:37 +01:00
|
|
|
function mcl_title.times(playername, stay)
|
2021-03-08 16:15:52 +01:00
|
|
|
local player = minetest.get_player_by_name(playername)
|
|
|
|
if player then
|
|
|
|
local meta = player:get_meta()
|
2021-03-09 19:04:37 +01:00
|
|
|
--meta:set_int("mcl_title:fadeIn", gametick_to_secondes(fadein))
|
2021-03-08 16:15:52 +01:00
|
|
|
meta:set_int("mcl_title:stay", gametick_to_secondes(stay))
|
2021-03-09 19:04:37 +01:00
|
|
|
--meta:set_int("mcl_title:fadeOut", gametick_to_secondes(fadeout))
|
2021-03-08 16:15:52 +01:00
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false, "Player doesn't exist"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_on_newplayer(function(player)
|
|
|
|
local meta = player:get_meta()
|
2021-03-09 19:04:37 +01:00
|
|
|
--meta:set_int("mcl_title:fadeIn", gametick_to_secondes(mcl_title.defaults.fadein))
|
2021-03-08 16:15:52 +01:00
|
|
|
meta:set_int("mcl_title:stay", gametick_to_secondes(mcl_title.defaults.stay))
|
2021-03-09 19:04:37 +01:00
|
|
|
--meta:set_int("mcl_title:fadeOut", gametick_to_secondes(mcl_title.defaults.fadeout))
|
2021-03-08 16:15:52 +01:00
|
|
|
end)
|
|
|
|
|
|
|
|
function mcl_title.clear(playername)
|
|
|
|
mcl_title.remove(playername, "title")
|
|
|
|
mcl_title.remove(playername, "subtitle")
|
|
|
|
mcl_title.remove(playername, "actionbar")
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function mcl_title.reset(playername)
|
|
|
|
local player = minetest.get_player_by_name(playername)
|
|
|
|
if player then
|
|
|
|
local meta = player:get_meta()
|
2021-03-09 19:04:37 +01:00
|
|
|
--meta:set_int("mcl_title:fadeIn", gametick_to_secondes(mcl_title.defaults.fadein))
|
2021-03-08 16:15:52 +01:00
|
|
|
meta:set_int("mcl_title:stay", gametick_to_secondes(mcl_title.defaults.stay))
|
2021-03-09 19:04:37 +01:00
|
|
|
--meta:set_int("mcl_title:fadeOut", gametick_to_secondes(mcl_title.defaults.fadeout))
|
2021-03-08 16:15:52 +01:00
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false, "Player not found!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-08 22:48:44 +01:00
|
|
|
mcl_commands.register_command("title", {
|
|
|
|
func = function(cmd)
|
|
|
|
cmd:sub(":name:username title :params:json", {
|
|
|
|
func = function(name, target, json)
|
|
|
|
return mcl_title.set(target, "title", json)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
cmd:sub(":name:username subtitle :params:json", {
|
|
|
|
func = function(name, target, json)
|
|
|
|
return mcl_title.set(target, "subtitle", json)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
cmd:sub(":name:username actionbar :params:json", {
|
|
|
|
func = function(name, target, json)
|
|
|
|
return mcl_title.set(target, "actionbar", json)
|
|
|
|
end,
|
|
|
|
})
|
2021-03-09 19:04:37 +01:00
|
|
|
cmd:sub(":name:username times :stay:int", {
|
|
|
|
func = function(name, target, stay)
|
|
|
|
return mcl_title.times(target, stay)
|
2021-03-08 22:48:44 +01:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
cmd:sub(":name:username clear", {
|
|
|
|
func = function(name, target)
|
|
|
|
return mcl_title.clear(target)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
cmd:sub(":name:username reset", {
|
|
|
|
func = function(name, target)
|
|
|
|
return mcl_title.reset(target)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end,
|
2021-03-08 16:15:52 +01:00
|
|
|
description = "Controls text displayed on the screen.",
|
2021-03-08 20:34:36 +01:00
|
|
|
params = "<target> command <params>",
|
2021-03-08 22:48:44 +01:00
|
|
|
privs = {server = true},
|
2021-03-08 16:15:52 +01:00
|
|
|
})
|