Skip to content

Commit

Permalink
Fixed chests rendering double with sodium
Browse files Browse the repository at this point in the history
  • Loading branch information
StewStrong committed Nov 12, 2023
1 parent 7155e68 commit edc9712
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@
import java.util.function.Consumer;
import net.minecraft.client.multiplayer.ClientChunkCache;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.SectionPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.game.ClientboundLevelChunkPacketData.BlockEntityTagOutput;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.LevelChunkSection;
import net.minecraft.world.level.lighting.LevelLightEngine;
import org.spongepowered.asm.mixin.Final;
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 org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
Expand All @@ -43,11 +41,12 @@ public abstract class MixinClientChunkCache implements ClientChunkCacheDuck {
@Final
ClientLevel level;

public LongObjectMap<LevelChunk> vs_getShipChunks() {
return shipChunks;
public LongObjectMap<LevelChunk> vs$getShipChunks() {
return vs$shipChunks;
}

private final LongObjectMap<LevelChunk> shipChunks = new LongObjectHashMap<>();
@Unique
private final LongObjectMap<LevelChunk> vs$shipChunks = new LongObjectHashMap<>();

@Inject(method = "replaceWithPacketData", at = @At("HEAD"), cancellable = true)
private void preLoadChunkFromPacket(final int x, final int z,
Expand All @@ -60,33 +59,27 @@ private void preLoadChunkFromPacket(final int x, final int z,
if (VSGameUtilsKt.isChunkInShipyard(level, x, z)) {
final long chunkPosLong = ChunkPos.asLong(x, z);

final LevelChunk worldChunk = new LevelChunk(this.level, new ChunkPos(x, z));
worldChunk.replaceWithPacketData(buf, tag, consumer);
shipChunks.put(chunkPosLong, worldChunk);

final LevelChunkSection[] chunkSections = worldChunk.getSections();
final LevelLightEngine lightingProvider = this.getLightEngine();
lightingProvider.enableLightSources(new ChunkPos(x, z), true);

for (int j = 0; j < chunkSections.length; ++j) {
final LevelChunkSection chunkSection = chunkSections[j];
lightingProvider
.updateSectionStatus(SectionPos.of(x, level.getSectionYFromSectionIndex(j), z),
chunkSection.hasOnlyAir());
final LevelChunk oldChunk = vs$shipChunks.get(chunkPosLong);
final LevelChunk worldChunk;
if (oldChunk != null) {
worldChunk = oldChunk;
oldChunk.replaceWithPacketData(buf, tag, consumer);
} else {
worldChunk = new LevelChunk(this.level, new ChunkPos(x, z));
worldChunk.replaceWithPacketData(buf, tag, consumer);
vs$shipChunks.put(chunkPosLong, worldChunk);
}

this.level.onChunkLoaded(new ChunkPos(x, z));

SodiumCompat.onChunkAdded(x, z);

cir.setReturnValue(worldChunk);
}
}
}

@Inject(method = "drop", at = @At("HEAD"), cancellable = true)
public void preUnload(final int chunkX, final int chunkZ, final CallbackInfo ci) {
shipChunks.remove(ChunkPos.asLong(chunkX, chunkZ));
vs$shipChunks.remove(ChunkPos.asLong(chunkX, chunkZ));
if (ValkyrienCommonMixinConfigPlugin.getVSRenderer() != VSRenderer.SODIUM) {
((IVSViewAreaMethods) ((LevelRendererAccessor) ((ClientLevelAccessor) level).getLevelRenderer()).getViewArea())
.unloadChunk(chunkX, chunkZ);
Expand All @@ -100,12 +93,9 @@ public void preUnload(final int chunkX, final int chunkZ, final CallbackInfo ci)
at = @At("HEAD"), cancellable = true)
public void preGetChunk(final int chunkX, final int chunkZ, final ChunkStatus chunkStatus, final boolean bl,
final CallbackInfoReturnable<LevelChunk> cir) {
final LevelChunk shipChunk = shipChunks.get(ChunkPos.asLong(chunkX, chunkZ));
final LevelChunk shipChunk = vs$shipChunks.get(ChunkPos.asLong(chunkX, chunkZ));
if (shipChunk != null) {
cir.setReturnValue(shipChunk);
}
}

@Shadow
public abstract LevelLightEngine getLightEngine();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.instancing.InstanceManager;
import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry;
import com.jozufozu.flywheel.backend.instancing.RenderDispatcher;
import com.jozufozu.flywheel.backend.instancing.batching.BatchingEngine;
import com.jozufozu.flywheel.backend.instancing.blockentity.BlockEntityInstanceManager;
import com.jozufozu.flywheel.backend.instancing.instancing.InstancingEngine;
Expand Down Expand Up @@ -70,7 +71,10 @@ void preCreateRaw(final BlockEntity blockEntity, final CallbackInfoReturnable<In

@Override
public void vs$removeShipManager(final ClientShip clientShip) {
vs$shipMaterialManagers.remove(clientShip);
final MaterialManager removed = vs$shipMaterialManagers.remove(clientShip);
if (removed instanceof final RenderDispatcher removedRenderer) {
removedRenderer.delete();
}
}

@Unique
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public abstract class MixinLevelRenderer {
)
)
private void afterRefresh(final CallbackInfo ci) {
((ClientChunkCacheDuck) this.level.getChunkSource()).vs_getShipChunks().forEach((pos, chunk) -> {
((ClientChunkCacheDuck) this.level.getChunkSource()).vs$getShipChunks().forEach((pos, chunk) -> {
for (int y = level.getMinSection(); y < level.getMaxSection(); y++) {
viewArea.setDirty(ChunkPos.getX(pos), y, ChunkPos.getZ(pos), false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import net.minecraft.world.level.chunk.LevelChunk;

public interface ClientChunkCacheDuck {
LongObjectMap<LevelChunk> vs_getShipChunks();
LongObjectMap<LevelChunk> vs$getShipChunks();
}

0 comments on commit edc9712

Please sign in to comment.