Skip to content

Commit

Permalink
fix: Move lighting to after chunk loading completes
Browse files Browse the repository at this point in the history
This fixes lighting, as before it was most likely lighting `IoWorldChunks`.
  • Loading branch information
Steveplays28 committed Jul 31, 2024
1 parent 1eb68ea commit 561b4c4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ protected ServerWorldMixin(MutableWorldProperties properties, RegistryKey<World>
this::noisiumchunkmanager$getServerWorldLightingProvider,
noisiumchunkmanager$getServerWorldLightingProvider()::initializeLight,
noisiumchunkmanager$getServerWorldLightingProvider()::light,
(chunkPosition) -> {
if (noisiumchunkmanager$serverWorldTicketTracker == null) {
return false;
}

return noisiumchunkmanager$serverWorldTicketTracker.hasTicketAtPosition(chunkPosition);
},
session.getWorldDirectory(worldKey), dataFixer
);
noisiumchunkmanager$serverWorldTicketTracker = new ServerWorldTicketTracker(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class ServerWorldChunkManager {
private final Supplier<LightingProvider> lightingProviderSupplier;
private final BiFunction<Chunk, Boolean, CompletableFuture<Chunk>> initializeChunkLightingBiFunction;
private final BiFunction<Chunk, Boolean, CompletableFuture<Chunk>> lightChunkBiFunction;
private final Function<ChunkPos, Boolean> hasTicketAtPositionSupplier;
private final PersistentStateManager persistentStateManager;
private final PointOfInterestStorage pointOfInterestStorage;
private final VersionedChunkStorage versionedChunkStorage;
Expand All @@ -74,14 +75,15 @@ public class ServerWorldChunkManager {
private boolean isStopping;

@SuppressWarnings("ResultOfMethodCallIgnored")
public ServerWorldChunkManager(@NotNull ServerWorld serverWorld, @NotNull ChunkGenerator chunkGenerator, @NotNull NoiseConfig noiseConfig, @NotNull Consumer<Runnable> syncRunnableConsumer, @NotNull Supplier<LightingProvider> lightingProviderSupplier, @NotNull BiFunction<Chunk, Boolean, CompletableFuture<Chunk>> initializeChunkLightingBiFunction, @NotNull BiFunction<Chunk, Boolean, CompletableFuture<Chunk>> lightChunkBiFunction, @NotNull Path worldDirectoryPath, @NotNull DataFixer dataFixer) {
public ServerWorldChunkManager(@NotNull ServerWorld serverWorld, @NotNull ChunkGenerator chunkGenerator, @NotNull NoiseConfig noiseConfig, @NotNull Consumer<Runnable> syncRunnableConsumer, @NotNull Supplier<LightingProvider> lightingProviderSupplier, @NotNull BiFunction<Chunk, Boolean, CompletableFuture<Chunk>> initializeChunkLightingBiFunction, @NotNull BiFunction<Chunk, Boolean, CompletableFuture<Chunk>> lightChunkBiFunction, @NotNull Function<ChunkPos, Boolean> hasTicketAtPositionSupplier, @NotNull Path worldDirectoryPath, @NotNull DataFixer dataFixer) {
this.serverWorld = serverWorld;
this.chunkGenerator = chunkGenerator;
this.noiseConfig = noiseConfig;
this.syncRunnableConsumer = syncRunnableConsumer;
this.lightingProviderSupplier = lightingProviderSupplier;
this.initializeChunkLightingBiFunction = initializeChunkLightingBiFunction;
this.lightChunkBiFunction = lightChunkBiFunction;
this.hasTicketAtPositionSupplier = hasTicketAtPositionSupplier;

var worldDataFile = worldDirectoryPath.resolve("data").toFile();
worldDataFile.mkdirs();
Expand Down Expand Up @@ -173,7 +175,7 @@ public ServerWorldChunkManager(@NotNull ServerWorld serverWorld, @NotNull ChunkG
syncRunnableConsumer.accept(
() -> ServerChunkEvent.WORLD_CHUNK_LOADED.invoker().onWorldChunkLoaded(serverWorld, fetchedWorldChunk));

if (unloadingWorldChunks.contains(chunkPos)) {
if (unloadingWorldChunks.contains(chunkPos) && !hasTicketAtPositionSupplier.apply(chunkPos)) {
loadedWorldChunks.remove(chunkPos);
unloadingWorldChunks.remove(chunkPos);
syncRunnableConsumer.accept(
Expand Down Expand Up @@ -217,7 +219,7 @@ public ServerWorldChunkManager(@NotNull ServerWorld serverWorld, @NotNull ChunkG
syncRunnableConsumer.accept(
() -> ServerChunkEvent.WORLD_CHUNK_LOADED.invoker().onWorldChunkLoaded(serverWorld, fetchedWorldChunk));

if (unloadingWorldChunks.contains(chunkPos)) {
if (unloadingWorldChunks.contains(chunkPos) && !hasTicketAtPositionSupplier.apply(chunkPos)) {
loadedWorldChunks.remove(chunkPos);
unloadingWorldChunks.remove(chunkPos);
syncRunnableConsumer.accept(
Expand All @@ -238,7 +240,7 @@ public ServerWorldChunkManager(@NotNull ServerWorld serverWorld, @NotNull ChunkG
syncRunnableConsumer.accept(
() -> ServerChunkEvent.WORLD_CHUNK_LOADED.invoker().onWorldChunkLoaded(serverWorld, fetchedWorldChunk));

if (unloadingWorldChunks.contains(chunkPos)) {
if (unloadingWorldChunks.contains(chunkPos) && !hasTicketAtPositionSupplier.apply(chunkPos)) {
loadedWorldChunks.remove(chunkPos);
unloadingWorldChunks.remove(chunkPos);
syncRunnableConsumer.accept(
Expand Down Expand Up @@ -450,13 +452,6 @@ private void onBlockChange(@NotNull BlockPos blockPos, @NotNull BlockState oldBl

ioWorldChunkRemoveFunction.apply(chunkPos);

protoChunk.setStatus(ChunkStatus.INITIALIZE_LIGHT);
protoChunk.refreshSurfaceY();
initializeChunkLightingBiConsumer.apply(protoChunk, protoChunk.isLightOn()).join();

protoChunk.setStatus(ChunkStatus.LIGHT);
lightChunkBiConsumer.apply(protoChunk, protoChunk.isLightOn()).join();

protoChunk.setStatus(ChunkStatus.SPAWN);
chunkGenerator.populateEntities(chunkRegion);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import dev.architectury.event.events.common.TickEvent;
import io.github.steveplays28.noisiumchunkmanager.NoisiumChunkManager;
import io.github.steveplays28.noisiumchunkmanager.config.NoisiumChunkManagerConfig;
import io.github.steveplays28.noisiumchunkmanager.extension.world.chunk.WorldChunkExtension;
import io.github.steveplays28.noisiumchunkmanager.server.event.world.chunk.ServerChunkEvent;
import io.github.steveplays28.noisiumchunkmanager.server.extension.world.ServerWorldExtension;
import io.github.steveplays28.noisiumchunkmanager.util.world.chunk.ChunkUtil;
import net.minecraft.server.world.ChunkTicketType;
import net.minecraft.server.world.ServerLightingProvider;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
Expand All @@ -16,6 +18,7 @@
import net.minecraft.world.LightType;
import net.minecraft.world.chunk.*;
import net.minecraft.world.chunk.light.*;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -58,6 +61,22 @@ public void onLightUpdate(@NotNull LightType lightType, @NotNull ChunkSectionPos
"Noisium Server World Lighting Provider " + serverWorld.getDimension().effects() + " %d").build()
);

ServerChunkEvent.WORLD_CHUNK_LOADED.register((instance, worldChunk) -> {
if (!worldChunk.isLightOn()) {
@NotNull var worldChunkPosition = worldChunk.getPos();
instance.getChunkManager().addTicket(ChunkTicketType.LIGHT, worldChunkPosition, 1, worldChunkPosition);
initializeLight(worldChunk, false).thenCompose(chunkWithInitializedLighting ->
light(chunkWithInitializedLighting, false)).whenComplete((litChunk, throwable) -> {
if (throwable != null) {
NoisiumChunkManager.LOGGER.error(
"Exception thrown while lighting a chunk asynchronously:\n{}", ExceptionUtils.getStackTrace(throwable));
return;
}

instance.getChunkManager().removeTicket(ChunkTicketType.LIGHT, worldChunkPosition, 1, worldChunkPosition);
});
}
});
ServerChunkEvent.LIGHT_UPDATE.register((instance, lightType, chunkSectionPosition) -> {
if (instance != serverWorld) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import net.minecraft.util.math.ChunkPos;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

Expand All @@ -23,7 +23,7 @@ public ServerWorldTicketTracker(@NotNull ServerWorld serverWorld, @NotNull BiCon
this.loadChunksInRadiusBiConsumer = loadChunksInRadiusBiConsumer;
this.unloadChunkConsumer = unloadChunkConsumer;

this.tickets = new HashMap<>();
this.tickets = new ConcurrentHashMap<>();

ServerWorldTicketEvent.TICKET_CREATED.register((ticketServerWorld, ticketType, chunkPosition, radius) -> {
if (ticketServerWorld != serverWorld) {
Expand All @@ -48,6 +48,10 @@ public ServerWorldTicketTracker(@NotNull ServerWorld serverWorld, @NotNull BiCon
});
}

public boolean hasTicketAtPosition(@NotNull ChunkPos chunkPosition) {
return this.tickets.containsKey(chunkPosition);
}

private void onTicketCreated(@NotNull ServerWorld serverWorld, @NotNull ChunkTicketType<?> ticketType, @NotNull ChunkPos chunkPosition, int radius) {
if (tickets.containsKey(chunkPosition)) {
return;
Expand Down

0 comments on commit 561b4c4

Please sign in to comment.