Add EventEmitter support to wea_c.pos

This commit is contained in:
Starbeamrainbowlabs 2022-09-18 16:06:23 +01:00
parent 817387d7f2
commit 17d92870b3
No known key found for this signature in database
GPG Key ID: 1BE5172E637709C2
3 changed files with 24 additions and 13 deletions

@ -6,12 +6,11 @@ EventEmitter.__index = EventEmitter
EventEmitter.__name = "EventEmitter" -- A hack to allow identification in wea.inspect
function EventEmitter.new()
local result = {
events = {}
}
setmetatable(result, EventEmitter)
return result
function EventEmitter.new(tbl)
if not tbl then tbl = {} end
tbl.events = {}
setmetatable(tbl, EventEmitter)
return tbl
end
function EventEmitter.addEventListener(this, event_name, func)

@ -2,6 +2,7 @@ local wea_c = worldeditadditions_core
local positions_count_limit = 999
local positions = {}
local anchor = nil
local function ensure_player(player_name)
if player_name == nil then
@ -36,28 +37,37 @@ local function set_pos(player_name, i, pos)
if i > positions_count_limit then return false end
ensure_player(player_name)
positions[player_name][i] = pos
anchor:emit("set", { i = i, pos = pos })
return true
end
local function set_pos_all(player_name, i, pos_list)
if #pos_list > positions_count_limit then return false end
positions[player_name] = pos_list
for _i,pos_new in ipairs(positions[player_name]) do
anchor:emit("push", { pos = pos_new })
end
end
local function clear(player_name)
if positions[player_name] then
positions[player_name] = nil
end
anchor:emit("clear")
end
local function pop_pos(player_name)
ensure_player(player_name)
if #positions[player_name] <= 0 then return nil end
return table.remove(positions[player_name])
local last_pos = table.remove(positions[player_name])
anchor:emit("pop", { pos = last_pos })
return last_pos
end
local function push_pos(player_name, pos)
ensure_player(player_name)
table.insert(positions[player_name], pos)
anchor:emit("push", { pos = pos })
end
return {
anchor = wea_c.EventEmitter.new({
get = get_pos,
get1 = get_pos1,
get2 = get_pos2,
@ -67,5 +77,7 @@ return {
pop = pop_pos,
push = push_pos,
set = set_pos,
set_all = set_pos_all,
}
set_all = set_pos_all
})
return anchor

@ -21,13 +21,13 @@ worldeditadditions_core = {
-- The default limit for new players on the number of potential nodes changed before safe_region kicks in.
safe_region_limit_default = 100000,
}
local wea_c = worldeditadditions_core
wea_c.EventEmitter = dofile(modpath.."/core/lib/EventEmitter.lua")
wea_c.pos = dofile(modpath.."/core/pos.lua")
wea_c.register_command = dofile(modpath.."/core/register_command.lua")
wea_c.fetch_command_def = dofile(modpath.."/core/fetch_command_def.lua")
wea_c.register_alias = dofile(modpath.."/core/register_alias.lua")
wea_c.pos = dofile(modpath.."/core/pos.lua")
wea_c.EventEmitter = dofile(modpath.."/core/lib/EventEmitter.lua")
print("WEA_C pos", wea_c.pos.push)
-- Initialise WorldEdit stuff if the WorldEdit mod is not present