Search in sources :

Example 1 with BlockStateChange

use of com.bergerkiller.bukkit.common.wrappers.BlockStateChange 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());
        CommonTagCompound metadata = packet.getData();
        if (metadata != null) {
            // There's metadata, nothing special needs to be done
            BlockStateChange change = BlockStateChange.deferred(packet.getPosition(), packet.getType(), 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(), packet.getType(), 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 : 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)

Example 2 with BlockStateChange

use of com.bergerkiller.bukkit.common.wrappers.BlockStateChange in project BKCommonLib by bergerhealer.

the class PacketPlayOutMapChunkHandle method setBlockStates.

public void setBlockStates(List<BlockStateChange> states) {
    List<BlockStateChange> baseStates = this.getBlockStates();
    int count = states.size();
    int limit = Math.min(count, baseStates.size());
    for (int i = 0; i < limit; i++) {
        BlockStateChange change = states.get(i);
        if (baseStates.get(i) != change) {
            baseStates.set(i, change);
        }
    }
    for (int i = limit; i < count; i++) {
        baseStates.add(states.get(i));
    }
    while (baseStates.size() > count) {
        baseStates.remove(baseStates.size() - 1);
    }
}
Also used : BlockStateChange(com.bergerkiller.bukkit.common.wrappers.BlockStateChange)

Aggregations

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