From b47c16b646917e04bb2978c4bef6212ddb551685 Mon Sep 17 00:00:00 2001 From: Fireblade <3+fireblade@noreply.neolegacy.dev> Date: Sun, 28 Jun 2026 13:16:27 -0400 Subject: [PATCH] fix: persisting portal linkages --- Minecraft.World/PortalForcer.cpp | 77 +++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/Minecraft.World/PortalForcer.cpp b/Minecraft.World/PortalForcer.cpp index cf6956c9..9c23fb75 100644 --- a/Minecraft.World/PortalForcer.cpp +++ b/Minecraft.World/PortalForcer.cpp @@ -101,14 +101,77 @@ bool PortalForcer::findPortal(shared_ptr e, double xOriginal, double yOr { PortalPosition *pos = it->second; - closest = 0; - xTarget = pos->x; - yTarget = pos->y; - zTarget = pos->z; - pos->lastUsed = level->getGameTime(); - updateCache = false; + if (level->getTile(pos->x, pos->y, pos->z) == Tile::portal_Id) + { + closest = 0; + xTarget = pos->x; + yTarget = pos->y; + zTarget = pos->z; + pos->lastUsed = level->getGameTime(); + updateCache = false; + } + else + { + delete pos; + cachedPortals.erase(it); + for (auto keyIt = cachedPortalKeys.begin(); keyIt != cachedPortalKeys.end(); ++keyIt) + { + if (*keyIt == hash) + { + cachedPortalKeys.erase(keyIt); + break; + } + } + } } - else + + if (updateCache) + { + const int localRadius = 16; + double localClosest = -1; + int localX = 0; + int localY = 0; + int localZ = 0; + + for (int x = xc - localRadius; x <= xc + localRadius; x++) + { + double xd = (x + 0.5) - e->x; + for (int z = zc - localRadius; z <= zc + localRadius; z++) + { + double zd = (z + 0.5) - e->z; + for (int y = level->getHeight() - 1; y >= 0; y--) + { + if (level->getTile(x, y, z) == Tile::portal_Id) + { + while (level->getTile(x, y - 1, z) == Tile::portal_Id) + { + y--; + } + + double yd = (y + 0.5) - e->y; + double dist = xd * xd + yd * yd + zd * zd; + if (localClosest < 0 || dist < localClosest) + { + localClosest = dist; + localX = x; + localY = y; + localZ = z; + } + } + } + } + } + + if (localClosest >= 0) + { + closest = localClosest; + xTarget = localX; + yTarget = localY; + zTarget = localZ; + } + } + + if (closest < 0) { for (int x = xc - r; x <= xc + r; x++) {