From 59ab7e6ae67c70dc2ae7b910c58a9c088009d6c5 Mon Sep 17 00:00:00 2001 From: AFCMS Date: Mon, 8 Mar 2021 16:15:52 +0100 Subject: [PATCH] add /title command --- mods/MISC/mcl_commands/init.lua | 9 ++- mods/MISC/mcl_commands/mod.conf | 2 + mods/MISC/mcl_commands/title.lua | 130 +++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 mods/MISC/mcl_commands/title.lua diff --git a/mods/MISC/mcl_commands/init.lua b/mods/MISC/mcl_commands/init.lua index e797c2c068..381413c469 100644 --- a/mods/MISC/mcl_commands/init.lua +++ b/mods/MISC/mcl_commands/init.lua @@ -7,6 +7,8 @@ local mod_death_messages = minetest.get_modpath("mcl_death_messages") local modpath = minetest.get_modpath(minetest.get_current_modname()) +local parse_json = minetest.parse_json + mcl_commands = {} mcl_commands.types = { @@ -19,7 +21,8 @@ mcl_commands.types = { modname = "([a-z0-9_]+)", alphascore = "([A-Za-z_]+)", 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) @@ -206,6 +209,9 @@ function mcl_commands.build(func) elseif param == "number" or param == "int" then table.insert(params, tonumber(res[pointer])) pointer = pointer + 1 + elseif param == "json" then + table.insert(params, parse_json(res[pointer])) + pointer = pointer + 1 else table.insert(params, res[pointer]) pointer = pointer + 1 @@ -235,5 +241,6 @@ dofile(modpath.."/summon.lua") dofile(modpath.."/say.lua") dofile(modpath.."/list.lua") dofile(modpath.."/sound.lua") +dofile(modpath.."/title.lua") dofile(modpath.."/alias.lua") \ No newline at end of file diff --git a/mods/MISC/mcl_commands/mod.conf b/mods/MISC/mcl_commands/mod.conf index d651fad7b6..0fed926a55 100644 --- a/mods/MISC/mcl_commands/mod.conf +++ b/mods/MISC/mcl_commands/mod.conf @@ -1,4 +1,6 @@ name = mcl_commands author = Wuzzy description = MCL2 commands +depends = mcl_colors optional_depends = mcl_death_messages + diff --git a/mods/MISC/mcl_commands/title.lua b/mods/MISC/mcl_commands/title.lua new file mode 100644 index 0000000000..d92ccf9069 --- /dev/null +++ b/mods/MISC/mcl_commands/title.lua @@ -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, + } +}) \ No newline at end of file