Search in sources :

Example 21 with BaseFullChunk

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;
}
Also used : BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk)

Example 22 with BaseFullChunk

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;
}
Also used : BlockEntity(cn.nukkit.blockentity.BlockEntity) Entity(cn.nukkit.entity.Entity) Player(cn.nukkit.Player) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) IOException(java.io.IOException)

Example 23 with BaseFullChunk

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();
}
Also used : BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) FullChunk(cn.nukkit.level.format.FullChunk)

Example 24 with BaseFullChunk

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();
}
Also used : BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) AsyncTask(cn.nukkit.scheduler.AsyncTask)

Example 25 with BaseFullChunk

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;
}
Also used : BaseRegionLoader(cn.nukkit.level.format.generic.BaseRegionLoader) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) IOException(java.io.IOException)

Aggregations

BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)26 IOException (java.io.IOException)5 BlockEntity (cn.nukkit.blockentity.BlockEntity)4 FullChunk (cn.nukkit.level.format.FullChunk)4 Player (cn.nukkit.Player)3 Entity (cn.nukkit.entity.Entity)3 Level (cn.nukkit.level.Level)3 Block (cn.nukkit.block.Block)2 ItemBlock (cn.nukkit.item.ItemBlock)2 SimpleChunkManager (cn.nukkit.level.SimpleChunkManager)2 BaseRegionLoader (cn.nukkit.level.format.generic.BaseRegionLoader)2 Generator (cn.nukkit.level.generator.Generator)2 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)2 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)2 Long2ObjectMap (it.unimi.dsi.fastutil.longs.Long2ObjectMap)2 Long2ObjectOpenHashMap (it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 BlockEntitySpawnable (cn.nukkit.blockentity.BlockEntitySpawnable)1 BlockUpdateEvent (cn.nukkit.event.block.BlockUpdateEvent)1 LevelProvider (cn.nukkit.level.format.LevelProvider)1