Search in sources :

Example 1 with PacketPlayOutMapChunkHandle

use of com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutMapChunkHandle in project BKCommonLib by bergerhealer.

the class BlockStateChangePacketHandler_1_9_3 method enable.

@Override
public void enable() {
    register(PacketType.OUT_TILE_ENTITY_DATA, (player, commonPacket, listener) -> {
        final PacketPlayOutTileEntityDataHandle packet = PacketPlayOutTileEntityDataHandle.createHandle(commonPacket.getHandle());
        // Parse BlockState type. If unknown/invalid, ignore the packet.
        BlockStateType tileType = packet.getType();
        if (tileType == null) {
            return true;
        }
        CommonTagCompound metadata = packet.getData();
        if (metadata != null) {
            // There's metadata, nothing special needs to be done
            BlockStateChange change = BlockStateChange.deferred(packet.getPosition(), tileType, LogicUtil.constantSupplier(metadata), () -> true);
            // Handle it, if false, cancel the packet entirely
            if (!listener.onBlockChange(player, change)) {
                return false;
            }
        } else {
            // Initialize metadata on first use
            final DeferredSupplier<CommonTagCompound> metadataSupplier = DeferredSupplier.of(CommonTagCompound::new);
            BlockStateChange change = BlockStateChange.deferred(packet.getPosition(), tileType, metadataSupplier, metadataSupplier::isInitialized);
            // Handle it, if false, cancel the packet entirely
            if (!listener.onBlockChange(player, change)) {
                return false;
            }
            // If metadata was created and it's not empty, apply it to the packet
            if (metadataSupplier.isInitialized() && !metadataSupplier.get().isEmpty()) {
                packet.setData(metadataSupplier.get());
            }
        }
        return true;
    });
    register(PacketType.OUT_MAP_CHUNK, (player, commonPacket, listener) -> {
        PacketPlayOutMapChunkHandle packet = PacketPlayOutMapChunkHandle.createHandle(commonPacket.getHandle());
        Iterator<BlockStateChange> iter = packet.getBlockStates().iterator();
        while (iter.hasNext()) {
            BlockStateChange change = iter.next();
            if (!listener.onBlockChange(player, change)) {
                iter.remove();
            }
        }
        return true;
    });
}
Also used : BlockStateType(com.bergerkiller.bukkit.common.resources.BlockStateType) BlockStateChange(com.bergerkiller.bukkit.common.wrappers.BlockStateChange) PacketPlayOutTileEntityDataHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutTileEntityDataHandle) CommonTagCompound(com.bergerkiller.bukkit.common.nbt.CommonTagCompound) PacketPlayOutMapChunkHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutMapChunkHandle)

Aggregations

CommonTagCompound (com.bergerkiller.bukkit.common.nbt.CommonTagCompound)1 BlockStateType (com.bergerkiller.bukkit.common.resources.BlockStateType)1 BlockStateChange (com.bergerkiller.bukkit.common.wrappers.BlockStateChange)1 PacketPlayOutMapChunkHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutMapChunkHandle)1 PacketPlayOutTileEntityDataHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutTileEntityDataHandle)1