From 6b7b073cd9738d03a036f1ef71c96629a8564bcd Mon Sep 17 00:00:00 2001 From: LeMagnesium Date: Thu, 30 Jul 2015 23:37:25 +0200 Subject: [PATCH] Fixed pclasses' reservation system - Moved callbacks call when setting class - Fixed reservation system --- mods/pclasses/api.lua | 53 ++++++++++++++---------------------------- mods/pclasses/init.lua | 13 ++++------- 2 files changed, 23 insertions(+), 43 deletions(-) diff --git a/mods/pclasses/api.lua b/mods/pclasses/api.lua index a22fa560..64beca08 100755 --- a/mods/pclasses/api.lua +++ b/mods/pclasses/api.lua @@ -35,14 +35,14 @@ end -- Get single player function pclasses.api.get_player_class(pname) - return pclasses.datas.players[pname] + return pclasses.data.players[pname] end -- Get all players for a class function pclasses.api.get_class_players(cname) local pnames = {} if pclasses.api.get_class_by_name(cname) then - for p,c in ipairs(pclasses.datas.players) do + for p,c in ipairs(pclasses.data.players) do if c == cname then table.insert(pnames, table.getn(pnames)+1) end @@ -53,7 +53,13 @@ end -- Set single player function pclasses.api.set_player_class(pname, cname) if pclasses.api.get_class_by_name(cname) then - pclasses.datas.players[pname] = cname + if pclasses.api.get_player_class(pname) ~= cname then + if pclasses.api.get_player_class(pname) and pclasses.classes[pclasses.api.get_player_class(pname)].on_unassigned then + pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned(pname) + end + pclasses.data.players[pname] = cname + pclasses.api.get_class_by_name(cname).on_assigned(pname) + end return true end return false @@ -84,39 +90,16 @@ function pclasses.api.assign_class(player) local pname = player:get_player_name() if pclasses.classes["admin"].determination(player) then - if pclasses.api.get_player_class(pname) ~= "admin" then - if pclasses.classes[pclasses.api.get_player_class(pname)].on_unassigned then - pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned(pname) - end - pclasses.api.set_player_class(pname, "admin") - pclasses.api.get_class_by_name("admin").on_assigned(pname) - end + pclasses.api.set_player_class(pname, "admin") elseif pclasses.classes["hunter"].determination(player) then - if pclasses.api.get_player_class(pname) ~= "hunter" then - if pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned then - pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned(pname) - end - pclasses.api.set_player_class(pname, "hunter") - pclasses.api.get_class_by_name("hunter").on_assigned(pname) - end + pclasses.api.set_player_class(pname, "hunter") elseif pclasses.api.get_class_by_name("warrior").determination(player) then - if pclasses.api.get_player_class(pname) ~= "warrior" then - if pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned then - pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned(pname) - end - pclasses.api.set_player_class(pname, "warrior") - pclasses.api.get_class_by_name("warrior").on_assigned(pname) - end + pclasses.api.set_player_class(pname, "warrior") + elseif pclasses.conf.default_class then - if pclasses.api.get_player_class(pname) ~= pclasses.conf.default_class then - if pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned then - pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned(pname) - end - pclasses.api.set_player_class(pname, pclasses.conf.default_class) - pclasses.api.get_class_by_name(pclasses.conf.default_class).on_assigned(pname) - end + pclasses.api.set_player_class(pname, pclasses.conf.default_class) end end @@ -132,8 +115,8 @@ end) -- Reserved items -- function pclasses.api.reserve_item(cname, itemstring) - pclasses.datas.reserved_items[itemstring] = pclasses.datas.reserved_items or {} - table.insert(pclasses.datas.reserved_items[itemstring], 1, cname) + pclasses.data.reserved_items[itemstring] = pclasses.data.reserved_items[itemstring] or {} + table.insert(pclasses.data.reserved_items[itemstring], 1, cname) end minetest.register_globalstep(function(dtime) @@ -142,9 +125,9 @@ minetest.register_globalstep(function(dtime) local inv = minetest.get_inventory({type="player", name = name}) for i = 1, inv:get_size("main") do local stack = inv:get_stack("main", i) - if pclasses.datas.reserved_items[stack:get_name()] then + if pclasses.data.reserved_items[stack:get_name()] then local drop_stack = true - for _, class in pairs(pclasses.datas.reserved_items) do + for index, class in pairs(pclasses.data.reserved_items[stack:get_name()]) do if pclasses.api.get_player_class(name) == class then drop_stack = false end diff --git a/mods/pclasses/init.lua b/mods/pclasses/init.lua index 43fc8c90..b9e4afc8 100755 --- a/mods/pclasses/init.lua +++ b/mods/pclasses/init.lua @@ -22,10 +22,10 @@ pclasses.conf.datafile = minetest.get_worldpath() .. "/pclasses" pclasses.classes = {} -- Data -pclasses.datas = {} -pclasses.datas.players = {} -pclasses.datas.reserved_items = {} -pclasses.datas.hud_ids = {} -- HUD maybe? +pclasses.data = {} +pclasses.data.players = {} +pclasses.data.reserved_items = {} +pclasses.data.hud_ids = {} -- HUD maybe? dofile(minetest.get_modpath("pclasses") .. "/api.lua") @@ -39,10 +39,7 @@ if pclasses.conf.default_class then dofile(minetest.get_modpath("pclasses") .. "/" .. pclasses.conf.default_class .. ".lua") if pclasses.api.get_class_by_name(pclasses.conf.default_class) then minetest.register_on_joinplayer(function(player) - if not pclasses.api.get_player_class(player:get_player_name()) then - pclasses.api.set_player_class(player:get_player_name(), - pclasses.conf.default_class) - end + pclasses.api.assign_class(player) end) end end