Skip to content

Commit

Permalink
Reinstate the generated chunk tracker.
Browse files Browse the repository at this point in the history
This might hopefully resolve the issue #312.
  • Loading branch information
noobanidus committed Mar 30, 2024
1 parent 9229f4c commit 265dc95
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Expand Up @@ -19,6 +19,7 @@
import noobanidus.mods.lootr.api.LootrAPI;
import noobanidus.mods.lootr.api.blockentity.ILootBlockEntity;
import noobanidus.mods.lootr.config.ConfigManager;
import noobanidus.mods.lootr.event.HandleChunk;

import java.util.Set;

Expand Down Expand Up @@ -107,14 +108,24 @@ public static void serverTick(TickEvent.ServerTickEvent event) {
continue;
}


boolean skip = false;
for (ChunkPos chunkPos : entry.getChunkPositions()) {
if (!level.getChunkSource().hasChunk(chunkPos.x, chunkPos.z)) {
skip = true;
break;
}
}
synchronized (HandleChunk.LOADED_CHUNKS) {
Set<ChunkPos> loadedChunks = HandleChunk.LOADED_CHUNKS.get(entry.dimension);
if (loadedChunks != null) {
for (ChunkPos chunkPos : entry.getChunkPositions()) {
if (!loadedChunks.contains(chunkPos)) {
skip = true;
break;
}
}
}
}
if (skip) {
continue;
}
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/noobanidus/mods/lootr/event/HandleChunk.java
@@ -0,0 +1,49 @@
package noobanidus.mods.lootr.event;

import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraftforge.event.level.ChunkEvent;
import net.minecraftforge.event.server.*;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import noobanidus.mods.lootr.api.LootrAPI;

import java.util.*;

@Mod.EventBusSubscriber(modid = LootrAPI.MODID)
public class HandleChunk {
public static final Map<ResourceKey<Level>, Set<ChunkPos>> LOADED_CHUNKS = Collections.synchronizedMap(new Object2ObjectLinkedOpenHashMap<>());

@SubscribeEvent
public static void onChunkLoad(ChunkEvent.Load event) {
if (!event.getLevel().isClientSide()) {
ChunkAccess chunk = event.getChunk();
if (chunk.getStatus().isOrAfter(ChunkStatus.FULL) && chunk instanceof LevelChunk lChunk) {
synchronized (LOADED_CHUNKS) {
Set<ChunkPos> chunkSet = LOADED_CHUNKS.computeIfAbsent(lChunk.getLevel().dimension(), k -> Collections.synchronizedSet(new ObjectLinkedOpenHashSet<>()));
chunkSet.add(chunk.getPos());
}
}
}
}

@SubscribeEvent
public static void onServerStarted(ServerAboutToStartEvent event) {
synchronized (LOADED_CHUNKS) {
LOADED_CHUNKS.clear();
}
}

@SubscribeEvent
public static void onServerStopped(ServerStoppedEvent event) {
synchronized (LOADED_CHUNKS) {
LOADED_CHUNKS.clear();
}
}
}

0 comments on commit 265dc95

Please sign in to comment.