add /title command

This commit is contained in:
AFCMS 2021-03-08 16:15:52 +01:00
parent d5874b4062
commit 59ab7e6ae6
3 changed files with 140 additions and 1 deletions

View File

@ -7,6 +7,8 @@ local mod_death_messages = minetest.get_modpath("mcl_death_messages")
local modpath = minetest.get_modpath(minetest.get_current_modname()) local modpath = minetest.get_modpath(minetest.get_current_modname())
local parse_json = minetest.parse_json
mcl_commands = {} mcl_commands = {}
mcl_commands.types = { mcl_commands.types = {
@ -19,7 +21,8 @@ mcl_commands.types = {
modname = "([a-z0-9_]+)", modname = "([a-z0-9_]+)",
alphascore = "([A-Za-z_]+)", alphascore = "([A-Za-z_]+)",
alphanumeric = "([A-Za-z0-9]+)", alphanumeric = "([A-Za-z0-9]+)",
username = "([A-Za-z0-9-_]+)", --TODO: add json type username = "([A-Za-z0-9-_]+)",
json = "(.+)", --TODO: add json specification
} }
function mcl_commands.register_command(name, func, def) function mcl_commands.register_command(name, func, def)
@ -206,6 +209,9 @@ function mcl_commands.build(func)
elseif param == "number" or param == "int" then elseif param == "number" or param == "int" then
table.insert(params, tonumber(res[pointer])) table.insert(params, tonumber(res[pointer]))
pointer = pointer + 1 pointer = pointer + 1
elseif param == "json" then
table.insert(params, parse_json(res[pointer]))
pointer = pointer + 1
else else
table.insert(params, res[pointer]) table.insert(params, res[pointer])
pointer = pointer + 1 pointer = pointer + 1
@ -235,5 +241,6 @@ dofile(modpath.."/summon.lua")
dofile(modpath.."/say.lua") dofile(modpath.."/say.lua")
dofile(modpath.."/list.lua") dofile(modpath.."/list.lua")
dofile(modpath.."/sound.lua") dofile(modpath.."/sound.lua")
dofile(modpath.."/title.lua")
dofile(modpath.."/alias.lua") dofile(modpath.."/alias.lua")

View File

@ -1,4 +1,6 @@
name = mcl_commands name = mcl_commands
author = Wuzzy author = Wuzzy
description = MCL2 commands description = MCL2 commands
depends = mcl_colors
optional_depends = mcl_death_messages optional_depends = mcl_death_messages

View File

@ -0,0 +1,130 @@
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, fadein, stay, fadeout)
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", function(cmd)
cmd:sub(":name:username title :text:json", function(name, target, json)
return mcl_title.set(target, "title", json)
end)
cmd:sub(":name:username subtitle :text:json", function(name, target, json)
return mcl_title.set(target, "subtitle", json)
end)
cmd:sub(":name:username actionbar :text:json", function(name, target, json)
return mcl_title.set(target, "actionbar", json)
end)
cmd:sub(":name:username times :fadeIn:int :stay:int :fadeOut:int", function(name, target, fadeIn, stay, fadeOut) --WIP
return mcl_title.times(target, fadeIn, stay, fadeOut)
end)
cmd:sub(":name:username clear", function(name, target)
return mcl_title.clear(target)
end)
cmd:sub(":name:username reset", function(name, target)
return mcl_title.reset(target)
end)
end, {
description = "Controls text displayed on the screen.",
privs = {
server = true,
}
})