Skip to content

Commit

Permalink
Copy leftover changes to chunk system from Folia
Browse files Browse the repository at this point in the history
  • Loading branch information
Spottedleaf committed Jul 28, 2024
1 parent 4995ec5 commit c2375fe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
30 changes: 15 additions & 15 deletions src/main/java/ca/spottedleaf/moonrise/common/util/TickThread.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package ca.spottedleaf.moonrise.common.util;

import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import org.slf4j.Logger;
Expand All @@ -26,21 +26,21 @@ public static void ensureTickThread(final String reason) {
}
}

public static void ensureTickThread(final ServerLevel world, final BlockPos pos, final String reason) {
public static void ensureTickThread(final Level world, final BlockPos pos, final String reason) {
if (!isTickThreadFor(world, pos)) {
LOGGER.error("Thread " + Thread.currentThread().getName() + " failed main thread check: " + reason, new Throwable());
throw new IllegalStateException(reason);
}
}

public static void ensureTickThread(final ServerLevel world, final ChunkPos pos, final String reason) {
public static void ensureTickThread(final Level world, final ChunkPos pos, final String reason) {
if (!isTickThreadFor(world, pos)) {
LOGGER.error("Thread " + Thread.currentThread().getName() + " failed main thread check: " + reason, new Throwable());
throw new IllegalStateException(reason);
}
}

public static void ensureTickThread(final ServerLevel world, final int chunkX, final int chunkZ, final String reason) {
public static void ensureTickThread(final Level world, final int chunkX, final int chunkZ, final String reason) {
if (!isTickThreadFor(world, chunkX, chunkZ)) {
LOGGER.error("Thread " + Thread.currentThread().getName() + " failed main thread check: " + reason, new Throwable());
throw new IllegalStateException(reason);
Expand All @@ -54,14 +54,14 @@ public static void ensureTickThread(final Entity entity, final String reason) {
}
}

public static void ensureTickThread(final ServerLevel world, final AABB aabb, final String reason) {
public static void ensureTickThread(final Level world, final AABB aabb, final String reason) {
if (!isTickThreadFor(world, aabb)) {
LOGGER.error("Thread " + Thread.currentThread().getName() + " failed main thread check: " + reason, new Throwable());
throw new IllegalStateException(reason);
}
}

public static void ensureTickThread(final ServerLevel world, final double blockX, final double blockZ, final String reason) {
public static void ensureTickThread(final Level world, final double blockX, final double blockZ, final String reason) {
if (!isTickThreadFor(world, blockX, blockZ)) {
LOGGER.error("Thread " + Thread.currentThread().getName() + " failed main thread check: " + reason, new Throwable());
throw new IllegalStateException(reason);
Expand Down Expand Up @@ -97,39 +97,39 @@ public static boolean isShutdownThread() {
return false;
}

public static boolean isTickThreadFor(final ServerLevel world, final BlockPos pos) {
public static boolean isTickThreadFor(final Level world, final BlockPos pos) {
return isTickThread();
}

public static boolean isTickThreadFor(final ServerLevel world, final ChunkPos pos) {
public static boolean isTickThreadFor(final Level world, final ChunkPos pos) {
return isTickThread();
}

public static boolean isTickThreadFor(final ServerLevel world, final Vec3 pos) {
public static boolean isTickThreadFor(final Level world, final Vec3 pos) {
return isTickThread();
}

public static boolean isTickThreadFor(final ServerLevel world, final int chunkX, final int chunkZ) {
public static boolean isTickThreadFor(final Level world, final int chunkX, final int chunkZ) {
return isTickThread();
}

public static boolean isTickThreadFor(final ServerLevel world, final AABB aabb) {
public static boolean isTickThreadFor(final Level world, final AABB aabb) {
return isTickThread();
}

public static boolean isTickThreadFor(final ServerLevel world, final double blockX, final double blockZ) {
public static boolean isTickThreadFor(final Level world, final double blockX, final double blockZ) {
return isTickThread();
}

public static boolean isTickThreadFor(final ServerLevel world, final Vec3 position, final Vec3 deltaMovement, final int buffer) {
public static boolean isTickThreadFor(final Level world, final Vec3 position, final Vec3 deltaMovement, final int buffer) {
return isTickThread();
}

public static boolean isTickThreadFor(final ServerLevel world, final int fromChunkX, final int fromChunkZ, final int toChunkX, final int toChunkZ) {
public static boolean isTickThreadFor(final Level world, final int fromChunkX, final int fromChunkZ, final int toChunkX, final int toChunkZ) {
return isTickThread();
}

public static boolean isTickThreadFor(final ServerLevel world, final int chunkX, final int chunkZ, final int radius) {
public static boolean isTickThreadFor(final Level world, final int chunkX, final int chunkZ, final int radius) {
return isTickThread();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,9 @@ public void lowerPriority(final int x, final int z, final PrioritisedExecutor.Pr
public void scheduleTickingState(final int chunkX, final int chunkZ, final FullChunkStatus toStatus,
final boolean addTicket, final PrioritisedExecutor.Priority priority,
final Consumer<LevelChunk> onComplete) {
if (!TickThread.isTickThread()) {
final int radius = toStatus.ordinal() - 1; // 0 -> BORDER, 1 -> TICKING, 2 -> ENTITY_TICKING

if (!TickThread.isTickThreadFor(this.world, chunkX, chunkZ, Math.max(0, radius))) {
this.scheduleChunkTask(chunkX, chunkZ, () -> {
ChunkTaskScheduler.this.scheduleTickingState(chunkX, chunkZ, toStatus, addTicket, priority, onComplete);
}, priority);
Expand All @@ -398,7 +400,7 @@ public void scheduleTickingState(final int chunkX, final int chunkZ, final FullC
this.chunkHolderManager.processTicketUpdates();
}

final Consumer<LevelChunk> loadCallback = (final LevelChunk chunk) -> {
final Consumer<LevelChunk> loadCallback = onComplete == null && !addTicket ? null : (final LevelChunk chunk) -> {
try {
if (onComplete != null) {
onComplete.accept(chunk);
Expand Down Expand Up @@ -429,7 +431,6 @@ public void scheduleTickingState(final int chunkX, final int chunkZ, final FullC
scheduled = true;
chunk = null;

final int radius = toStatus.ordinal() - 1; // 0 -> BORDER, 1 -> TICKING, 2 -> ENTITY_TICKING
for (int dz = -radius; dz <= radius; ++dz) {
for (int dx = -radius; dx <= radius; ++dx) {
final NewChunkHolder neighbour =
Expand All @@ -441,7 +442,9 @@ public void scheduleTickingState(final int chunkX, final int chunkZ, final FullC
}

// ticket level should schedule for us
chunkHolder.addFullStatusConsumer(toStatus, loadCallback);
if (loadCallback != null) {
chunkHolder.addFullStatusConsumer(toStatus, loadCallback);
}
}
}
} finally {
Expand All @@ -451,7 +454,7 @@ public void scheduleTickingState(final int chunkX, final int chunkZ, final FullC
this.chunkHolderManager.ticketLockArea.unlock(ticketLock);
}

if (!scheduled) {
if (loadCallback != null && !scheduled) {
// couldn't schedule
try {
loadCallback.accept(chunk);
Expand Down Expand Up @@ -562,7 +565,7 @@ public ChunkAccess syncLoadNonFull(final int chunkX, final int chunkZ, final Chu

public void scheduleChunkLoad(final int chunkX, final int chunkZ, final ChunkStatus toStatus, final boolean addTicket,
final PrioritisedExecutor.Priority priority, final Consumer<ChunkAccess> onComplete) {
if (!TickThread.isTickThread()) {
if (!TickThread.isTickThreadFor(this.world, chunkX, chunkZ)) {
this.scheduleChunkTask(chunkX, chunkZ, () -> {
ChunkTaskScheduler.this.scheduleChunkLoad(chunkX, chunkZ, toStatus, addTicket, priority, onComplete);
}, priority);
Expand Down Expand Up @@ -834,7 +837,7 @@ public PrioritisedExecutor.PrioritisedTask createChunkTask(final int chunkX, fin
}

public PrioritisedExecutor.PrioritisedTask scheduleChunkTask(final int chunkX, final int chunkZ, final Runnable run) {
return this.mainThreadExecutor.queueRunnable(run);
return this.scheduleChunkTask(chunkX, chunkZ, run, PrioritisedExecutor.Priority.NORMAL);
}

public PrioritisedExecutor.PrioritisedTask scheduleChunkTask(final int chunkX, final int chunkZ, final Runnable run,
Expand Down

0 comments on commit c2375fe

Please sign in to comment.