mirror of
https://github.com/minetest-mods/player_monoids.git
synced 2025-01-09 17:10:19 +01:00
Make monoids remove players from themselves on leave
This commit is contained in:
parent
3b3904d1d6
commit
a5c5184933
21
init.lua
21
init.lua
@ -4,10 +4,9 @@
|
|||||||
-- Any documentation here are internal details, please avoid using them in your
|
-- Any documentation here are internal details, please avoid using them in your
|
||||||
-- mod.
|
-- mod.
|
||||||
|
|
||||||
player_monoids = {}
|
local modpath = minetest.get_modpath(minetest.get_current_modname())
|
||||||
|
|
||||||
-- Needed for removing players from monoid lists when they leave.
|
player_monoids = {}
|
||||||
local player_maps = {}
|
|
||||||
|
|
||||||
local mon_meta = {}
|
local mon_meta = {}
|
||||||
|
|
||||||
@ -22,14 +21,22 @@ mon_meta.__index = mon_meta
|
|||||||
local function monoid(def)
|
local function monoid(def)
|
||||||
local mon = {}
|
local mon = {}
|
||||||
|
|
||||||
local player_map = {}
|
local p_map = {}
|
||||||
mon.players = player_map
|
mon.player_map = p_map
|
||||||
table.insert(player_maps, player_map)
|
|
||||||
|
|
||||||
mon.next_id = 1
|
mon.next_id = 1
|
||||||
|
|
||||||
|
local v_cache = {}
|
||||||
|
mon.value_cache = v_cache
|
||||||
|
|
||||||
setmetatable(mon, mon_methods)
|
setmetatable(mon, mon_methods)
|
||||||
|
|
||||||
|
minetest.register_on_leaveplayer(function(player)
|
||||||
|
local p_name = player:get_player_name()
|
||||||
|
p_map[p_name] = nil
|
||||||
|
v_cache[p_name] = nil
|
||||||
|
end)
|
||||||
|
|
||||||
return mon
|
return mon
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -55,7 +62,7 @@ function mon_meta:add_change(player, value)
|
|||||||
|
|
||||||
local old_total = self.value_cache[p_name]
|
local old_total = self.value_cache[p_name]
|
||||||
p_effects[actual_id] = value
|
p_effects[actual_id] = value
|
||||||
local new_total = self.fold(p_effects)
|
local new_total = self.fold(p_effects)
|
||||||
self.value_cache[p_name] = new_total
|
self.value_cache[p_name] = new_total
|
||||||
|
|
||||||
self.apply(new_total, player)
|
self.apply(new_total, player)
|
||||||
|
Loading…
Reference in New Issue
Block a user