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);
}
}
Aggregations