core pos: Add (complicated) compat w/worldedit

This commit is contained in:
Starbeamrainbowlabs 2022-09-25 01:43:30 +01:00
parent 285572e16b
commit f998110303
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
3 changed files with 60 additions and 17 deletions

View file

@ -29,7 +29,13 @@ end
-- @returns void -- @returns void
local function compat_worldedit_pos1_get(player_name) local function compat_worldedit_pos1_get(player_name)
if worldedit and worldedit.pos1 and worldedit.pos1[player_name] then if worldedit and worldedit.pos1 and worldedit.pos1[player_name] then
positions[player_name][1] = Vector3.clone(worldedit.pos1[player_name]) ensure_player(player_name)
local new_pos1 = Vector3.clone(worldedit.pos1[player_name])
local existing_pos1 = positions[player_name][1]
positions[player_name][1] = new_pos1
if new_pos1 ~= existing_pos1 then
anchor:emit("set", { player_name = player_name, i = 1, pos = new_pos1 })
end
end end
end end
--- Transparently fetches from worldedit pos2 for compatibility. --- Transparently fetches from worldedit pos2 for compatibility.
@ -39,7 +45,13 @@ end
-- @returns void -- @returns void
local function compat_worldedit_pos2_get(player_name) local function compat_worldedit_pos2_get(player_name)
if worldedit and worldedit.pos2 and worldedit.pos2[player_name] then if worldedit and worldedit.pos2 and worldedit.pos2[player_name] then
positions[player_name][2] = Vector3.clone(worldedit.pos2[player_name]) ensure_player(player_name)
local new_pos2 = Vector3.clone(worldedit.pos2[player_name])
local existing_pos2 = positions[player_name][2]
positions[player_name][2] = new_pos2
if new_pos2 ~= existing_pos2 then
anchor:emit("set", { player_name = player_name, i = 2, pos = new_pos2 })
end
end end
end end
@ -48,23 +60,33 @@ end
-- @param i number The index of the position to set. -- @param i number The index of the position to set.
-- @returns Vector3? The position to set. -- @returns Vector3? The position to set.
local function compat_worldedit_set(player_name, i, pos) local function compat_worldedit_set(player_name, i, pos)
print("DEBUG:compat_worldedit_set i", i, "pos", pos)
if not worldedit then return end if not worldedit then return end
if i == 1 and worldedit.pos1 then if i == 1 and worldedit.pos1 then
worldedit.pos1[player_name] = nil
if worldedit.marker_update then worldedit.marker_update(player_name) end
worldedit.pos1[player_name] = pos:clone() worldedit.pos1[player_name] = pos:clone()
if worldedit.mark_pos1 then worldedit.mark_pos1(player_name) end
elseif i == 2 and worldedit.pos2 then elseif i == 2 and worldedit.pos2 then
worldedit.pos2[player_name] = nil
if worldedit.marker_update then worldedit.marker_update(player_name) end
worldedit.pos2[player_name] = pos:clone() worldedit.pos2[player_name] = pos:clone()
if worldedit.mark_pos2 then worldedit.mark_pos2(player_name) end
end end
end end
--- Fetches pos1/pos2 from WorldEdit (if available) and sets them in WorldEditAdditions' postional subsystem
-- @param player_name string The name of the player to sync the positions for.
local function compat_worldedit_get(player_name)
compat_worldedit_pos1_get(player_name)
compat_worldedit_pos2_get(player_name)
end
--- Gets the position with the given index for the given player. --- Gets the position with the given index for the given player.
-- @param player_name string The name of the player to fetch the position for. -- @param player_name string The name of the player to fetch the position for.
-- @param i number The index of the position to fetch. -- @param i number The index of the position to fetch.
-- @returns Vector3? The position requested, or nil if it doesn't exist. -- @returns Vector3? The position requested, or nil if it doesn't exist.
local function get_pos(player_name, i) local function get_pos(player_name, i)
ensure_player(player_name) ensure_player(player_name)
if i == 1 then compat_worldedit_pos1_get(player_name) if i == 2 then compat_worldedit_pos1_get(player_name)
elseif i == 2 then compat_worldedit_pos2_get(player_name) end elseif i == 2 then compat_worldedit_pos2_get(player_name) end
return positions[player_name][i] return positions[player_name][i]
@ -201,7 +223,8 @@ 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)
compat_worldedit_set(player_name, #positions, pos) print("DEBUG poslist", wea_c.inspect(positions[player_name]))
compat_worldedit_set(player_name, #positions[player_name], pos)
anchor:emit("push", { player_name = player_name, pos = pos, i = #positions[player_name] }) anchor:emit("push", { player_name = player_name, pos = pos, i = #positions[player_name] })
return #positions[player_name] return #positions[player_name]
@ -221,7 +244,8 @@ anchor = wea_c.EventEmitter.new({
set = set_pos, set = set_pos,
set_pos1 = set_pos1, set_pos1 = set_pos1,
set_pos2 = set_pos2, set_pos2 = set_pos2,
set_all = set_pos_all set_all = set_pos_all,
compat_worldedit_get = compat_worldedit_get
}) })
return anchor return anchor

View file

@ -2,6 +2,20 @@ local wea_c = worldeditadditions_core
local position_entities = {} local position_entities = {}
local function compat_worldedit_hide_pos1(player_name)
if not worldedit or not worldedit.pos1 or not worldedit.mark_pos1 then return end
local pos1 = worldedit.pos1[player_name]
worldedit.pos1[player_name] = nil
worldedit.mark_pos1(player_name)
worldedit.pos1[player_name] = nil
end
local function compat_worldedit_hide_pos2(player_name)
if not worldedit or not worldedit.pos2 or not worldedit.mark_pos2 then return end
local pos2 = worldedit.pos2[player_name]
worldedit.pos2[player_name] = nil
worldedit.mark_pos2(player_name)
worldedit.pos2[player_name] = nil
end
--- Ensures that a table exists for the given player. --- Ensures that a table exists for the given player.
-- @param player_name string The name of the player to check. -- @param player_name string The name of the player to check.
@ -18,6 +32,9 @@ end
local function do_create(event) local function do_create(event)
ensure_player(event.player_name) ensure_player(event.player_name)
if event.i == 1 then compat_worldedit_hide_pos1(event.player_name) end
if event.i == 2 then compat_worldedit_hide_pos2(event.player_name) end
local new_entity = wea_c.entities.pos_marker.create( local new_entity = wea_c.entities.pos_marker.create(
event.player_name, event.player_name,
event.pos, event.pos,

View file

@ -32,25 +32,27 @@ minetest.register_tool(":worldeditadditions:multiwand", {
on_place = function(itemstack, player, pointed_thing) on_place = function(itemstack, player, pointed_thing)
-- Right click when pointing at something -- Right click when pointing at something
-- Pointed thing: https://rubenwardy.com/minetest_modding_book/lua_api.html#pointed_thing -- Pointed thing: https://rubenwardy.com/minetest_modding_book/lua_api.html#pointed_thing
-- print("[farwand] on_place", name) local player_name = player:get_player_name()
local name = player:get_player_name() wea_c.pos.compat_worldedit_get(player_name)
pop_pos(name) -- print("[farwand] on_place", player_name)
pop_pos(player_name)
end, end,
on_use = function(itemstack, player, pointed_thing) on_use = function(itemstack, player, pointed_thing)
-- Left click when pointing at something or nothing -- Left click when pointing at something or nothing
local name = player:get_player_name() local player_name = player:get_player_name()
-- print("[farwand] on_use", name) wea_c.pos.compat_worldedit_get(player_name)
-- print("[farwand] on_use", player_name)
local looking_pos, node_id = wea.farwand.do_raycast(player) local looking_pos, node_id = wea.farwand.do_raycast(player)
push_pos(name, looking_pos) push_pos(player_name, looking_pos)
end, end,
on_secondary_use = function(itemstack, player, pointed_thing) on_secondary_use = function(itemstack, player, pointed_thing)
-- Right click when pointing at nothing -- Right click when pointing at nothing
-- print("[farwand] on_secondary_use", name) local player_name = player:get_player_name()
local name = player:get_player_name() -- print("[farwand] on_secondary_use", player_name)
wea_c.pos.compat_worldedit_get(player_name)
-- local looking_pos, node_id = do_raycast(player) -- local looking_pos, node_id = do_raycast(player)
pop_pos(name) pop_pos(player_name)
end end
}) })