Need to get better at git

master
Austin 2017-07-31 01:12:00 -07:00
parent 59c0fdc22b
commit 7c2b539079
1 changed files with 259 additions and 78 deletions

337
init.lua
View File

@ -10,84 +10,277 @@ This mod adds a protection system in the form of towns.
Initialization
]===]
--Add Chat Command Builder By rubenwardy
dofile(minetest.get_modpath("money") .. "/ChatCmdBuilder.lua")
towns = {} -- Set up town arrays
towns.data = {}
towns.data.townlist = {}
towns.townlist = {}
towns.settings= {}
towns.data.player = {}
towns.settings.tset = true
--[==[
Functions
]==]--
ChatCmdBuilder.new("town",
function(cmd)
cmd:sub("new :townname",
function(name, townname)
if towns.data.townlist[townname] ~= nil then
return false, "Town with the name " .. townname .. " already exists. Try another name."
end
if towns.data.player[name].town ~= nil then
return false, "You are already in " .. towns.data.player[name].town .. ". You must leave with /town leave " .. towns.data.player[name].town .. " before you can create a new town"
end
towns.data.townlist[townname] = {}
towns.data.townlist[townname].member = {}
towns.data.townlist[townname].mayor = name
towns.data.player[name].town = townname
minetest.chat_send_player(name, "You are now the mayor of " .. townname)
end
)
end,{
description = "Commands for towns",
privs = {
basic_privs
}
}
)
function towns.checkassistant(player)
local townname = player:get_attribute("towns:town")
if towns.townlist[townname].members[playername] == "assistant" or "mayor" then
return true
else
return false
end
end
ChatCmdBuilder.new("town",
function(cmd)
cmd:sub("leave",
function(name)
local town = towns.data.player[name].town
minetest.chat_send_player(name, "you have left " .. town)
towns.data.player[name].town = nil
towns.data.townlist[town].member[name] = nil
end
)
function towns.checkmayor(player)
local townname = player:get_attribute("towns:town")
if towns.townlist[townname].members[playername] == "mayor" then
return true
else
return false
end
end
cmd:sub("invite :player",
function(name, player)
local town = towns.data.player[name].town
minetest.chat_send_player(playername, name .. " has invited you to join " .. town " .. respond with /town join " .. town)
town.invite = player
end
)
function towns.new(mayor, townname)
local player = minetest.get_player_by_name(mayor)
if towns.townlist[townname] == nil then
towns.townlist[townname] = {}
towns.townlist[townname].mayor = mayor
towns.townlist[townname].members = {}
towns.townlist[townname].members[mayor] = "mayor"
towns.townlist[townname].description = "New Town!!!"
minetest.chat_send_player(mayor, "You are now the mayor of " .. townname)
player:set_attribute("towns:town", townname)
else
minetest.chat_send_player(mayor, "You need to leave your old town before making a new one")
return false
end
end
cmd:sub("join :town",
function(name, town)
if(townlist[town].invite == name) then
towns.data.townlist[town].member[name] = {}
towns.data.townlist[town].member[name].rank = citizen
towns.data.player[name].town = town
end
end
)
end,{
description = "Commands for towns",
privs = {
basic_privs
}
}
)
function towns.invite(player, townname)
local playername = player:get_player_name()
if townname then
towns.townlist[townname].members[playername] = "invited"
else
return false
end
end
function towns.join(player, townname)
if player:get_attribute("towns:town") == nil then
if towns.townlist[townname] ~= nil then
if towns.townlist[townname].members[player] == "invited" then
player:set_attribute("towns:town", townname)
return true
else
minetest.chat_send_player(player, "You must be invited before joining a town")
return false
end
else
minetest.chat_send_player(player, "That town does not exist")
return false
end
else
minetest.chat_send_player(player, "You can't join a new town, use /town leave to leave your current town first.")
return false
end
end
function towns.leave(playername)
local player = minetest.get_player_by_name(playername)
local townname = player:get_attribute("town:town")
if townname then
towns.townlist[townname].members[playername] = nil
player:set_attribute("town:town", nil)
return true
else
return false
end
end
function towns.delete(playername)
end
function towns.promote(player)
local townname = player:get_attribute("town:town")
local playername = player:get_player_name()
if townname then
if towns.townlist[townname].members[playername] == "citizen" then
towns.townlist[townname].members[playername] = "assistant"
return true
else
return false
end
else
return false
end
end
function towns.demote(player)
local townname = player:get_attribute("towns:town")
local playername = player:get_player_name()
if townname then
if towns.townlist[townname].members[playername] == "assistant" then
towns.townlist[townname].members[playername] = "citizen"
return true
else
return false
end
else
return false
end
end
function towns.transferowner(from, to)
local townname = from:get_attribute("towns:town")
if towns.townlist[townname].members[from] == "mayor" and town.townlist[townname].members[to] == "citizen" or "assistant" then
towns.townlist[townname].members[to] = "mayor"
towns.townlist[townname].members[from] = "assistant"
towns.townlist[townname].mayor = to
return true
else
return false
end
end
function towns.sethome(player)
local townname = player:get_attribute("towns:town")
if towns.townlist[townname] then
towns.townlist[townname].home = player:getpos()
return true
else
return false
end
end
function towns.home(player)
local townname = player:get_attribute("towns:town")
if towns.townlist[townname].home then
player:setpos(towns.townlist[townname].home)
return true
else
return false
end
end
function towns.claim(player)
local townname = player:get_attribute("towns:town")
if towns.townlist[townname] then
end
end
function towns.info(playername)
local player = minetest.get_player_by_name(playername)
local townname = player:get_attribute("towns:town")
if townname then
if towns.townlist[townname] then
minetest.chat_send_player(playername, "==========================================================")
minetest.chat_send_player(playername, "Info for: " .. townname)
minetest.chat_send_player(playername, "Mayor: " .. towns.townlist[townname].mayor)
minetest.chat_send_player(playername, towns.townlist[townname].description)
minetest.chat_send_player(playername, "==========================================================")
end
else
minetest.chat_send_player(playername, "You are not in a town")
end
end
function towns.help(name)
minetest.chat_send_player(name, "======================Towns===============================")
minetest.chat_send_player(name, "/town new <townname> -> Creates a new town")
minetest.chat_send_player(name, "/town invite <playername> -> Invites a player to your town")
minetest.chat_send_player(name, "/town join <townname> -> Joins a town (must be invited")
minetest.chat_send_player(name, "/town leave -> Leaves current town")
minetest.chat_send_player(name, "/town promote <player> -> Promotes player to assistant")
minetest.chat_send_player(name, "/town demote <player> -> Demotes player to citizen")
minetest.chat_send_player(name, "/town transferowner <player> -> Transfers towns ownership to specified player")
minetest.chat_send_player(name, "/town sethome -> Sets town home to current position")
minetest.chat_send_player(name, "/town home -> Teleports you to your town home")
minetest.chat_send_player(name, "/town claim -> Claims current block in towns name")
minetest.chat_send_player(name, "/town unclaim -> Removes current block from town claims")
minetest.chat_send_player(name, "/town help -> Prints command list")
minetest.chat_send_player(name, "/town here -> Displays town info for current position")
minetest.chat_send_player(name, "/town info -> Displays town info for your town")
end
--[===[
Chat commands
]===]
--[[
suplimentary commands
]]--
ChatCmdBuilder.new("town", function(cmd)
cmd:sub("new :townname", function(name, townname)
towns.new(name, townname)
end)
cmd:sub("invite :playername", function(name, player)
if towns.checkassistant(name) then
towns.invite(get_player_by_name(name), command2)
return true
else
minetest.chat_send_player(name, "You must be atleast an assistant to do that")
return false
end
end)
)
end, {
description = "Town commands",
privs = {}
})
]]
function towns.commands(name, param)
local args = param:split(" ")
if args[1] == "help" then
towns.help(name)
return
elseif args[1] == "info" then
towns.info(name)
return
elseif args[1] == "new" then
towns.new(name, args[2])
return
elseif args[1] == "invite" then
if towns.checkassistant(name) then
towns.invite(get_player_by_name(name), args[2])
return true
else
minetest.chat_send_player(name, "You must be atleast an assistant to do that")
return false
end
elseif args[1] == "join" then
towns.join(get_player_by_name(name), args[2])
return
elseif args[1] == "leave" then
towns.leave(name)
return
else
towns.help(name)
end
end
minetest.register_chatcommand("town", {
description = "Towns command interface, type /town help",
privs = {interact = true},
func = function(name, param)
towns.commands(name, param)
end
})
minetest.register_chatcommand("towns", {
description = "Towns command interface, type /town help",
privs = {interact = true},
func = function(name, param)
towns.commands(name, param)
end
})
if towns.settings.tset then
minetest.register_chatcommand("t", { description = "Towns command interface, type /t help",
privs = {interact = true},
func = function(name, param)
towns.commands(name, param)
end
})
end
--[===[
@ -103,7 +296,7 @@ do
local string = file:read()
io.close(file)
if(string ~= nil) then
towns.data = minetest.deserialize(string)
towns.townlist = minetest.deserialize(string)
minetest.debug("[towns] towns.mt successfully read.")
else
@ -115,7 +308,7 @@ end
--Save towns database to file
function towns.save_to_file()
local save = towns.data
local save = towns.townlist
local savestring = minetest.serialize(save)
local filepath = minetest.get_worldpath().."/towns.mt"
@ -136,16 +329,4 @@ minetest.register_on_shutdown(
minetest.log("action", "[towns] Server shuts down. Rescuing data into towns.mt")
towns.save_to_file()
end
)
--Add a new player to the database if they are not already there
minetest.register_on_joinplayer(
function(player)
local playername = player:get_player_name()
if towns.data.player[playername] == nil then
towns.data.player[playername] = {}
end
end
)