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:
- `protector_redo`: Protect faction territory using protection blocks
- `intllib`: Provide localizations for different languages
### 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")
-- Data
factions = {}
local facts = {}
local storage = minetest.get_mod_storage()
@ -16,12 +17,12 @@ local function save_factions()
end
-- Data manipulation
function get_player_faction(name)
function factions.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")
local faction = player:get_meta():get_string("faction")
if faction == "" then
return nil
else
@ -30,7 +31,7 @@ function get_player_faction(name)
end
end
function get_owner(name)
function factions.get_owner(name)
if facts[name] == nil then
return nil
else
@ -38,7 +39,7 @@ function get_owner(name)
end
end
function register_faction(fname, founder, pw)
function factions.register_faction(fname, founder, pw)
facts[fname] = {
name = fname,
owner = founder,
@ -47,25 +48,25 @@ function register_faction(fname, founder, pw)
save_factions()
end
function disband_faction(name)
function factions.disband_faction(name)
facts[name] = nil
save_factions()
end
function get_password(name)
function factions.get_password(name)
return facts[name].password
end
function set_password(name, password)
function factions.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)
function factions.join_faction(name, player)
factions.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", "")
function factions.leave_faction(name)
factions.get_player_by_name(name):get_meta():set_string("faction", "")
end
-- Chat commands
@ -87,26 +88,26 @@ local function handle_command(name, param)
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
elseif factions.get_owner(faction_name) ~= nil then
minetest.chat_send_player(name, S("That faction already exists"))
else
register_faction(faction_name, name, password)
factions.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)
elseif action == "factions.disband" then
local faction_name = factions.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 name ~= get_owner(faction_name) then
elseif name ~= factions.get_owner(faction_name) then
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"))
elseif password ~= get_password(faction_name) then
elseif password ~= factions.get_password(faction_name) then
minetest.chat_send_player(name, S("Permission denied"))
else
disband_faction(faction_name, name, name)
factions.disband_faction(faction_name, name, name)
minetest.chat_send_player(name, S("Disbanded @1", faction_name))
return true
end
@ -124,84 +125,84 @@ local function handle_command(name, param)
elseif action == "info" then
local faction_name = params[2]
if faction_name == nil then
faction_name = get_player_faction(name)
faction_name = factions.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)))
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)))
end
end
elseif action == "join" then
local faction_name = params[2]
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"))
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))
elseif get_password(faction_name) ~= password then
elseif factions.get_password(faction_name) ~= password then
minetest.chat_send_player(name, S("Permission denied"))
else
join_faction(faction_name, name)
factions.join_faction(faction_name, name)
minetest.chat_send_player(name, S("Joined @1", faction_name))
return true
end
elseif action == "leave" then
local faction_name = get_player_faction(name)
local faction_name = factions.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
elseif factions.get_owner(faction_name) == name then
minetest.chat_send_player(name, S("You cannot leave your own faction"))
else
leave_faction(name)
factions.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 faction_name = factions.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
elseif factions.get_owner(faction_name) ~= name or factions.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)
factions.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 faction_name = factions.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
elseif factions.get_owner(faction_name) ~= name then
minetest.chat_send_player(name, S("Permission denied"))
else
set_password(faction_name, password)
factions.set_password(faction_name, password)
minetest.chat_send_player(name, S("Password has been updated"))
return true
end
elseif action == "chown" then
local faction_name = get_player_faction(name)
local faction_name = factions.get_player_faction(name)
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"))
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))
elseif get_owner(faction_name) ~= name then
elseif factions.get_owner(faction_name) ~= name then
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"))
elseif password ~= get_password(faction_name) then
elseif password ~= factions.get_password(faction_name) then
minetest.chat_send_player(name, S("Permission denied"))
else
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"
.."leave: "..S("Leave 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"
.."chown <player>:"..S("Transfer ownership of your faction").."\n",