Solved Detecting block break by itself

Discussion in 'Spigot Plugin Development' started by Eprex1337, Mar 19, 2020.

Thread Status:
Not open for further replies.
  1. Hello! I would like to make event that activate when banner breaks by itself (for example when block under banner is broken). Unfortunelly i can't find solution for this! Can you help me please?
     
  2. Hello,

    Simply check if the breaked block is an instance of Banner into a BlockBreakEvent.
    Code (Java):
    @EventHandler
    public void on(final BlockBreakEvent e) {
        if (e.getBlock() instanceof Banner) {
            // Banner broken
        }
    }
    I do not know if it's called when a banner is on the block. If it's not, simply check if the breaked block has a Banner on it.
     
  3. Thanks for your reply, unfortunelly your code is not working :/

    Code (Text):
        @EventHandler
        public void on(final BlockBreakEvent e) {
            if (e.getBlock() instanceof Banner) {
                Utils.Debug("yep"); //Utils.Debug should send message, but nothing is happening :(
            }
        }
     
  4. Code (Text):
    @EventHandler
    public void on(final BlockBreakEvent e) {
        final Block up = e.getBlock().getRelative(BlockFace.UP);
        if (up instanceof Banner) {
            // The block upside is a banner, and gonna be breaked
        }
    }
     
  5. Did you register the Listener, and are you sure your Utils method works?
     
  6. The BlockBreakEvent only triggers, if a player destroyed the Block. As far as I can see, the only way determining if a banner broke under any circumstances is listening for the BlockPhysicsEvent, but be careful since the event gets called really often
     
  7. Yes this Utils metod works and Listener is registered.
     
  8. Unfortunelly, this ain't working too :/ but thanks for your reply!
     
  9. Then another solution could be to listen to the ItemSpawnEvent, which should be triggered when the banner is breaked.
    Simply check if the ItemStack type from the Item is a Banner.
    But it can easily be confused with a player who dropped the item from inventory..
     
  10. Haven't tested this and there might be typo or two, this basically just checks if the block above the broken block is banner...

    Code (Java):
    @EventHandler

    public void onBlockBreak(BlockBreakEvent e) {
     
        Block b = e.getBlock().getLocation()

        Block block = b.getWorld().getBlockAt(x, y, z);
        Block upperBlock = b.getWorld.getBlockAt(x, y + 1, z)

        if(upperBlock.getType() === MATERIAL.BANNER){
            #stuff happens
        }
    }
     
  11. Thats also what I've proposed (cleaner) here:
     
  12. Thanks for your reply! I think this would work, but im not looking for it. I'm making minigame and i want to prevent banner from dropping when it's broken caused for example by breaking it's floor/wall by player or plugin.
     
  13. Thanks! that's what i was looking for!
     
Thread Status:
Not open for further replies.