2020-08-20 18:57:22 +02:00
|
|
|
-- Translation support
|
|
|
|
local S = minetest.get_translator("playerfactions")
|
|
|
|
|
2024-09-16 15:54:00 +02:00
|
|
|
-- Global factions table
|
2019-09-09 21:56:38 +02:00
|
|
|
factions = {}
|
2024-09-16 16:31:27 +02:00
|
|
|
-- This variable "version" can be used by other mods to check
|
|
|
|
-- the compatibility of this mod
|
2020-07-28 22:50:31 +02:00
|
|
|
factions.version = 2
|
|
|
|
|
2024-09-16 15:54:34 +02:00
|
|
|
-- Settings
|
|
|
|
factions.mode_unique_faction = minetest.settings:get_bool(
|
|
|
|
"player_factions.mode_unique_faction", true)
|
|
|
|
factions.max_members_list = tonumber(minetest.settings:get(
|
|
|
|
"player_factions.max_members_list")) or 50
|
|
|
|
factions.priv = minetest.settings:get(
|
|
|
|
"player_factions.priv_admin") or "playerfactions_admin"
|
|
|
|
|
|
|
|
-- Privilege registration (if needed)
|
2023-04-14 18:35:31 +02:00
|
|
|
minetest.register_on_mods_loaded(function()
|
|
|
|
if not minetest.registered_privileges[factions.priv] then
|
2024-09-16 15:51:45 +02:00
|
|
|
minetest.register_privilege(factions.priv, {
|
2023-04-14 18:35:31 +02:00
|
|
|
description = S("Allow the use of all playerfactions commands"),
|
|
|
|
give_to_singleplayer = false
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2024-09-16 15:54:00 +02:00
|
|
|
-- Data
|
2019-09-09 04:11:55 +02:00
|
|
|
local facts = {}
|
|
|
|
local storage = minetest.get_mod_storage()
|
2019-09-09 23:59:09 +02:00
|
|
|
if storage:get_string("facts") ~= "" then
|
2019-09-09 04:11:55 +02:00
|
|
|
facts = minetest.deserialize(storage:get_string("facts"))
|
|
|
|
end
|
2020-02-08 18:08:01 +01:00
|
|
|
|
2019-09-09 04:11:55 +02:00
|
|
|
local function save_factions()
|
|
|
|
storage:set_string("facts", minetest.serialize(facts))
|
|
|
|
end
|
|
|
|
|
2020-08-22 00:59:29 +02:00
|
|
|
local function table_copy(data)
|
|
|
|
local copy = {}
|
|
|
|
if type(data) == "table" then
|
2024-09-16 16:31:27 +02:00
|
|
|
for k, v in pairs(data) do
|
2020-08-22 00:59:29 +02:00
|
|
|
copy[k]=table_copy(v)
|
|
|
|
end
|
|
|
|
return copy
|
|
|
|
else
|
|
|
|
return data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-16 16:31:27 +02:00
|
|
|
-- Data manipulation (API)
|
2020-07-28 22:50:31 +02:00
|
|
|
function factions.get_facts()
|
2020-08-22 00:59:29 +02:00
|
|
|
return table_copy(facts)
|
2020-07-28 22:50:31 +02:00
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function factions.player_is_in_faction(faction_name, player_name)
|
2024-09-16 16:31:27 +02:00
|
|
|
if not (facts[faction_name] and minetest.player_exists(player_name)) then
|
2020-07-28 22:50:31 +02:00
|
|
|
return false
|
2020-08-18 11:25:28 +02:00
|
|
|
end
|
2024-09-16 16:27:27 +02:00
|
|
|
return facts[faction_name].members[player_name]
|
2020-07-28 22:50:31 +02:00
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function factions.get_player_faction(player_name)
|
2024-09-16 16:31:27 +02:00
|
|
|
minetest.log("warning", "Function factions.get_player_faction() "
|
|
|
|
.. "is deprecated in favor of factions.get_player_factions(). "
|
|
|
|
.. "Please check updates of mods depending on playerfactions.")
|
2024-09-16 16:27:27 +02:00
|
|
|
if not minetest.player_exists(player_name) then
|
2020-07-28 22:47:50 +02:00
|
|
|
return false
|
|
|
|
end
|
2024-09-16 16:27:27 +02:00
|
|
|
for faction_name, fact in pairs(facts) do
|
|
|
|
if fact.members[player_name] then
|
|
|
|
return faction_name
|
2020-07-28 22:47:50 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
2020-07-28 22:29:38 +02:00
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function factions.get_player_factions(player_name)
|
|
|
|
if not minetest.player_exists(player_name) then
|
2020-07-28 22:47:50 +02:00
|
|
|
return false
|
|
|
|
end
|
2024-09-15 04:58:57 +02:00
|
|
|
local player_factions = {}
|
2024-09-16 16:27:27 +02:00
|
|
|
for faction_name, fact in pairs(facts) do
|
|
|
|
if fact.members[player_name] then
|
|
|
|
table.insert(player_factions, faction_name)
|
2020-02-08 18:08:01 +01:00
|
|
|
end
|
2020-07-28 22:47:50 +02:00
|
|
|
end
|
2024-09-15 04:58:57 +02:00
|
|
|
return 0 < table.getn(player_factions) and player_factions or false
|
2020-07-28 22:47:50 +02:00
|
|
|
end
|
2020-07-28 22:29:38 +02:00
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function factions.get_owned_factions(player_name)
|
|
|
|
local owned_factions = {}
|
|
|
|
for faction_name, fact in pairs(facts) do
|
|
|
|
if fact.owner == player_name then
|
|
|
|
table.insert(owned_factions, faction_name)
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
|
|
|
end
|
2024-09-16 16:27:27 +02:00
|
|
|
return 0 < table.getn(owned_factions) and owned_factions or false
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function factions.get_administered_factions(player_name)
|
|
|
|
local is_admin = minetest.get_player_privs(player_name)[factions.priv]
|
2024-09-15 06:09:33 +02:00
|
|
|
local adm_factions = {}
|
2024-09-16 16:27:27 +02:00
|
|
|
for faction_name, fact in pairs(facts) do
|
|
|
|
if is_admin or fact.owner == player_name then
|
|
|
|
table.insert(adm_factions, faction_name)
|
2020-08-22 01:31:21 +02:00
|
|
|
end
|
|
|
|
end
|
2024-09-15 06:09:33 +02:00
|
|
|
return 0 < table.getn(adm_factions) and adm_factions or false
|
2020-08-22 01:31:21 +02:00
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function factions.get_owner(faction_name)
|
|
|
|
if not facts[faction_name] then
|
2020-07-28 22:47:50 +02:00
|
|
|
return false
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
2024-09-16 16:27:27 +02:00
|
|
|
return facts[faction_name].owner
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function factions.chown(faction_name, player_name)
|
|
|
|
if not facts[faction_name] then
|
2020-07-28 22:47:50 +02:00
|
|
|
return false
|
|
|
|
end
|
2024-09-16 16:27:27 +02:00
|
|
|
facts[faction_name].owner = player_name
|
2020-07-28 22:47:50 +02:00
|
|
|
save_factions()
|
|
|
|
return true
|
2019-09-10 00:20:58 +02:00
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function factions.register_faction(faction_name, player_name, password)
|
|
|
|
if not ('string' == type(faction_name) and 'string' == type(player_name)
|
|
|
|
and 'string' == type(password)) then
|
2020-07-28 22:47:50 +02:00
|
|
|
return false
|
|
|
|
end
|
2024-09-16 16:29:02 +02:00
|
|
|
if facts[faction_name] then
|
|
|
|
return false
|
|
|
|
end
|
2024-09-16 16:27:27 +02:00
|
|
|
facts[faction_name] = {
|
|
|
|
name = faction_name,
|
|
|
|
owner = player_name,
|
|
|
|
password256 = factions.hash_password(password),
|
|
|
|
members = { [player_name] = true }
|
2019-09-09 04:11:55 +02:00
|
|
|
}
|
|
|
|
save_factions()
|
2020-07-28 22:47:50 +02:00
|
|
|
return true
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function factions.disband_faction(faction_name)
|
|
|
|
if not facts[faction_name] then
|
2020-07-28 22:47:50 +02:00
|
|
|
return false
|
|
|
|
end
|
2024-09-16 16:27:27 +02:00
|
|
|
facts[faction_name] = nil
|
2019-09-09 04:11:55 +02:00
|
|
|
save_factions()
|
2020-07-28 22:47:50 +02:00
|
|
|
return true
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
|
|
|
|
2024-09-11 10:49:17 +02:00
|
|
|
function factions.hash_password(password)
|
|
|
|
return minetest.sha256(password)
|
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function factions.valid_password(faction_name, password)
|
|
|
|
if not facts[faction_name] or not password then
|
2024-09-11 10:49:17 +02:00
|
|
|
return false
|
|
|
|
end
|
2024-09-16 16:27:27 +02:00
|
|
|
return factions.hash_password(password) == facts[faction_name].password256
|
2024-09-11 10:49:17 +02:00
|
|
|
end
|
|
|
|
|
2024-09-16 16:30:15 +02:00
|
|
|
function factions.get_password()
|
2024-09-11 10:50:52 +02:00
|
|
|
minetest.log("warning", "Deprecated use of factions.get_password(). "
|
|
|
|
.. "Please update to using factions.valid_password() instead.")
|
2024-09-16 16:30:15 +02:00
|
|
|
return nil
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function factions.set_password(faction_name, password)
|
2024-09-16 16:31:09 +02:00
|
|
|
if not (facts[faction_name] and 'string' == type(password)) then
|
2020-07-28 22:47:50 +02:00
|
|
|
return false
|
|
|
|
end
|
2024-09-16 16:27:27 +02:00
|
|
|
facts[faction_name].password256 = factions.hash_password(password)
|
2020-07-28 22:47:50 +02:00
|
|
|
save_factions()
|
|
|
|
return true
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function factions.join_faction(faction_name, player_name)
|
2024-09-16 16:31:09 +02:00
|
|
|
if not (facts[faction_name] and 'string' == type(player_name)
|
|
|
|
and minetest.player_exists(player_name)) then
|
2020-07-28 22:47:50 +02:00
|
|
|
return false
|
|
|
|
end
|
2024-09-16 16:27:27 +02:00
|
|
|
facts[faction_name].members[player_name] = true
|
2020-07-28 22:47:50 +02:00
|
|
|
save_factions()
|
|
|
|
return true
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function factions.leave_faction(faction_name, player_name)
|
2024-09-16 16:31:09 +02:00
|
|
|
if not (facts[faction_name] and 'string' == type(player_name)
|
|
|
|
and minetest.player_exists(player_name)) then
|
2020-07-28 22:47:50 +02:00
|
|
|
return false
|
2020-02-08 18:08:01 +01:00
|
|
|
end
|
2024-09-16 16:27:27 +02:00
|
|
|
facts[faction_name].members[player_name] = nil
|
2020-07-28 22:47:50 +02:00
|
|
|
save_factions()
|
|
|
|
return true
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Chat commands
|
2024-09-16 01:59:56 +02:00
|
|
|
local cc = {}
|
2024-09-16 01:03:57 +02:00
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function cc.create(player_name, params)
|
2024-09-16 01:03:57 +02:00
|
|
|
local faction_name = params[2]
|
|
|
|
local password = params[3]
|
|
|
|
if not faction_name then
|
|
|
|
return false, S("Missing faction name.")
|
|
|
|
elseif not password then
|
|
|
|
return false, S("Missing password.")
|
2024-09-16 16:27:27 +02:00
|
|
|
elseif factions.mode_unique_faction and factions.get_player_factions(player_name) then
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("You are already in a faction.")
|
|
|
|
elseif facts[faction_name] then
|
|
|
|
return false, S("Faction @1 already exists.", faction_name)
|
|
|
|
else
|
2024-09-16 16:27:27 +02:00
|
|
|
factions.register_faction(faction_name, player_name, password)
|
2024-09-16 01:03:57 +02:00
|
|
|
return true, S("Registered @1.", faction_name)
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
2024-09-16 01:03:57 +02:00
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function cc.disband(player_name, params, not_admin)
|
2024-09-16 01:03:57 +02:00
|
|
|
local password = params[2]
|
|
|
|
if not password then
|
|
|
|
return false, S("Missing password.")
|
|
|
|
end
|
|
|
|
local faction_name = params[3]
|
2024-09-16 16:27:27 +02:00
|
|
|
local owned_factions = factions.get_administered_factions(player_name)
|
|
|
|
local number_factions = owned_factions and table.getn(owned_factions) or 0
|
2024-09-16 01:03:57 +02:00
|
|
|
if not_admin and number_factions == 0 then
|
|
|
|
return false, S("You don't own any factions.")
|
|
|
|
elseif not faction_name and number_factions == 1 then
|
2024-09-16 16:27:27 +02:00
|
|
|
faction_name = owned_factions[1]
|
2024-09-16 01:03:57 +02:00
|
|
|
elseif not faction_name then
|
|
|
|
return false, S(
|
|
|
|
"You are the owner of multiple factions, you have to choose one of them: @1.",
|
2024-09-16 16:27:27 +02:00
|
|
|
table.concat(owned_factions, ", ")
|
2024-09-16 01:03:57 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
if not facts[faction_name] then
|
|
|
|
return false, S("Faction @1 doesn't exist.", faction_name)
|
2024-09-16 16:27:27 +02:00
|
|
|
elseif not_admin and player_name ~= factions.get_owner(faction_name) then
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("Permission denied: You are not the owner of that faction,"
|
|
|
|
.. " and don't have the @1 privilege.", factions.priv)
|
2024-09-16 23:53:57 +02:00
|
|
|
elseif not_admin and not factions.valid_password(faction_name, password) then
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("Permission denied: Wrong password.")
|
|
|
|
else
|
|
|
|
factions.disband_faction(faction_name)
|
|
|
|
return true, S("Disbanded @1.", faction_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-16 01:59:56 +02:00
|
|
|
function cc.list()
|
2024-09-16 01:03:57 +02:00
|
|
|
local faction_list = {}
|
|
|
|
for k in pairs(facts) do
|
|
|
|
table.insert(faction_list, k)
|
|
|
|
end
|
|
|
|
if table.getn(faction_list) == 0 then
|
|
|
|
return true, S("There are no factions yet.")
|
|
|
|
else
|
|
|
|
return true, S("Factions (@1): @2.",
|
|
|
|
table.getn(faction_list), table.concat(faction_list, ", "))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function cc.info(player_name, params)
|
2024-09-16 01:03:57 +02:00
|
|
|
local faction_name = params[2]
|
|
|
|
if not faction_name then
|
2024-09-16 16:27:27 +02:00
|
|
|
local player_factions = factions.get_player_factions(player_name)
|
2024-09-16 01:03:57 +02:00
|
|
|
if not player_factions then
|
|
|
|
return true, S("No factions found.")
|
|
|
|
elseif table.getn(player_factions) == 1 then
|
|
|
|
faction_name = player_factions[1]
|
2019-09-09 04:11:55 +02:00
|
|
|
else
|
2021-04-02 22:08:35 +02:00
|
|
|
return false, S(
|
2024-09-16 01:03:57 +02:00
|
|
|
"You are in multiple factions, you have to choose one of them: @1.",
|
|
|
|
table.concat(player_factions, ", ")
|
2021-04-02 22:08:35 +02:00
|
|
|
)
|
2024-09-15 10:08:26 +02:00
|
|
|
end
|
2024-09-16 01:03:57 +02:00
|
|
|
end
|
|
|
|
if not facts[faction_name] then
|
|
|
|
return false, S("Faction @1 doesn't exist.", faction_name)
|
|
|
|
else
|
|
|
|
local fmembers = {}
|
|
|
|
if table.getn(facts[faction_name].members) > factions.max_members_list then
|
|
|
|
table.insert(fmembers, S("The faction has more than @1 members,"
|
|
|
|
.. " the members list can't be shown.", factions.max_members_list))
|
2019-09-09 04:11:55 +02:00
|
|
|
else
|
2024-09-16 01:03:57 +02:00
|
|
|
for play in pairs(facts[faction_name].members) do
|
|
|
|
table.insert(fmembers, play)
|
2020-07-28 22:47:50 +02:00
|
|
|
end
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
2024-09-16 01:03:57 +02:00
|
|
|
local summary = S("Name: @1\nOwner: @2\nMembers: @3",
|
|
|
|
faction_name, factions.get_owner(faction_name),
|
|
|
|
table.concat(fmembers, ", "))
|
|
|
|
return true, summary
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function cc.player_info(player_name, params)
|
|
|
|
player_name = params[2] or player_name
|
2024-09-16 01:03:57 +02:00
|
|
|
if not player_name then
|
|
|
|
return false, S("Missing player name.")
|
|
|
|
end
|
|
|
|
local player_factions = factions.get_player_factions(player_name)
|
|
|
|
if not player_factions then
|
|
|
|
return false, S(
|
|
|
|
"Player @1 doesn't exist or isn't in any faction.", player_name)
|
|
|
|
else
|
|
|
|
local summary = S("@1 is in the following factions: @2.",
|
2024-09-16 16:28:13 +02:00
|
|
|
player_name, table.concat(player_factions, ", "))
|
2024-09-16 01:03:57 +02:00
|
|
|
local owned_factions = factions.get_owned_factions(player_name)
|
|
|
|
if not owned_factions then
|
|
|
|
summary = summary .. "\n" .. S(
|
|
|
|
"@1 doesn't own any factions.", player_name)
|
2019-09-09 04:11:55 +02:00
|
|
|
else
|
2024-09-16 01:03:57 +02:00
|
|
|
summary = summary .. "\n" .. S(
|
|
|
|
"@1 is the owner of the following factions: @2.",
|
2024-09-16 16:28:13 +02:00
|
|
|
player_name, table.concat(owned_factions, ", ")
|
2021-04-02 22:08:35 +02:00
|
|
|
)
|
2024-09-15 06:09:33 +02:00
|
|
|
end
|
2024-09-16 01:03:57 +02:00
|
|
|
if minetest.get_player_privs(player_name)[factions.priv] then
|
|
|
|
summary = summary .. "\n" .. S(
|
|
|
|
"@1 has the @2 privilege so they can admin every faction.",
|
|
|
|
player_name, factions.priv
|
2021-04-02 22:08:35 +02:00
|
|
|
)
|
2024-09-15 06:35:18 +02:00
|
|
|
end
|
2024-09-16 01:03:57 +02:00
|
|
|
return true, summary
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function cc.join(player_name, params)
|
2024-09-16 01:03:57 +02:00
|
|
|
local faction_name = params[2]
|
|
|
|
local password = params[3]
|
2024-09-16 16:27:27 +02:00
|
|
|
if factions.mode_unique_faction and factions.get_player_factions(player_name) then
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("You are already in a faction.")
|
|
|
|
elseif not faction_name then
|
|
|
|
return false, S("Missing faction name.")
|
|
|
|
elseif not facts[faction_name] then
|
|
|
|
return false, S("Faction @1 doesn't exist.", faction_name)
|
2024-09-16 16:27:27 +02:00
|
|
|
elseif facts[faction_name].members[player_name] then
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("You are already in faction @1.", faction_name)
|
|
|
|
elseif not factions.valid_password(faction_name, password) then
|
|
|
|
return false, S("Permission denied: Wrong password.")
|
|
|
|
else
|
2024-09-16 16:27:27 +02:00
|
|
|
if factions.join_faction(faction_name, player_name) then
|
2024-09-16 01:03:57 +02:00
|
|
|
return true, S("Joined @1.", faction_name)
|
2019-09-09 04:11:55 +02:00
|
|
|
else
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("Error joining faction.")
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
2024-09-16 01:03:57 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function cc.leave(player_name, params)
|
|
|
|
local player_factions = factions.get_player_factions(player_name)
|
2024-09-16 01:03:57 +02:00
|
|
|
local number_factions = player_factions and table.getn(player_factions) or 0
|
|
|
|
local faction_name = params[2]
|
|
|
|
if number_factions == 0 then
|
|
|
|
return false, S("You are not in a faction.")
|
|
|
|
elseif not faction_name then
|
|
|
|
if number_factions == 1 then
|
|
|
|
faction_name = player_factions[1]
|
|
|
|
else
|
2021-04-02 22:08:35 +02:00
|
|
|
return false, S(
|
2024-09-16 01:03:57 +02:00
|
|
|
"You are in multiple factions, you have to choose one of them: @1.",
|
|
|
|
table.concat(player_factions, ", ")
|
2021-04-02 22:08:35 +02:00
|
|
|
)
|
2024-09-15 07:01:32 +02:00
|
|
|
end
|
2024-09-16 01:03:57 +02:00
|
|
|
end
|
|
|
|
if not facts[faction_name] then
|
|
|
|
return false, S("Faction @1 doesn't exist.", faction_name)
|
2024-09-16 16:27:27 +02:00
|
|
|
elseif factions.get_owner(faction_name) == player_name then
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("You cannot leave your own faction, change owner or disband it.")
|
2024-09-16 16:27:27 +02:00
|
|
|
elseif not facts[faction_name].members[player_name] then
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("You aren't part of faction @1.", faction_name)
|
|
|
|
else
|
2024-09-16 16:27:27 +02:00
|
|
|
if factions.leave_faction(faction_name, player_name) then
|
2024-09-16 01:03:57 +02:00
|
|
|
return true, S("Left @1.", faction_name)
|
2019-09-09 05:30:31 +02:00
|
|
|
else
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("Error leaving faction.")
|
2019-09-09 05:30:31 +02:00
|
|
|
end
|
2024-09-16 01:03:57 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function cc.kick(player_name, params, not_admin)
|
|
|
|
local target_name = params[2]
|
|
|
|
if not target_name then
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("Missing player name.")
|
|
|
|
end
|
|
|
|
local faction_name = params[3]
|
2024-09-16 16:27:27 +02:00
|
|
|
local owned_factions = factions.get_administered_factions(player_name)
|
|
|
|
local number_factions = owned_factions and table.getn(owned_factions) or 0
|
2024-09-16 01:03:57 +02:00
|
|
|
if number_factions == 0 then
|
|
|
|
return false, S("You don't own any factions, you can't use this command.")
|
|
|
|
elseif not faction_name and number_factions == 1 then
|
2024-09-16 16:27:27 +02:00
|
|
|
faction_name = owned_factions[1]
|
2024-09-16 01:03:57 +02:00
|
|
|
elseif not faction_name then
|
|
|
|
return false, S(
|
|
|
|
"You are the owner of multiple factions, you have to choose one of them: @1.",
|
2024-09-16 16:27:27 +02:00
|
|
|
table.concat(owned_factions, ", ")
|
2024-09-16 01:03:57 +02:00
|
|
|
)
|
|
|
|
end
|
2024-09-16 16:27:27 +02:00
|
|
|
if not_admin and factions.get_owner(faction_name) ~= player_name then
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("Permission denied: You are not the owner of that faction, "
|
|
|
|
.. "and don't have the @1 privilege.", factions.priv)
|
2024-09-16 16:27:27 +02:00
|
|
|
elseif not facts[faction_name].members[target_name] then
|
|
|
|
return false, S("@1 is not in the specified faction.", target_name)
|
|
|
|
elseif target_name == factions.get_owner(faction_name) then
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("You cannot kick the owner of a faction, "
|
|
|
|
.. "use '/factions chown <player> <password> [<faction>]' "
|
|
|
|
.. "to change the ownership.")
|
|
|
|
else
|
2024-09-16 16:27:27 +02:00
|
|
|
if factions.leave_faction(faction_name, target_name) then
|
|
|
|
return true, S("Kicked @1 from faction.", target_name)
|
2024-09-16 01:03:57 +02:00
|
|
|
else
|
2024-09-16 16:27:27 +02:00
|
|
|
return false, S("Error kicking @1 from faction.", target_name)
|
2024-09-15 07:31:01 +02:00
|
|
|
end
|
2024-09-16 01:03:57 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function cc.passwd(player_name, params, not_admin)
|
2024-09-16 01:03:57 +02:00
|
|
|
local password = params[2]
|
|
|
|
if not password then
|
|
|
|
return false, S("Missing password.")
|
|
|
|
end
|
|
|
|
local faction_name = params[3]
|
2024-09-16 16:27:27 +02:00
|
|
|
local owned_factions = factions.get_administered_factions(player_name)
|
|
|
|
local number_factions = owned_factions and table.getn(owned_factions) or 0
|
2024-09-16 01:03:57 +02:00
|
|
|
if number_factions == 0 then
|
|
|
|
return false, S("You don't own any factions, you can't use this command.")
|
|
|
|
elseif not faction_name and number_factions == 1 then
|
2024-09-16 16:27:27 +02:00
|
|
|
faction_name = owned_factions[1]
|
2024-09-16 01:03:57 +02:00
|
|
|
elseif not faction_name then
|
|
|
|
return false, S(
|
|
|
|
"You are the owner of multiple factions, you have to choose one of them: @1.",
|
2024-09-16 16:27:27 +02:00
|
|
|
table.concat(owned_factions, ", ")
|
2024-09-16 01:03:57 +02:00
|
|
|
)
|
|
|
|
end
|
2024-09-16 16:27:27 +02:00
|
|
|
if not_admin and factions.get_owner(faction_name) ~= player_name then
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("Permission denied: You are not the owner of that faction, "
|
|
|
|
.. "and don't have the @1 privilege.", factions.priv)
|
|
|
|
else
|
|
|
|
if factions.set_password(faction_name, password) then
|
|
|
|
return true, S("Password has been updated.")
|
|
|
|
else
|
|
|
|
return false, S("Failed to change password.")
|
2024-09-15 07:31:01 +02:00
|
|
|
end
|
2024-09-16 01:03:57 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
function cc.chown(player_name, params, not_admin)
|
|
|
|
local target_name = params[2]
|
2024-09-16 01:03:57 +02:00
|
|
|
local password = params[3]
|
|
|
|
local faction_name = params[4]
|
2024-09-16 16:27:27 +02:00
|
|
|
if not target_name then
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("Missing player name.")
|
|
|
|
elseif not password then
|
|
|
|
return false, S("Missing password.")
|
|
|
|
end
|
2024-09-16 16:27:27 +02:00
|
|
|
local owned_factions = factions.get_administered_factions(player_name)
|
|
|
|
local number_factions = owned_factions and table.getn(owned_factions) or 0
|
2024-09-16 01:03:57 +02:00
|
|
|
if number_factions == 0 then
|
|
|
|
return false, S("You don't own any factions, you can't use this command.")
|
|
|
|
elseif not faction_name and number_factions == 1 then
|
2024-09-16 16:27:27 +02:00
|
|
|
faction_name = owned_factions[1]
|
2024-09-16 01:03:57 +02:00
|
|
|
elseif not faction_name then
|
|
|
|
return false, S(
|
|
|
|
"You are the owner of multiple factions, you have to choose one of them: @1.",
|
2024-09-16 16:27:27 +02:00
|
|
|
table.concat(owned_factions, ", ")
|
2024-09-16 01:03:57 +02:00
|
|
|
)
|
|
|
|
end
|
2024-09-16 16:27:27 +02:00
|
|
|
if not_admin and player_name ~= factions.get_owner(faction_name) then
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("Permission denied: You are not the owner of that faction, "
|
|
|
|
.. "and don't have the @1 privilege.", factions.priv)
|
2024-09-16 16:27:27 +02:00
|
|
|
elseif not facts[faction_name].members[target_name] then
|
|
|
|
return false, S("@1 isn't in faction @2.", target_name, faction_name)
|
2024-09-16 01:03:57 +02:00
|
|
|
elseif not_admin and not factions.valid_password(faction_name, password) then
|
|
|
|
return false, S("Permission denied: Wrong password.")
|
|
|
|
else
|
2024-09-16 16:27:27 +02:00
|
|
|
if factions.chown(faction_name, target_name) then
|
|
|
|
return true, S("Ownership has been transferred to @1.", target_name)
|
2020-07-28 22:42:57 +02:00
|
|
|
else
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("Failed to transfer ownership.")
|
2020-07-28 22:42:57 +02:00
|
|
|
end
|
2024-09-16 01:03:57 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-16 01:59:56 +02:00
|
|
|
function cc.invite(_, params, not_admin)
|
2024-09-16 01:03:57 +02:00
|
|
|
if not_admin then
|
|
|
|
return false, S(
|
|
|
|
"Permission denied: You can't use this command, @1 priv is needed.",
|
|
|
|
factions.priv
|
|
|
|
)
|
|
|
|
end
|
2024-09-16 16:27:27 +02:00
|
|
|
local target_name = params[2]
|
2024-09-16 01:03:57 +02:00
|
|
|
local faction_name = params[3]
|
2024-09-16 16:27:27 +02:00
|
|
|
if not target_name then
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("Missing player name.")
|
|
|
|
elseif not faction_name then
|
|
|
|
return false, S("Missing faction name.")
|
|
|
|
elseif not facts[faction_name] then
|
|
|
|
return false, S("Faction @1 doesn't exist.", faction_name)
|
2024-09-16 16:27:27 +02:00
|
|
|
elseif not minetest.player_exists(target_name) then
|
|
|
|
return false, S("Player @1 doesn't exist.", target_name)
|
2024-09-16 01:03:57 +02:00
|
|
|
end
|
2024-09-16 16:27:27 +02:00
|
|
|
local player_factions = factions.get_player_factions(target_name)
|
|
|
|
if player_factions and facts[faction_name].members[target_name] then
|
2024-09-16 01:03:57 +02:00
|
|
|
return false, S("Player @1 is already in faction @2.",
|
2024-09-16 16:27:27 +02:00
|
|
|
target_name, faction_name)
|
2024-09-16 01:03:57 +02:00
|
|
|
elseif player_factions and factions.mode_unique_faction then
|
|
|
|
return false, S("Player @1 is already in faction @2.",
|
2024-09-16 16:27:27 +02:00
|
|
|
target_name, player_factions[1])
|
2019-09-18 22:52:14 +02:00
|
|
|
else
|
2024-09-16 16:27:27 +02:00
|
|
|
if factions.join_faction(faction_name, target_name) then
|
|
|
|
return true, S("@1 is now a member of faction @2.", target_name, faction_name)
|
2024-09-16 01:03:57 +02:00
|
|
|
else
|
2024-09-16 16:27:27 +02:00
|
|
|
return false, S("Error adding @1 to @2.", target_name, faction_name)
|
2024-09-16 01:03:57 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
local function handle_command(player_name, param)
|
2024-09-16 01:03:57 +02:00
|
|
|
local params = {}
|
|
|
|
for p in string.gmatch(param, "[^%s]+") do
|
|
|
|
table.insert(params, p)
|
|
|
|
end
|
|
|
|
local action = params[1]
|
2024-09-16 01:59:56 +02:00
|
|
|
if not action or not cc[action:lower()] then
|
2021-04-02 13:08:15 +02:00
|
|
|
return false, S("Unknown subcommand. Run '/help factions' for help.")
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
2024-09-16 01:03:57 +02:00
|
|
|
|
2024-09-16 16:27:27 +02:00
|
|
|
local not_admin = not minetest.get_player_privs(player_name)[factions.priv]
|
|
|
|
return cc[action:lower()](player_name, params, not_admin)
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
|
|
|
|
2019-09-09 05:30:31 +02:00
|
|
|
minetest.register_chatcommand("factions", {
|
2024-09-16 16:31:27 +02:00
|
|
|
params = "create <faction> <password>: " .. S("Create a new faction") .. "\n"
|
|
|
|
.. "list: " .. S("List available factions") .. "\n"
|
|
|
|
.. "info [<faction>]: " .. S("See information about a faction") .. "\n"
|
|
|
|
.. "player_info [<player>]: " .. S("See information about a player") .. "\n"
|
|
|
|
.. "join <faction> <password>: " .. S("Join an existing faction") .. "\n"
|
|
|
|
.. "leave [<faction>]: " .. S("Leave your faction") .. "\n"
|
|
|
|
.. "kick <player> [<faction>]: "
|
|
|
|
.. S("Kick someone from your faction or from the given faction") .. "\n"
|
|
|
|
.. "disband <password> [<faction>]: "
|
|
|
|
.. S("Disband your faction or the given faction") .. "\n"
|
|
|
|
.. "passwd <password> [<faction>]: "
|
|
|
|
.. S("Change your faction's password or the password of the given faction") .." \n"
|
|
|
|
.. "chown <player> <password> [<faction>]: "
|
|
|
|
.. S("Transfer ownership of your faction") .. "\n"
|
|
|
|
.. "invite <player> <faction>: "
|
|
|
|
.. S("Add player to a faction, you need @1 priv", factions.priv) .. "\n",
|
2020-07-28 22:29:38 +02:00
|
|
|
|
2019-09-09 04:11:55 +02:00
|
|
|
description = "",
|
|
|
|
privs = {},
|
|
|
|
func = handle_command
|
|
|
|
})
|
2024-09-11 10:49:17 +02:00
|
|
|
|
|
|
|
-- Fix factions
|
2024-09-15 00:06:17 +02:00
|
|
|
do
|
|
|
|
local save_needed = false
|
|
|
|
for _, fact in pairs(facts) do
|
|
|
|
if not fact.members then
|
2024-09-16 01:49:38 +02:00
|
|
|
fact.members = {
|
|
|
|
[fact.owner] = true
|
|
|
|
}
|
2024-09-15 00:06:17 +02:00
|
|
|
end
|
|
|
|
if fact.password then
|
|
|
|
fact.password256 = factions.hash_password(fact.password)
|
|
|
|
fact.password = nil
|
|
|
|
save_needed = true
|
|
|
|
end
|
2024-09-11 10:49:17 +02:00
|
|
|
end
|
2024-09-15 00:06:17 +02:00
|
|
|
if save_needed then
|
|
|
|
save_factions()
|
2024-09-11 10:49:17 +02:00
|
|
|
end
|
|
|
|
end
|
2024-09-11 10:51:57 +02:00
|
|
|
|
2024-09-16 01:38:54 +02:00
|
|
|
-- Integration testing
|
|
|
|
if minetest.get_modpath("mtt") and mtt.enabled then
|
2024-09-16 23:22:14 +02:00
|
|
|
factions.S = S
|
2024-09-16 01:38:54 +02:00
|
|
|
factions.handle_command = handle_command
|
2024-09-16 01:00:34 +02:00
|
|
|
dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/mtt.lua")
|
|
|
|
end
|
|
|
|
|
2024-09-11 10:51:57 +02:00
|
|
|
print("[playerfactions] loaded")
|