feat: implemented auto unloading chunks
This commit is contained in:
parent
e4fd8542da
commit
ae93f00397
@ -3,6 +3,8 @@ package ru.dragonestia.msb3.api.world;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
import net.minestom.server.MinecraftServer;
|
import net.minestom.server.MinecraftServer;
|
||||||
|
import net.minestom.server.event.EventListener;
|
||||||
|
import net.minestom.server.event.player.PlayerChunkUnloadEvent;
|
||||||
import net.minestom.server.instance.EntityTracker;
|
import net.minestom.server.instance.EntityTracker;
|
||||||
import net.minestom.server.instance.InstanceContainer;
|
import net.minestom.server.instance.InstanceContainer;
|
||||||
|
|
||||||
@ -12,6 +14,8 @@ public class World {
|
|||||||
|
|
||||||
private final InstanceContainer instance;
|
private final InstanceContainer instance;
|
||||||
|
|
||||||
|
private EventListener<PlayerChunkUnloadEvent> chunkUnloadListener;
|
||||||
|
|
||||||
World(InstanceContainer instance) {
|
World(InstanceContainer instance) {
|
||||||
this.instance = instance;
|
this.instance = instance;
|
||||||
}
|
}
|
||||||
@ -28,4 +32,28 @@ public class World {
|
|||||||
|
|
||||||
MinecraftServer.getInstanceManager().unregisterInstance(instance);
|
MinecraftServer.getInstanceManager().unregisterInstance(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized void setChunkUnloading(boolean unload) {
|
||||||
|
if (chunkUnloadListener == null) {
|
||||||
|
if (!unload) return;
|
||||||
|
|
||||||
|
chunkUnloadListener = EventListener.of(PlayerChunkUnloadEvent.class, event -> {
|
||||||
|
var chunk = instance.getChunk(event.getChunkX(), event.getChunkZ());
|
||||||
|
if (chunk == null) return;
|
||||||
|
|
||||||
|
if (chunk.getViewers().isEmpty()) {
|
||||||
|
try {
|
||||||
|
instance.unloadChunk(event.getChunkX(), event.getChunkZ());
|
||||||
|
} catch (NullPointerException ignored) {}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
instance.eventNode().addListener(chunkUnloadListener);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (unload) return;
|
||||||
|
|
||||||
|
instance.eventNode().removeListener(chunkUnloadListener);
|
||||||
|
chunkUnloadListener = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user