Add world storage for player ranks (#3)

This commit is contained in:
An0n3m0us 2021-06-17 22:27:52 +01:00 committed by GitHub
parent 5c01f365b7
commit a597520a74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

130
init.lua
View File

@ -6,6 +6,9 @@ local chat3_exists = minetest.get_modpath("chat3")
local registered = {} local registered = {}
local default local default
-- Load mod storage
local storage = minetest.get_mod_storage()
--- ---
--- API --- API
--- ---
@ -51,13 +54,13 @@ function ranks.list_plaintext()
end end
-- [function] Get player rank -- [function] Get player rank
function ranks.get_rank(player) function ranks.get_rank(name)
if type(player) == "string" then if type(name) ~= "string" then
player = minetest.get_player_by_name(player) name = minetest.get_player_by_name(name)
end end
local rank = player:get_attribute("ranks:rank") local rank = storage:get_string(name)
if rank and registered[rank] then if rank ~= "" and registered[rank] then
return rank return rank
end end
end end
@ -72,18 +75,13 @@ function ranks.get_def(rank)
end end
-- [function] Update player privileges -- [function] Update player privileges
function ranks.update_privs(player, trigger) function ranks.update_privs(name, trigger)
if type(player) == "string" then if type(name) ~= "string" then
player = minetest.get_player_by_name(player) name = name:get_player_name()
end end
if not player then local rank = ranks.get_rank(name)
return if rank ~= nil then
end
local name = player:get_player_name()
local rank = ranks.get_rank(player)
if rank then
-- [local function] Warn -- [local function] Warn
local function warn(msg) local function warn(msg)
if msg and trigger and minetest.get_player_by_name(trigger) then if msg and trigger and minetest.get_player_by_name(trigger) then
@ -132,10 +130,9 @@ function ranks.update_privs(player, trigger)
end end
end end
local admin = player:get_player_name() == minetest.settings:get("name") local admin = name == minetest.settings:get("name")
-- If owner, grant `rank` privilege -- If owner, grant `rank` privilege
if admin then if admin then
local name = player:get_player_name()
local privs = minetest.get_player_privs(name) local privs = minetest.get_player_privs(name)
privs["rank"] = true privs["rank"] = true
minetest.set_player_privs(name, privs) minetest.set_player_privs(name, privs)
@ -147,18 +144,19 @@ function ranks.update_privs(player, trigger)
end end
-- [function] Update player nametag -- [function] Update player nametag
function ranks.update_nametag(player) function ranks.update_nametag(name)
if minetest.settings:get("ranks.prefix_nametag") == "false" then if minetest.settings:get("ranks.prefix_nametag") == "false" then
return return
end end
if type(player) == "string" then if type(name) ~= "string" then
player = minetest.get_player_by_name(player) name = name:get_player_name()
else
player = minetest.get_player_by_name(name)
end end
local name = player:get_player_name() local rank = ranks.get_rank(name)
local rank = ranks.get_rank(player) if rank ~= nil then
if rank then
local def = ranks.get_def(rank) local def = ranks.get_def(rank)
local colour = get_colour(def.colour) local colour = get_colour(def.colour)
local prefix = def.prefix local prefix = def.prefix
@ -169,44 +167,49 @@ function ranks.update_nametag(player)
prefix = "" prefix = ""
end end
local player = minetest.get_player_by_name(name)
if player then
player:set_nametag_attributes({ player:set_nametag_attributes({
text = prefix..name, text = prefix..name,
}) })
end
return true return true
end end
end end
-- [function] Set player rank -- [function] Set player rank
function ranks.set_rank(player, rank) function ranks.set_rank(name, rank)
if type(player) == "string" then if type(name) ~= "string" then
player = minetest.get_player_by_name(player) name = name:get_player_name()
end end
if registered[rank] then if registered[rank] and minetest.player_exists(name) then
-- Set attribute storage:set_string(name, rank)
player:set_attribute("ranks:rank", rank)
-- Update nametag -- Update nametag
ranks.update_nametag(player) ranks.update_nametag(name)
-- Update privileges -- Update privileges
ranks.update_privs(player) ranks.update_privs(name)
return true return true
end end
end end
-- [function] Remove rank from player -- [function] Remove rank from player
function ranks.remove_rank(player) function ranks.remove_rank(name)
if type(player) == "string" then if type(name) ~= "string" then
player = minetest.get_player_by_name(player) name = name:get_player_name()
else
player = minetest.get_player_by_name(name)
end end
local rank = ranks.get_rank(player) local rank = ranks.get_rank(name)
if rank then if rank ~= nil then
local name = player:get_player_name() storage:set_string(name, nil)
-- Clear attribute local player = minetest.get_player_by_name(name)
player:set_attribute("ranks:rank", nil) if player then
-- Update nametag -- Update nametag
player:set_nametag_attributes({ player:set_nametag_attributes({
text = name, text = name,
@ -217,13 +220,14 @@ function ranks.remove_rank(player)
minetest.string_to_privs(minetest.settings:get("basic_privs") or "interact,shout") minetest.string_to_privs(minetest.settings:get("basic_privs") or "interact,shout")
minetest.set_player_privs(name, basic_privs) minetest.set_player_privs(name, basic_privs)
end end
end
end end
-- [function] Send prefixed message (if enabled) -- [function] Send prefixed message (if enabled)
function ranks.chat_send(name, message) function ranks.chat_send(name, message)
if minetest.settings:get("ranks.prefix_chat") ~= "false" then if minetest.settings:get("ranks.prefix_chat") ~= "false" then
local rank = ranks.get_rank(name) local rank = ranks.get_rank(name)
if rank then if rank ~= nil then
local def = ranks.get_def(rank) local def = ranks.get_def(rank)
if def.prefix then if def.prefix then
local colour = get_colour(def.colour) local colour = get_colour(def.colour)
@ -252,14 +256,32 @@ minetest.register_privilege("rank", {
-- Assign/update rank on join player -- Assign/update rank on join player
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
if ranks.get_rank(player) then 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
if ranks.get_rank(name) then
-- Update nametag -- Update nametag
ranks.update_nametag(player) ranks.update_nametag(name)
-- Update privileges -- Update privileges
ranks.update_privs(player) ranks.update_privs(name)
else else
if ranks.default then if ranks.default then
ranks.set_rank(player, ranks.default) ranks.set_rank(name, ranks.default)
end end
end end
end) end)
@ -283,7 +305,10 @@ minetest.register_chatcommand("rank", {
if #param == 1 and param[1] == "list" then if #param == 1 and param[1] == "list" then
return true, "Available Ranks: "..ranks.list_plaintext() return true, "Available Ranks: "..ranks.list_plaintext()
elseif #param == 2 then elseif #param == 2 then
if minetest.get_player_by_name(param[1]) then if minetest.player_exists(param[1]) == false then
return false, "Player does not exist"
end
if ranks.get_def(param[2]) then if ranks.get_def(param[2]) then
if ranks.set_rank(param[1], param[2]) then if ranks.set_rank(param[1], param[2]) then
if name ~= param[1] then if name ~= param[1] then
@ -300,9 +325,6 @@ minetest.register_chatcommand("rank", {
else else
return false, "Invalid rank (see /rank list)" return false, "Invalid rank (see /rank list)"
end end
else
return false, "Invalid player \""..param[1].."\""
end
else else
return false, "Invalid usage (see /help rank)" return false, "Invalid usage (see /help rank)"
end end
@ -315,15 +337,17 @@ minetest.register_chatcommand("getrank", {
params = "<name> | name of player", params = "<name> | name of player",
func = function(name, param) func = function(name, param)
if param and param ~= "" then if param and param ~= "" then
if minetest.get_player_by_name(param) then local rank = ranks.get_rank(param)
local rank = ranks.get_rank(param) or "No rank" if rank then
return true, "Rank of "..param..": "..rank return true, "Rank of " .. param .. ": " .. rank:gsub("^%l", string.upper)
elseif minetest.player_exists(param) then
return false, "Rank of " .. param .. ": No rank"
else else
return false, "Invalid player \""..name.."\"" return false, "Player does not exist"
end end
else else
local rank = ranks.get_rank(name) or "No rank" local rank = ranks.get_rank(name) or "No rank"
return false, "Your rank: "..rank return true, "Your rank: " .. rank:gsub("^%l", string.upper)
end end
end, end,
}) })