Search in sources :

Example 1 with BlockBreakEvent

use of org.bukkit.event.block.BlockBreakEvent in project GameCore by Warvale.

the class GlobalEvent method onBlockBreak.

@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
    if (event.getBlock().getType().equals(Material.IRON_ORE)) {
        event.getPlayer().giveExp(10);
        for (int i = 0; i < NumberUtils.random(3, 1); i++) {
            event.getPlayer().getInventory().addItem(new ItemStack(Material.IRON_INGOT));
        }
        event.setCancelled(true);
        Location blockLoc = event.getBlock().getLocation();
        event.getBlock().setType(Material.STONE);
        List<Entity> entityList = new ArrayList<>();
        for (int i = 0; i < NumberUtils.random(5, 2) + 1; i++) {
            ItemStack stack = new ItemStack(Material.IRON_NUGGET);
            stack.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, NumberUtils.random(100, 1));
            entityList.add(event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation().setDirection(Vector.getRandom()).add(Vector.getRandom()), stack));
        }
        new BukkitRunnable() {

            @Override
            public void run() {
                entityList.forEach(Entity::remove);
            }
        }.runTaskLater(Main.get(), 60L);
        new BukkitRunnable() {

            @Override
            public void run() {
                event.getPlayer().getWorld().getBlockAt(blockLoc).setType(Material.IRON_ORE);
                event.getPlayer().getWorld().playSound(blockLoc, Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 5, 1);
            }
        }.runTaskLater(Main.get(), 400L);
    }
    if (event.getBlock().getType().equals(Material.GOLD_ORE)) {
        event.getPlayer().giveExp(30);
        for (int i = 0; i < NumberUtils.random(2, 0); i++) {
            event.getPlayer().getInventory().addItem(new ItemStack(Material.GOLD_INGOT));
        }
        event.setCancelled(true);
        Location blockLoc = event.getBlock().getLocation();
        event.getBlock().setType(Material.STONE);
        List<Entity> entityList = new ArrayList<>();
        for (int i = 0; i < NumberUtils.random(5, 2) + 1; i++) {
            ItemStack stack = new ItemStack(Material.GOLD_NUGGET);
            stack.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, NumberUtils.random(100, 1));
            entityList.add(event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation().setDirection(Vector.getRandom()).add(Vector.getRandom()), stack));
        }
        new BukkitRunnable() {

            @Override
            public void run() {
                entityList.stream().forEach(entity -> entity.remove());
            }
        }.runTaskLater(Main.get(), 60L);
        new BukkitRunnable() {

            @Override
            public void run() {
                event.getPlayer().getWorld().getBlockAt(blockLoc).setType(Material.GOLD_ORE);
                event.getPlayer().getWorld().playSound(blockLoc, Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 10, 1);
            }
        }.runTaskLater(Main.get(), 400L);
    }
    if (event.getBlock().getType().equals(Material.DIAMOND_ORE)) {
        event.getPlayer().giveExp(50);
        for (int i = 0; i < NumberUtils.random(1, 0); i++) {
            event.getPlayer().getInventory().addItem(new ItemStack(Material.DIAMOND));
        }
        event.setCancelled(true);
        Location blockLoc = event.getBlock().getLocation();
        event.getBlock().setType(Material.STONE);
        List<Entity> entityList = new ArrayList<>();
        for (int i = 0; i < NumberUtils.random(5, 2) + 1; i++) {
            ItemStack stack = new ItemStack(Material.PRISMARINE_CRYSTALS);
            stack.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, NumberUtils.random(100, 1));
            entityList.add(event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation().setDirection(Vector.getRandom()).add(Vector.getRandom()), stack));
        }
        new BukkitRunnable() {

            @Override
            public void run() {
                entityList.stream().forEach(entity -> entity.remove());
            }
        }.runTaskLater(Main.get(), 60L);
        new BukkitRunnable() {

            @Override
            public void run() {
                event.getPlayer().getWorld().getBlockAt(blockLoc).setType(Material.DIAMOND_ORE);
                event.getPlayer().getWorld().playSound(blockLoc, Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 25, 1);
            }
        }.runTaskLater(Main.get(), 400L);
    }
    if (event.getBlock().getType().equals(Material.LAPIS_ORE)) {
        event.getPlayer().giveExp(50);
        for (int i = 0; i < NumberUtils.random(1, 0); i++) {
            event.getPlayer().getInventory().addItem(new ItemStack(Material.LAPIS_BLOCK));
        }
        event.setCancelled(true);
        Location blockLoc = event.getBlock().getLocation();
        event.getBlock().setType(Material.STONE);
        List<Entity> entityList = new ArrayList<>();
        for (int i = 0; i < NumberUtils.random(5, 2) + 1; i++) {
            ItemStack stack = new ItemStack(Material.EYE_OF_ENDER);
            stack.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, NumberUtils.random(100, 1));
            entityList.add(event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation().setDirection(Vector.getRandom()).add(Vector.getRandom()), stack));
        }
        new BukkitRunnable() {

            @Override
            public void run() {
                entityList.stream().forEach(entity -> entity.remove());
            }
        }.runTaskLater(Main.get(), 60L);
        new BukkitRunnable() {

            @Override
            public void run() {
                event.getPlayer().getWorld().getBlockAt(blockLoc).setType(Material.LAPIS_ORE);
                event.getPlayer().getWorld().playSound(blockLoc, Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 15, 1);
            }
        }.runTaskLater(Main.get(), 400L);
    }
}
Also used : Arrays(java.util.Arrays) Entity(org.bukkit.entity.Entity) Enchantment(org.bukkit.enchantments.Enchantment) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) BlockBreakEvent(org.bukkit.event.block.BlockBreakEvent) NumberUtils(net.warvale.core.utils.NumberUtils) Sound(org.bukkit.Sound) Player(org.bukkit.entity.Player) PlayerPickupItemEvent(org.bukkit.event.player.PlayerPickupItemEvent) ItemStack(org.bukkit.inventory.ItemStack) ArrayList(java.util.ArrayList) TeamManager(net.warvale.core.game.logic.TeamManager) EventHandler(org.bukkit.event.EventHandler) Vector(org.bukkit.util.Vector) PlayerDeathEvent(org.bukkit.event.entity.PlayerDeathEvent) List(java.util.List) ChatUtils(net.warvale.core.utils.chat.ChatUtils) Location(org.bukkit.Location) ChatColor(org.bukkit.ChatColor) Material(org.bukkit.Material) Listener(org.bukkit.event.Listener) Entity(org.bukkit.entity.Entity) ArrayList(java.util.ArrayList) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) ItemStack(org.bukkit.inventory.ItemStack) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 2 with BlockBreakEvent

use of org.bukkit.event.block.BlockBreakEvent in project TriggerReactor by wysohn.

the class LocationBasedTriggerManager method onSignBreak.

@EventHandler(priority = EventPriority.LOW)
public void onSignBreak(BlockBreakEvent e) {
    if (e.isCancelled())
        return;
    Block block = e.getBlock();
    Block above = block.getRelative(BlockFace.UP);
    if (above.getType() != Material.SIGN && above.getType() != Material.SIGN_POST)
        return;
    BlockBreakEvent bbe = new BlockBreakEvent(above, e.getPlayer());
    onBreak(bbe);
    e.setCancelled(bbe.isCancelled());
}
Also used : Block(org.bukkit.block.Block) BlockBreakEvent(org.bukkit.event.block.BlockBreakEvent) EventHandler(org.bukkit.event.EventHandler)

Example 3 with BlockBreakEvent

use of org.bukkit.event.block.BlockBreakEvent in project AuthMeReloaded by AuthMe.

the class BlockListenerTest method shouldAllowBreakEvent.

@Test
public void shouldAllowBreakEvent() {
    // given
    Player player = mock(Player.class);
    BlockBreakEvent event = mock(BlockBreakEvent.class);
    given(event.getPlayer()).willReturn(player);
    given(listenerService.shouldCancelEvent(player)).willReturn(false);
    // when
    listener.onBlockBreak(event);
    // then
    verify(event).getPlayer();
    verifyNoMoreInteractions(event);
}
Also used : Player(org.bukkit.entity.Player) BlockBreakEvent(org.bukkit.event.block.BlockBreakEvent) Test(org.junit.Test)

Example 4 with BlockBreakEvent

use of org.bukkit.event.block.BlockBreakEvent in project AuthMeReloaded by AuthMe.

the class BlockListenerTest method shouldDenyBreakEvent.

@Test
public void shouldDenyBreakEvent() {
    // given
    Player player = mock(Player.class);
    BlockBreakEvent event = mock(BlockBreakEvent.class);
    given(event.getPlayer()).willReturn(player);
    given(listenerService.shouldCancelEvent(player)).willReturn(true);
    // when
    listener.onBlockBreak(event);
    // then
    verify(event).setCancelled(true);
    verify(event).getPlayer();
    verifyNoMoreInteractions(event);
}
Also used : Player(org.bukkit.entity.Player) BlockBreakEvent(org.bukkit.event.block.BlockBreakEvent) Test(org.junit.Test)

Example 5 with BlockBreakEvent

use of org.bukkit.event.block.BlockBreakEvent in project Glowstone by GlowstoneMC.

the class DiggingHandler method handle.

@Override
public void handle(GlowSession session, DiggingMessage message) {
    GlowPlayer player = session.getPlayer();
    GlowWorld world = player.getWorld();
    EventFactory eventFactory = EventFactory.getInstance();
    GlowBlock block = world.getBlockAt(message.getX(), message.getY(), message.getZ());
    BlockFace face = BlockPlacementHandler.convertFace(message.getFace());
    ItemStack holding = player.getItemInHand();
    if (block.getRelative(face).getType() == Material.FIRE) {
        block.getRelative(face).breakNaturally();
        // returns to avoid breaking block in creative
        return;
    }
    boolean blockBroken = false;
    boolean revert = false;
    Material material = block.getType();
    switch(message.getState()) {
        case START_DIGGING:
            if (block.equals(player.getDigging()) || block.isLiquid()) {
                return;
            }
            // call interact event
            Action action = Action.LEFT_CLICK_BLOCK;
            Block eventBlock = block;
            if (player.getLocation().distanceSquared(block.getLocation()) > 36 || ExtraMaterialTags.AIR_VARIANTS.isTagged(material)) {
                action = Action.LEFT_CLICK_AIR;
                eventBlock = null;
            }
            PlayerInteractEvent interactEvent = eventFactory.onPlayerInteract(player, action, EquipmentSlot.HAND, eventBlock, face);
            // attempt to use item in hand, that is, dig up the block
            if (!BlockPlacementHandler.selectResult(interactEvent.useItemInHand(), true)) {
                // the event was cancelled, get out of here
                revert = true;
            } else if (player.getGameMode() != GameMode.SPECTATOR) {
                player.setDigging(null);
                // emit damage event - cancel by default if holding a sword
                boolean instaBreak = player.getGameMode() == GameMode.CREATIVE || block.getMaterialValues().getHardness() == 0;
                BlockDamageEvent damageEvent = new BlockDamageEvent(player, block, player.getItemInHand(), instaBreak);
                if (player.getGameMode() == GameMode.CREATIVE && holding != null && EnchantmentTarget.WEAPON.includes(holding.getType())) {
                    damageEvent.setCancelled(true);
                }
                eventFactory.callEvent(damageEvent);
                // follow orders
                if (damageEvent.isCancelled()) {
                    revert = true;
                } else {
                    // in creative, break even if denied in the event, or the block
                    // can never be broken (client does not send DONE_DIGGING).
                    blockBroken = damageEvent.getInstaBreak();
                    if (!blockBroken) {
                        // TODO: add a delay here based on hardness
                        player.setDigging(block);
                    }
                }
            }
            break;
        case DiggingMessage.CANCEL_DIGGING:
            player.setDigging(null);
            break;
        case DiggingMessage.FINISH_DIGGING:
            // untrusted)
            break;
        case DiggingMessage.STATE_DROP_ITEM:
            player.dropItemInHand(false);
            return;
        case DiggingMessage.STATE_DROP_ITEMSTACK:
            player.dropItemInHand(true);
            return;
        case DiggingMessage.STATE_SHOT_ARROW_FINISH_EATING:
            final ItemStack usageItem = player.getUsageItem();
            if (usageItem != null) {
                if (Objects.equals(usageItem, holding)) {
                    ItemType type = ItemTable.instance().getItem(usageItem.getType());
                    if (type != null && type instanceof ItemTimedUsage) {
                        ((ItemTimedUsage) type).endUse(player, usageItem);
                    } else {
                    // todo: inform the player that this item cannot be consumed/used
                    }
                } else {
                // todo: verification against malicious clients
                // todo: inform player their item is wrong
                }
            }
            return;
        case DiggingMessage.SWAP_ITEM_IN_HAND:
            ItemStack main = player.getInventory().getItemInMainHand();
            ItemStack off = player.getInventory().getItemInOffHand();
            PlayerSwapHandItemsEvent event = EventFactory.getInstance().callEvent(new PlayerSwapHandItemsEvent(player, off, main));
            if (!event.isCancelled()) {
                player.getInventory().setItemInOffHand(main);
                player.getInventory().setItemInMainHand(off);
                player.updateInventory();
            }
            return;
        default:
            return;
    }
    if (blockBroken && !revert) {
        // fire the block break event
        BlockBreakEvent breakEvent = eventFactory.callEvent(new BlockBreakEvent(block, player));
        if (breakEvent.isCancelled()) {
            BlockPlacementHandler.revert(player, block);
            return;
        }
        MaterialData data = block.getState().getData();
        if (ExtraMaterialTags.BISECTED_BLOCKS.isTagged(material)) {
            if (block.getRelative(BlockFace.DOWN).getType() == material) {
                block = block.getRelative(BlockFace.DOWN);
            }
        }
        BlockType blockType = ItemTable.instance().getBlock(material);
        if (blockType != null) {
            blockType.blockDestroy(player, block, face);
        }
        // destroy the block
        if (!block.isEmpty() && !block.isLiquid() && (player.getGameMode() != GameMode.CREATIVE || blockType instanceof BlockContainer) && world.getGameRuleMap().getBoolean(GameRules.DO_TILE_DROPS)) {
            Collection<ItemStack> drops = blockType.getDrops(block, holding);
            if (blockType instanceof BlockContainer && player.getGameMode() == GameMode.CREATIVE) {
                drops = ((BlockContainer) blockType).getContentDrops(block);
            }
            for (ItemStack drop : drops) {
                GlowItem item = world.dropItemNaturally(block.getLocation(), drop);
                item.setPickupDelay(30);
                item.setBias(player);
            }
        }
        player.addExhaustion(0.005f);
        // STEP_SOUND actually is the block break particles
        world.playEffectExceptTo(block.getLocation(), Effect.STEP_SOUND, block.getType().getId(), 64, player);
        GlowBlockState state = block.getState();
        block.setType(Material.AIR);
        if (blockType != null) {
            blockType.afterDestroy(player, block, face, state);
        }
    } else if (revert) {
        // replace the block that wasn't really dug
        BlockPlacementHandler.revert(player, block);
    } else if (!ExtraMaterialTags.AIR_VARIANTS.isTagged(material)) {
        BlockType blockType = ItemTable.instance().getBlock(material);
        blockType.leftClickBlock(player, block, holding);
    }
}
Also used : ItemTimedUsage(net.glowstone.block.itemtype.ItemTimedUsage) Action(org.bukkit.event.block.Action) GlowPlayer(net.glowstone.entity.GlowPlayer) BlockFace(org.bukkit.block.BlockFace) BlockContainer(net.glowstone.block.blocktype.BlockContainer) GlowBlockState(net.glowstone.block.GlowBlockState) PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) ItemType(net.glowstone.block.itemtype.ItemType) EventFactory(net.glowstone.EventFactory) Material(org.bukkit.Material) GlowItem(net.glowstone.entity.objects.GlowItem) GlowBlock(net.glowstone.block.GlowBlock) BlockType(net.glowstone.block.blocktype.BlockType) BlockDamageEvent(org.bukkit.event.block.BlockDamageEvent) GlowWorld(net.glowstone.GlowWorld) GlowBlock(net.glowstone.block.GlowBlock) Block(org.bukkit.block.Block) BlockBreakEvent(org.bukkit.event.block.BlockBreakEvent) MaterialData(org.bukkit.material.MaterialData) ItemStack(org.bukkit.inventory.ItemStack) PlayerSwapHandItemsEvent(org.bukkit.event.player.PlayerSwapHandItemsEvent)

Aggregations

BlockBreakEvent (org.bukkit.event.block.BlockBreakEvent)9 Block (org.bukkit.block.Block)5 Material (org.bukkit.Material)4 Player (org.bukkit.entity.Player)3 Test (org.junit.Test)3 Location (org.bukkit.Location)2 Event (org.bukkit.event.Event)2 EventHandler (org.bukkit.event.EventHandler)2 Listener (org.bukkit.event.Listener)2 PlayerInteractEvent (org.bukkit.event.player.PlayerInteractEvent)2 ItemStack (org.bukkit.inventory.ItemStack)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 EventFactory (net.glowstone.EventFactory)1 GlowWorld (net.glowstone.GlowWorld)1 GlowBlock (net.glowstone.block.GlowBlock)1 GlowBlockState (net.glowstone.block.GlowBlockState)1 BlockContainer (net.glowstone.block.blocktype.BlockContainer)1 BlockType (net.glowstone.block.blocktype.BlockType)1