1.15.2 Problem with BlockBreakEvent(Solved)

Discussion in 'Spigot Plugin Development' started by Voxu, Apr 2, 2020.

Thread Status:
Not open for further replies.
  1. Code (Java):
    @EventHandler(priority = EventPriority.NORMAL)
        public void onBlockBreak(BlockBreakEvent e) {
            Player p = e.getPlayer();
            if(e.getBlock().getType().equals(Material.OAK_LOG));
            OakLogBreakEvent event = new OakLogBreakEvent(e.getPlayer());
            Bukkit.getServer().getPluginManager().callEvent(event);
       
        }
        @EventHandler(priority = EventPriority.NORMAL)
        public void onOakLogBreak(OakLogBreakEvent e) {
            Player p = e.getPlayer();
            p.sendMessage("Wow you're OP");
            p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS,600,1));
            p.playSound(p.getLocation(), Sound.MUSIC_DISC_11, 80,10);
        }
    I had a think that when I destroy oak log will be effect blindness and music disc 11. Unfortunately everything what I destroy starts these effects and i don't know what can I do to fix it.
    Thanks for answers and help :D
     
    #1 Voxu, Apr 2, 2020
    Last edited: Apr 2, 2020
  2. Choco

    Moderator

    Take a close look at your if statement
    if(e.getBlock().getType().equals(Material.OAK_LOG));
    Do you notice anything strange?

    Other notes:
    • EventPriority.NORMAL is the default. You can remove that bit and just put @EventHandler
    • Compare constants with ==, not .equals(). e.getBlock().getType() == Material.OAK_LOG
     
  3. First of all, please wrap your code into code-brackets ([ code=Java][/code]), you can also find them in the bar right over the edit-field.
    You have written
    Code (Text):
    if(e.getBlock().getType().equals(Material.OAK_LOG));
    which performs your desired check, but doesn't do anything with the result. You'll need to do it like this:
    Code (Text):
    if(e.getBlock().getType().equals(Material.OAK_LOG)) {
        // conditional code will get executed
    }
    Also, the EventPriority is Normal by default.
    If you don't plan to listen to oak-log break events from an outside source, I would also recommend not using an Event, but instead just putting the code inside your if-statement or using a simple method.
     
  4. Thanks guys thats really help me !
     
Thread Status:
Not open for further replies.