Search in sources :

Example 6 with BlockChangeMessage

use of net.glowstone.net.message.play.game.BlockChangeMessage in project Glowstone by GlowstoneMC.

the class GlowPlayer method processPersonalBlockChanges.

/**
 * Process and send packets sent specifically to us.
 */
private void processPersonalBlockChanges() {
    if (blockChanges.isEmpty()) {
        return;
    }
    // separate messages by chunk
    // inner map is used to only send one entry for same coordinates
    Map<Key, Map<BlockVector, BlockChangeMessage>> chunks = new HashMap<>();
    BlockChangeMessage message;
    while ((message = blockChanges.poll()) != null) {
        Key key = GlowChunk.Key.of(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));
        }
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MultiBlockChangeMessage(net.glowstone.net.message.play.game.MultiBlockChangeMessage) BlockChangeMessage(net.glowstone.net.message.play.game.BlockChangeMessage) MultiBlockChangeMessage(net.glowstone.net.message.play.game.MultiBlockChangeMessage) BlockVector(org.bukkit.util.BlockVector) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StatisticMap(net.glowstone.util.StatisticMap) MetadataMap(net.glowstone.entity.meta.MetadataMap) HashMap(java.util.HashMap) Key(net.glowstone.chunk.GlowChunk.Key) NamespacedKey(org.bukkit.NamespacedKey)

Example 7 with BlockChangeMessage

use of net.glowstone.net.message.play.game.BlockChangeMessage in project Dragonet-Legacy by DragonetMC.

the class MultiBlockChangeMessageTranslator method handleSpecific.

@Override
public PEPacket[] handleSpecific(MultiBlockChangeMessage packet) {
    UpdateBlockPacket pkBC = new UpdateBlockPacket();
    pkBC.records = new UpdateBlockPacket.UpdateBlockRecord[packet.records.size()];
    // PEPacket[] packets = new PEPacket[msgMBC.records.size()];
    int i = 0;
    for (BlockChangeMessage msgBC : packet.records) {
        // packets[i] = this.translateToPE(msgBC)[0];
        pkBC.records[i] = new UpdateBlockPacket.UpdateBlockRecord();
        pkBC.records[i].x = msgBC.x;
        pkBC.records[i].z = msgBC.z;
        pkBC.records[i].y = (byte) (msgBC.y & 0xFF);
        pkBC.records[i].block = (byte) (this.getTranslator().getItemTranslator().translateToPE(msgBC.type >> 4) & 0xFF);
        pkBC.records[i].meta = UpdateBlockPacket.FLAG_ALL_PRIORITY;
        i++;
    }
    return new PEPacket[] { pkBC };
}
Also used : PEPacket(org.dragonet.net.packet.minecraft.PEPacket) MultiBlockChangeMessage(net.glowstone.net.message.play.game.MultiBlockChangeMessage) BlockChangeMessage(net.glowstone.net.message.play.game.BlockChangeMessage) UpdateBlockPacket(org.dragonet.net.packet.minecraft.UpdateBlockPacket)

Example 8 with BlockChangeMessage

use of net.glowstone.net.message.play.game.BlockChangeMessage in project Glowstone by GlowstoneMC.

the class GlowBlock method setTypeIdAndData.

@Deprecated
public boolean setTypeIdAndData(int type, byte data, boolean applyPhysics) {
    Material oldTypeId = getType();
    byte oldData = getData();
    GlowChunk chunk = (GlowChunk) world.getChunkAt(this);
    Material material = ((GlowServer) Bukkit.getServer()).getBlockDataManager().convertToBlockData(type).getMaterial();
    chunk.setType(x & 0xf, z & 0xf, y, material);
    chunk.setMetaData(x & 0xf, z & 0xf, y, data);
    if (applyPhysics) {
        applyPhysics(oldTypeId, material, oldData, data);
    }
    GlowChunk.Key key = GlowChunk.Key.of(x >> 4, z >> 4);
    BlockChangeMessage bcmsg = new BlockChangeMessage(x, y, z, type, data);
    world.broadcastBlockChangeInRange(key, bcmsg);
    return true;
}
Also used : Material(org.bukkit.Material) BlockChangeMessage(net.glowstone.net.message.play.game.BlockChangeMessage) GlowServer(net.glowstone.GlowServer) GlowChunk(net.glowstone.chunk.GlowChunk)

Example 9 with BlockChangeMessage

use of net.glowstone.net.message.play.game.BlockChangeMessage in project Glowstone by GlowstoneMC.

the class GlowPlayer method pulseDigging.

private void pulseDigging() {
    if (++diggingTicks <= totalDiggingTicks) {
        // diggingTicks starts at 1 and progresses to totalDiggingTicks, but animation stages
        // are 0 through 9, so subtract 1 from the current tick
        int stage = (int) (10.0 * ((double) (diggingTicks - 1)) / totalDiggingTicks);
        broadcastBlockBreakAnimation(digging, stage);
        return;
    }
    ItemStack tool = getItemInHand();
    short durability = tool.getDurability();
    short maxDurability = tool.getType().getMaxDurability();
    if (!InventoryUtil.isEmpty(tool) && maxDurability != 0 && durability != maxDurability) {
        // Before applying unbreaking enchantment
        int baseDamage;
        switch(digging.getType()) {
            case GRASS_BLOCK:
            case DIRT:
            case SAND:
            case GRAVEL:
            case MYCELIUM:
            case SOUL_SAND:
                baseDamage = ToolType.SHOVEL.matches(tool.getType()) ? 1 : 2;
                break;
            case OAK_LOG:
            case DARK_OAK_LOG:
            case ACACIA_LOG:
            case BIRCH_LOG:
            case JUNGLE_LOG:
            case SPRUCE_LOG:
            case OAK_WOOD:
            case DARK_OAK_WOOD:
            case ACACIA_WOOD:
            case BIRCH_WOOD:
            case JUNGLE_WOOD:
            case SPRUCE_WOOD:
            case CHEST:
                baseDamage = ToolType.AXE.matches(tool.getType()) ? 1 : 2;
                break;
            case STONE:
            case COBBLESTONE:
                baseDamage = ToolType.PICKAXE.matches(tool.getType()) ? 1 : 2;
                break;
            default:
                baseDamage = 2;
                break;
        }
        for (int i = 0; i < baseDamage; i++) {
            tool = InventoryUtil.damageItem(this, tool);
        }
    }
    // Force-update item
    setItemInHand(tool);
    // Break the block
    digging.breakNaturally(tool);
    // Send block status to clients
    int blockX = digging.getX();
    int blockY = digging.getY();
    int blockZ = digging.getZ();
    // Tell the whole chunk
    world.broadcastBlockChangeInRange(Key.of(blockX >> 4, blockZ >> 4), new BlockChangeMessage(blockX, blockY, blockZ, Material.AIR.getId(), 0));
    setDigging(null);
}
Also used : BlockChangeMessage(net.glowstone.net.message.play.game.BlockChangeMessage) MultiBlockChangeMessage(net.glowstone.net.message.play.game.MultiBlockChangeMessage) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

BlockChangeMessage (net.glowstone.net.message.play.game.BlockChangeMessage)9 MultiBlockChangeMessage (net.glowstone.net.message.play.game.MultiBlockChangeMessage)5 GlowServer (net.glowstone.GlowServer)3 GlowChunk (net.glowstone.chunk.GlowChunk)3 ArrayList (java.util.ArrayList)2 BlockDataManager (net.glowstone.block.data.BlockDataManager)2 Key (net.glowstone.chunk.GlowChunk.Key)2 NamespacedKey (org.bukkit.NamespacedKey)2 BlockVector (org.bukkit.util.BlockVector)2 Message (com.flowpowered.network.Message)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 StatefulBlockData (net.glowstone.block.data.states.StatefulBlockData)1 MetadataMap (net.glowstone.entity.meta.MetadataMap)1 DestroyEntitiesMessage (net.glowstone.net.message.play.entity.DestroyEntitiesMessage)1 EntityAnimationMessage (net.glowstone.net.message.play.entity.EntityAnimationMessage)1 EntityMetadataMessage (net.glowstone.net.message.play.entity.EntityMetadataMessage)1 EntityVelocityMessage (net.glowstone.net.message.play.entity.EntityVelocityMessage)1 SetPassengerMessage (net.glowstone.net.message.play.entity.SetPassengerMessage)1