From 14678af2ed71c844f60eb8aba179a32992aba50e Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Thu, 17 Aug 2017 18:14:49 +0200 Subject: [PATCH] Portals: Fix rapid back-and-forth teleportation --- mods/ITEMS/mcl_portals/portal_end.lua | 22 ++++++++++++++++++++-- mods/ITEMS/mcl_portals/portal_nether.lua | 20 +++++++++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/mods/ITEMS/mcl_portals/portal_end.lua b/mods/ITEMS/mcl_portals/portal_end.lua index 44f0e1436d..d21c3ac9be 100644 --- a/mods/ITEMS/mcl_portals/portal_end.lua +++ b/mods/ITEMS/mcl_portals/portal_end.lua @@ -12,10 +12,14 @@ local np_cave = { octaves = 5, persist = 0.7 } - -- Portal frame material local portal_frame = "mcl_nether:quartz_block" +-- Table of objects (including players) which recently teleported by a +-- End portal. Those objects have a brief cooloff period before they +-- can teleport again. This prevents annoying back-and-forth teleportation. +local portal_cooloff = {} + -- Destroy portal if pos (portal frame or portal node) got destroyed local destroy_portal = function(pos) -- Deactivate Nether portal @@ -326,6 +330,10 @@ minetest.register_abm({ for _,obj in ipairs(minetest.get_objects_inside_radius(pos,1)) do --maikerumine added for objects to travel local lua_entity = obj:get_luaentity() --maikerumine added for objects to travel if obj:is_player() or lua_entity then + -- No rapid back-and-forth teleportatio + if portal_cooloff[obj] then + return + end local meta = minetest.get_meta(pos) local target3 = minetest.string_to_pos(meta:get_string("portal_target")) if target3 then @@ -336,8 +344,12 @@ minetest.register_abm({ vector.subtract(target3, 4), vector.add(target3, 4)) end - -- teleport the player + -- teleport the object minetest.after(3, function(obj, pos, target3) + -- No rapid back-and-forth teleportatio + if portal_cooloff[obj] then + return + end local objpos = obj:getpos() if objpos == nil then return @@ -366,6 +378,12 @@ minetest.register_abm({ obj:setpos(target3) minetest.sound_play("mcl_portals_teleport", {pos=target3, gain=0.5, max_hear_distance = 16}) + -- Enable teleportation cooloff to prevent frequent back-and-forth teleportation + portal_cooloff[obj] = true + minetest.after(3, function(o) + portal_cooloff[o] = false + end, obj) + end, obj, pos, target3) end end diff --git a/mods/ITEMS/mcl_portals/portal_nether.lua b/mods/ITEMS/mcl_portals/portal_nether.lua index 221af24aca..02430b2312 100644 --- a/mods/ITEMS/mcl_portals/portal_nether.lua +++ b/mods/ITEMS/mcl_portals/portal_nether.lua @@ -19,6 +19,11 @@ local np_cave = { persist = 0.7 } +-- Table of objects (including players) which recently teleported by a +-- Nether portal. Those objects have a brief cooloff period before they +-- can teleport again. This prevents annoying back-and-forth teleportation. +local portal_cooloff = {} + -- Destroy portal if pos (portal frame or portal node) got destroyed local destroy_portal = function(pos) -- Deactivate Nether portal @@ -357,6 +362,10 @@ minetest.register_abm({ for _,obj in ipairs(minetest.get_objects_inside_radius(pos,1)) do --maikerumine added for objects to travel local lua_entity = obj:get_luaentity() --maikerumine added for objects to travel if obj:is_player() or lua_entity then + -- Prevent quick back-and-forth teleportation + if portal_cooloff[obj] then + return + end local meta = minetest.get_meta(pos) local target = minetest.string_to_pos(meta:get_string("portal_target")) if target then @@ -366,8 +375,12 @@ minetest.register_abm({ minetest.emerge_area( vector.subtract(target, 4), vector.add(target, 4)) end - -- teleport the player + -- teleport the object minetest.after(3, function(obj, pos, target) + -- Prevent quick back-and-forth teleportation + if portal_cooloff[obj] then + return + end local objpos = obj:getpos() if objpos == nil then return @@ -397,6 +410,11 @@ minetest.register_abm({ obj:setpos(target) minetest.sound_play("mcl_portals_teleport", {pos=target, gain=0.5, max_hear_distance = 16}) + -- Enable teleportation cooloff for 4 seconds, to prevent back-and-forth teleportation + portal_cooloff[obj] = true + minetest.after(4, function(o) + portal_cooloff[o] = false + end, obj) end, obj, pos, target) end