2019-01-13 18:42:14 +00:00
|
|
|
-- Commands
|
2019-01-12 15:49:44 +00:00
|
|
|
|
|
|
|
-- Privileges
|
|
|
|
|
|
|
|
minetest.register_privilege("towny", {
|
|
|
|
description = "Can create and join towns",
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_privilege("towny_admin", {
|
|
|
|
description = "Can administrate other people's towns",
|
|
|
|
give_to_singleplayer = false
|
|
|
|
})
|
|
|
|
|
2019-01-12 20:12:06 +00:00
|
|
|
-- API
|
|
|
|
|
|
|
|
-- Send message to all town members who are online
|
2019-01-13 01:54:59 +00:00
|
|
|
function towny.chat.announce_to_members(town,message)
|
2019-01-12 20:12:06 +00:00
|
|
|
local tdata = towny.towns[town]
|
2019-01-13 17:39:18 +00:00
|
|
|
if tdata then return end
|
|
|
|
for member in pairs(tdata.members) do
|
|
|
|
if minetest.get_player_by_name(member) then
|
|
|
|
minetest.chat_send_player(member,message)
|
2019-01-12 20:12:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-12 15:49:44 +00:00
|
|
|
-- Commands
|
|
|
|
|
2019-01-12 20:12:06 +00:00
|
|
|
local function invite_player(town,player,target)
|
2019-01-13 01:54:59 +00:00
|
|
|
local utown = towny.get_player_town(player)
|
2019-01-12 20:12:06 +00:00
|
|
|
if not utown or utown ~= town then
|
|
|
|
return false, "You are not in a town."
|
|
|
|
end
|
|
|
|
|
2019-01-12 21:31:11 +00:00
|
|
|
if not minetest.get_player_by_name(target) then
|
|
|
|
return false, "You can only invite online players to your town."
|
|
|
|
end
|
|
|
|
|
2019-01-13 01:54:59 +00:00
|
|
|
local target_town = towny.get_player_town(target)
|
2019-01-12 20:12:06 +00:00
|
|
|
if target_town then
|
|
|
|
return false, "This player is already in a town!"
|
|
|
|
end
|
|
|
|
|
|
|
|
if towny.chat.invites[town.."-"..target] then
|
|
|
|
return false, "This player has already been invited to join your town!"
|
|
|
|
end
|
|
|
|
|
|
|
|
local tdata = towny.towns[town]
|
|
|
|
|
2019-01-13 17:39:18 +00:00
|
|
|
minetest.chat_send_player(target, ("You have been invited to join town '%s' by %s"):format(tdata.name, player))
|
2019-01-12 20:12:06 +00:00
|
|
|
minetest.chat_send_player(target, "You can accept this invite by typing '/town invite accept' or deny '/town invite deny'")
|
|
|
|
|
|
|
|
towny.chat.invites[town.."-"..target] = { rejected = false, town = town, player = target, invited = player }
|
2019-01-12 21:31:11 +00:00
|
|
|
return true, ("Player %s has been invited to join your town."):format(target)
|
2019-01-12 20:12:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function join_town(town,player,from_invite)
|
|
|
|
local tdata = towny.towns[town]
|
|
|
|
if not tdata then return false, "No such town" end
|
|
|
|
if (not from_invite and not tdata.flags['joinable']) then return false, "You cannot join this town." end
|
2019-01-13 17:39:18 +00:00
|
|
|
towny.chat.announce_to_members(town, minetest.colorize("#02aacc", ("%s has joined the town!"):format(player)))
|
2019-01-12 21:31:11 +00:00
|
|
|
minetest.chat_send_player(player, ("You have successfully joined the town '%s'!"):format(tdata.name))
|
2019-01-12 20:12:06 +00:00
|
|
|
tdata.members[player] = {}
|
2019-01-13 01:54:59 +00:00
|
|
|
towny.mark_dirty(town,false)
|
2019-01-12 20:12:06 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local function invite_respond(player,response)
|
2019-01-13 01:54:59 +00:00
|
|
|
local utown = towny.get_player_town(player)
|
2019-01-12 20:12:06 +00:00
|
|
|
if utown or utown ~= town then
|
|
|
|
return false, "You are already in a town."
|
|
|
|
end
|
|
|
|
|
|
|
|
for id,data in pairs(towny.chat.invites) do
|
|
|
|
if data.player == player then
|
|
|
|
if not data.rejected then
|
|
|
|
if response == true then
|
|
|
|
towny.chat.invites[id] = nil
|
|
|
|
return join_town(data.town,player,true)
|
|
|
|
else
|
|
|
|
towny.chat.invites[id] = { rejected = true }
|
|
|
|
return true, "You have rejected the join request."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false, "You do not have any pending invites."
|
|
|
|
end
|
|
|
|
|
2019-01-13 17:39:18 +00:00
|
|
|
local function send_flags (flags,message)
|
2019-01-12 20:12:06 +00:00
|
|
|
local shiny = {}
|
|
|
|
for flag,value in pairs(flags) do
|
|
|
|
if type(value) == "table" then
|
|
|
|
if value.x and value.y and value.z then
|
|
|
|
value = minetest.pos_to_string(value)
|
|
|
|
else
|
|
|
|
value = dump(value)
|
|
|
|
end
|
|
|
|
elseif type(value) == "boolean" then
|
|
|
|
local str_value = "true"
|
|
|
|
if value == false then str_value = "false" end
|
|
|
|
value = str_value
|
|
|
|
end
|
|
|
|
shiny[#shiny+1] = flag..": "..value
|
|
|
|
end
|
|
|
|
|
|
|
|
return true, message ..": "..table.concat( shiny, ", " )
|
|
|
|
end
|
|
|
|
|
2019-01-12 15:49:44 +00:00
|
|
|
local function town_command (name, param)
|
2019-01-13 17:39:18 +00:00
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
if not player then return false, "Can't run command on behalf of offline player." end
|
|
|
|
|
2019-01-12 15:49:44 +00:00
|
|
|
local pr1, pr2 = string.match(param, "^([%a%d_-]+) (.+)$")
|
2019-01-13 01:54:59 +00:00
|
|
|
local town = towny.get_player_town(name)
|
2019-01-12 15:49:44 +00:00
|
|
|
|
|
|
|
-- Pre town requirement
|
2019-01-12 23:27:26 +00:00
|
|
|
local town_info = nil
|
2019-01-12 15:49:44 +00:00
|
|
|
|
|
|
|
if (pr1 == "create" or pr1 == "new") and pr2 then
|
2019-01-13 01:54:59 +00:00
|
|
|
return towny.create_town(nil, name, pr2)
|
2019-01-12 20:12:06 +00:00
|
|
|
elseif (pr1 == "invite" and not minetest.get_player_by_name(pr2)) then
|
2019-01-12 21:31:11 +00:00
|
|
|
return invite_respond(name, (tyes:lower() == "accept" or minetest.is_yes(tyes)))
|
2019-01-13 01:54:59 +00:00
|
|
|
elseif pr1 == "join" and towny.get_town_by_name(pr2) and not town then
|
2019-01-12 20:12:06 +00:00
|
|
|
return join_town(pr2,name,false)
|
|
|
|
elseif pr1 == "show" or pr1 == "info" then
|
2019-01-13 17:39:18 +00:00
|
|
|
if not towny.get_town_by_name(pr2) then
|
2019-01-12 20:12:06 +00:00
|
|
|
return false, "No such town."
|
|
|
|
end
|
2019-01-13 17:39:18 +00:00
|
|
|
town_info = pr2
|
2019-01-12 20:12:06 +00:00
|
|
|
elseif param == "" and town then
|
2019-01-12 23:27:26 +00:00
|
|
|
town_info = town
|
2019-01-12 20:12:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Print town information
|
2019-01-12 23:27:26 +00:00
|
|
|
if town_info then
|
2019-01-12 20:12:06 +00:00
|
|
|
return false, "Not yet implemented!"
|
2019-01-12 15:49:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if not town then
|
|
|
|
return false, "You are not currently in a town."
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Town management commands
|
|
|
|
local tdata = towny.towns[town]
|
|
|
|
|
|
|
|
if param == "extend" or param == "claim" then
|
2019-01-13 01:54:59 +00:00
|
|
|
return towny.extend_town(nil, name)
|
2019-01-12 15:49:44 +00:00
|
|
|
elseif param == "leave" then
|
2019-01-13 01:54:59 +00:00
|
|
|
return towny.leave_town(name)
|
2019-01-13 17:39:18 +00:00
|
|
|
elseif param == "teleport" then
|
|
|
|
local portal = tdata.flags['teleport']
|
|
|
|
if not portal then portal = tdata.flags['origin'] end
|
|
|
|
player:set_pos(portal)
|
|
|
|
return true, "Teleporting you to town.."
|
2019-01-12 15:49:44 +00:00
|
|
|
elseif param == "unclaim" then
|
2019-01-13 01:54:59 +00:00
|
|
|
return towny.abridge_town(nil, name)
|
2019-01-12 15:49:44 +00:00
|
|
|
elseif param == "visualize" then
|
2019-01-13 01:54:59 +00:00
|
|
|
towny.regions.visualize_town(town)
|
2019-01-12 15:49:44 +00:00
|
|
|
return true
|
2019-01-12 20:12:06 +00:00
|
|
|
elseif param == "flags" then
|
2019-01-13 01:54:59 +00:00
|
|
|
local flags = towny.get_flags(town)
|
2019-01-12 20:12:06 +00:00
|
|
|
if flags then
|
2019-01-13 17:39:18 +00:00
|
|
|
return send_flags(flags,"Flags of your town")
|
2019-01-12 20:12:06 +00:00
|
|
|
end
|
2019-01-12 17:30:52 +00:00
|
|
|
elseif (param == "delete" or param == "abandon") or (pr1 == "delete" or pr1 == "abandon") then
|
2019-01-12 15:49:44 +00:00
|
|
|
if towny.chat['delete_verify_' .. name] and pr2 == "I WANT TO DELETE MY TOWN" then
|
|
|
|
towny.chat['delete_verify_' .. name] = nil
|
2019-01-13 01:54:59 +00:00
|
|
|
return towny.delete_town(nil, name)
|
2019-01-12 15:49:44 +00:00
|
|
|
else
|
|
|
|
towny.chat['delete_verify_' .. name] = true
|
|
|
|
minetest.chat_send_player(name, minetest.colorize("#f79204",
|
|
|
|
"WARNING! Deleting your town will render ALL of the buildings in it without protection!"))
|
|
|
|
return false, "Please run the command again with 'I WANT TO DELETE MY TOWN' in all caps written after it."
|
|
|
|
end
|
2019-01-12 23:27:26 +00:00
|
|
|
elseif param == "greeting" then
|
|
|
|
local tdata = towny.towns[town]
|
|
|
|
if not tdata.flags["greeting"] then return false, "This town has no greeting message." end
|
|
|
|
|
|
|
|
return true,
|
2019-01-13 01:54:59 +00:00
|
|
|
minetest.colorize("#078e36", ("[%s] "):format(towny.get_full_name(town))) ..
|
2019-01-12 23:27:26 +00:00
|
|
|
minetest.colorize("#02aacc", tdata.flags["greeting"])
|
2019-01-12 20:12:06 +00:00
|
|
|
elseif pr1 == "kick" then
|
2019-01-13 01:54:59 +00:00
|
|
|
return towny.kick_member(town,name,pr2)
|
2019-01-12 20:12:06 +00:00
|
|
|
elseif pr1 == "set" then
|
|
|
|
local flag, value = string.match(pr2, "^([%a%d_-]+) (.+)$")
|
2019-01-13 01:54:59 +00:00
|
|
|
return towny.set_town_flags(nil,name,flag,value)
|
2019-01-12 23:27:26 +00:00
|
|
|
elseif pr1 == "member" then
|
|
|
|
local action, user = string.match(pr2, "^([%a%d_-]+) (.+)$")
|
|
|
|
if action == "kick" then
|
2019-01-13 01:54:59 +00:00
|
|
|
return towny.kick_member(town,name,pr2)
|
2019-01-12 23:27:26 +00:00
|
|
|
elseif action == "set" then
|
|
|
|
local target, flag, value = string.match(user, "^([%a%d_-]+) ([%a%d_-]+) (.+)$")
|
2019-01-13 01:54:59 +00:00
|
|
|
return towny.set_town_member_flags(nil,name,target,flag,value)
|
2019-01-12 23:27:26 +00:00
|
|
|
end
|
2019-01-12 15:49:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Plot management commands
|
|
|
|
if pr1 == "plot" then
|
|
|
|
local pl1, pl2 = string.match(pr2, "^([%a%d_-]+) (.+)$")
|
2019-01-12 20:12:06 +00:00
|
|
|
if pr2 == "claim" then
|
2019-01-13 01:54:59 +00:00
|
|
|
return towny.claim_plot(nil,name)
|
2019-01-12 20:12:06 +00:00
|
|
|
elseif pr2 == "abandon" then
|
2019-01-13 01:54:59 +00:00
|
|
|
return towny.abandon_plot(nil,name)
|
2019-01-12 20:12:06 +00:00
|
|
|
elseif pr2 == "delete" then
|
2019-01-13 01:54:59 +00:00
|
|
|
return towny.delete_plot(nil,name)
|
2019-01-12 20:12:06 +00:00
|
|
|
elseif pr2 == "flags" then
|
2019-01-13 01:54:59 +00:00
|
|
|
local flags = towny.get_plot_flags(town,nil,name)
|
2019-01-12 20:12:06 +00:00
|
|
|
if flags then
|
2019-01-13 17:39:18 +00:00
|
|
|
return send_flags(flags,"Flags of this plot")
|
2019-01-12 20:12:06 +00:00
|
|
|
else
|
|
|
|
return false, "There's no plot here."
|
|
|
|
end
|
|
|
|
elseif pl1 == "set" and pl2 then
|
|
|
|
local flag, value = string.match(pl2, "^([%a%d_-]+) (.+)$")
|
2019-01-13 01:54:59 +00:00
|
|
|
return towny.set_plot_flags(nil,name,flag,value)
|
2019-01-12 20:12:06 +00:00
|
|
|
elseif pl1 == "member" and pl2 then
|
|
|
|
local action, user = string.match(pl2, "^([%a%d_-]+) (.+)$")
|
|
|
|
if action == "add" then
|
2019-01-13 01:54:59 +00:00
|
|
|
return towny.plot_member(nil,name,user,1)
|
2019-01-12 20:12:06 +00:00
|
|
|
elseif action == "remove" or action == "del" or action == "kick" then
|
2019-01-13 01:54:59 +00:00
|
|
|
return towny.plot_member(nil,name,user,0)
|
2019-01-12 20:12:06 +00:00
|
|
|
elseif action == "set" then
|
|
|
|
local target, flag, value = string.match(user, "^([%a%d_-]+) ([%a%d_-]+) (.+)$")
|
2019-01-13 01:54:59 +00:00
|
|
|
return towny.set_plot_member_flags(nil,name,target,flag,value)
|
2019-01-12 20:12:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif pr1 == "invite" and minetest.get_player_by_name(pr2) then
|
|
|
|
return invite_player(town,name,pr2)
|
2019-01-12 15:49:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return false, "Invalid command usage."
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_chatcommand("town", {
|
|
|
|
description = "Manage your town",
|
|
|
|
privs = {towny = true},
|
|
|
|
func = town_command
|
|
|
|
})
|
2019-01-12 23:29:17 +00:00
|
|
|
|
|
|
|
minetest.register_chatcommand("plot", {
|
|
|
|
description = "Manage your town plot",
|
|
|
|
privs = {towny = true},
|
|
|
|
func = function (name, param)
|
|
|
|
return town_command(name, "plot " .. param)
|
|
|
|
end
|
|
|
|
})
|