From 363872c760ff9de178397add29f1631888803d5b Mon Sep 17 00:00:00 2001 From: Treer Date: Sun, 12 Jan 2020 15:46:37 +1100 Subject: [PATCH] Avoid storing the playerRef See https://rubenwardy.com/minetest_modding_book/en/quality/common_mistakes.html#never-store-objectrefs-ie-players-or-entities Related: https://github.com/minetest-mods/nether/pull/12 --- portal_api.lua | 20 +++++++++++--------- portal_api.txt | 3 ++- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/portal_api.lua b/portal_api.lua index 9aa9f87..0f9ebf2 100644 --- a/portal_api.lua +++ b/portal_api.lua @@ -1167,13 +1167,14 @@ local function ignite_portal(ignition_pos, ignition_node_name) end -- invoked when a player is standing in a portal -local function ensure_remote_portal_then_teleport(player, portal_definition, local_anchorPos, local_orientation, destination_wormholePos) +local function ensure_remote_portal_then_teleport(playerName, portal_definition, local_anchorPos, local_orientation, destination_wormholePos) + + local player = minetest.get_player_by_name(playerName) + if player == nil then return end -- player quit the game while teleporting + local playerPos = player:get_pos() + if playerPos == nil then return end -- player quit the game while teleporting -- check player is still standing in a portal - local playerPos = player:get_pos() - if playerPos == nil then - return -- player quit the game while teleporting - end playerPos.y = playerPos.y + 0.1 -- Fix some glitches at -8000 if minetest.get_node(playerPos).name ~= portal_definition.wormhole_node_name then return -- the player has moved out of the portal @@ -1193,7 +1194,7 @@ local function ensure_remote_portal_then_teleport(player, portal_definition, loc if dest_wormhole_node == nil then -- area not emerged yet, delay and retry if DEBUG then minetest.chat_send_all("ensure_remote_portal_then_teleport() could not find anything yet at " .. minetest.pos_to_string(destination_wormholePos)) end - minetest.after(1, ensure_remote_portal_then_teleport, player, portal_definition, local_anchorPos, local_orientation, destination_wormholePos) + minetest.after(1, ensure_remote_portal_then_teleport, playerName, portal_definition, local_anchorPos, local_orientation, destination_wormholePos) else local local_wormholePos = portal_definition.shape.get_wormholePos_from_anchorPos(local_anchorPos, local_orientation) @@ -1225,7 +1226,7 @@ local function ensure_remote_portal_then_teleport(player, portal_definition, loc -- play the teleport sound if portal_definition.sounds.teleport ~= nil then - minetest.sound_play(portal_definition.sounds.teleport, {to_player = player.name}) + minetest.sound_play(portal_definition.sounds.teleport, {to_player = playerName}) end -- rotate the player if the destination portal is a different orientation @@ -1265,7 +1266,7 @@ local function ensure_remote_portal_then_teleport(player, portal_definition, loc destination_wormholePos ) end - minetest.after(0.1, ensure_remote_portal_then_teleport, player, portal_definition, local_anchorPos, local_orientation, destination_wormholePos) + minetest.after(0.1, ensure_remote_portal_then_teleport, playerName, portal_definition, local_anchorPos, local_orientation, destination_wormholePos) end end end @@ -1315,11 +1316,12 @@ function run_wormhole(timerPos, time_elapsed) end local local_anchorPos, local_orientation = portal_definition.shape.get_anchorPos_and_orientation_from_p1_and_p2(local_p1, local_p2) + local playerName = obj:get_player_name() minetest.after( 3, -- hopefully target area is emerged in 3 seconds function() ensure_remote_portal_then_teleport( - obj, + playerName, portal_definition, local_anchorPos, local_orientation, diff --git a/portal_api.txt b/portal_api.txt index eb8f1af..4991281 100644 --- a/portal_api.txt +++ b/portal_api.txt @@ -16,7 +16,8 @@ Optionally decorate by choosing portal colors, particles, media etc. See `init.lua` and `portal_examples.lua` for examples of 3 different portals. Portal code is more efficient when each type of portal uses a different type -of node to build its frame out of, however it is possible to register more than +of node to build its frame out of - consider creating your own node for +players to build portals from. However it is possible to register more than one kind of portal with the same frame material — such as obsidian — provided the size of the PortalShape is distinct from any other type of portal that is using the same node for its frame, and portal sizes remain small.