From 418f67ff7ef2e3b08b4078476001c8f40275ab56 Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Sun, 7 Aug 2016 15:17:33 +0200 Subject: [PATCH 1/6] Enhanced commands system --- chatcommands.lua | 542 ++++++++++++++++++++++++++++------------------- 1 file changed, 328 insertions(+), 214 deletions(-) diff --git a/chatcommands.lua b/chatcommands.lua index b779600..92f20d6 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -23,6 +23,88 @@ factionsmod_chat = {} --! @memberof factionsmod_chat --! @public ------------------------------------------------------------------------------- + +factionsmod.register_command = function(cmd_name, cmd) + factionsmod.commands[cmd_name] = { -- default command + name = cmd_name, + faction_permissions = {}, + global_privileges = {}, + format = {}, + description = "This command has no description.", + run = function(self, player, argv) + -- check global privileges + local global_privs = {} + for i in ipairs(global_privileges) do + global_privs[global_privileges] = true + end + local can, missing = minetest.check_player_privs(player, global_privs) + if not can then + --TODO: error (show missing privs?) + return false + end + -- checks argument formats + local args = { + factions = {}, + players = {}, + strings = {}, + other = {} + } + for i in ipairs(format) do + local argtype = format[i] + local arg = argv[i] + if argtype == "faction" then + local fac = factionsmod.factions[arg] + if not fac then + --TODO: error (faction required) + return false + else + table.insert(args.factions, fac) + end + elseif argtype == "player" then + local pl = minetest.get_player_by_name(arg) + if not pl then + --TODO: error (player is not connected) --TODO: track existing players for offsync invites and the like + return false + else + table.insert(args.players, pl) + end + elseif argtype == "string" then + table.insert(args.strings, arg) + else + --TODO: error (badly formatted command definition, log to admin) + return false + end + end + for i=#format, #argv, 1 do + table.insert(args.other, argv[i]) + end + + -- checks permissions + local player_faction = factionsmod.players[faction] + + if #faction_permissions > 1 and not player_faction then + minetest.chat_send_player(player, "You are not part of any faction") + return false + elseif #faction_permissions > 1 then + end + + -- get some more data + local pos = minetest.get_player_by_name(player):getpos() + local chunkpos = factionsmod.get_chunk_pos(pos) + return self.on_success(player, player_faction, pos, chunkpos, args) + end, + on_success = function(player, faction, pos, chunkpos, args) + minetest.chat_send_player(player, "Not implemented yet!") + end + } + -- override defaults + for k, v in pairs(cmd) do + factionsmod.commands[cmd_name][k] = v + end +end + + + function factionsmod_chat.init() minetest.register_privilege("faction_user", @@ -58,6 +140,235 @@ function factionsmod_chat.init() } ) end + + +------------------------------------------- +-- R E G I S T E R E D C O M M A N D S | +------------------------------------------- + +factionsmod.register_command ("claim", { + faction_permissions = {"claim"}, + description = "Claim the plot of land you're on.", + on_success = function(player, faction, pos, chunkpos, args) + local chunk = factionsmod.chunk[chunkpos] + if not chunk then + --TODO: success message + player_faction:claim_chunk(chunkpos) + return true + else + if chunk == player_faction.name then + --TODO: error (chunk already claimed by faction) + return false + else + --TODO: error (chunk claimed by another faction) + return false + end + end + end +}) + +factionsmod.register_command("unclaim", { + faction_permissions = {"claim"}, + description = "Unclaim the plot of land you're on.", + on_success = function(player, faction, pos, chunkpos, args) + local chunk = factionsmod.chunk[chunkpos] + if chunk ~= player_faction.name then + --TODO: error (not your faction's chunk) + return false + else + player_faction:unclaim_chunk(chunkpos) + return true + end + end +}) + +--list all known factions +factionsmod.register_command("list", { + description = "List all registered factions.", + on_success = function(player, faction, pos, chunkpos, args) + local list = factionsmod.get_faction_list() + local tosend = "factionsmod: current available factionsmod:" + + for i,v in ipairs(list) do + if i ~= #list then + tosend = tosend .. " " .. v .. "," + else + tosend = tosend .. " " .. v + end + end + minetest.chat_send_player(player, tosend, false) + return true + end +}) + +--show factionsmod mod version +factionsmod.register_command("version", { + description = "Displays mod version.", + on_success = function(player, faction, pos, chunkpos, args) + if cmd == "version" then + minetest.chat_send_player(player, "factionsmod: version " .. factionsmod_version , false) + return true + end + end +}) + +--show description of faction +factionsmod.register_command("info", { + format = {"faction"}, + description = "Shows a faction's description.", + on_success = function(player, faction, pos, chunkpos, args) + minetest.chat_send_player(player, + "factionsmod: " .. args.factions[1].name .. ": " .. + args.factions[1].description, false) + return true + end +}) + +factionsmod.register_command("leave", { + description = "Leave your faction." + on_success = function(player, faction, pos, chunkpos, args) + if faction then + faction:remove_player(player) + else + --TODO: error (not in a faction) + return false + end + --TODO: message? + return true + end +}) + +factionsmod.register_command("kick", { + faction_permissions = {"playerlist"}, + format = {"player"}, + description = "Kick a player from your faction.", + on_success = function(player, faction, pos, chunkpos, args) + local victim = args.players[1] + if factionsmod.players[victim.name] == faction.name then + and victim.name ~= faction.leader -- can't kick da king + faction:remove_player(player) + --TODO: message? + return true + else + --TODO: error (player is leader or in faction) + return false + end + end +}) + +--create new faction +factionsmod.register_command("create", { + format = {"string"}, + description = "Create a new faction.", + on_success = function(player, faction, pos, chunkpos, args) + if faction then + --TODO: error (cannot create faction while in faction) + return false + end + local factioname = args.strings[1] + if factionsmod.can_create_faction(factionname) then + new_faction = factionsmod.create_faction(faction) + new_faction:add_player(player) + new_faction:set_leader(player) + return true + else + --TODO: error (cannot create faction) + return false + end + end +}) + +factionsmod.register_commmand("join", { + format = {"faction"}, + description = "Join a faction.", + on_success = function(player, faction, pos, chunkpos, args) + local new_faction = args.factions[1] + if new_faction:can_join(player) then + if player_faction then -- leave old faction + player_faction:remove_player(player) + --TODO: message + end + new_faction:add_player(player) + else + --TODO: error (could not join faction) + return false + end + return true + end +}) + +factionsmod.register_command("disband", { + faction_permissions = {"disband"}, + description = "Disband your faction.", + on_success = function(player, faction, pos, chunkpos, args) + faction:disband() + --TODO: message + return true + end +}) + +factionsmod.register_command("close", { + faction_permissions = {"playerslist"}, + description = "Make your faction invite-only.", + on_success = function(player, faction, pos, chunkpos, args) + faction:toggle_join_free(false) + --TODO: message + return true + end +}) + +factionsmod.register_command("open", { + faction_permissions = {"playerslist"}, + description = "Allow any player to join your faction.", + on_success = function(player, faction, pos, chunkpos, args) + faction:toggle_join_free(true) + --TODO: message + return true + end +}) + +factionsmod.register_command("description", { + faction_permissions = {"description"}, + description = "Set your faction's description", + on_success = function(player, faction, pos, chunkpos, args) + faction:set_description(args.other.concat(" ")) + --TODO: message + return true + end +}) + +factionsmod.register_command("invite", { + format = {"player"}, + faction_permissions = {"playerslist"}, + description = "Invite a player to your faction.", + on_success = function(player, faction, pos, chunkpos, args) + faction:invite_player(args.players[1]:get_player_name) + --TODO: message + return true + end +}) + +factionsmod.register_command("uninvite", { + format = {"player"}, + faction_permissions = {"playerslist"}, + description = "Revoke a player's invite.", + on_success = function(player, faction, pos, chunkpos, args) + faction:revoke_invite(args.players[1]:get_player_name) + --TODO: message + return true + end +}) + +factionsmod.register_command("delete", { + global_privileges = {"faction_admin"}, + format = {"faction"}, + description = "Delete a faction.", + on_success = function(player, faction, pos, chunkpos, args) + args.factions[1]:disband() + --TODO: message + return true + end +}) ------------------------------------------------------------------------------- -- name: cmdhandler(playername,parameter) @@ -71,16 +382,10 @@ end ------------------------------------------------------------------------------- function factionsmod_chat.cmdhandler(playername,parameter) - --let's not duplicate code local player = minetest.env:get_player_by_name(playername) local params = parameter:split(" ") - local cmd = params[1] - local player_faction = factionsmod[playername] - local player_position = player:getpos() - local chunk = factionsmod.get_chunk(player_position) - local chunkpos = factionsmod.get_chunkpos(player_position) - - --handle common commands + local player_faction = factionsmod.players[playersname] + if parameter == nil or parameter == "" then if player_faction then @@ -90,201 +395,19 @@ function factionsmod_chat.cmdhandler(playername,parameter) end return end - - if cmd == "claim" then - if player_faction:has_permission(playername, "claim") then - if not chunk then - player_faction:claim_chunk(chunkpos) - else - if chunk == player_faction.name then - --TODO: error (chunk already claimed by faction) - else - --TODO: error (chunk claimed by another faction) - end - end - else - --TODO: error message (no permission to claim) - end - return - end - if cmd == "unclaim" then - if player_faction:has_permission(playername, "claim") then - if chunk ~= player_faction.name then - --TODO: error (not your faction's chunk) - else - player_faction:unclaim_chunk(chunkpos) - end - else - --TODO: error (no permission to claim) - end - end + local cmd = factionsmod.commands[params[1]] + if not cmd then + --TODO: error (unknown command) + end - --list all known factionsmod - if cmd == "list" then - local list = factionsmod.get_faction_list() - local tosend = "factionsmod: current available factionsmod:" - - for i,v in ipairs(list) do - if i ~= #list then - tosend = tosend .. " " .. v .. "," - else - tosend = tosend .. " " .. v - end - end - minetest.chat_send_player(playername, tosend, false) - return - end - - --show factionsmod mod version - if cmd == "version" then - minetest.chat_send_player(playername, "factionsmod: version " .. factionsmod_version , false) - return - end - - --show description of faction - if cmd == "info" then - if params[2] ~= nil then - minetest.chat_send_player(playername, - "factionsmod: " .. params[2] .. ": " .. - factionsmod.factions[params[2]].description, false) - return - end - end - - if cmd == "leave" then - player_faction:remove_player(playername) - --TODO: message? - end - - if cmd == "kick" then - if param[2] then - if player_faction:has_permission(playername, "playerlist") then - player_faction:remove_player(playername) - --TODO: message? - else - --TODO: error (no permissions) - end - else - --TODO: error (no player specified) - end + local argv = {} + for i=2, #params, 1 do + table.insert(argv, params[i]) end - --create new faction - if cmd == "create" then - if params[2] ~= nil then - local factioname = params[2] - if factionsmod.can_create_faction(factionname) then - player_faction = factionsmod.create_faction(faction) - player_faction:add_player(playername) - player_faction:set_leader(playername) - else - --TODO: error (cannot create faction) - end - else - --TODO: error (help message?) - end - end + cmd.run(player, argv) - if cmd == "join" then - if params[2] then - local factionname = params[2] - local faction = factionsmod.factons[factionname] - if not faction then - --TODO: error (faction doesn't exist) - else - if faction:can_join(playername) then - if player_faction then -- leave old faction - player_faction:remove_player(playername) - --TODO: message - end - faction:add_player(playername) - else - --TODO: error (could not join faction) - end - end - end - end - - if cmd == "disband" then - if player_faction:has_permission(playername, "disband") then - player_faction:disband() - else - --TODO: error (no permission) - end - end - - if cmd == "close" then - if player_faction:has_permission(playername, "playerlist") then - player_faction:toggle_join_free(false) - else - --TODO: error (no permission) - end - end - - if cmd == "open" then - if player_faction:has_permission(playername, "playerlist") then - player_faction:toggle_join_free(true) - else - --TODO: error (no permission) - end - end - - if cmd == "description" then - if player_faction:has_permission(playername, "description") then - local description = {} - for i=2, #params, 1 do - table.insert(description, params[i]) - end - player_faction:set_description(description.concat(" ")) - else - --TODO: error (no permission) - end - end - - if cmd == "invite" then - if params[2] then - if player_faction:has_permission(playername, "playerlist") then - player_faction:invite_player(params[2]) - else - --TODO: error (no permission) - end - else - --TODO: error (player unspecified) - end - end - - if cmd == "uninvite" then - if params[2] then - if player_faction:has_permission(playername, "playerlist") then - player_faction:revoke_invite(params[2]) - else - --TODO: error (no permission) - end - else - --TODO: error (player unspecified) - end - end - - --all following commands require at least two parameters - if params[2] then - if minetest.check_player_privs(playername,{ faction_admin=true }) then - - --delete faction - if cmd == "delete" then - faction = factionsmod.factions[params[2]] - if faction then - faction:disband() - --TODO: message - else - --TODO: error (no such faction) - end - end - - end - end - - factionsmod_chat.show_help(playername) end ------------------------------------------------------------------------------- @@ -304,21 +427,12 @@ function factionsmod_chat.show_help(playername) MSG("factionsmod mod") MSG("Usage:") - MSG("\tUser commands:") - MSG("\t\t/factionsmod -> info on your current faction") - MSG("\t\t/factionsmod info -> show description of faction") - MSG("\t\t/factionsmod list -> show list of factions") - MSG("\t\t/factionsmod leave -> leave current faction") - MSG("\t\t/factionsmod join -> join specified faction") - MSG("\t\t/factionsmod version -> show version number of mod") - - MSG("\tAdmin commands:") - MSG("\t\t/factionsmod create -> create a new faction") - MSG("\t\t/factionsmod delete -> delete a faction faction") - MSG("\t\t/factionsmod leave -> remove player from faction") - MSG("\t\t/factionsmod invite -> invite player to faction") - MSG("\t\t/factionsmod set_free -> set faction free to join") - MSG("\t\t/factionsmod admin -> make player admin of faction") - MSG("\t\t/factionsmod description -> set description for faction") + for k, v in pairs(factionsmod.commands) do + local args = {} + for i in ipairs(v.format) do + table.insert(args, v.format[i]) + end + MSG{"\t/factionsmod "..k.." <"..table.concat(args, "> <").."> : "..v.description} + end end From 1145203353d270b441d0dcf555ccca9dd0d84a4a Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Sun, 7 Aug 2016 19:22:31 +0200 Subject: [PATCH 2/6] Added commands to delete and add ranks, set spawn, and added check for wether a player is in a faction or not --- chatcommands.lua | 92 +++++++++++++++++++++++++++++++++++++++++------- factionsmod.lua | 33 +++++++++++++++-- 2 files changed, 110 insertions(+), 15 deletions(-) diff --git a/chatcommands.lua b/chatcommands.lua index 92f20d6..54ee9b9 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -30,6 +30,7 @@ factionsmod.register_command = function(cmd_name, cmd) faction_permissions = {}, global_privileges = {}, format = {}, + infaction = true, description = "This command has no description.", run = function(self, player, argv) -- check global privileges @@ -79,9 +80,14 @@ factionsmod.register_command = function(cmd_name, cmd) table.insert(args.other, argv[i]) end - -- checks permissions + -- checks if player in faction local player_faction = factionsmod.players[faction] + if not player_faction and self.infaction then + --TODO: error message + return false + end + -- checks permissions if #faction_permissions > 1 and not player_faction then minetest.chat_send_player(player, "You are not part of any faction") return false @@ -205,10 +211,7 @@ factionsmod.register_command("list", { factionsmod.register_command("version", { description = "Displays mod version.", on_success = function(player, faction, pos, chunkpos, args) - if cmd == "version" then - minetest.chat_send_player(player, "factionsmod: version " .. factionsmod_version , false) - return true - end + minetest.chat_send_player(player, "factionsmod: version " .. factionsmod_version , false) end }) @@ -227,14 +230,9 @@ factionsmod.register_command("info", { factionsmod.register_command("leave", { description = "Leave your faction." on_success = function(player, faction, pos, chunkpos, args) - if faction then - faction:remove_player(player) - else - --TODO: error (not in a faction) - return false - end + faction:remove_player(player) --TODO: message? - return true + return true end }) @@ -259,6 +257,7 @@ factionsmod.register_command("kick", { --create new faction factionsmod.register_command("create", { format = {"string"}, + infaction = false, description = "Create a new faction.", on_success = function(player, faction, pos, chunkpos, args) if faction then @@ -281,6 +280,7 @@ factionsmod.register_command("create", { factionsmod.register_commmand("join", { format = {"faction"}, description = "Join a faction.", + infaction = false, on_success = function(player, faction, pos, chunkpos, args) local new_faction = args.factions[1] if new_faction:can_join(player) then @@ -370,6 +370,74 @@ factionsmod.register_command("delete", { end }) +factionsmod.register_command("ranks", { + description = "List ranks within your faction", + on_success = function(player, faction, pos, chunkpos, args) + if not faction then + --TODO: error message + return false + end + for rank, permissions in pairs(faction.ranks) do + minetest.chat_send_player(player:get_player_name(), rank..": "..table.concat(permissions, " ")) + end + return true + end +}) + +factionsmod.register_command("who", { + description = "List players in your faction, and their ranks.", + on_success = function(player, faction, pos, chunkpos, args) + if not faction then + --TODO: error message + return false + end + for player, rank in ipairs(faction.players) do + minetest.chat_send_player(player:get_player_name(), player.." ("..rank..")") + end + return true + end +}) + +factionsmod.register_command("newrank", { + description = "Add a new rank.", + format = {"string"}, + faction_permissions = {"ranks"}, + on_success = function(player, faction, pos, chunkpos, args) + local rank = args.strings[1] + if faction.ranks[rank] then + --TODO: rank already exists + return false + end + faction:new_rank(rank, args.other) + return true + end +}) + +factionsmod.register_command("delrank", { + description = "Replace and delete a rank.", + format = {"string", "string"}, + faction_permissions = {"ranks"}, + on_success = function(player, faction, pos, chunkpos, args) + local rank = args.strings[1] + local newrank = args.string[2] + if not faction.ranks[rank] or not faction.ranks[rank] then + --TODO: error (one of either ranks do not exist) + return false + end + faction:delete_rank(rank, newrank) + return true + end +}) + +factionsmod.register_command("setspawn", { + description = "Set the faction's spawn", + faction_permissions = {"spawn"}, + on_success = function(player, faction, pos, chunkpos, args) + faction:set_spawn(pos) + return true + end +}) + ------------------------------------------------------------------------------- -- name: cmdhandler(playername,parameter) -- diff --git a/factionsmod.lua b/factionsmod.lua index bb27edb..034909d 100644 --- a/factionsmod.lua +++ b/factionsmod.lua @@ -53,7 +53,7 @@ factionsmod.new_faction = function(name) name = name, power = 0., players = {}, - ranks = {["leader"] = {"disband", "claim", "playerlist", "build", "edit"}, + ranks = {["leader"] = {"disband", "claim", "playerlist", "build", "edit", "ranks"}, ["member"] = {"build"} }, leader = nil, @@ -64,7 +64,8 @@ factionsmod.new_faction = function(name) land = {}, allies = {}, enemies = {}, - join_free = false + join_free = false, + spawn = nil, ---------------------- -- methods @@ -157,7 +158,24 @@ factionsmod.new_faction = function(name) end, end_enemy = function(self, faction) self.enemies[faction] = nil - self:on_end_enemy[faction] + self:on_end_enemy(faction) + end, + set_spawn = function(self, pos) + self.spawn = pos + self:on_set_spawn() + end, + add_rank = function(self, rank, perms) + self.ranks[rank] = perms + self:on_new_rank(rank) + end, + delete_rank = function(self, rank, newrank) + for player, r in pairs(self.players) do + if r == rank then + self.players[player] = newrank + end + end + self.ranks[rank] = nil + self:on_delete_rank(rank, newrank) end, ----------------------- @@ -198,6 +216,15 @@ factionsmod.new_faction = function(name) on_end_alliance = function(self, faction) --TODO: implement end, + on_set_spawn = function(self) + --TODO: implement + end, + on_add_rank = function(self, rank) + --TODO: implement + end, + on_delete_rank = function(self, rank, newrank) + --TODO: implement + end, } factionsmod[name] = faction return faction From 62b8f82c6a0fefabf9622d1903dda7f71c1360a7 Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Sun, 7 Aug 2016 19:31:11 +0200 Subject: [PATCH 3/6] Renamed mod to "factions" --- chatcommands.lua | 112 ++++++++++++++++---------------- factionsmod.lua => factions.lua | 104 ++++++++++++++--------------- init.lua | 20 +++--- 3 files changed, 118 insertions(+), 118 deletions(-) rename factionsmod.lua => factions.lua (77%) diff --git a/chatcommands.lua b/chatcommands.lua index 54ee9b9..4f32c76 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -1,10 +1,10 @@ ------------------------------------------------------------------------------- --- factionsmod Mod by Sapier +-- factions Mod by Sapier -- -- License WTFPL -- --! @file chatcommnd.lua ---! @brief factionsmod chat interface +--! @brief factions chat interface --! @copyright Sapier --! @author Sapier --! @date 2013-05-08 @@ -12,20 +12,20 @@ -- Contact sapier a t gmx net ------------------------------------------------------------------------------- ---! @class factionsmod_chat +--! @class factions_chat --! @brief chat interface class -factionsmod_chat = {} +factions_chat = {} ------------------------------------------------------------------------------- -- name: init() -- --! @brief initialize chat interface ---! @memberof factionsmod_chat +--! @memberof factions_chat --! @public ------------------------------------------------------------------------------- -factionsmod.register_command = function(cmd_name, cmd) - factionsmod.commands[cmd_name] = { -- default command +factions.register_command = function(cmd_name, cmd) + factions.commands[cmd_name] = { -- default command name = cmd_name, faction_permissions = {}, global_privileges = {}, @@ -54,7 +54,7 @@ factionsmod.register_command = function(cmd_name, cmd) local argtype = format[i] local arg = argv[i] if argtype == "faction" then - local fac = factionsmod.factions[arg] + local fac = factions.factions[arg] if not fac then --TODO: error (faction required) return false @@ -81,7 +81,7 @@ factionsmod.register_command = function(cmd_name, cmd) end -- checks if player in faction - local player_faction = factionsmod.players[faction] + local player_faction = factions.players[faction] if not player_faction and self.infaction then --TODO: error message return false @@ -96,7 +96,7 @@ factionsmod.register_command = function(cmd_name, cmd) -- get some more data local pos = minetest.get_player_by_name(player):getpos() - local chunkpos = factionsmod.get_chunk_pos(pos) + local chunkpos = factions.get_chunk_pos(pos) return self.on_success(player, player_faction, pos, chunkpos, args) end, on_success = function(player, faction, pos, chunkpos, args) @@ -105,13 +105,13 @@ factionsmod.register_command = function(cmd_name, cmd) } -- override defaults for k, v in pairs(cmd) do - factionsmod.commands[cmd_name][k] = v + factions.commands[cmd_name][k] = v end end -function factionsmod_chat.init() +function factions_chat.init() minetest.register_privilege("faction_user", { @@ -122,17 +122,17 @@ function factionsmod_chat.init() minetest.register_privilege("faction_admin", { - description = "this user is allowed to create or delete factionsmod", + description = "this user is allowed to create or delete factions", give_to_singleplayer = true, } ) - minetest.register_chatcommand("factionsmod", + minetest.register_chatcommand("factions", { params = " .. ", description = "faction administration functions", privs = { interact=true }, - func = factionsmod_chat.cmdhandler, + func = factions_chat.cmdhandler, } ) @@ -142,7 +142,7 @@ function factionsmod_chat.init() params = " text", description = "send message to a specific faction", privs = { faction_user=true }, - func = factionsmod_chat.chathandler, + func = factions_chat.chathandler, } ) end @@ -152,11 +152,11 @@ end -- R E G I S T E R E D C O M M A N D S | ------------------------------------------- -factionsmod.register_command ("claim", { +factions.register_command ("claim", { faction_permissions = {"claim"}, description = "Claim the plot of land you're on.", on_success = function(player, faction, pos, chunkpos, args) - local chunk = factionsmod.chunk[chunkpos] + local chunk = factions.chunk[chunkpos] if not chunk then --TODO: success message player_faction:claim_chunk(chunkpos) @@ -173,11 +173,11 @@ factionsmod.register_command ("claim", { end }) -factionsmod.register_command("unclaim", { +factions.register_command("unclaim", { faction_permissions = {"claim"}, description = "Unclaim the plot of land you're on.", on_success = function(player, faction, pos, chunkpos, args) - local chunk = factionsmod.chunk[chunkpos] + local chunk = factions.chunk[chunkpos] if chunk ~= player_faction.name then --TODO: error (not your faction's chunk) return false @@ -189,11 +189,11 @@ factionsmod.register_command("unclaim", { }) --list all known factions -factionsmod.register_command("list", { +factions.register_command("list", { description = "List all registered factions.", on_success = function(player, faction, pos, chunkpos, args) - local list = factionsmod.get_faction_list() - local tosend = "factionsmod: current available factionsmod:" + local list = factions.get_faction_list() + local tosend = "factions: current available factionsmod:" for i,v in ipairs(list) do if i ~= #list then @@ -207,27 +207,27 @@ factionsmod.register_command("list", { end }) ---show factionsmod mod version -factionsmod.register_command("version", { +--show factions mod version +factions.register_command("version", { description = "Displays mod version.", on_success = function(player, faction, pos, chunkpos, args) - minetest.chat_send_player(player, "factionsmod: version " .. factionsmod_version , false) + minetest.chat_send_player(player, "factions: version " .. factionsmod_version , false) end }) --show description of faction -factionsmod.register_command("info", { +factions.register_command("info", { format = {"faction"}, description = "Shows a faction's description.", on_success = function(player, faction, pos, chunkpos, args) minetest.chat_send_player(player, - "factionsmod: " .. args.factions[1].name .. ": " .. + "factions: " .. args.factions[1].name .. ": " .. args.factions[1].description, false) return true end }) -factionsmod.register_command("leave", { +factions.register_command("leave", { description = "Leave your faction." on_success = function(player, faction, pos, chunkpos, args) faction:remove_player(player) @@ -236,13 +236,13 @@ factionsmod.register_command("leave", { end }) -factionsmod.register_command("kick", { +factions.register_command("kick", { faction_permissions = {"playerlist"}, format = {"player"}, description = "Kick a player from your faction.", on_success = function(player, faction, pos, chunkpos, args) local victim = args.players[1] - if factionsmod.players[victim.name] == faction.name then + if factions.players[victim.name] == faction.name then and victim.name ~= faction.leader -- can't kick da king faction:remove_player(player) --TODO: message? @@ -255,7 +255,7 @@ factionsmod.register_command("kick", { }) --create new faction -factionsmod.register_command("create", { +factions.register_command("create", { format = {"string"}, infaction = false, description = "Create a new faction.", @@ -265,8 +265,8 @@ factionsmod.register_command("create", { return false end local factioname = args.strings[1] - if factionsmod.can_create_faction(factionname) then - new_faction = factionsmod.create_faction(faction) + if factions.can_create_faction(factionname) then + new_faction = factions.create_faction(faction) new_faction:add_player(player) new_faction:set_leader(player) return true @@ -277,7 +277,7 @@ factionsmod.register_command("create", { end }) -factionsmod.register_commmand("join", { +factions.register_commmand("join", { format = {"faction"}, description = "Join a faction.", infaction = false, @@ -297,7 +297,7 @@ factionsmod.register_commmand("join", { end }) -factionsmod.register_command("disband", { +factions.register_command("disband", { faction_permissions = {"disband"}, description = "Disband your faction.", on_success = function(player, faction, pos, chunkpos, args) @@ -307,7 +307,7 @@ factionsmod.register_command("disband", { end }) -factionsmod.register_command("close", { +factions.register_command("close", { faction_permissions = {"playerslist"}, description = "Make your faction invite-only.", on_success = function(player, faction, pos, chunkpos, args) @@ -317,7 +317,7 @@ factionsmod.register_command("close", { end }) -factionsmod.register_command("open", { +factions.register_command("open", { faction_permissions = {"playerslist"}, description = "Allow any player to join your faction.", on_success = function(player, faction, pos, chunkpos, args) @@ -327,7 +327,7 @@ factionsmod.register_command("open", { end }) -factionsmod.register_command("description", { +factions.register_command("description", { faction_permissions = {"description"}, description = "Set your faction's description", on_success = function(player, faction, pos, chunkpos, args) @@ -337,7 +337,7 @@ factionsmod.register_command("description", { end }) -factionsmod.register_command("invite", { +factions.register_command("invite", { format = {"player"}, faction_permissions = {"playerslist"}, description = "Invite a player to your faction.", @@ -348,7 +348,7 @@ factionsmod.register_command("invite", { end }) -factionsmod.register_command("uninvite", { +factions.register_command("uninvite", { format = {"player"}, faction_permissions = {"playerslist"}, description = "Revoke a player's invite.", @@ -359,7 +359,7 @@ factionsmod.register_command("uninvite", { end }) -factionsmod.register_command("delete", { +factions.register_command("delete", { global_privileges = {"faction_admin"}, format = {"faction"}, description = "Delete a faction.", @@ -370,7 +370,7 @@ factionsmod.register_command("delete", { end }) -factionsmod.register_command("ranks", { +factions.register_command("ranks", { description = "List ranks within your faction", on_success = function(player, faction, pos, chunkpos, args) if not faction then @@ -384,7 +384,7 @@ factionsmod.register_command("ranks", { end }) -factionsmod.register_command("who", { +factions.register_command("who", { description = "List players in your faction, and their ranks.", on_success = function(player, faction, pos, chunkpos, args) if not faction then @@ -398,7 +398,7 @@ factionsmod.register_command("who", { end }) -factionsmod.register_command("newrank", { +factions.register_command("newrank", { description = "Add a new rank.", format = {"string"}, faction_permissions = {"ranks"}, @@ -413,7 +413,7 @@ factionsmod.register_command("newrank", { end }) -factionsmod.register_command("delrank", { +factions.register_command("delrank", { description = "Replace and delete a rank.", format = {"string", "string"}, faction_permissions = {"ranks"}, @@ -429,7 +429,7 @@ factionsmod.register_command("delrank", { end }) -factionsmod.register_command("setspawn", { +factions.register_command("setspawn", { description = "Set the faction's spawn", faction_permissions = {"spawn"}, on_success = function(player, faction, pos, chunkpos, args) @@ -442,17 +442,17 @@ factionsmod.register_command("setspawn", { -- name: cmdhandler(playername,parameter) -- --! @brief chat command handler ---! @memberof factionsmod_chat +--! @memberof factions_chat --! @private -- --! @param playername name --! @param parameter data supplied to command ------------------------------------------------------------------------------- -function factionsmod_chat.cmdhandler(playername,parameter) +function factions_chat.cmdhandler(playername,parameter) local player = minetest.env:get_player_by_name(playername) local params = parameter:split(" ") - local player_faction = factionsmod.players[playersname] + local player_faction = factions.players[playersname] if parameter == nil or parameter == "" then @@ -464,7 +464,7 @@ function factionsmod_chat.cmdhandler(playername,parameter) return end - local cmd = factionsmod.commands[params[1]] + local cmd = factions.commands[params[1]] if not cmd then --TODO: error (unknown command) end @@ -482,25 +482,25 @@ end -- name: show_help(playername,parameter) -- --! @brief send help message to player ---! @memberof factionsmod_chat +--! @memberof factions_chat --! @private -- --! @param playername name ------------------------------------------------------------------------------- -function factionsmod_chat.show_help(playername) +function factions_chat.show_help(playername) local MSG = function(text) minetest.chat_send_player(playername,text,false) end - MSG("factionsmod mod") + MSG("factions mod") MSG("Usage:") - for k, v in pairs(factionsmod.commands) do + for k, v in pairs(factions.commands) do local args = {} for i in ipairs(v.format) do table.insert(args, v.format[i]) end - MSG{"\t/factionsmod "..k.." <"..table.concat(args, "> <").."> : "..v.description} + MSG{"\t/factions "..k.." <"..table.concat(args, "> <").."> : "..v.description} end end diff --git a/factionsmod.lua b/factions.lua similarity index 77% rename from factionsmod.lua rename to factions.lua index 034909d..e9af07b 100644 --- a/factionsmod.lua +++ b/factions.lua @@ -1,10 +1,10 @@ ------------------------------------------------------------------------------- --- factionsmod Mod by Sapier +-- factions Mod by Sapier -- -- License WTFPL -- ---! @file factionsmod.lua ---! @brief factionsmod core file containing datastorage +--! @file factions.lua +--! @brief factions core file containing datastorage --! @copyright Sapier --! @author Sapier --! @date 2013-05-08 @@ -13,33 +13,33 @@ ------------------------------------------------------------------------------- --read some basic information -local factionsmod_worldid = minetest.get_worldpath() +local factions_worldid = minetest.get_worldpath() ---! @class factionsmod ---! @brief main class for factionsmod -factionsmod = {} +--! @class factions +--! @brief main class for factions +factions = {} --! @brief runtime data -factionsmod.factions = {} -factionsmod.chunks = {} -factionsmod.players = {} +factions.factions = {} +factions.chunks = {} +factions.players = {} -factionsmod.print = function(text) - print("factionsmod: " .. dump(text)) +factions.print = function(text) + print("factions: " .. dump(text)) end -factionsmod.dbg_lvl1 = function() end --factionsmod.print -- errors -factionsmod.dbg_lvl2 = function() end --factionsmod.print -- non cyclic trace -factionsmod.dbg_lvl3 = function() end --factionsmod.print -- cyclic trace +factions.dbg_lvl1 = function() end --factionsmod.print -- errors +factions.dbg_lvl2 = function() end --factionsmod.print -- non cyclic trace +factions.dbg_lvl3 = function() end --factionsmod.print -- cyclic trace -factionsmod.factions = {} +factions.factions = {} --- settings -factionsmod.lower_claimable_height = -512 +factions.lower_claimable_height = -512 --------------------- --! @brief returns whether a faction can be created or not (allows for implementation of blacklists and the like) -factionsmod.can_create_faction = function(name) - if factionsmod.factions[name] then +factions.can_create_faction = function(name) + if factions.factions[name] then return false else return true @@ -48,7 +48,7 @@ end --------------------- --! @brief create a faction object -factionsmod.new_faction = function(name) +factions.new_faction = function(name) local faction = { name = name, power = 0., @@ -85,22 +85,22 @@ factionsmod.new_faction = function(name) self:on_player_leave(player) end, claim_chunk = function(self, chunkpos) - factionsmod.chunks[chunkpos] = self.name + factions.chunks[chunkpos] = self.name self.land[chunkpos] = true self:on_claim_chunk(chunkpos) end, unclaim_chunk = function(self, chunkpos) - factionsmod.chunks[chunkpos] = nil + factions.chunks[chunkpos] = nil self.land[chunkpos] = nil self:on_unclaim_chunks(chunkpos) end, disband = function(self) - factionsmod.factions[self.name] = nil + factions.factions[self.name] = nil for i in ipairs(self.players) do -- remove players affiliation - factionsmod.players[self.players[i]] = nil + factions.players[self.players[i]] = nil end for k, v in self.land do -- remove chunk claims - factionsmod.chunks[v] = nil + factions.chunks[v] = nil end self:on_disband() end, @@ -226,21 +226,21 @@ factionsmod.new_faction = function(name) --TODO: implement end, } - factionsmod[name] = faction + factions[name] = faction return faction end --?????????????? -function factionsmod.fix_powercap(name) - factionsmod.data.factionsmod[name].powercap = #factionsmod.dynamic_data.membertable[name] + 10 +function factions.fix_powercap(name) + factions.data.factionsmod[name].powercap = #factionsmod.dynamic_data.membertable[name] + 10 end --?????????????? -function factionsmod.get_chunk(pos) - return factionsmod.chunks[factionsmod.get_chunkpos(pos)] +function factions.get_chunk(pos) + return factions.chunks[factionsmod.get_chunkpos(pos)] end -function factionsmod.get_chunkpos(pos) +function factions.get_chunkpos(pos) return {math.floor(pos.x / 16.), math.floor(pos.z / 16.)} @@ -248,16 +248,16 @@ function factionsmod.get_chunkpos(pos) -- name: add_faction(name) -- --! @brief add a faction ---! @memberof factionsmod +--! @memberof factions --! @public -- --! @param name of faction to add --! --! @return faction object/false (succesfully added faction or not) ------------------------------------------------------------------------------- -function factionsmod.add_faction(name) - if factionsmod.can_create_faction(name) then - local fac = factionsmod.new_faction(name) +function factions.add_faction(name) + if factions.can_create_faction(name) then + local fac = factions.new_faction(name) fac:on_create() return fac else @@ -268,17 +268,17 @@ end ------------------------------------------------------------------------------- -- name: get_faction_list() -- ---! @brief get list of factionsmod ---! @memberof factionsmod +--! @brief get list of factions +--! @memberof factions --! @public --! ---! @return list of factionsmod +--! @return list of factions ------------------------------------------------------------------------------- -function factionsmod.get_faction_list() +function factions.get_faction_list() local retval = {} - for key,value in pairs(factionsmod.factions) do + for key,value in pairs(factions.factions) do table.insert(retval,key) end @@ -289,23 +289,23 @@ end -- name: save() -- --! @brief save data to file ---! @memberof factionsmod +--! @memberof factions --! @private ------------------------------------------------------------------------------- -function factionsmod.save() +function factions.save() --saving is done much more often than reading data to avoid delay --due to figuring out which data to save and which is temporary only --all data is saved here --this implies data needs to be cleant up on load - local file,error = io.open(factionsmod_worldid .. "/" .. "factionsmod.conf","w") + local file,error = io.open(factions_worldid .. "/" .. "factionsmod.conf","w") if file ~= nil then - file:write(minetest.serialize(factionsmod.factions)) + file:write(minetest.serialize(factions.factions)) file:close() else - minetest.log("error","MOD factionsmod: unable to save factionsmod world specific data!: " .. error) + minetest.log("error","MOD factions: unable to save factionsmod world specific data!: " .. error) end end @@ -314,23 +314,23 @@ end -- name: load() -- --! @brief load data from file ---! @memberof factionsmod +--! @memberof factions --! @private -- --! @return true/false ------------------------------------------------------------------------------- -function factionsmod.load() - local file,error = io.open(factionsmod_worldid .. "/" .. "factionsmod.conf","r") +function factions.load() + local file,error = io.open(factions_worldid .. "/" .. "factionsmod.conf","r") if file ~= nil then local raw_data = file:read("*a") - factionsmod.factions = minetest.deserialize(raw_data) - for facname, faction in pairs(factionsmod.factions) do + factions.factions = minetest.deserialize(raw_data) + for facname, faction in pairs(factions.factions) do for i in ipairs(faction.players) do - factionsmod.players[faction.players[i]] = facname + factions.players[faction.players[i]] = facname end for chunkpos, val in pairs(faction.land) do - factionsmod.chunks[chunkpos] = val + factions.chunks[chunkpos] = val end end file:close() diff --git a/init.lua b/init.lua index a809f5a..9719b1e 100644 --- a/init.lua +++ b/init.lua @@ -1,10 +1,10 @@ ------------------------------------------------------------------------------- --- factionsmod Mod by Sapier +-- factions Mod by Sapier -- -- License WTFPL -- --! @file init.lua ---! @brief factionsmod mod to be used by other mods +--! @brief factions mod to be used by other mods --! @copyright Sapier --! @author Sapier --! @date 2013-05-08 @@ -12,18 +12,18 @@ -- Contact sapier a t gmx net ------------------------------------------------------------------------------- -local factionsmod_version = "0.1.6" +local factions_version = "0.1.6" -core.log("action", "MOD: factionsmod (by sapier) loading ...") +core.log("action", "MOD: factions (by sapier) loading ...") --!path of mod -factionsmod_modpath = minetest.get_modpath("factionsmod") +factions_modpath = minetest.get_modpath("factions") -dofile (factionsmod_modpath .. "/factionsmod.lua") -dofile (factionsmod_modpath .. "/chatcommands.lua") +dofile (factions_modpath .. "/factionsmod.lua") +dofile (factions_modpath .. "/chatcommands.lua") -factionsmod.load() -factionsmod_chat.init() +factions.load() +factions_chat.init() -core.log("action","MOD: factionsmod (by sapier) " .. factionsmod_version .. " loaded.") +core.log("action","MOD: factions (by sapier) " .. factions_version .. " loaded.") From 37b948f1f617102e51803df27afd465c6289953d Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Sun, 7 Aug 2016 19:32:27 +0200 Subject: [PATCH 4/6] Fixed dofile() --- init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.lua b/init.lua index 9719b1e..a5003b4 100644 --- a/init.lua +++ b/init.lua @@ -20,7 +20,7 @@ core.log("action", "MOD: factions (by sapier) loading ...") factions_modpath = minetest.get_modpath("factions") -dofile (factions_modpath .. "/factionsmod.lua") +dofile (factions_modpath .. "/factions.lua") dofile (factions_modpath .. "/chatcommands.lua") factions.load() From a52fe6a6bf81f8d464261301c2be72cb771bf301 Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Sun, 7 Aug 2016 19:36:56 +0200 Subject: [PATCH 5/6] Fixed all syntax errors --- chatcommands.lua | 14 ++++++++------ factions.lua | 23 ++++++++++++----------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/chatcommands.lua b/chatcommands.lua index 4f32c76..f95db19 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -16,6 +16,8 @@ --! @brief chat interface class factions_chat = {} +factions.commands = {} + ------------------------------------------------------------------------------- -- name: init() -- @@ -228,7 +230,7 @@ factions.register_command("info", { }) factions.register_command("leave", { - description = "Leave your faction." + description = "Leave your faction.", on_success = function(player, faction, pos, chunkpos, args) faction:remove_player(player) --TODO: message? @@ -242,8 +244,8 @@ factions.register_command("kick", { description = "Kick a player from your faction.", on_success = function(player, faction, pos, chunkpos, args) local victim = args.players[1] - if factions.players[victim.name] == faction.name then - and victim.name ~= faction.leader -- can't kick da king + if factions.players[victim.name] == faction.name + and victim.name ~= faction.leader then -- can't kick da king faction:remove_player(player) --TODO: message? return true @@ -277,7 +279,7 @@ factions.register_command("create", { end }) -factions.register_commmand("join", { +factions.register_command("join", { format = {"faction"}, description = "Join a faction.", infaction = false, @@ -342,7 +344,7 @@ factions.register_command("invite", { faction_permissions = {"playerslist"}, description = "Invite a player to your faction.", on_success = function(player, faction, pos, chunkpos, args) - faction:invite_player(args.players[1]:get_player_name) + faction:invite_player(args.players[1]:get_player_name()) --TODO: message return true end @@ -353,7 +355,7 @@ factions.register_command("uninvite", { faction_permissions = {"playerslist"}, description = "Revoke a player's invite.", on_success = function(player, faction, pos, chunkpos, args) - faction:revoke_invite(args.players[1]:get_player_name) + faction:revoke_invite(args.players[1]:get_player_name()) --TODO: message return true end diff --git a/factions.lua b/factions.lua index e9af07b..99377a6 100644 --- a/factions.lua +++ b/factions.lua @@ -43,7 +43,7 @@ factions.can_create_faction = function(name) return false else return true - end, + end end --------------------- @@ -125,7 +125,7 @@ factions.new_faction = function(name) self:on_player_invited(player) end, revoke_invite = function(self, player) - self.invited_player[player = nil + self.invited_player[player] = nil self:on_revoke_invite(player) end, is_invited = function(self, player) @@ -151,7 +151,7 @@ factions.new_faction = function(name) end, new_enemy = function(self, faction) self.enemies[faction] = true - self:on_new_enemy[faction] + self:on_new_enemy(faction) if self.allies[faction] then self:end_alliance(faction) end @@ -226,7 +226,7 @@ factions.new_faction = function(name) --TODO: implement end, } - factions[name] = faction + factions.factions[name] = faction return faction end @@ -242,6 +242,7 @@ end function factions.get_chunkpos(pos) return {math.floor(pos.x / 16.), math.floor(pos.z / 16.)} +end ------------------------------------------------------------------------------- @@ -256,13 +257,13 @@ function factions.get_chunkpos(pos) --! @return faction object/false (succesfully added faction or not) ------------------------------------------------------------------------------- function factions.add_faction(name) - if factions.can_create_faction(name) then - local fac = factions.new_faction(name) - fac:on_create() - return fac - else - return nil - end, + if factions.can_create_faction(name) then + local fac = factions.new_faction(name) + fac:on_create() + return fac + else + return nil + end end ------------------------------------------------------------------------------- From 55fe191cfda28bb7a02bd435ca13750988d6d59a Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Mon, 8 Aug 2016 01:10:47 +0200 Subject: [PATCH 6/6] Fixed a shitton of bugs + added command "where" to check chunk ownership --- chatcommands.lua | 148 ++++++++++++++++++++++++++--------------------- factions.lua | 86 ++++++++++++++++++++------- init.lua | 1 - 3 files changed, 147 insertions(+), 88 deletions(-) diff --git a/chatcommands.lua b/chatcommands.lua index f95db19..261c05f 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -12,20 +12,14 @@ -- Contact sapier a t gmx net ------------------------------------------------------------------------------- ---! @class factions_chat ---! @brief chat interface class +local send_error = function(player, message) + minetest.chat_send_player(player, message) +end + factions_chat = {} factions.commands = {} -------------------------------------------------------------------------------- --- name: init() --- ---! @brief initialize chat interface ---! @memberof factions_chat ---! @public -------------------------------------------------------------------------------- - factions.register_command = function(cmd_name, cmd) factions.commands[cmd_name] = { -- default command name = cmd_name, @@ -35,16 +29,14 @@ factions.register_command = function(cmd_name, cmd) infaction = true, description = "This command has no description.", run = function(self, player, argv) - -- check global privileges - local global_privs = {} - for i in ipairs(global_privileges) do - global_privs[global_privileges] = true - end - local can, missing = minetest.check_player_privs(player, global_privs) - if not can then - --TODO: error (show missing privs?) - return false - end + --[[ check global privileges + if self.global_privileges then + local bool, missing_privs = minetest.check_player_privs(player, self.global_privileges) + if not bool then + send_error(player, "Missing global privileges: "..table.concat(missing_privs, ", ")) + return false + end + end]] -- checks argument formats local args = { factions = {}, @@ -52,13 +44,17 @@ factions.register_command = function(cmd_name, cmd) strings = {}, other = {} } - for i in ipairs(format) do - local argtype = format[i] + if #argv < #(self.format) then + send_error(player, "Not enough parameters.") + return false + end + for i in ipairs(self.format) do + local argtype = self.format[i] local arg = argv[i] if argtype == "faction" then local fac = factions.factions[arg] if not fac then - --TODO: error (faction required) + send_error(player, "Specified faction "..arg.." does not exist") return false else table.insert(args.factions, fac) @@ -66,7 +62,8 @@ factions.register_command = function(cmd_name, cmd) elseif argtype == "player" then local pl = minetest.get_player_by_name(arg) if not pl then - --TODO: error (player is not connected) --TODO: track existing players for offsync invites and the like + send_error(player, "Player is not invited.") + --TODO: track existing players for offsync invites and the like return false else table.insert(args.players, pl) @@ -74,26 +71,29 @@ factions.register_command = function(cmd_name, cmd) elseif argtype == "string" then table.insert(args.strings, arg) else - --TODO: error (badly formatted command definition, log to admin) + minetest.log("error", "Bad format definition for function "..self.name) + send_error(player, "Internal server error") return false end end - for i=#format, #argv, 1 do + for i=#self.format, #argv, 1 do table.insert(args.other, argv[i]) end - -- checks if player in faction - local player_faction = factions.players[faction] - if not player_faction and self.infaction then - --TODO: error message + -- checks permissions + local player_faction = factions.players[player] + if self.infaction and not player_faction then + minetest.chat_send_player(player, "This command is only available within a faction.") return false end - - -- checks permissions - if #faction_permissions > 1 and not player_faction then - minetest.chat_send_player(player, "You are not part of any faction") - return false - elseif #faction_permissions > 1 then + player_faction = factions.factions[player_faction] + if faction_permissions then + for i in ipairs(self.faction_permissions) do + if not player_faction:has_permission(player, self.faction_permissions[i]) then + send_error(player, "You don't have permissions to do that.") + return false + end + end end -- get some more data @@ -112,8 +112,8 @@ factions.register_command = function(cmd_name, cmd) end - -function factions_chat.init() +local init_commands +init_commands = function() minetest.register_privilege("faction_user", { @@ -144,7 +144,7 @@ function factions_chat.init() params = " text", description = "send message to a specific faction", privs = { faction_user=true }, - func = factions_chat.chathandler, + func = factions_chat.cmdhandler, } ) end @@ -158,17 +158,17 @@ factions.register_command ("claim", { faction_permissions = {"claim"}, description = "Claim the plot of land you're on.", on_success = function(player, faction, pos, chunkpos, args) - local chunk = factions.chunk[chunkpos] + local chunk = factions.chunks[chunkpos] if not chunk then - --TODO: success message - player_faction:claim_chunk(chunkpos) + minetest.chat_send_player(player, "Claming chunk "..chunkpos) + faction:claim_chunk(chunkpos) return true else - if chunk == player_faction.name then - --TODO: error (chunk already claimed by faction) + if chunk == faction.name then + send_error(player, "This chunk already belongs to your faction.") return false else - --TODO: error (chunk claimed by another faction) + send_error(player, "This chunk belongs to another faction.") return false end end @@ -193,9 +193,10 @@ factions.register_command("unclaim", { --list all known factions factions.register_command("list", { description = "List all registered factions.", + infaction = false, on_success = function(player, faction, pos, chunkpos, args) local list = factions.get_faction_list() - local tosend = "factions: current available factionsmod:" + local tosend = "Existing factions:" for i,v in ipairs(list) do if i ~= #list then @@ -213,7 +214,7 @@ factions.register_command("list", { factions.register_command("version", { description = "Displays mod version.", on_success = function(player, faction, pos, chunkpos, args) - minetest.chat_send_player(player, "factions: version " .. factionsmod_version , false) + minetest.chat_send_player(player, "factions: version " .. factions_version , false) end }) @@ -263,17 +264,16 @@ factions.register_command("create", { description = "Create a new faction.", on_success = function(player, faction, pos, chunkpos, args) if faction then - --TODO: error (cannot create faction while in faction) + send_error(player, "You are already in a faction.") return false end - local factioname = args.strings[1] + local factionname = args.strings[1] if factions.can_create_faction(factionname) then - new_faction = factions.create_faction(faction) - new_faction:add_player(player) - new_faction:set_leader(player) + new_faction = factions.new_faction(factionname) + new_faction:add_player(player, new_faction.default_leader_rank) return true else - --TODO: error (cannot create faction) + send_error(player, "Faction cannot be created.") return false end end @@ -292,7 +292,7 @@ factions.register_command("join", { end new_faction:add_player(player) else - --TODO: error (could not join faction) + send_error(player, "You cannot join this faction.") return false end return true @@ -304,7 +304,6 @@ factions.register_command("disband", { description = "Disband your faction.", on_success = function(player, faction, pos, chunkpos, args) faction:disband() - --TODO: message return true end }) @@ -364,6 +363,7 @@ factions.register_command("uninvite", { factions.register_command("delete", { global_privileges = {"faction_admin"}, format = {"faction"}, + infaction = false, description = "Delete a faction.", on_success = function(player, faction, pos, chunkpos, args) args.factions[1]:disband() @@ -380,7 +380,7 @@ factions.register_command("ranks", { return false end for rank, permissions in pairs(faction.ranks) do - minetest.chat_send_player(player:get_player_name(), rank..": "..table.concat(permissions, " ")) + minetest.chat_send_player(player, rank..": "..table.concat(permissions, " ")) end return true end @@ -389,12 +389,12 @@ factions.register_command("ranks", { factions.register_command("who", { description = "List players in your faction, and their ranks.", on_success = function(player, faction, pos, chunkpos, args) - if not faction then - --TODO: error message - return false + if not faction.players then + minetest.chat_send_player(player, "There is nobody in this faction ("..faction.name..")") + return true end - for player, rank in ipairs(faction.players) do - minetest.chat_send_player(player:get_player_name(), player.." ("..rank..")") + for player, rank in pairs(faction.players) do + minetest.chat_send_player(player, player.." ("..rank..")") end return true end @@ -440,6 +440,21 @@ factions.register_command("setspawn", { end }) +factions.register_command("where", { + description = "See whose chunk you stand on", + infaction = false, + on_success = function(player, faction, pos, chunkpos, args) + local chunk = factions.chunks[chunkpos] + minetest.chat_send_player(player, "You are standing on chunk "..chunkpos) + if not chunk then + minetest.chat_send_player(player, "This chunk is free.") + else + minetest.chat_send_player(player, "This chunk belongs to "..chunk) + end + return true + end +}) + ------------------------------------------------------------------------------- -- name: cmdhandler(playername,parameter) -- @@ -450,7 +465,7 @@ factions.register_command("setspawn", { --! @param playername name --! @param parameter data supplied to command ------------------------------------------------------------------------------- -function factions_chat.cmdhandler(playername,parameter) +factions_chat.cmdhandler = function (playername,parameter) local player = minetest.env:get_player_by_name(playername) local params = parameter:split(" ") @@ -461,14 +476,15 @@ function factions_chat.cmdhandler(playername,parameter) if player_faction then minetest.chat_send_player(playername, player_faction.description) else - --TODO: message, no faction + minetest.chat_send_player(playername, "You are part of no faction") end return end local cmd = factions.commands[params[1]] if not cmd then - --TODO: error (unknown command) + send_error(player, "Unknown command.") + return false end local argv = {} @@ -476,7 +492,7 @@ function factions_chat.cmdhandler(playername,parameter) table.insert(argv, params[i]) end - cmd.run(player, argv) + cmd:run(playername, argv) end @@ -506,3 +522,5 @@ function factions_chat.show_help(playername) end end +init_commands() + diff --git a/factions.lua b/factions.lua index 99377a6..1637c99 100644 --- a/factions.lua +++ b/factions.lua @@ -28,13 +28,13 @@ factions.print = function(text) print("factions: " .. dump(text)) end -factions.dbg_lvl1 = function() end --factionsmod.print -- errors -factions.dbg_lvl2 = function() end --factionsmod.print -- non cyclic trace -factions.dbg_lvl3 = function() end --factionsmod.print -- cyclic trace +factions.dbg_lvl1 = function() end --factions.print -- errors +factions.dbg_lvl2 = function() end --factions.print -- non cyclic trace +factions.dbg_lvl3 = function() end --factions.print -- cyclic trace factions.factions = {} --- settings -factions.lower_claimable_height = -512 +factions.lower_laimable_height = -512 --------------------- --! @brief returns whether a faction can be created or not (allows for implementation of blacklists and the like) @@ -71,62 +71,81 @@ factions.new_faction = function(name) -- methods increase_power = function(self, power) self.power = self.power + power + factions.save() end, decrease_power = function(self, power) self.power = self.power - power + factions.save() end, add_player = function(self, player, rank) self.players[player] = rank or self.default_rank + factions.players[player] = self.name self:on_player_join(player) self.invited_players[player] = nil + factions.save() end, remove_player = function(self, player) self.players[player] = nil + factions.players[player] = nil self:on_player_leave(player) + factions.save() end, claim_chunk = function(self, chunkpos) factions.chunks[chunkpos] = self.name self.land[chunkpos] = true self:on_claim_chunk(chunkpos) + factions.save() end, unclaim_chunk = function(self, chunkpos) factions.chunks[chunkpos] = nil self.land[chunkpos] = nil self:on_unclaim_chunks(chunkpos) + factions.save() end, disband = function(self) - factions.factions[self.name] = nil for i in ipairs(self.players) do -- remove players affiliation factions.players[self.players[i]] = nil end - for k, v in self.land do -- remove chunk claims + for k, v in pairs(self.land) do -- remove chunk claims factions.chunks[v] = nil end self:on_disband() + factions.factions[self.name] = nil + factions.save() end, set_leader = function(self, player) self.leader = player self.players[player] = self.default_leader_rank self:on_new_leader() + factions.save() end, has_permission = function(self, player, permission) local p = self.players[player] if not p then return false end - return table.contains(self.groups[p], permission) + local perms = self.ranks[p] + for i in ipairs(perms) do + if perms[i] == permission then + return true + end + end + return false end, set_description = function(self, new) self.description = new self:on_change_description() + factions.save() end, invite_player = function(self, player) self.invited_players[player] = true self:on_player_invited(player) + factions.save() end, revoke_invite = function(self, player) - self.invited_player[player] = nil + self.invited_players[player] = nil self:on_revoke_invite(player) + factions.save() end, is_invited = function(self, player) return table.contains(self.invited_players, player) @@ -134,9 +153,10 @@ factions.new_faction = function(name) toggle_join_free = function(self, bool) self.join_free = bool self:on_toggle_join_free() + factions.save() end, can_join = function(self, player) - return self.join_free or invited_players[player] + return self.join_free or self.invited_players[player] end, new_alliance = function(self, faction) self.allies[faction] = true @@ -144,10 +164,12 @@ factions.new_faction = function(name) if self.enemies[faction] then self:end_enemy(faction) end + factions.save() end, end_alliance = function(self, faction) self.allies[faction] = nil self:on_end_alliance(faction) + factions.save() end, new_enemy = function(self, faction) self.enemies[faction] = true @@ -155,18 +177,22 @@ factions.new_faction = function(name) if self.allies[faction] then self:end_alliance(faction) end + factions.save() end, end_enemy = function(self, faction) self.enemies[faction] = nil self:on_end_enemy(faction) + factions.save() end, set_spawn = function(self, pos) self.spawn = pos self:on_set_spawn() + factions.save() end, add_rank = function(self, rank, perms) self.ranks[rank] = perms self:on_new_rank(rank) + factions.save() end, delete_rank = function(self, rank, newrank) for player, r in pairs(self.players) do @@ -176,10 +202,11 @@ factions.new_faction = function(name) end self.ranks[rank] = nil self:on_delete_rank(rank, newrank) + factions.save() end, - ----------------------- - -- callbacks for events + -------------------------- + -- callbacks for events -- on_create = function(self) --! @brief called when the faction is added to the global faction list --TODO: implement end, @@ -227,21 +254,22 @@ factions.new_faction = function(name) end, } factions.factions[name] = faction + factions.save() return faction end --?????????????? function factions.fix_powercap(name) - factions.data.factionsmod[name].powercap = #factionsmod.dynamic_data.membertable[name] + 10 + factions.data.factions[name].powercap = #factions.dynamic_data.membertable[name] + 10 end --?????????????? function factions.get_chunk(pos) - return factions.chunks[factionsmod.get_chunkpos(pos)] + return factions.chunks[factions.get_chunkpos(pos)] end -function factions.get_chunkpos(pos) - return {math.floor(pos.x / 16.), math.floor(pos.z / 16.)} +function factions.get_chunk_pos(pos) + return math.floor(pos.x / 16.)..","..math.floor(pos.z / 16.) end @@ -300,13 +328,13 @@ function factions.save() --all data is saved here --this implies data needs to be cleant up on load - local file,error = io.open(factions_worldid .. "/" .. "factionsmod.conf","w") + local file,error = io.open(factions_worldid .. "/" .. "factions.conf","w") if file ~= nil then file:write(minetest.serialize(factions.factions)) file:close() else - minetest.log("error","MOD factions: unable to save factionsmod world specific data!: " .. error) + minetest.log("error","MOD factions: unable to save factions world specific data!: " .. error) end end @@ -321,24 +349,25 @@ end --! @return true/false ------------------------------------------------------------------------------- function factions.load() - local file,error = io.open(factions_worldid .. "/" .. "factionsmod.conf","r") + local file,error = io.open(factions_worldid .. "/" .. "factions.conf","r") if file ~= nil then local raw_data = file:read("*a") factions.factions = minetest.deserialize(raw_data) for facname, faction in pairs(factions.factions) do - for i in ipairs(faction.players) do - factions.players[faction.players[i]] = facname + minetest.log("action", facname..","..faction.name) + for player, rank in pairs(faction.players) do + minetest.log("action", player..","..rank) + factions.players[player] = facname end for chunkpos, val in pairs(faction.land) do - factions.chunks[chunkpos] = val + factions.chunks[chunkpos] = facname end end file:close() end end ---autojoin players to faction players minetest.register_on_dieplayer( function(player) end @@ -353,3 +382,16 @@ minetest.register_on_joinplayer( end ) + +local default_is_protected = minetest.is_protected +minetest.is_protected = function(pos, player) + local chunkpos = factions.get_chunk_pos(pos) + local faction = factions.chunks[chunkpos] + if not faction then + return default_is_protected(pos, player) + else + faction = factions.factions[faction] + return not faction:has_permission(player, "build") + end +end + diff --git a/init.lua b/init.lua index a5003b4..5572087 100644 --- a/init.lua +++ b/init.lua @@ -24,6 +24,5 @@ dofile (factions_modpath .. "/factions.lua") dofile (factions_modpath .. "/chatcommands.lua") factions.load() -factions_chat.init() core.log("action","MOD: factions (by sapier) " .. factions_version .. " loaded.")