1
0
forked from Reload/ranks

api rework, formatting messages, removed callbacks

This commit is contained in:
Evgeniy 2025-01-31 17:15:34 +03:00
parent a597520a74
commit 0818b3f948
6 changed files with 88 additions and 53 deletions

View File

@ -1 +0,0 @@
chat3?

View File

@ -1 +0,0 @@
Ranks is an advanced ranks mod geared towards larger servers helps to both distiguish between players and make managing privileges much easier. With several ranks premade and a simplistic API, ranks is a good addition to any server, especially those with many players.

View File

@ -5,7 +5,7 @@ ranks = {}
local chat3_exists = minetest.get_modpath("chat3")
local registered = {}
local default
local player
-- Load mod storage
local storage = minetest.get_mod_storage()
@ -224,29 +224,49 @@ function ranks.remove_rank(name)
end
-- [function] Send prefixed message (if enabled)
function ranks.chat_send(name, message)
if minetest.settings:get("ranks.prefix_chat") ~= "false" then
local rank = ranks.get_rank(name)
if rank ~= nil then
local def = ranks.get_def(rank)
if def.prefix then
local colour = get_colour(def.colour)
local prefix = minetest.colorize(colour, def.prefix)
if chat3_exists then
chat3.send(name, message, prefix.." ", "ranks")
else
minetest.chat_send_all(prefix.." <"..name.."> "..message)
minetest.log("action", "CHAT: ".."<"..name.."> "..message)
end
return true
end
end
local function safe_gsub(s, replace, with)
local i1, i2 = s:find(replace, 1, true)
if not i1 then
return s, false
end
return s:sub(1, i1 - 1) .. with .. s:sub(i2 + 1), true
end
---
--- Registrations
---
if minetest.settings:get("ranks.prefix_chat") ~= "false" then
local old_format_chat_message = core.format_chat_message
function core.format_chat_message(name, message)
-- Calling the previous version of function
local str = old_format_chat_message(name, message)
local replaced
-- We check if there is a prefix in the custom template.
-- If not, add it to the beginning of the line.
-- This is necessary to be able to configure via `chat_message_format`
local is_prefix_placeholder = str:find("@prefix")
if not is_prefix_placeholder then
str = "@prefix " .. str
end
local rank = ranks.get_rank(name)
if rank then
local def = ranks.get_def(rank)
local colour = get_colour(def.colour)
local prefix = def.prefix
if prefix then
prefix = minetest.colorize(colour, prefix)
str, replaced = safe_gsub(str, "@prefix", prefix)
else
str, replaced = safe_gsub(str, "@prefix", "unknown")
end
else
str, replaced = safe_gsub(str, "@prefix", "unranked")
end
return str
end
end
-- [privilege] Rank
minetest.register_privilege("rank", {
@ -257,22 +277,8 @@ minetest.register_privilege("rank", {
-- Assign/update rank on join player
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
-- If database item exists and new storage item does not, use database item
if player:get_attribute("ranks:rank") ~= nil and storage:get_string(name, rank) == "" then
-- Add entry into new storage system
storage:set_string(name, player:get_attribute("ranks:rank"))
-- Store backup then invalidate database item
player:set_attribute("ranks:rank-old", player:get_attribute("ranks:rank"))
player:set_attribute("ranks:rank", nil)
end
-- Both items exist, remove old one
if player:get_attribute("ranks:rank") ~= nil and storage:get_string(name, rank) ~= "" then
player:set_attribute("ranks:rank-old", player:get_attribute("ranks:rank"))
player:set_attribute("ranks:rank", nil)
end
-- local meta = player:get_meta()
-- meta:get_string("ranks:rank")
if ranks.get_rank(name) then
-- Update nametag
@ -286,10 +292,6 @@ minetest.register_on_joinplayer(function(player)
end
end)
-- Prefix messages if enabled
minetest.register_on_chat_message(function(name, message)
return ranks.chat_send(name, message)
end)
-- [chatcommand] /rank
minetest.register_chatcommand("rank", {

View File

@ -1 +1,4 @@
name = ranks
optional_depends = chat3,telegram
description = Ranks is an advanced ranks mod geared towards larger servers helps to both distiguish between players and make managing privileges much easier. With several ranks premade and a simplistic API, ranks is a good addition to any server, especially those with many players.

View File

@ -2,20 +2,51 @@
ranks.register("admin", {
prefix = "Admin",
colour = {a = 255, r = 230, g = 33, b = 23},
colour = {a = 255, r = 255, g = 0, b = 0},
})
ranks.register("moderator", {
prefix = "Moderator",
colour = {a = 255, r = 255, g = 83, b = 37},
prefix = "Moder",
colour = {a = 255, r = 139, g = 0, b = 0},
})
ranks.register("guardian", {
prefix = "Guardian",
colour = {a = 255, r = 255, g = 132, b = 0},
ranks.register("developer", {
prefix = "Dev",
colour = {a = 255, r = 255, g = 69, b = 0},
})
ranks.register("youtube", {
prefix = "YouTube",
colour = {a = 255, r = 255, g = 80, b = 71},
ranks.register("guru", {
prefix = "Guru",
colour = {a = 255, r = 154, g = 205, b = 50},
})
ranks.register("helper", {
prefix = "Helper",
colour = {a = 255, r = 139, g = 0, b = 255},
})
ranks.register("businessman", {
prefix = "BusMan",
colour = {a = 255, r = 218, g = 165, b = 32},
})
ranks.register("builder", {
prefix = "Builder",
colour = {a = 255, r = 64, g = 224, b = 208},
})
ranks.register("avenger", {
prefix = "Avenger",
colour = {a = 255, r = 139, g = 69, b = 19},
})
ranks.register("gamer", {
prefix = "Gamer",
colour = {a = 255, r = 112, g = 128, b = 144},
})
ranks.register("regular", {
prefix = "Regular",
colour = {a = 255, r = 0, g = 128, b = 128},
})

View File

@ -1,5 +1,6 @@
# If this is enabled, chat send by a player with a rank including a defined
# prefix is prefixed with the rank prefix.
# prefix is prefixed with the rank prefix. You can define prefix at minetest.conf.
# chat_message_format = <@timestamp> [@prefix] @name: @message
ranks.prefix_chat (Prefix Chat) bool true
# If this is enabled, player with a rank that has a prefix will have this