Skip to content

Commit

Permalink
Fix dynamic loading of worlds
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorinwasher committed Sep 22, 2024
1 parent f23874d commit a6dd633
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,9 @@ public interface RealPortal extends Portal, MetadataHolder {
* @param target <p>The entity to teleport</p>
*/
void doTeleport(@NotNull Entity target);

/**
* @return The UUID of the world this portal is in
*/
UUID getWorldUuid();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,14 +19,19 @@ public WorldEventListener(StargateAPI stargateAPI) {

@EventHandler
void onWordUnload(WorldUnloadEvent event) {
stargateAPI.getRegistry().getAllPortals().filter(portal -> portal instanceof RealPortal)
List<RealPortal> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class StargatePortal implements RealPortal {
public static final Set<PortalFlag> 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;
Expand Down Expand Up @@ -113,6 +114,7 @@ public StargatePortal(Network network, String name, Set<PortalFlag> 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)) {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public void doTeleport(Entity target) {

}

@Override
public UUID getWorldUuid() {
return null;
}

@Override
public void close(boolean forceClose) {

Expand Down

0 comments on commit a6dd633

Please sign in to comment.