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
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
3 changed files with 24 additions and 13 deletions

View file

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

View file

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

View file

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