Search in sources :

Example 1 with PacketChunkCompressedData

use of io.xol.chunkstories.net.packets.PacketChunkCompressedData in project chunkstories by Hugobros3.

the class ChunkHolderImplementation method registerUser.

@Override
public boolean registerUser(WorldUser user) {
    try {
        usersLock.lock();
        /*boolean ok = */
        users.add(user);
        // if(!ok)
        // System.out.println("warn: adding twice user to ch");
        // TODO lock
        CubicChunk chunk = this.chunk;
        // Chunk already loaded ? Compress and send it immediately
        if (user instanceof RemotePlayer) {
            RemotePlayer player = (RemotePlayer) user;
            if (chunk != null) {
                // TODO recompress chunk data each tick it's needed
                player.pushPacket(new PacketChunkCompressedData(chunk, this.getCompressedData()));
            } else {
                usersWaitingForIntialData.add(player);
            }
        }
        // This runs under a lock so we can afford to be lazy about thread safety
        if (chunk == null && loadChunkTask == null) {
            // We create a task only if one isn't already ongoing.
            loadChunkTask = getRegion().getWorld().ioHandler.requestChunkLoad(this);
        }
        return true;
    } finally {
        usersLock.unlock();
    }
}
Also used : PacketChunkCompressedData(io.xol.chunkstories.net.packets.PacketChunkCompressedData) RemotePlayer(io.xol.chunkstories.api.server.RemotePlayer)

Example 2 with PacketChunkCompressedData

use of io.xol.chunkstories.net.packets.PacketChunkCompressedData in project chunkstories by Hugobros3.

the class ChunkHolderImplementation method createChunk.

public CubicChunk createChunk(CompressedData data) {
    this.chunkLock.writeLock().lock();
    if (this.chunk != null) {
        System.out.println("Warning: creating a chunk but the chunkholder already had one, ignoring");
        this.chunkLock.writeLock().unlock();
        return this.chunk;
    }
    CubicChunk chunk;
    if (region.world instanceof WorldClient) {
        chunk = createClientChunk(this, x, y, z, data);
    // chunk = data == null ? new ClientChunk(this, x, y, z) : new ClientChunk(this, x, y, z, data);
    } else
        chunk = data == null ? new CubicChunk(this, x, y, z) : new CubicChunk(this, x, y, z, data);
    if (this.chunk == null && chunk != null)
        regionLoadedChunks.add(chunk);
    this.chunk = chunk;
    if (region.getWorld() instanceof WorldClient)
        ((WorldClient) region.getWorld()).getWorldRenderer().flagChunksModified();
    this.chunkLock.writeLock().unlock();
    // Already have clients waiting for it ? Satisfy these messieurs
    usersLock.lock();
    for (RemotePlayer user : usersWaitingForIntialData) {
        user.pushPacket(new PacketChunkCompressedData(chunk, data));
    }
    usersWaitingForIntialData.clear();
    usersLock.unlock();
    return chunk;
}
Also used : PacketChunkCompressedData(io.xol.chunkstories.net.packets.PacketChunkCompressedData) RemotePlayer(io.xol.chunkstories.api.server.RemotePlayer) WorldClient(io.xol.chunkstories.api.world.WorldClient)

Aggregations

RemotePlayer (io.xol.chunkstories.api.server.RemotePlayer)2 PacketChunkCompressedData (io.xol.chunkstories.net.packets.PacketChunkCompressedData)2 WorldClient (io.xol.chunkstories.api.world.WorldClient)1