MineClone2/mods/MISC/mcl_basic_commands/title.lua

142 lines
4.3 KiB
Lua

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
function mcl_title.times(playername, stay)
local player = minetest.get_player_by_name(playername)
if player then
local meta = player:get_meta()
--meta:set_int("mcl_title:fadeIn", gametick_to_secondes(fadein))
meta:set_int("mcl_title:stay", gametick_to_secondes(stay))
--meta:set_int("mcl_title:fadeOut", gametick_to_secondes(fadeout))
return true
else
return false, "Player doesn't exist"
end
end
minetest.register_on_newplayer(function(player)
local meta = player:get_meta()
--meta:set_int("mcl_title:fadeIn", gametick_to_secondes(mcl_title.defaults.fadein))
meta:set_int("mcl_title:stay", gametick_to_secondes(mcl_title.defaults.stay))
--meta:set_int("mcl_title:fadeOut", gametick_to_secondes(mcl_title.defaults.fadeout))
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()
--meta:set_int("mcl_title:fadeIn", gametick_to_secondes(mcl_title.defaults.fadein))
meta:set_int("mcl_title:stay", gametick_to_secondes(mcl_title.defaults.stay))
--meta:set_int("mcl_title:fadeOut", gametick_to_secondes(mcl_title.defaults.fadeout))
return true
else
return false, "Player not found!"
end
end
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,
})
cmd:sub(":name:username times :stay:int", {
func = function(name, target, stay)
return mcl_title.times(target, stay)
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,
description = "Controls text displayed on the screen.",
params = "<target> command <params>",
privs = {server = true},
})