Skip to content

Commit

Permalink
refactor: Make ServerWorldLightingProvider extend `ServerLightingPr…
Browse files Browse the repository at this point in the history
…ovider`
  • Loading branch information
Steveplays28 committed Jul 28, 2024
1 parent f4f257f commit 0bbbd4b
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ public abstract class ServerChunkManagerMixin {
private StructurePlacementCalculator noisiumchunkmanager$structurePlacementCalculator;

@Inject(method = "<init>", at = @At(value = "TAIL"))
private void noisiumchunkmanager$constructorInject(ServerWorld world, LevelStorage.Session session, DataFixer dataFixer, StructureTemplateManager structureTemplateManager, Executor workerExecutor, @NotNull ChunkGenerator chunkGenerator, int viewDistance, int simulationDistance, boolean dsync, WorldGenerationProgressListener worldGenerationProgressListener, ChunkStatusChangeListener chunkStatusChangeListener, Supplier<PersistentStateManager> persistentStateManagerFactory, CallbackInfo ci) {
private void noisiumchunkmanager$constructorInject(@NotNull ServerWorld serverWorld, LevelStorage.Session session, DataFixer dataFixer, StructureTemplateManager structureTemplateManager, Executor workerExecutor, @NotNull ChunkGenerator chunkGenerator, int viewDistance, int simulationDistance, boolean dsync, WorldGenerationProgressListener worldGenerationProgressListener, ChunkStatusChangeListener chunkStatusChangeListener, Supplier<PersistentStateManager> persistentStateManagerFactory, CallbackInfo ci) {
noisiumchunkmanager$chunkGenerator = chunkGenerator;
noisiumchunkmanager$structurePlacementCalculator = this.getChunkGenerator().createStructurePlacementCalculator(
this.getWorld().getRegistryManager().getWrapperOrThrow(RegistryKeys.STRUCTURE_SET), this.getNoiseConfig(),
((ServerWorld) this.getWorld()).getSeed()
);
this.lightingProvider = null;
this.lightingProvider = (ServerLightingProvider) serverWorld.getLightingProvider();
this.threadedAnvilChunkStorage = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,29 @@
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.network.packet.Packet;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.WorldGenerationProgressListener;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ChunkLevelType;
import net.minecraft.server.world.ServerChunkManager;
import net.minecraft.server.world.ServerEntityManager;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.random.RandomSequencesState;
import net.minecraft.util.profiler.Profiler;
import net.minecraft.world.LightType;
import net.minecraft.world.MutableWorldProperties;
import net.minecraft.world.PersistentStateManager;
import net.minecraft.world.World;
import net.minecraft.world.chunk.WorldChunk;
import net.minecraft.world.chunk.light.LightingProvider;
import net.minecraft.world.dimension.DimensionOptions;
import net.minecraft.world.dimension.DimensionType;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.chunk.ChunkGeneratorSettings;
import net.minecraft.world.gen.chunk.NoiseChunkGenerator;
Expand All @@ -50,10 +56,17 @@
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Supplier;

@Debug(export = true)
@Mixin(ServerWorld.class)
public abstract class ServerWorldMixin implements ServerWorldExtension {
public abstract class ServerWorldMixin extends World implements ServerWorldExtension {
protected ServerWorldMixin(MutableWorldProperties properties, RegistryKey<World> registryRef, DynamicRegistryManager registryManager, RegistryEntry<DimensionType> dimensionEntry, Supplier<Profiler> profiler, boolean isClient, boolean debugWorld, long biomeAccess, int maxChainedNeighborUpdates) {
super(
properties, registryRef, registryManager, dimensionEntry, profiler, isClient, debugWorld, biomeAccess,
maxChainedNeighborUpdates
);
}

@Shadow
@Final
private ServerEntityManager<Entity> entityManager;
Expand All @@ -70,22 +83,25 @@ public abstract class ServerWorldMixin implements ServerWorldExtension {
@Shadow
public abstract @NotNull List<ServerPlayerEntity> getPlayers();

@Shadow
public abstract ServerChunkManager getChunkManager();

/**
* Keeps a reference to this {@link ServerWorld}'s {@link NoiseConfig}, to make sure it doesn't get garbage collected until the object is no longer necessary.
*/
@Unique
private NoiseConfig noisiumchunkmanager$noiseConfig;
/**
* Keeps a reference to this {@link ServerWorld}'s {@link ServerWorldChunkManager}, to make sure it doesn't get garbage collected until the object is no longer necessary.
*/
@Unique
private ServerWorldChunkManager noisiumchunkmanager$serverWorldChunkManager;
/**
* Keeps a reference to this {@link ServerWorld}'s {@link ServerWorldLightingProvider}, to make sure it doesn't get garbage collected until the object is no longer necessary.
*/
@SuppressWarnings("unused")
@Unique
private ServerWorldLightingProvider noisiumchunkmanager$serverWorldLightingProvider;
/**
* Keeps a reference to this {@link ServerWorld}'s {@link ServerWorldChunkManager}, to make sure it doesn't get garbage collected until the object is no longer necessary.
*/
@Unique
private ServerWorldChunkManager noisiumchunkmanager$serverWorldChunkManager;
/**
* Keeps a reference to this {@link ServerWorld}'s {@link ServerWorldTicketTracker}, to make sure it doesn't get garbage collected until the object is no longer necessary.
*/
Expand All @@ -111,6 +127,7 @@ public abstract class ServerWorldMixin implements ServerWorldExtension {
@Unique
private ServerWorldPlayerChunkLoader noisiumchunkmanager$serverWorldPlayerChunkLoader;

@SuppressWarnings("resource")
@Inject(method = "<init>", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/server/MinecraftServer;getDataFixer()Lcom/mojang/datafixers/DataFixer;", shift = At.Shift.AFTER))
private void noisiumchunkmanager$constructorCreateServerWorldChunkManager(@NotNull MinecraftServer server, Executor workerExecutor, @NotNull LevelStorage.Session session, @NotNull ServerWorldProperties serverWorldProperties, @NotNull RegistryKey<World> worldKey, @NotNull DimensionOptions dimensionOptions, WorldGenerationProgressListener worldGenerationProgressListener, boolean debugWorld, long seed, List<?> spawners, boolean shouldTickTime, RandomSequencesState randomSequencesState, @NotNull CallbackInfo ci, @Local @NotNull DataFixer dataFixer) {
@SuppressWarnings("DataFlowIssue")
Expand Down Expand Up @@ -142,8 +159,8 @@ public abstract class ServerWorldMixin implements ServerWorldExtension {
noisiumchunkmanager$serverWorldEntityManager = new ServerWorldEntityTracker(
packet -> PacketUtil.sendPacketToPlayers(serverWorld.getPlayers(), packet));
noisiumchunkmanager$serverWorldPlayerChunkLoader = new ServerWorldPlayerChunkLoader(
serverWorld, () -> noisiumchunkmanager$serverWorldLightingProvider.getChunkLightingView(LightType.SKY),
() -> noisiumchunkmanager$serverWorldLightingProvider.getChunkLightingView(LightType.BLOCK),
serverWorld, () -> noisiumchunkmanager$getServerWorldLightingProvider().get(LightType.SKY),
() -> noisiumchunkmanager$getServerWorldLightingProvider().get(LightType.BLOCK),
noisiumchunkmanager$serverWorldChunkManager::getChunksInRadiusAsync,
noisiumchunkmanager$serverWorldChunkManager::getChunkAsync,
noisiumchunkmanager$serverWorldChunkManager::unloadChunk, server.getPlayerManager()::getViewDistance
Expand Down Expand Up @@ -219,6 +236,11 @@ public abstract class ServerWorldMixin implements ServerWorldExtension {
ci.cancel();
}

@Override
public LightingProvider getLightingProvider() {
return noisiumchunkmanager$getServerWorldLightingProvider();
}

@Override
public ServerWorldChunkManager noisiumchunkmanager$getServerWorldChunkManager() {
return noisiumchunkmanager$serverWorldChunkManager;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.ChunkStatus;
import net.minecraft.world.chunk.light.LightingProvider;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -21,15 +20,6 @@ public abstract class WorldMixin {
@Shadow
public abstract boolean isClient();

@Inject(method = "getLightingProvider", at = @At(value = "RETURN"), cancellable = true)
private void noisiumchunkmanager$getServerWorldLightingProvider(@NotNull CallbackInfoReturnable<LightingProvider> cir) {
if (this.isClient()) {
return;
}

cir.setReturnValue(((ServerWorldExtension) this).noisiumchunkmanager$getServerWorldLightingProvider());
}

@Inject(method = "getChunk(IILnet/minecraft/world/chunk/ChunkStatus;Z)Lnet/minecraft/world/chunk/Chunk;", at = @At(value = "HEAD"), cancellable = true)
private void noisiumchunkmanager$getChunkFromNoisiumServerChunkManager(int chunkPositionX, int chunkPositionZ, @NotNull ChunkStatus leastStatus, boolean create, @NotNull CallbackInfoReturnable<Chunk> cir) {
if (this.isClient()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
package io.github.steveplays28.noisiumchunkmanager.mixin.world.chunk;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import io.github.steveplays28.noisiumchunkmanager.extension.world.chunk.WorldChunkExtension;
import io.github.steveplays28.noisiumchunkmanager.server.extension.world.ServerWorldExtension;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.WorldChunk;
import net.minecraft.world.chunk.light.LightingProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;

import java.util.BitSet;

Expand All @@ -37,25 +29,4 @@ public abstract class WorldChunkMixin implements WorldChunkExtension {
public @NotNull BitSet noisiumchunkmanager$getSkyLightBits() {
return noisiumchunkmanager$skyLightBits;
}

@WrapOperation(method = "setBlockState", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/chunk/light/LightingProvider;setSectionStatus(Lnet/minecraft/util/math/BlockPos;Z)V"))
private void noisiumchunkmanager$redirectSetSectionStatusToServerWorldLightingProvider(@Nullable LightingProvider instance, @NotNull BlockPos blockPosition, boolean notReady, @NotNull Operation<Void> original) {
if (getWorld() instanceof ServerWorld serverWorld) {
((ServerWorldExtension) serverWorld).noisiumchunkmanager$getServerWorldLightingProvider().setSectionStatus(
blockPosition, notReady);
return;
}

original.call(instance, blockPosition, notReady);
}

@WrapOperation(method = "setBlockState", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/chunk/light/LightingProvider;checkBlock(Lnet/minecraft/util/math/BlockPos;)V"))
private void noisiumchunkmanager$redirectCheckBlockToServerWorldLightingProvider(@Nullable LightingProvider instance, @NotNull BlockPos blockPosition, @NotNull Operation<Void> original) {
if (getWorld() instanceof ServerWorld serverWorld) {
((ServerWorldExtension) serverWorld).noisiumchunkmanager$getServerWorldLightingProvider().checkBlock(blockPosition);
return;
}

original.call(instance, blockPosition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@

/**
* A lighting provider for {@link ServerWorld}s.
* This class cannot extend {@link ServerLightingProvider} due to the superclass being hard to change, causing {@link ServerWorldLightingProvider} to require a lot of workarounds to function.
*/
@SuppressWarnings("DataFlowIssue")
public class ServerWorldLightingProvider extends LightingProvider {
public class ServerWorldLightingProvider extends ServerLightingProvider {
private final @NotNull ServerWorld serverWorld;
private final @NotNull Executor threadPoolExecutor;

Expand All @@ -48,7 +47,7 @@ public ServerWorldLightingProvider(@NotNull ServerWorld serverWorld) {
public void onLightUpdate(@NotNull LightType lightType, @NotNull ChunkSectionPos chunkSectionPosition) {
ServerChunkEvent.LIGHT_UPDATE.invoker().onLightUpdate(serverWorld, lightType, chunkSectionPosition);
}
}, true, true);
}, null, true, null, null);
this.serverWorld = serverWorld;

this.threadPoolExecutor = Executors.newFixedThreadPool(
Expand Down Expand Up @@ -127,7 +126,8 @@ public void enqueueSectionData(@NotNull LightType lightType, @NotNull ChunkSecti
}

@SuppressWarnings("DuplicateBranchesInSwitch")
public @NotNull ChunkLightProvider<?, ?> getChunkLightingView(@NotNull LightType lightType) {
@Override
public @NotNull ChunkLightProvider<?, ?> get(@NotNull LightType lightType) {
@NotNull ChunkLightProvider<?, ?> chunkLightProvider;
switch (lightType) {
case BLOCK -> chunkLightProvider = blockLightProvider;
Expand All @@ -137,6 +137,7 @@ public void enqueueSectionData(@NotNull LightType lightType, @NotNull ChunkSecti
return chunkLightProvider;
}

@Override
public @NotNull CompletableFuture<Chunk> initializeLight(@NotNull Chunk chunk, boolean retainLightingData) {
return CompletableFuture.supplyAsync(() -> {
@NotNull var chunkPosition = chunk.getPos();
Expand All @@ -149,9 +150,9 @@ public void enqueueSectionData(@NotNull LightType lightType, @NotNull ChunkSecti

@NotNull var chunkSectionPosition = ChunkSectionPos.from(chunkPosition, serverWorld.sectionIndexToCoord(i));
enqueueSectionData(
LightType.SKY, chunkSectionPosition, getChunkLightingView(LightType.SKY).getLightSection(chunkSectionPosition));
LightType.SKY, chunkSectionPosition, get(LightType.SKY).getLightSection(chunkSectionPosition));
enqueueSectionData(
LightType.BLOCK, chunkSectionPosition, getChunkLightingView(LightType.BLOCK).getLightSection(chunkSectionPosition));
LightType.BLOCK, chunkSectionPosition, get(LightType.BLOCK).getLightSection(chunkSectionPosition));
setSectionStatus(chunkSectionPosition, false);
}

Expand All @@ -161,6 +162,7 @@ public void enqueueSectionData(@NotNull LightType lightType, @NotNull ChunkSecti
}, threadPoolExecutor);
}

@Override
public @NotNull CompletableFuture<Chunk> light(@NotNull Chunk chunk, boolean excludeBlocks) {
return CompletableFuture.supplyAsync(() -> {
@NotNull ChunkPos chunkPos = chunk.getPos();
Expand Down Expand Up @@ -211,7 +213,8 @@ private void onLightUpdateAsync(@NotNull LightType lightType, @NotNull ChunkSect
}, threadPoolExecutor);
}

private void tick() {
@Override
public void tick() {
doLightUpdatesAsync();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"server.world.ThreadedAnvilChunkStorageMixin",
"world.ChunkRegionMixin",
"world.ChunkSectionCacheMixin",
"world.ChunkSerializerMixin",
"world.WorldMixin",
"world.chunk.ChunkToNibbleArrayMapMixin",
"world.chunk.PalettedContainerMixin",
Expand Down

0 comments on commit 0bbbd4b

Please sign in to comment.