Cancel drops from BlockPistonExtendEvent

Discussion in 'Plugin Development' started by XXLuigiMario, Nov 22, 2014.

Thread Status:
Not open for further replies.
  1. I'm trying to cancel the drops from blocks in the BlockPistonExtendEvent but I don't seem to be doing it right...

    I'm updating the MiniChest plugin, but I need to make them drop their contents when pushed by a piston.

    Code:java
    1. @EventHandler(priority = EventPriority.MONITOR)
    2. public void onPistonExtend(BlockPistonExtendEvent event) {
    3. if (event.isCancelled())
    4. return;
    5. for (Block block : event.getBlocks()) {
    6. if (isMinichest(block)) {
    7. block.getDrops().clear();
    8. block.getDrops().add(new ItemStack(Material.AIR));
    9. destroyMinichest(block, true);
    10. }
    11. }
    12. }
     
  2. Offline

    Dragonphase

    XXLuigiMario

    EventPriority.MONITOR is used to monitor the event, and should not be used to make changes to the event. Use a lower priority.

    More information about this can be found at the Event API Reference on the Bukkit Wiki.

    As for your code logic, it won't be completely easy to assist you without knowing what your destroyMinechest(Block, Boolean); method does; from the code above, it seems that you are removing all drops from the block, thus causing it to drop nothing.
     
  3. I'll lower it to HIGHEST, then.
    And I know it's going to drop nothing, it's what I'm trying to do, but it's not working for some reason.

    Oh and, destroyMinechest(Block, Boolean), makes them drop they content and remove them from the hashmap.

    <Edit by mrCookieSlime: Merged Posts. Please dont double post. There is an Edit-Button right next to the Date.>
     
  4. Offline

    xTigerRebornx

    XXLuigiMario Block#getDrops() doesn't actually control the drops of that specific block, rather its based on its type and other properties (such as having an inventory).
    Another approach to this would have to be done, such as cancelling the event and mimicking the movement of other blocks and the destruction of yours.

    Showing outside methods on here would be a start to getting help with that, as we have no idea what a "minechest" is or how its stored
     
  5. I forgot I had made this post, I fixed it, not as I would've liked but it does work.
    The issue was that event.getBlocks() doesn't provide fragile blocks...
    https://bukkit.atlassian.net/browse/BUKKIT-269

    So I ended up doing this:
    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onBlockPistonExtend(BlockPistonExtendEvent event) {
            if (event.isCancelled())
                return;
            BlockFace face = event.getDirection();
            if (!handleRelative(event.getBlock(), face)) {
                for (Block block : event.getBlocks()) {
                    handleRelative(block, face);
                }
            }
        }
    
        public boolean handleRelative(Block block, BlockFace face) {
            Block relative = block.getRelative(face);
            if (relative != null) {
                if (isMinichest(relative)) {
                    destroyMinichest(relative, true);
                    return true;
                }
            }
            return false;
        }
    I thought could also do this:

    Code:
    public void onBlockPistonExtend(BlockPistonExtendEvent event) {
        if (event.isCancelled())
            return;
        ArrayList<Block> blocks = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            blocks.add(event.getBlock().getRelative(event.getDirection(), i));
        }
    }
    
    But then I figured out it wouldn't be as precise because maybe it would include blocks that wouldn't be moved by the piston.
     
Thread Status:
Not open for further replies.

Share This Page