2015-07-15 17:08:22 +02:00
|
|
|
-------------------
|
|
|
|
-- Player Classes
|
|
|
|
--
|
|
|
|
|
|
|
|
-- NOTE: This is a very simple interface for classes, more features will be
|
|
|
|
-- added depending on the various mechanisms we will need
|
|
|
|
|
|
|
|
-- Global namespace
|
|
|
|
pclasses = {}
|
|
|
|
|
|
|
|
-- API
|
|
|
|
pclasses.api = {}
|
2015-07-26 01:05:10 +02:00
|
|
|
pclasses.api.util = {}
|
2015-07-15 17:08:22 +02:00
|
|
|
|
2015-07-17 13:44:11 +02:00
|
|
|
-- Configuration
|
|
|
|
pclasses.conf = {}
|
|
|
|
pclasses.conf.default_class = "adventurer"
|
2015-10-22 23:16:38 +02:00
|
|
|
pclasses.conf.superuser_class = "admin"
|
2015-07-17 13:44:11 +02:00
|
|
|
pclasses.conf.save_interval = 3 * 60
|
|
|
|
pclasses.conf.datafile = minetest.get_worldpath() .. "/pclasses"
|
2015-08-01 19:26:50 +02:00
|
|
|
pclasses.conf.gravefile = minetest.get_worldpath() .. "/graveyards"
|
2015-08-20 17:11:46 +02:00
|
|
|
|
2015-07-15 17:08:22 +02:00
|
|
|
-- Classes
|
|
|
|
pclasses.classes = {}
|
|
|
|
|
|
|
|
-- Data
|
2015-07-30 23:37:25 +02:00
|
|
|
pclasses.data = {}
|
|
|
|
pclasses.data.players = {}
|
|
|
|
pclasses.data.reserved_items = {}
|
|
|
|
pclasses.data.hud_ids = {} -- HUD maybe?
|
2015-07-15 17:08:22 +02:00
|
|
|
|
2015-07-27 19:13:30 +02:00
|
|
|
dofile(minetest.get_modpath("pclasses") .. "/api.lua")
|
2015-08-01 19:26:50 +02:00
|
|
|
dofile(minetest.get_modpath("pclasses") .. "/inventory.lua")
|
2015-07-31 21:47:55 +02:00
|
|
|
dofile(minetest.get_modpath("pclasses") .. "/nodes.lua")
|
|
|
|
|
|
|
|
function pclasses.data.load()
|
2015-08-01 19:26:50 +02:00
|
|
|
local file = io.open(pclasses.conf.datafile, "r")
|
2015-07-31 21:47:55 +02:00
|
|
|
if file then
|
|
|
|
local loaded = minetest.deserialize(file:read("*all"))
|
|
|
|
file:close()
|
2015-08-01 19:26:50 +02:00
|
|
|
if loaded then
|
|
|
|
pclasses.data.players = loaded.players or pclasses.data.players
|
|
|
|
minetest.log("action", "[PClasses] Loaded data")
|
|
|
|
end
|
2015-07-31 21:47:55 +02:00
|
|
|
end
|
|
|
|
end
|
2015-07-15 17:08:22 +02:00
|
|
|
|
2015-07-31 21:47:55 +02:00
|
|
|
function pclasses.data.save()
|
|
|
|
local file, err = io.open(pclasses.conf.datafile, "w")
|
|
|
|
if file then
|
|
|
|
file:write(minetest.serialize({
|
|
|
|
players = pclasses.data.players,
|
|
|
|
}))
|
|
|
|
file:close()
|
2016-08-03 14:13:32 +02:00
|
|
|
--minetest.log("action", "[PClasses] Saved data")
|
2015-07-31 21:47:55 +02:00
|
|
|
else
|
|
|
|
minetest.log("error", "[PClasses] Data save failed: open failed: " .. err)
|
|
|
|
end
|
|
|
|
end
|
2015-07-15 17:08:22 +02:00
|
|
|
|
2015-07-31 21:47:55 +02:00
|
|
|
local function data_save_loop()
|
|
|
|
pclasses.data.save()
|
2015-08-01 19:26:50 +02:00
|
|
|
minetest.after(pclasses.conf.save_interval, data_save_loop)
|
2015-07-31 21:47:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
pclasses.data.load()
|
2015-07-17 13:44:11 +02:00
|
|
|
|
2015-07-27 19:13:30 +02:00
|
|
|
------------------
|
|
|
|
-- Default class
|
2015-07-17 13:44:11 +02:00
|
|
|
--
|
2015-07-27 19:13:30 +02:00
|
|
|
|
2015-07-17 13:44:11 +02:00
|
|
|
if pclasses.conf.default_class then
|
2015-07-27 19:13:30 +02:00
|
|
|
dofile(minetest.get_modpath("pclasses") .. "/" .. pclasses.conf.default_class .. ".lua")
|
2015-07-17 13:44:11 +02:00
|
|
|
end
|
2015-08-01 19:26:50 +02:00
|
|
|
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
2018-07-22 18:53:57 +02:00
|
|
|
|
|
|
|
minetest.after(5, function(player)
|
|
|
|
local pname = player:get_player_name()
|
|
|
|
pclasses.api.create_graveyard_inventory(player) --create inventory before
|
|
|
|
local cname = pclasses.api.get_player_class(pname)
|
|
|
|
if cname ~= nil and pclasses.api.get_class_by_name(cname) then
|
|
|
|
pclasses.api.set_player_class(pname, cname)
|
|
|
|
elseif pclasses.api.get_class_by_name(pclasses.conf.default_class) then
|
|
|
|
pclasses.api.set_player_class(pname, pclasses.conf.default_class)
|
|
|
|
end
|
|
|
|
end, player)
|
2015-08-01 19:26:50 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_on_shutdown(function()
|
|
|
|
pclasses.data.save()
|
|
|
|
end)
|
|
|
|
|
|
|
|
data_save_loop()
|