2019-11-12 18:29:46 +00:00
|
|
|
|
|
|
|
local kits_cache = {}
|
|
|
|
local kits_give_timers = {}
|
|
|
|
local path = minetest.get_worldpath()
|
|
|
|
|
|
|
|
local kit_privs = minetest.settings:get_bool("ess_privilege_per_kit", false)
|
|
|
|
local kit_give = minetest.settings:get("ess_default_kit")
|
|
|
|
if not kit_give then kit_give = "" end
|
|
|
|
|
|
|
|
local function load_from_file()
|
|
|
|
local file = io.open(path.."/kits.ref")
|
|
|
|
if not file then return end
|
|
|
|
|
|
|
|
local str = ""
|
|
|
|
for line in file:lines() do
|
|
|
|
str = str..line
|
|
|
|
end
|
|
|
|
|
|
|
|
file:close()
|
|
|
|
kits_cache = minetest.deserialize(str)
|
|
|
|
if not kits_cache then
|
|
|
|
kits_cache = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
for kit in pairs(kits_cache) do
|
|
|
|
minetest.register_privilege("ess.kits.kit." .. kit, {
|
|
|
|
description = "Kit " .. kit,
|
|
|
|
give_to_singleplayer = false,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function write_to_file()
|
|
|
|
local data = minetest.serialize(kits_cache)
|
|
|
|
minetest.safe_file_write(path.."/kits.ref", data)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function give_kit(player, kit)
|
|
|
|
kit = kit:lower()
|
|
|
|
if not kits_cache[kit] then return false, "Invalid kit name." end
|
|
|
|
if type(player) == "string" then
|
|
|
|
player = minetest.get_player_by_name(player)
|
|
|
|
end
|
|
|
|
|
|
|
|
if not player then return false, "Invalid player." end
|
|
|
|
|
|
|
|
local pname = player:get_player_name()
|
2019-11-12 18:39:49 +00:00
|
|
|
local allprivs = ess.priv_match(pname, "ess.kits.kits.all")
|
2019-11-12 18:29:46 +00:00
|
|
|
if kit_privs and not ess.priv_match(pname, "ess.kits.kit." .. kit) and
|
|
|
|
not allprivs and kit ~= kit_give then
|
|
|
|
return false, "Insufficient permissions."
|
|
|
|
end
|
|
|
|
|
|
|
|
local kdata = kits_cache[kit]
|
|
|
|
|
|
|
|
if kits_give_timers[pname] and kits_give_timers[pname][kit] and kdata._timeout and
|
|
|
|
kits_give_timers[pname][kit] > minetest.get_gametime() - kdata._timeout and not allprivs then
|
|
|
|
return false, string.format("You have to wait %d more seconds before giving this kit again.",
|
|
|
|
(kdata._timeout + (kits_give_timers[pname][kit] - minetest.get_gametime())))
|
|
|
|
end
|
|
|
|
|
|
|
|
local inv = player:get_inventory()
|
|
|
|
local stacks_to_discard = {}
|
|
|
|
for i,tbl in pairs(kdata) do
|
|
|
|
if i ~= "_timeout" then
|
|
|
|
local stack = ItemStack(tbl)
|
|
|
|
if inv:room_for_item("main", stack) then
|
|
|
|
inv:add_item("main", stack)
|
|
|
|
else
|
|
|
|
table.insert(stacks_to_discard, stack)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not kits_give_timers[pname] then
|
|
|
|
kits_give_timers[pname] = {}
|
|
|
|
end
|
|
|
|
kits_give_timers[pname][kit] = minetest.get_gametime()
|
|
|
|
|
|
|
|
return true, stacks_to_discard
|
|
|
|
end
|
|
|
|
|
|
|
|
local function cmd_create(name, param, splitparams)
|
|
|
|
if param == "" or kits_cache[param] then
|
|
|
|
return false, "Invalid name for kit."
|
|
|
|
end
|
|
|
|
|
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
local inv = player:get_inventory()
|
|
|
|
local stacks = {}
|
|
|
|
for _,stack in pairs(inv:get_list("main")) do
|
|
|
|
if not stack:is_empty() then
|
|
|
|
table.insert(stacks, stack:to_table())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if #stacks == 0 then
|
|
|
|
return false, "Please put some items in your inventory before attempting to create a kit!"
|
|
|
|
end
|
|
|
|
|
|
|
|
local timeout = tonumber(splitparams[2])
|
|
|
|
local kitname = splitparams[1]:lower()
|
|
|
|
|
|
|
|
kits_cache[kitname] = stacks
|
|
|
|
|
|
|
|
if timeout then
|
|
|
|
kits_cache[kitname]._timeout = timeout
|
|
|
|
end
|
|
|
|
|
|
|
|
write_to_file()
|
|
|
|
|
|
|
|
return true, "Successfully created the kit \""..kitname.."\"!"
|
|
|
|
end
|
|
|
|
|
|
|
|
local function cmd_delete(name, param)
|
|
|
|
if param == "" or not kits_cache[param] then
|
|
|
|
return false, "Invalid name for kit."
|
|
|
|
end
|
|
|
|
|
|
|
|
kits_cache[param] = nil
|
|
|
|
write_to_file()
|
|
|
|
|
|
|
|
return true, "Successfully removed the kit."
|
|
|
|
end
|
|
|
|
|
|
|
|
local function cmd_kits(name, param, splitparams)
|
|
|
|
-- Include convenience functions in this command
|
|
|
|
local fst = splitparams[1]
|
|
|
|
if fst == "create" or fst == "make" or fst == "remove" or fst == "delete" then
|
|
|
|
if not ess.priv_match(name, "ess.kits.create") then
|
|
|
|
return ess.reject_permission()
|
|
|
|
end
|
|
|
|
|
|
|
|
if fst == "create" or fst == "make" then
|
|
|
|
return cmd_create(name, splitparams[2], {splitparams[2], splitparams[3]})
|
|
|
|
else
|
|
|
|
return cmd_delete(name, splitparams[2], {splitparams[2], splitparams[3]})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Give a specific kit
|
|
|
|
if param ~= "" then
|
|
|
|
local target = name
|
|
|
|
if splitparams[2] and splitparams[2] ~= name and not ess.priv_match(name, "ess.kits.other") then
|
|
|
|
return ess.reject_permission()
|
|
|
|
end
|
|
|
|
|
|
|
|
local give, leftover = give_kit(target, param)
|
|
|
|
if not give then
|
|
|
|
return false, "Could not give kit. " .. leftover
|
|
|
|
end
|
|
|
|
|
|
|
|
return true, "Kit given successfully."
|
|
|
|
end
|
|
|
|
|
|
|
|
-- List available kits
|
|
|
|
local available = {}
|
|
|
|
for kit in pairs(kits_cache) do
|
|
|
|
local perms = true
|
|
|
|
if kit_privs then
|
2019-11-20 09:58:09 +00:00
|
|
|
perms = ess.priv_match(name, "ess.kits.kit." .. kit) or ess.priv_match(name, "ess.kits.kits.all")
|
2019-11-12 18:29:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if perms then
|
|
|
|
table.insert(available, kit)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true, "Kits: " .. table.concat(available, ", ")
|
|
|
|
end
|
|
|
|
|
|
|
|
local commands = {
|
|
|
|
["kits"] = {
|
|
|
|
description = "Give or list available kits",
|
2019-11-20 09:58:09 +00:00
|
|
|
params = "(delete|create) <kit> [<timeout>] | [<kit>] [<playername>]",
|
2019-11-12 18:29:46 +00:00
|
|
|
aliases = {"kit"},
|
|
|
|
privs = {
|
2019-11-12 18:39:49 +00:00
|
|
|
["ess.kits.kits"] = true,
|
|
|
|
["ess.kits.kits.all"] = true,
|
2019-11-12 18:29:46 +00:00
|
|
|
["ess.kits.other"] = true,
|
|
|
|
},
|
|
|
|
func = cmd_kits,
|
|
|
|
},
|
|
|
|
["createkit"] = {
|
|
|
|
description = "Create a kit from the items in your inventory",
|
|
|
|
params = "<kitname> [<timeout>]",
|
|
|
|
aliases = {"mkkit", "addkit"},
|
|
|
|
privs = {
|
|
|
|
["ess.kits.create"] = true,
|
|
|
|
},
|
|
|
|
func = cmd_create,
|
|
|
|
},
|
|
|
|
["deletekit"] = {
|
|
|
|
description = "Delete a kit",
|
|
|
|
params = "<kitname>",
|
|
|
|
aliases = {"rmkit", "delkit"},
|
|
|
|
privs = {
|
|
|
|
["ess.kits.create"] = true,
|
|
|
|
},
|
|
|
|
func = cmd_delete,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ess.autoregister(commands, "kits")
|
|
|
|
|
|
|
|
if kit_give ~= "" then
|
|
|
|
minetest.register_on_newplayer(function(player)
|
|
|
|
give_kit(player, kit_give)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Load for the first time
|
|
|
|
load_from_file()
|