Skip to content

Commit

Permalink
feat: find suitable pos in virtual thread
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Dec 1, 2024
1 parent af6db8b commit 5a186c9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
8 changes: 7 additions & 1 deletion api/src/main/java/org/allaymc/api/world/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.scheduler.Scheduler;
import org.allaymc.api.scheduler.TaskCreator;
import org.cloudburstmc.protocol.bedrock.packet.BedrockPacket;
import org.jetbrains.annotations.UnmodifiableView;

Expand All @@ -14,7 +15,7 @@
*
* @author daoge_cmd
*/
public interface World {
public interface World extends TaskCreator {
/**
* Get the thread which the world is running on.
*
Expand Down Expand Up @@ -167,4 +168,9 @@ default void broadcastPacket(BedrockPacket packet) {
* Set the weather to {@link Weather#CLEAR}.
*/
void clearWeather();

@Override
default boolean isValid() {
return isRunning();
}
}
23 changes: 15 additions & 8 deletions server/src/main/java/org/allaymc/server/world/AllayWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,25 @@ protected void checkFirstTick() {
}
isFirstTick = false;

var overworld = getOverWorld();
if (Server.SETTINGS.worldSettings().loadSpawnPointChunks()) {
// Add spawn point chunk loader
getOverWorld().getChunkService().addChunkLoader(new SpawnPointChunkLoader());
overworld.getChunkService().addChunkLoader(new SpawnPointChunkLoader());
}

if (!isSafeStandingPos(new Position3i(worldData.getSpawnPoint(), getOverWorld()))) {
var newSpawnPoint = getOverWorld().findSuitableGroundPosAround(this::isSafeStandingPos, 0, 0, 32);
if (newSpawnPoint == null) {
log.warn("Cannot find a safe spawn point in the overworld dimension of world {}", worldData.getName());
newSpawnPoint = new Vector3i(0, getOverWorld().getHeight(0, 0) + 1, 0);
}
worldData.setSpawnPoint(newSpawnPoint);
if (!isSafeStandingPos(new Position3i(worldData.getSpawnPoint(), overworld))) {
Thread.ofVirtual().name("Spawn Point Finding Thread - " + worldData.getName()).start(() -> {
var newSpawnPoint = overworld.findSuitableGroundPosAround(this::isSafeStandingPos, 0, 0, 32);
if (newSpawnPoint == null) {
log.warn("Cannot find a safe spawn point in the overworld dimension of world {}", worldData.getName());
newSpawnPoint = new Vector3i(0, overworld.getHeight(0, 0) + 1, 0);
}
var finalNewSpawnPoint = newSpawnPoint;
overworld.getWorld().getScheduler().runLater(this, () -> {
// Set new spawn point in world thread as world data object is not thread-safe
worldData.setSpawnPoint(finalNewSpawnPoint);
});
});
}
}

Expand Down

0 comments on commit 5a186c9

Please sign in to comment.