Search in sources :

Example 1 with PlayerBucketFillEvent

use of org.bukkit.event.player.PlayerBucketFillEvent in project Glowstone by GlowstoneMC.

the class ItemBucket method rightClickAir.

@Override
public void rightClickAir(GlowPlayer player, ItemStack holding) {
    Iterator<Block> itr = new BlockIterator(player, 5);
    Block target = null;
    // Used to determine the side the block was clicked on:
    Block previousTarget = null;
    BlockType targetBlockType = null;
    boolean validTarget = false;
    // Find the next available non-air liquid block type which is collectible in a radius of 5 blocks
    while (itr.hasNext()) {
        previousTarget = target;
        target = itr.next();
        targetBlockType = ItemTable.instance().getBlock(target.getType());
        if (targetBlockType instanceof BlockLiquid) {
            if (((BlockLiquid) targetBlockType).isCollectible((GlowBlockState) target.getState())) {
                validTarget = true;
                break;
            }
        }
    }
    if (target != null && validTarget) {
        // Get the direction of the bucket fill
        BlockFace face;
        if (previousTarget != null) {
            face = target.getFace(previousTarget);
        } else {
            face = BlockFace.SELF;
        }
        Material replaceWith = ((BlockLiquid) targetBlockType).getBucketType();
        PlayerBucketFillEvent event = EventFactory.callEvent(new PlayerBucketFillEvent(player, target, face, holding.getType(), holding));
        if (event.isCancelled()) {
            return;
        }
        if (player.getGameMode() != GameMode.CREATIVE) {
            if (holding.getAmount() == 1) {
                holding.setType(replaceWith);
            } else {
                holding.setAmount(holding.getAmount() - 1);
                player.getInventory().addItem(new ItemStack(replaceWith));
            }
        }
        target.setType(Material.AIR);
    }
}
Also used : BlockIterator(org.bukkit.util.BlockIterator) PlayerBucketFillEvent(org.bukkit.event.player.PlayerBucketFillEvent) BlockLiquid(net.glowstone.block.blocktype.BlockLiquid) BlockType(net.glowstone.block.blocktype.BlockType) BlockFace(org.bukkit.block.BlockFace) Block(org.bukkit.block.Block) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

BlockLiquid (net.glowstone.block.blocktype.BlockLiquid)1 BlockType (net.glowstone.block.blocktype.BlockType)1 Material (org.bukkit.Material)1 Block (org.bukkit.block.Block)1 BlockFace (org.bukkit.block.BlockFace)1 PlayerBucketFillEvent (org.bukkit.event.player.PlayerBucketFillEvent)1 ItemStack (org.bukkit.inventory.ItemStack)1 BlockIterator (org.bukkit.util.BlockIterator)1