Initial commit

This commit is contained in:
Alden Peeters 2019-09-08 22:11:55 -04:00
commit b72bc3db56
No known key found for this signature in database
GPG Key ID: CDAD63AE9DA934BE
4 changed files with 257 additions and 0 deletions

4
.gitignore vendored Normal file

@ -0,0 +1,4 @@
*
!*.lua
!mod.conf
!.gitignore

206
init.lua Normal file

@ -0,0 +1,206 @@
-- Load support for intllib.
local MP = minetest.get_modpath(minetest.get_current_modname())
local S, NS = dofile(MP.."/intllib.lua")
-- Data
local facts = {}
local storage = minetest.get_mod_storage()
if storage:get_string("facts") ~= nil then
facts = minetest.deserialize(storage:get_string("facts"))
end
local function save_factions()
storage:set_string("facts", minetest.serialize(facts))
end
-- Data manipulation
function get_player_faction(name)
local player = minetest.get_player_by_name(name)
if player == nil then
return nil
else
local faction = minetest.get_player_by_name(name):get_meta():get_string("faction")
if faction == "" then
return nil
else
return faction
end
end
end
function get_owner(name)
if facts[name] == nil then
return nil
else
return facts[name].owner
end
end
function register_faction(fname, founder, pw)
facts[fname] = {
name = fname,
owner = founder,
password = pw
}
save_factions()
end
function disband_faction(name)
facts[name] = nil
save_factions()
end
function get_password(name)
return facts[name].password
end
function set_password(name, password)
facts[name].password = password
end
function join_faction(name, player)
minetest.get_player_by_name(player):get_meta():set_string("faction", name)
end
function leave_faction(name)
minetest.get_player_by_name(name):get_meta():set_string("faction", "")
end
-- Chat commands
-- TODO: Command for setting owner
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"))
elseif get_owner(faction_name) ~= nil then
minetest.chat_send_player(name, S("That faction already exists"))
else
register_faction(faction_name, name, password)
minetest.chat_send_player(name, S("Registered @1", faction_name))
return true
end
elseif action == "disband" then
local faction_name = get_player_faction(name)
if faction_name ~= nil and get_owner(faction_name) == name then
disband_faction(faction_name, name, name)
-- TODO: Send a confirmation
minetest.chat_send_player(name, S("Disbanded @1", faction_name))
return true
else
minetest.chat_send_player(name, S("Permision denied"))
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
faction_name = get_player_faction(name)
end
if faction_name == nil then
minetest.chat_send_player(name, S("Missing faction name"))
else
minetest.chat_send_player(name, S("Owner: @1", get_owner(faction_name)))
if get_owner(faction_name) == name then
minetest.chat_send_player(name, S("Password: @1", get_password(faction_name)))
end
end
return false
elseif action == "join" then
local faction_name = params[2]
local password = params[3]
if get_player_faction(name) ~= nil then
minetest.chat_send_player(name, S("You are already in a faction"))
elseif get_owner(faction_name) == nil then
minetest.chat_send_player(name, S("The faction @1 doesn't exist", faction_name))
elseif get_password(faction_name) ~= password then
minetest.chat_send_player(name, S("Permission denied"))
else
join_faction(faction_name, name)
minetest.chat_send_player(name, S("Joined @1", faction_name))
return true
end
return false
elseif action == "leave" then
local faction_name = get_player_faction(name)
if faction_name == nil then
minetest.chat.send_player(name, S("You are not in a faction"))
elseif get_owner(faction_name) == name then
minetest.chat_send_player(name, S("You cannot leave your own faction"))
else
leave_faction(name)
minetest.chat_send_player(name, S("Left @1", faction_name))
return true
end
elseif action == "kick" then
local faction_name = get_player_faction(name)
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"))
elseif get_owner(faction_name) ~= name or get_player_faction(target) ~= faction_name then
minetest.chat_send_player(name, S("Permission denied"))
elseif target == name then
minetest.chat_send_player(name, S("You cannot kick yourself"))
else
leave_faction(name)
minetest.chat_send_player(name, S("Kicked @1 from faction", target))
return true
end
elseif action == "passwd" then
local faction_name = get_player_faction(name)
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"))
elseif get_owner(faction_name) ~= name then
minetest.chat_send_player(name, S("Permission denied"))
else
set_password(name, password)
minetest.chat_send_player(name, S("Password has been updated"))
return true
end
return false
end
return false
end
minetest.register_chatcommand("faction", {
params = "create <faction>: "..S("Found a new faction").."\n"
.."list: "..S("List available factions").."\n"
.."info <faction>: "..S("See information on a faction").."\n"
.."join <faction>: "..S("Join an existing faction").."\n"
.."leave: "..S("Leave your faction").."\n"
.."kick <player>: "..S("Kick someone from your faction").."\n"
.."disband: "..S("Disband your faction").."\n",
description = "",
privs = {},
func = handle_command
})

45
intllib.lua Normal file

@ -0,0 +1,45 @@
-- Fallback functions for when `intllib` is not installed.
-- Code released under Unlicense <http://unlicense.org>.
-- Get the latest version of this file at:
-- https://raw.githubusercontent.com/minetest-mods/intllib/master/lib/intllib.lua
local function format(str, ...)
local args = { ... }
local function repl(escape, open, num, close)
if escape == "" then
local replacement = tostring(args[tonumber(num)])
if open == "" then
replacement = replacement..close
end
return replacement
else
return "@"..open..num..close
end
end
return (str:gsub("(@?)@(%(?)(%d+)(%)?)", repl))
end
local gettext, ngettext
if minetest.get_modpath("intllib") then
if intllib.make_gettext_pair then
-- New method using gettext.
gettext, ngettext = intllib.make_gettext_pair()
else
-- Old method using text files.
gettext = intllib.Getter()
end
end
-- Fill in missing functions.
gettext = gettext or function(msgid, ...)
return format(msgid, ...)
end
ngettext = ngettext or function(msgid, msgid_plural, n, ...)
return format(n==1 and msgid or msgid_plural, ...)
end
return gettext, ngettext

2
mod.conf Normal file

@ -0,0 +1,2 @@
name = factions
optional_depends = intllib