Refactored to clean the global namespace

This commit is contained in:
Alden Peeters 2019-09-09 15:56:38 -04:00
parent 21537393ff
commit 9ed73d6da1
No known key found for this signature in database
GPG Key ID: CDAD63AE9DA934BE
2 changed files with 41 additions and 41 deletions

@ -31,7 +31,6 @@ The following mods have optional support for `factions`:
Additionally, `factions` can optionally depend on the following mods: Additionally, `factions` can optionally depend on the following mods:
- `protector_redo`: Protect faction territory using protection blocks
- `intllib`: Provide localizations for different languages - `intllib`: Provide localizations for different languages
### Using `factions` in your own mod ### Using `factions` in your own mod

@ -4,6 +4,7 @@ local MP = minetest.get_modpath(minetest.get_current_modname())
local S, NS = dofile(MP.."/intllib.lua") local S, NS = dofile(MP.."/intllib.lua")
-- Data -- Data
factions = {}
local facts = {} local facts = {}
local storage = minetest.get_mod_storage() local storage = minetest.get_mod_storage()
@ -16,12 +17,12 @@ local function save_factions()
end end
-- Data manipulation -- Data manipulation
function get_player_faction(name) function factions.get_player_faction(name)
local player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
if player == nil then if player == nil then
return nil return nil
else else
local faction = minetest.get_player_by_name(name):get_meta():get_string("faction") local faction = player:get_meta():get_string("faction")
if faction == "" then if faction == "" then
return nil return nil
else else
@ -30,7 +31,7 @@ function get_player_faction(name)
end end
end end
function get_owner(name) function factions.get_owner(name)
if facts[name] == nil then if facts[name] == nil then
return nil return nil
else else
@ -38,7 +39,7 @@ function get_owner(name)
end end
end end
function register_faction(fname, founder, pw) function factions.register_faction(fname, founder, pw)
facts[fname] = { facts[fname] = {
name = fname, name = fname,
owner = founder, owner = founder,
@ -47,25 +48,25 @@ function register_faction(fname, founder, pw)
save_factions() save_factions()
end end
function disband_faction(name) function factions.disband_faction(name)
facts[name] = nil facts[name] = nil
save_factions() save_factions()
end end
function get_password(name) function factions.get_password(name)
return facts[name].password return facts[name].password
end end
function set_password(name, password) function factions.set_password(name, password)
facts[name].password = password facts[name].password = password
end end
function join_faction(name, player) function factions.join_faction(name, player)
minetest.get_player_by_name(player):get_meta():set_string("faction", name) factions.get_player_by_name(player):get_meta():set_string("faction", name)
end end
function leave_faction(name) function factions.leave_faction(name)
minetest.get_player_by_name(name):get_meta():set_string("faction", "") factions.get_player_by_name(name):get_meta():set_string("faction", "")
end end
-- Chat commands -- Chat commands
@ -87,26 +88,26 @@ local function handle_command(name, param)
minetest.chat_send_player(name, S("Missing faction name")) minetest.chat_send_player(name, S("Missing faction name"))
elseif password == nil then elseif password == nil then
minetest.chat_send_player(name, S("Missing password")) minetest.chat_send_player(name, S("Missing password"))
elseif get_owner(faction_name) ~= nil then elseif factions.get_owner(faction_name) ~= nil then
minetest.chat_send_player(name, S("That faction already exists")) minetest.chat_send_player(name, S("That faction already exists"))
else else
register_faction(faction_name, name, password) factions.register_faction(faction_name, name, password)
minetest.chat_send_player(name, S("Registered @1", faction_name)) minetest.chat_send_player(name, S("Registered @1", faction_name))
return true return true
end end
elseif action == "disband" then elseif action == "factions.disband" then
local faction_name = get_player_faction(name) local faction_name = factions.get_player_faction(name)
local password = params[2] local password = params[2]
if faction_name == nil then if faction_name == nil then
minetest.chat_send_player(name, S("You are not in a faction")) minetest.chat_send_player(name, S("You are not in a faction"))
elseif name ~= get_owner(faction_name) then elseif name ~= factions.get_owner(faction_name) then
minetest.chat_send_player(name, S("Permission denied")) minetest.chat_send_player(name, S("Permission denied"))
elseif password == nil then 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")) minetest.chat_send_player(name, S("WARNING! This cannot be reversed! Run again with the password if you're absolutely certain"))
elseif password ~= get_password(faction_name) then elseif password ~= factions.get_password(faction_name) then
minetest.chat_send_player(name, S("Permission denied")) minetest.chat_send_player(name, S("Permission denied"))
else else
disband_faction(faction_name, name, name) factions.disband_faction(faction_name, name, name)
minetest.chat_send_player(name, S("Disbanded @1", faction_name)) minetest.chat_send_player(name, S("Disbanded @1", faction_name))
return true return true
end end
@ -124,84 +125,84 @@ local function handle_command(name, param)
elseif action == "info" then elseif action == "info" then
local faction_name = params[2] local faction_name = params[2]
if faction_name == nil then if faction_name == nil then
faction_name = get_player_faction(name) faction_name = factions.get_player_faction(name)
end end
if faction_name == nil then if faction_name == nil then
minetest.chat_send_player(name, S("Missing faction name")) minetest.chat_send_player(name, S("Missing faction name"))
else else
minetest.chat_send_player(name, S("Owner: @1", get_owner(faction_name))) minetest.chat_send_player(name, S("Owner: @1", factions.get_owner(faction_name)))
if get_owner(faction_name) == name then if factions.get_owner(faction_name) == name then
minetest.chat_send_player(name, S("Password: @1", get_password(faction_name))) minetest.chat_send_player(name, S("Password: @1", factions.get_password(faction_name)))
end end
end end
elseif action == "join" then elseif action == "join" then
local faction_name = params[2] local faction_name = params[2]
local password = params[3] local password = params[3]
if get_player_faction(name) ~= nil then if factions.get_player_faction(name) ~= nil then
minetest.chat_send_player(name, S("You are already in a faction")) minetest.chat_send_player(name, S("You are already in a faction"))
elseif get_owner(faction_name) == nil then elseif factions.get_owner(faction_name) == nil then
minetest.chat_send_player(name, S("The faction @1 doesn't exist", faction_name)) minetest.chat_send_player(name, S("The faction @1 doesn't exist", faction_name))
elseif get_password(faction_name) ~= password then elseif factions.get_password(faction_name) ~= password then
minetest.chat_send_player(name, S("Permission denied")) minetest.chat_send_player(name, S("Permission denied"))
else else
join_faction(faction_name, name) factions.join_faction(faction_name, name)
minetest.chat_send_player(name, S("Joined @1", faction_name)) minetest.chat_send_player(name, S("Joined @1", faction_name))
return true return true
end end
elseif action == "leave" then elseif action == "leave" then
local faction_name = get_player_faction(name) local faction_name = factions.get_player_faction(name)
if faction_name == nil then if faction_name == nil then
minetest.chat.send_player(name, S("You are not in a faction")) minetest.chat.send_player(name, S("You are not in a faction"))
elseif get_owner(faction_name) == name then elseif factions.get_owner(faction_name) == name then
minetest.chat_send_player(name, S("You cannot leave your own faction")) minetest.chat_send_player(name, S("You cannot leave your own faction"))
else else
leave_faction(name) factions.leave_faction(name)
minetest.chat_send_player(name, S("Left @1", faction_name)) minetest.chat_send_player(name, S("Left @1", faction_name))
return true return true
end end
elseif action == "kick" then elseif action == "kick" then
local faction_name = get_player_faction(name) local faction_name = factions.get_player_faction(name)
local target = params[2] local target = params[2]
if faction_name == nil then if faction_name == nil then
minetest.chat_send_player(name, S("You are not in a faction")) minetest.chat_send_player(name, S("You are not in a faction"))
elseif target == nil then elseif target == nil then
minetest.chat_send_player(name, S("Missing player name")) minetest.chat_send_player(name, S("Missing player name"))
elseif get_owner(faction_name) ~= name or get_player_faction(target) ~= faction_name then elseif factions.get_owner(faction_name) ~= name or factions.get_player_faction(target) ~= faction_name then
minetest.chat_send_player(name, S("Permission denied")) minetest.chat_send_player(name, S("Permission denied"))
elseif target == name then elseif target == name then
minetest.chat_send_player(name, S("You cannot kick yourself")) minetest.chat_send_player(name, S("You cannot kick yourself"))
else else
leave_faction(name) factions.leave_faction(name)
minetest.chat_send_player(name, S("Kicked @1 from faction", target)) minetest.chat_send_player(name, S("Kicked @1 from faction", target))
return true return true
end end
elseif action == "passwd" then elseif action == "passwd" then
local faction_name = get_player_faction(name) local faction_name = factions.get_player_faction(name)
local password = params[2] local password = params[2]
if faction_name == nil then if faction_name == nil then
minetest.chat_send_player(name, S("You are not in a faction")) minetest.chat_send_player(name, S("You are not in a faction"))
elseif password == nil then elseif password == nil then
minetest.chat_send_player(name, S("Missing password")) minetest.chat_send_player(name, S("Missing password"))
elseif get_owner(faction_name) ~= name then elseif factions.get_owner(faction_name) ~= name then
minetest.chat_send_player(name, S("Permission denied")) minetest.chat_send_player(name, S("Permission denied"))
else else
set_password(faction_name, password) factions.set_password(faction_name, password)
minetest.chat_send_player(name, S("Password has been updated")) minetest.chat_send_player(name, S("Password has been updated"))
return true return true
end end
elseif action == "chown" then elseif action == "chown" then
local faction_name = get_player_faction(name) local faction_name = factions.get_player_faction(name)
local target = params[2] local target = params[2]
local password = params[3] local password = params[3]
if faction_name == nil then if faction_name == nil then
minetest.chat_send_player(name, S("You are not in a faction")) minetest.chat_send_player(name, S("You are not in a faction"))
elseif get_player_faction(name) ~= faction_name then elseif factions.get_player_faction(name) ~= faction_name then
minetest.chat_send_player(name, S("@1 isn't in your faction", name)) minetest.chat_send_player(name, S("@1 isn't in your faction", name))
elseif get_owner(faction_name) ~= name then elseif factions.get_owner(faction_name) ~= name then
minetest.chat_send_player(name, S("Permission denied")) minetest.chat_send_player(name, S("Permission denied"))
elseif password == nil then 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")) minetest.chat_send_player(name, S("WARNING! This cannot be reversed! Run again with the password if you're absolutely certain"))
elseif password ~= get_password(faction_name) then elseif password ~= factions.get_password(faction_name) then
minetest.chat_send_player(name, S("Permission denied")) minetest.chat_send_player(name, S("Permission denied"))
else else
minetest.chat_send_player(name, S("Ownership has been transferred to @1", name)) minetest.chat_send_player(name, S("Ownership has been transferred to @1", name))
@ -218,7 +219,7 @@ minetest.register_chatcommand("factions", {
.."join <faction> <password>: "..S("Join an existing faction").."\n" .."join <faction> <password>: "..S("Join an existing faction").."\n"
.."leave: "..S("Leave your faction").."\n" .."leave: "..S("Leave your faction").."\n"
.."kick <player>: "..S("Kick someone from your faction").."\n" .."kick <player>: "..S("Kick someone from your faction").."\n"
.."disband: "..S("Disband your faction").."\n" .."factions.disband: "..S("Disband your faction").."\n"
.."passwd <password>: "..S("Change your faction's password").."\n" .."passwd <password>: "..S("Change your faction's password").."\n"
.."chown <player>:"..S("Transfer ownership of your faction").."\n", .."chown <player>:"..S("Transfer ownership of your faction").."\n",