use of cn.nukkit.level.format.generic.BaseFullChunk in project Nukkit by Nukkit.
the class Level method populateChunk.
public boolean populateChunk(int x, int z, boolean force) {
long index = Level.chunkHash(x, z);
if (this.chunkPopulationQueue.containsKey(index) || this.chunkPopulationQueue.size() >= this.chunkPopulationQueueSize && !force) {
return false;
}
BaseFullChunk chunk = this.getChunk(x, z, true);
boolean populate;
if (!chunk.isPopulated()) {
Timings.populationTimer.startTiming();
populate = true;
for (int xx = -1; xx <= 1; ++xx) {
for (int zz = -1; zz <= 1; ++zz) {
if (this.chunkPopulationLock.containsKey(Level.chunkHash(x + xx, z + zz))) {
populate = false;
break;
}
}
}
if (populate) {
if (!this.chunkPopulationQueue.containsKey(index)) {
this.chunkPopulationQueue.put(index, Boolean.TRUE);
for (int xx = -1; xx <= 1; ++xx) {
for (int zz = -1; zz <= 1; ++zz) {
this.chunkPopulationLock.put(Level.chunkHash(x + xx, z + zz), Boolean.TRUE);
}
}
PopulationTask task = new PopulationTask(this, chunk);
this.server.getScheduler().scheduleAsyncTask(task);
}
}
Timings.populationTimer.stopTiming();
return false;
}
return true;
}
use of cn.nukkit.level.format.generic.BaseFullChunk in project Nukkit by Nukkit.
the class Level method unloadChunk.
public boolean unloadChunk(int x, int z, boolean safe, boolean trySave) {
if (safe && this.isChunkInUse(x, z)) {
return false;
}
if (!this.isChunkLoaded(x, z)) {
return true;
}
this.timings.doChunkUnload.startTiming();
long index = Level.chunkHash(x, z);
BaseFullChunk chunk = this.getChunk(x, z);
if (chunk != null && chunk.getProvider() != null) {
ChunkUnloadEvent ev = new ChunkUnloadEvent(chunk);
this.server.getPluginManager().callEvent(ev);
if (ev.isCancelled()) {
this.timings.doChunkUnload.stopTiming();
return false;
}
}
try {
if (chunk != null) {
if (trySave && this.getAutoSave()) {
int entities = 0;
for (Entity e : chunk.getEntities().values()) {
if (e instanceof Player) {
continue;
}
++entities;
}
if (chunk.hasChanged() || !chunk.getBlockEntities().isEmpty() || entities > 0) {
this.provider.setChunk(x, z, chunk);
this.provider.saveChunk(x, z);
}
}
for (ChunkLoader loader : this.getChunkLoaders(x, z)) {
loader.onChunkUnloaded(chunk);
}
}
this.provider.unloadChunk(x, z, safe);
} catch (Exception e) {
MainLogger logger = this.server.getLogger();
logger.error(this.server.getLanguage().translateString("nukkit.level.chunkUnloadError", e.toString()));
logger.logException(e);
}
this.timings.doChunkUnload.stopTiming();
return true;
}
use of cn.nukkit.level.format.generic.BaseFullChunk in project Nukkit by Nukkit.
the class Level method generateChunkCallback.
public void generateChunkCallback(int x, int z, BaseFullChunk chunk) {
Timings.generationCallbackTimer.startTiming();
long index = Level.chunkHash(x, z);
if (this.chunkPopulationQueue.containsKey(index)) {
FullChunk oldChunk = this.getChunk(x, z, false);
for (int xx = -1; xx <= 1; ++xx) {
for (int zz = -1; zz <= 1; ++zz) {
this.chunkPopulationLock.remove(Level.chunkHash(x + xx, z + zz));
}
}
this.chunkPopulationQueue.remove(index);
chunk.setProvider(this.provider);
this.setChunk(x, z, chunk, false);
chunk = this.getChunk(x, z, false);
if (chunk != null && (oldChunk == null || !oldChunk.isPopulated()) && chunk.isPopulated() && chunk.getProvider() != null) {
this.server.getPluginManager().callEvent(new ChunkPopulateEvent(chunk));
for (ChunkLoader loader : this.getChunkLoaders(x, z)) {
loader.onChunkPopulated(chunk);
}
}
} else if (this.chunkGenerationQueue.containsKey(index) || this.chunkPopulationLock.containsKey(index)) {
this.chunkGenerationQueue.remove(index);
this.chunkPopulationLock.remove(index);
chunk.setProvider(this.provider);
this.setChunk(x, z, chunk, false);
} else {
chunk.setProvider(this.provider);
this.setChunk(x, z, chunk, false);
}
Timings.generationCallbackTimer.stopTiming();
}
use of cn.nukkit.level.format.generic.BaseFullChunk in project Nukkit by Nukkit.
the class Level method processChunkRequest.
private void processChunkRequest() {
this.timings.syncChunkSendTimer.startTiming();
for (Long index : ImmutableList.copyOf(this.chunkSendQueue.keySet())) {
if (this.chunkSendTasks.containsKey(index)) {
continue;
}
int x = getHashX(index);
int z = getHashZ(index);
this.chunkSendTasks.put(index, Boolean.TRUE);
BaseFullChunk chunk = getChunk(x, z);
if (chunk != null) {
BatchPacket packet = chunk.getChunkPacket();
if (packet != null) {
this.sendChunk(x, z, index, packet);
continue;
}
}
this.timings.syncChunkSendPrepareTimer.startTiming();
AsyncTask task = this.provider.requestChunkTask(x, z);
if (task != null) {
this.server.getScheduler().scheduleAsyncTask(task);
}
this.timings.syncChunkSendPrepareTimer.stopTiming();
}
this.timings.syncChunkSendTimer.stopTiming();
}
use of cn.nukkit.level.format.generic.BaseFullChunk in project Nukkit by Nukkit.
the class McRegion method loadChunk.
@Override
public BaseFullChunk loadChunk(long index, int chunkX, int chunkZ, boolean create) {
int regionX = getRegionIndexX(chunkX);
int regionZ = getRegionIndexZ(chunkZ);
BaseRegionLoader region = this.loadRegion(regionX, regionZ);
this.level.timings.syncChunkLoadDataTimer.startTiming();
BaseFullChunk chunk;
try {
chunk = region.readChunk(chunkX - regionX * 32, chunkZ - regionZ * 32);
} catch (IOException e) {
throw new RuntimeException(e);
}
if (chunk == null) {
if (create) {
chunk = this.getEmptyChunk(chunkX, chunkZ);
putChunk(index, chunk);
}
} else {
putChunk(index, chunk);
}
this.level.timings.syncChunkLoadDataTimer.stopTiming();
return chunk;
}
Aggregations