2019-09-09 04:11:55 +02:00
|
|
|
|
|
|
|
-- Load support for intllib.
|
|
|
|
local MP = minetest.get_modpath(minetest.get_current_modname())
|
|
|
|
local S, NS = dofile(MP.."/intllib.lua")
|
|
|
|
|
|
|
|
-- Data
|
2019-09-09 21:56:38 +02:00
|
|
|
factions = {}
|
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
|
|
|
|
|
|
|
|
local function save_factions()
|
|
|
|
storage:set_string("facts", minetest.serialize(facts))
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Data manipulation
|
2019-09-09 21:56:38 +02:00
|
|
|
function factions.get_player_faction(name)
|
2019-09-09 04:11:55 +02:00
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
if player == nil then
|
|
|
|
return nil
|
|
|
|
else
|
2019-09-09 21:56:38 +02:00
|
|
|
local faction = player:get_meta():get_string("faction")
|
2019-09-09 04:11:55 +02:00
|
|
|
if faction == "" then
|
|
|
|
return nil
|
|
|
|
else
|
|
|
|
return faction
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-09 21:56:38 +02:00
|
|
|
function factions.get_owner(name)
|
2019-09-09 04:11:55 +02:00
|
|
|
if facts[name] == nil then
|
|
|
|
return nil
|
|
|
|
else
|
|
|
|
return facts[name].owner
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-09 21:56:38 +02:00
|
|
|
function factions.register_faction(fname, founder, pw)
|
2019-09-09 04:11:55 +02:00
|
|
|
facts[fname] = {
|
|
|
|
name = fname,
|
|
|
|
owner = founder,
|
|
|
|
password = pw
|
|
|
|
}
|
|
|
|
save_factions()
|
|
|
|
end
|
|
|
|
|
2019-09-09 21:56:38 +02:00
|
|
|
function factions.disband_faction(name)
|
2019-09-09 04:11:55 +02:00
|
|
|
facts[name] = nil
|
|
|
|
save_factions()
|
|
|
|
end
|
|
|
|
|
2019-09-09 21:56:38 +02:00
|
|
|
function factions.get_password(name)
|
2019-09-09 04:11:55 +02:00
|
|
|
return facts[name].password
|
|
|
|
end
|
|
|
|
|
2019-09-09 21:56:38 +02:00
|
|
|
function factions.set_password(name, password)
|
2019-09-09 04:11:55 +02:00
|
|
|
facts[name].password = password
|
|
|
|
end
|
|
|
|
|
2019-09-09 21:56:38 +02:00
|
|
|
function factions.join_faction(name, player)
|
2019-09-09 23:59:09 +02:00
|
|
|
minetest.get_player_by_name(player):get_meta():set_string("faction", name)
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
|
|
|
|
2019-09-09 21:56:38 +02:00
|
|
|
function factions.leave_faction(name)
|
2019-09-09 23:59:09 +02:00
|
|
|
minetest.get_player_by_name(name):get_meta():set_string("faction", "")
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Chat commands
|
|
|
|
local function handle_command(name, param)
|
|
|
|
--local params = {string.match(param, "^([^ ]+)%s?(.*)")}
|
|
|
|
local params = {}
|
|
|
|
for p in string.gmatch(param, "[^%s]+") do
|
|
|
|
table.insert(params, p)
|
|
|
|
end
|
|
|
|
if params == nil then
|
|
|
|
minetest.chat_send_player(name, S("Unknown subcommand"))
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local action = params[1]
|
|
|
|
if action == "create" then
|
|
|
|
local faction_name = params[2]
|
|
|
|
local password = params[3]
|
|
|
|
if faction_name == nil then
|
|
|
|
minetest.chat_send_player(name, S("Missing faction name"))
|
|
|
|
elseif password == nil then
|
|
|
|
minetest.chat_send_player(name, S("Missing password"))
|
2019-09-09 21:56:38 +02:00
|
|
|
elseif factions.get_owner(faction_name) ~= nil then
|
2019-09-09 04:11:55 +02:00
|
|
|
minetest.chat_send_player(name, S("That faction already exists"))
|
|
|
|
else
|
2019-09-09 21:56:38 +02:00
|
|
|
factions.register_faction(faction_name, name, password)
|
2019-09-09 04:11:55 +02:00
|
|
|
minetest.chat_send_player(name, S("Registered @1", faction_name))
|
|
|
|
return true
|
|
|
|
end
|
2019-09-09 21:56:38 +02:00
|
|
|
elseif action == "factions.disband" then
|
|
|
|
local faction_name = factions.get_player_faction(name)
|
2019-09-09 05:30:31 +02:00
|
|
|
local password = params[2]
|
|
|
|
if faction_name == nil then
|
|
|
|
minetest.chat_send_player(name, S("You are not in a faction"))
|
2019-09-09 21:56:38 +02:00
|
|
|
elseif name ~= factions.get_owner(faction_name) then
|
2019-09-09 05:30:31 +02:00
|
|
|
minetest.chat_send_player(name, S("Permission denied"))
|
|
|
|
elseif password == nil then
|
|
|
|
minetest.chat_send_player(name, S("WARNING! This cannot be reversed! Run again with the password if you're absolutely certain"))
|
2019-09-09 21:56:38 +02:00
|
|
|
elseif password ~= factions.get_password(faction_name) then
|
2019-09-09 05:30:31 +02:00
|
|
|
minetest.chat_send_player(name, S("Permission denied"))
|
|
|
|
else
|
2019-09-09 21:56:38 +02:00
|
|
|
factions.disband_faction(faction_name, name, name)
|
2019-09-09 04:11:55 +02:00
|
|
|
minetest.chat_send_player(name, S("Disbanded @1", faction_name))
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
elseif action == "list" then
|
|
|
|
local faction_list = {}
|
|
|
|
for k, f in pairs(facts) do
|
|
|
|
table.insert(faction_list, k)
|
|
|
|
end
|
|
|
|
if #faction_list ~= 0 then
|
|
|
|
minetest.chat_send_player(name, "Factions("..#faction_list.."): "..table.concat(faction_list, ","))
|
|
|
|
else
|
|
|
|
minetest.chat_send_player(name, S("There are no factions yet"))
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
elseif action == "info" then
|
|
|
|
local faction_name = params[2]
|
|
|
|
if faction_name == nil then
|
2019-09-09 21:56:38 +02:00
|
|
|
faction_name = factions.get_player_faction(name)
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
|
|
|
if faction_name == nil then
|
|
|
|
minetest.chat_send_player(name, S("Missing faction name"))
|
|
|
|
else
|
2019-09-09 21:56:38 +02:00
|
|
|
minetest.chat_send_player(name, S("Owner: @1", factions.get_owner(faction_name)))
|
|
|
|
if factions.get_owner(faction_name) == name then
|
|
|
|
minetest.chat_send_player(name, S("Password: @1", factions.get_password(faction_name)))
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif action == "join" then
|
|
|
|
local faction_name = params[2]
|
|
|
|
local password = params[3]
|
2019-09-09 21:56:38 +02:00
|
|
|
if factions.get_player_faction(name) ~= nil then
|
2019-09-09 04:11:55 +02:00
|
|
|
minetest.chat_send_player(name, S("You are already in a faction"))
|
2019-09-09 21:56:38 +02:00
|
|
|
elseif factions.get_owner(faction_name) == nil then
|
2019-09-09 04:11:55 +02:00
|
|
|
minetest.chat_send_player(name, S("The faction @1 doesn't exist", faction_name))
|
2019-09-09 21:56:38 +02:00
|
|
|
elseif factions.get_password(faction_name) ~= password then
|
2019-09-09 04:11:55 +02:00
|
|
|
minetest.chat_send_player(name, S("Permission denied"))
|
|
|
|
else
|
2019-09-09 21:56:38 +02:00
|
|
|
factions.join_faction(faction_name, name)
|
2019-09-09 04:11:55 +02:00
|
|
|
minetest.chat_send_player(name, S("Joined @1", faction_name))
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
elseif action == "leave" then
|
2019-09-09 21:56:38 +02:00
|
|
|
local faction_name = factions.get_player_faction(name)
|
2019-09-09 04:11:55 +02:00
|
|
|
if faction_name == nil then
|
2019-09-09 23:59:09 +02:00
|
|
|
minetest.chat_send_player(name, S("You are not in a faction"))
|
2019-09-09 21:56:38 +02:00
|
|
|
elseif factions.get_owner(faction_name) == name then
|
2019-09-09 04:11:55 +02:00
|
|
|
minetest.chat_send_player(name, S("You cannot leave your own faction"))
|
|
|
|
else
|
2019-09-09 21:56:38 +02:00
|
|
|
factions.leave_faction(name)
|
2019-09-09 04:11:55 +02:00
|
|
|
minetest.chat_send_player(name, S("Left @1", faction_name))
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
elseif action == "kick" then
|
2019-09-09 21:56:38 +02:00
|
|
|
local faction_name = factions.get_player_faction(name)
|
2019-09-09 04:11:55 +02:00
|
|
|
local target = params[2]
|
|
|
|
if faction_name == nil then
|
|
|
|
minetest.chat_send_player(name, S("You are not in a faction"))
|
|
|
|
elseif target == nil then
|
|
|
|
minetest.chat_send_player(name, S("Missing player name"))
|
2019-09-09 21:56:38 +02:00
|
|
|
elseif factions.get_owner(faction_name) ~= name or factions.get_player_faction(target) ~= faction_name then
|
2019-09-09 04:11:55 +02:00
|
|
|
minetest.chat_send_player(name, S("Permission denied"))
|
|
|
|
elseif target == name then
|
|
|
|
minetest.chat_send_player(name, S("You cannot kick yourself"))
|
|
|
|
else
|
2019-09-09 21:56:38 +02:00
|
|
|
factions.leave_faction(name)
|
2019-09-09 04:11:55 +02:00
|
|
|
minetest.chat_send_player(name, S("Kicked @1 from faction", target))
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
elseif action == "passwd" then
|
2019-09-09 21:56:38 +02:00
|
|
|
local faction_name = factions.get_player_faction(name)
|
2019-09-09 04:11:55 +02:00
|
|
|
local password = params[2]
|
|
|
|
if faction_name == nil then
|
|
|
|
minetest.chat_send_player(name, S("You are not in a faction"))
|
|
|
|
elseif password == nil then
|
|
|
|
minetest.chat_send_player(name, S("Missing password"))
|
2019-09-09 21:56:38 +02:00
|
|
|
elseif factions.get_owner(faction_name) ~= name then
|
2019-09-09 04:11:55 +02:00
|
|
|
minetest.chat_send_player(name, S("Permission denied"))
|
|
|
|
else
|
2019-09-09 21:56:38 +02:00
|
|
|
factions.set_password(faction_name, password)
|
2019-09-09 04:11:55 +02:00
|
|
|
minetest.chat_send_player(name, S("Password has been updated"))
|
|
|
|
return true
|
|
|
|
end
|
2019-09-09 05:30:31 +02:00
|
|
|
elseif action == "chown" then
|
2019-09-09 21:56:38 +02:00
|
|
|
local faction_name = factions.get_player_faction(name)
|
2019-09-09 05:30:31 +02:00
|
|
|
local target = params[2]
|
|
|
|
local password = params[3]
|
|
|
|
if faction_name == nil then
|
|
|
|
minetest.chat_send_player(name, S("You are not in a faction"))
|
2019-09-09 21:56:38 +02:00
|
|
|
elseif factions.get_player_faction(name) ~= faction_name then
|
2019-09-09 05:30:31 +02:00
|
|
|
minetest.chat_send_player(name, S("@1 isn't in your faction", name))
|
2019-09-09 21:56:38 +02:00
|
|
|
elseif factions.get_owner(faction_name) ~= name then
|
2019-09-09 05:30:31 +02:00
|
|
|
minetest.chat_send_player(name, S("Permission denied"))
|
|
|
|
elseif password == nil then
|
|
|
|
minetest.chat_send_player(name, S("WARNING! This cannot be reversed! Run again with the password if you're absolutely certain"))
|
2019-09-09 21:56:38 +02:00
|
|
|
elseif password ~= factions.get_password(faction_name) then
|
2019-09-09 05:30:31 +02:00
|
|
|
minetest.chat_send_player(name, S("Permission denied"))
|
|
|
|
else
|
|
|
|
minetest.chat_send_player(name, S("Ownership has been transferred to @1", name))
|
|
|
|
return true
|
|
|
|
end
|
2019-09-09 04:11:55 +02:00
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2019-09-09 05:30:31 +02:00
|
|
|
minetest.register_chatcommand("factions", {
|
2019-09-09 05:53:15 +02:00
|
|
|
params = "create <faction> <password>: "..S("Create a new faction").."\n"
|
2019-09-09 04:11:55 +02:00
|
|
|
.."list: "..S("List available factions").."\n"
|
|
|
|
.."info <faction>: "..S("See information on a faction").."\n"
|
2019-09-09 05:53:15 +02:00
|
|
|
.."join <faction> <password>: "..S("Join an existing faction").."\n"
|
2019-09-09 04:11:55 +02:00
|
|
|
.."leave: "..S("Leave your faction").."\n"
|
|
|
|
.."kick <player>: "..S("Kick someone from your faction").."\n"
|
2019-09-09 21:56:38 +02:00
|
|
|
.."factions.disband: "..S("Disband your faction").."\n"
|
2019-09-09 05:30:31 +02:00
|
|
|
.."passwd <password>: "..S("Change your faction's password").."\n"
|
2019-09-09 05:53:15 +02:00
|
|
|
.."chown <player>:"..S("Transfer ownership of your faction").."\n",
|
2019-09-09 04:11:55 +02:00
|
|
|
|
|
|
|
description = "",
|
|
|
|
privs = {},
|
|
|
|
func = handle_command
|
|
|
|
})
|