diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/RealPortal.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/RealPortal.java
index 1ecc5bf3..ed9bc4ff 100644
--- a/src/main/java/org/sgrewritten/stargate/api/network/portal/RealPortal.java
+++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/RealPortal.java
@@ -137,4 +137,9 @@ public interface RealPortal extends Portal, MetadataHolder {
* @param target
The entity to teleport
*/
void doTeleport(@NotNull Entity target);
+
+ /**
+ * @return The UUID of the world this portal is in
+ */
+ UUID getWorldUuid();
}
diff --git a/src/main/java/org/sgrewritten/stargate/listener/WorldEventListener.java b/src/main/java/org/sgrewritten/stargate/listener/WorldEventListener.java
index c7c73031..d1f56fe2 100644
--- a/src/main/java/org/sgrewritten/stargate/listener/WorldEventListener.java
+++ b/src/main/java/org/sgrewritten/stargate/listener/WorldEventListener.java
@@ -7,6 +7,8 @@
import org.sgrewritten.stargate.api.StargateAPI;
import org.sgrewritten.stargate.api.network.portal.RealPortal;
+import java.util.List;
+
public class WorldEventListener implements Listener {
private final StargateAPI stargateAPI;
@@ -17,14 +19,19 @@ public WorldEventListener(StargateAPI stargateAPI) {
@EventHandler
void onWordUnload(WorldUnloadEvent event) {
- stargateAPI.getRegistry().getAllPortals().filter(portal -> portal instanceof RealPortal)
+ List unloadedPortals = stargateAPI.getRegistry().getAllPortals()
+ .filter(portal -> portal instanceof RealPortal)
.map(portal -> (RealPortal) portal)
- .filter(realPortal -> realPortal.getExit().getWorld().equals(event.getWorld()))
- .forEach(stargateAPI.getRegistry()::unregisterPortal);
+ .filter(realPortal -> realPortal.getWorldUuid().equals(event.getWorld().getUID()))
+ .toList();
+ unloadedPortals.forEach(stargateAPI.getRegistry()::unregisterPortal);
+ unloadedPortals.forEach(portal -> portal.getNetwork().removePortal(portal));
+ stargateAPI.getRegistry().updateAllPortals();
}
@EventHandler
void onWorldLoad(WorldLoadEvent event) {
stargateAPI.getNetworkManager().loadWorld(event.getWorld(), stargateAPI);
+ stargateAPI.getRegistry().updateAllPortals();
}
}
diff --git a/src/main/java/org/sgrewritten/stargate/network/portal/StargatePortal.java b/src/main/java/org/sgrewritten/stargate/network/portal/StargatePortal.java
index efdd6930..bb4ce87c 100644
--- a/src/main/java/org/sgrewritten/stargate/network/portal/StargatePortal.java
+++ b/src/main/java/org/sgrewritten/stargate/network/portal/StargatePortal.java
@@ -71,6 +71,7 @@ public class StargatePortal implements RealPortal {
public static final Set allUsedFlags = new HashSet<>();
private static final int OPEN_DELAY = 20 * 20; // ticks
+ private final UUID worldUuid;
protected Network network;
protected String name;
protected UUID openFor;
@@ -113,6 +114,7 @@ public StargatePortal(Network network, String name, Set flags, GateA
this.languageManager = Objects.requireNonNull(languageManager);
this.economyManager = Objects.requireNonNull(economyManager);
this.metaData = metaData;
+ this.worldUuid = gate.getExit().getWorld().getUID();
name = NameHelper.getTrimmedName(name);
if (NameHelper.isInvalidName(name)) {
@@ -325,6 +327,11 @@ public void doTeleport(@NotNull Entity target) {
this.doTeleport(target, destination);
}
+ @Override
+ public UUID getWorldUuid() {
+ return this.worldUuid;
+ }
+
@Override
public String getName() {
return name;
diff --git a/src/test/java/org/sgrewritten/stargate/util/portal/PortalMock.java b/src/test/java/org/sgrewritten/stargate/util/portal/PortalMock.java
index acda6bc7..abd699c3 100644
--- a/src/test/java/org/sgrewritten/stargate/util/portal/PortalMock.java
+++ b/src/test/java/org/sgrewritten/stargate/util/portal/PortalMock.java
@@ -48,6 +48,11 @@ public void doTeleport(Entity target) {
}
+ @Override
+ public UUID getWorldUuid() {
+ return null;
+ }
+
@Override
public void close(boolean forceClose) {