Search in sources :

Example 1 with Key

use of net.glowstone.chunk.GlowChunk.Key in project Glowstone by GlowstoneMC.

the class GlowNoteBlock method play.

////////////////////////////////////////////////////////////////////////////
// Internals
@Override
public boolean play(Instrument instrument, Note note) {
    if (getBlock().getType() != Material.NOTE_BLOCK) {
        return false;
    }
    NotePlayEvent event = EventFactory.callEvent(new NotePlayEvent(getBlock(), instrument, note));
    if (event.isCancelled()) {
        return false;
    }
    Location location = getBlock().getLocation();
    Key key = new Key(getX() >> 4, getZ() >> 4);
    getWorld().getRawPlayers().stream().filter(player -> player.canSeeChunk(key)).forEach(player -> player.playNote(location, instrument, note));
    return true;
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) EventFactory(net.glowstone.EventFactory) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) NoteblockEntity(net.glowstone.block.entity.NoteblockEntity) BlockFace(org.bukkit.block.BlockFace) GlowBlockState(net.glowstone.block.GlowBlockState) NoteBlock(org.bukkit.block.NoteBlock) NotePlayEvent(org.bukkit.event.block.NotePlayEvent) Location(org.bukkit.Location) Note(org.bukkit.Note) Key(net.glowstone.chunk.GlowChunk.Key) Instrument(org.bukkit.Instrument) Material(org.bukkit.Material) NotePlayEvent(org.bukkit.event.block.NotePlayEvent) Key(net.glowstone.chunk.GlowChunk.Key) Location(org.bukkit.Location)

Example 2 with Key

use of net.glowstone.chunk.GlowChunk.Key in project Glowstone by GlowstoneMC.

the class ChunkManager method isChunkInUse.

/**
     * Check whether a chunk has locks on it preventing it from being unloaded.
     *
     * @param x The X coordinate.
     * @param z The Z coordinate.
     * @return Whether the chunk is in use.
     */
public boolean isChunkInUse(int x, int z) {
    Key key = new Key(x, z);
    Set<ChunkLock> lockSet = locks.get(key);
    return lockSet != null && !lockSet.isEmpty();
}
Also used : Key(net.glowstone.chunk.GlowChunk.Key)

Example 3 with Key

use of net.glowstone.chunk.GlowChunk.Key in project Glowstone by GlowstoneMC.

the class ChunkManager method getChunk.

/**
     * Gets a chunk object representing the specified coordinates, which might
     * not yet be loaded.
     *
     * @param x The X coordinate.
     * @param z The Z coordinate.
     * @return The chunk.
     */
public GlowChunk getChunk(int x, int z) {
    Key key = new Key(x, z);
    if (chunks.containsKey(key)) {
        return chunks.get(key);
    } else {
        // only create chunk if it's not in the map already
        GlowChunk chunk = new GlowChunk(world, x, z);
        chunks.put(key, chunk);
        return chunk;
    }
}
Also used : Key(net.glowstone.chunk.GlowChunk.Key)

Example 4 with Key

use of net.glowstone.chunk.GlowChunk.Key in project Glowstone by GlowstoneMC.

the class GlowPlayer method processBlockChanges.

/**
     * Process and send pending BlockChangeMessages.
     */
private void processBlockChanges() {
    synchronized (blockChanges) {
        List<BlockChangeMessage> messages = new ArrayList<>(blockChanges);
        blockChanges.clear();
        // separate messages by chunk
        // inner map is used to only send one entry for same coordinates
        Map<Key, Map<BlockVector, BlockChangeMessage>> chunks = new HashMap<>();
        for (BlockChangeMessage message : messages) {
            if (message != null) {
                Key key = new Key(message.getX() >> 4, message.getZ() >> 4);
                if (canSeeChunk(key)) {
                    Map<BlockVector, BlockChangeMessage> map = chunks.computeIfAbsent(key, k -> new HashMap<>());
                    map.put(new BlockVector(message.getX(), message.getY(), message.getZ()), message);
                }
            }
        }
        // send away
        for (Map.Entry<Key, Map<BlockVector, BlockChangeMessage>> entry : chunks.entrySet()) {
            Key key = entry.getKey();
            List<BlockChangeMessage> value = new ArrayList<>(entry.getValue().values());
            if (value.size() == 1) {
                session.send(value.get(0));
            } else if (value.size() > 1) {
                session.send(new MultiBlockChangeMessage(key.getX(), key.getZ(), value));
            }
        }
        // now send post-block-change messages
        List<Message> postMessages = new ArrayList<>(afterBlockChanges);
        afterBlockChanges.clear();
        postMessages.forEach(session::send);
    }
}
Also used : UseBedMessage(net.glowstone.net.message.play.player.UseBedMessage) Message(com.flowpowered.network.Message) PlayerAbilitiesMessage(net.glowstone.net.message.play.player.PlayerAbilitiesMessage) TextMessage(net.glowstone.util.TextMessage) ResourcePackSendMessage(net.glowstone.net.message.play.player.ResourcePackSendMessage) BlockVector(org.bukkit.util.BlockVector) StatisticMap(net.glowstone.util.StatisticMap) MetadataMap(net.glowstone.entity.meta.MetadataMap) Key(net.glowstone.chunk.GlowChunk.Key)

Example 5 with Key

use of net.glowstone.chunk.GlowChunk.Key in project Glowstone by GlowstoneMC.

the class GlowPlayer method streamBlocks.

/**
     * Streams chunks to the player's client.
     */
private void streamBlocks() {
    Set<Key> previousChunks = new HashSet<>(knownChunks);
    ArrayList<Key> newChunks = new ArrayList<>();
    int centralX = location.getBlockX() >> 4;
    int centralZ = location.getBlockZ() >> 4;
    int radius = Math.min(server.getViewDistance(), 1 + settings.getViewDistance());
    for (int x = centralX - radius; x <= centralX + radius; x++) {
        for (int z = centralZ - radius; z <= centralZ + radius; z++) {
            Key key = new Key(x, z);
            if (knownChunks.contains(key)) {
                previousChunks.remove(key);
            } else {
                newChunks.add(key);
            }
        }
    }
    // early end if there's no changes
    if (newChunks.isEmpty() && previousChunks.isEmpty()) {
        return;
    }
    // sort chunks by distance from player - closer chunks sent first
    newChunks.sort((a, b) -> {
        double dx = 16 * a.getX() + 8 - location.getX();
        double dz = 16 * a.getZ() + 8 - location.getZ();
        double da = dx * dx + dz * dz;
        dx = 16 * b.getX() + 8 - location.getX();
        dz = 16 * b.getZ() + 8 - location.getZ();
        double db = dx * dx + dz * dz;
        return Double.compare(da, db);
    });
    // first step: force population then acquire lock on each chunk
    for (Key key : newChunks) {
        world.getChunkManager().forcePopulation(key.getX(), key.getZ());
        knownChunks.add(key);
        chunkLock.acquire(key);
    }
    boolean skylight = world.getEnvironment() == Environment.NORMAL;
    for (Key key : newChunks) {
        GlowChunk chunk = world.getChunkAt(key.getX(), key.getZ());
        session.send(chunk.toMessage(skylight));
    }
    // send visible block entity data
    for (Key key : newChunks) {
        GlowChunk chunk = world.getChunkAt(key.getX(), key.getZ());
        for (BlockEntity entity : chunk.getRawBlockEntities()) {
            entity.update(this);
        }
    }
    // and remove old chunks
    for (Key key : previousChunks) {
        session.send(new UnloadChunkMessage(key.getX(), key.getZ()));
        knownChunks.remove(key);
        chunkLock.release(key);
    }
    previousChunks.clear();
}
Also used : Key(net.glowstone.chunk.GlowChunk.Key) GlowChunk(net.glowstone.chunk.GlowChunk) GlowBlockEntity(net.glowstone.constants.GlowBlockEntity) BlockEntity(net.glowstone.block.entity.BlockEntity)

Aggregations

Key (net.glowstone.chunk.GlowChunk.Key)15 GlowBlock (net.glowstone.block.GlowBlock)4 GlowChunk (net.glowstone.chunk.GlowChunk)4 Message (com.flowpowered.network.Message)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)3 IOException (java.io.IOException)3 BlockEntity (net.glowstone.block.entity.BlockEntity)3 GlowBlockEntity (net.glowstone.constants.GlowBlockEntity)3 MetadataMap (net.glowstone.entity.meta.MetadataMap)3 CompoundTag (net.glowstone.util.nbt.CompoundTag)3 Title (com.destroystokyo.paper.Title)2 ByteBufUtils (com.flowpowered.network.util.ByteBufUtils)2 Preconditions (com.google.common.base.Preconditions)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 ByteBuf (io.netty.buffer.ByteBuf)2 Unpooled (io.netty.buffer.Unpooled)2 InetSocketAddress (java.net.InetSocketAddress)2 StandardCharsets (java.nio.charset.StandardCharsets)2 java.util (java.util)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2