Help with BlockBreakEvent

Discussion in 'Plugin Development' started by Mattz, Jan 27, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Mattz

    Is it possible to check if someone breaks a log and there's a dirt block under it, then do something? What would be the code for this?

    Thanks.
     
  2. Offline

    mastermustard

    more info please. what exactly would you want it to do
     
  3. Offline

    Jombi

    I don't think code will just be handed out to you. Show us what you have and we can help. You won't learn by asking us to spoon feed you.

    But yes, it is possible.
     
  4. Offline

    ZeusAllMighty11

    Code:
    @EventHandler
    public void onBlockBreak(BlockBreakEvent e){
        Player player = e.getPlayer();
        Block block = e.getBlock();
       
        if(block.getLocation().getY().subtract(0,1,0).getType().equalsIgnoreCase(Material.LDIRT)){
            // it was dirt
        }
    }
    
    something like that
     
  5. Offline

    Mattz

    Something like this:
    Code:
    if(event.getBlock() = Block.LOG) {
        if(event.getBlockBelow() = Block.DIRT) {
            // Do something
        }
    }
    
    The second if is the part I need help with (I know it's not a real method).

    I'm getting the error "Cannot invoke substract(int, int, int) on the primitive type double". I understand I have to parse it as an integer, but I can't manage to fit this in.

    Does anyone know why it's happening?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  6. Offline

    Th3Controller

    You can try:
    Code:
    if((event.getPlayer().getWorld().getBlockAt(x, y - 1, z).getType() == Material.DIRT)) {
    But instead of getting the player, look for a block, and then you know the rest, as other people said above we won't spoon feed ye ;)
     
  7. Offline

    Mattz

    Thanks, this fixed the error, but now the events don't seem to be registering:

    Main.class:
    Code:
    package net.thepvp.prison;
    
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class PrisonAPI extends JavaPlugin {
        
        @Override
        public void onEnable() {
            getLogger().info("The plugin is now enabled!");
            
            PluginManager pluginManager = this.getServer().getPluginManager();
            pluginManager.registerEvents(new Events(), this);
        }
        
        @Override
        public void onDisable() {
            getLogger().info("The plugin has been disabled.");
        }
    }
    
    Events.class:
    Code:
    package net.thepvp.prison;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    
    public class Events implements Listener {
        
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            if(event.getPlayer().isOp() == false ) {
                Bukkit.broadcastMessage("Line 1");
                if(event.getBlock().getType() == Material.LOG) {
                    Bukkit.broadcastMessage("Line 2");
                    int x = (int)event.getBlock().getLocation().getX();
                    int y = (int)event.getBlock().getLocation().getY();
                    int z = (int)event.getBlock().getLocation().getZ();
                    if(event.getBlock().getWorld().getBlockAt(x, y-1, z).getType() == Material.DIRT) {
                        Bukkit.broadcastMessage("Line 3");
                        Location plantSapling = event.getBlock().getLocation();
                        plantSapling.getBlock().setType(Material.SAPLING);
                    }
                }
            } else {
                
            }
        }
    }
    
     
  8. Offline

    Th3Controller

    What do you mean not registering?

    You need to give more detail.

    For your "event.getPlayer.isOp()" use an exclamation mark instead of comparing it to false.

    As for this
    Code:
    int x = (int)event.getBlock().getLocation().getX();
    int y = (int)event.getBlock().getLocation().getY();
    int z = (int)event.getBlock().getLocation().getZ();
    You do not need "(int)" it's already defined as an integer.
     
  9. Offline

    Mattz

    By registering, I mean that the event is not called at all whenever I break a block. The lines 1, 2, and 3 that I broadcast are tests for how far the code gets, and none gets shown.

    As for the (int), I actually do need them:
    [​IMG]

    None of this would affect whether the event is called or not, doesn't it?
     
  10. Offline

    Th3Controller

    Hmm try making a constructor
    Main.class:
    Code:
    package net.thepvp.prison;
     
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class PrisonAPI extends JavaPlugin {
     
        public void onEnable() {
            getLogger().info("The plugin is now enabled!");
            getServer().getPluginManager().registerEvents(new Events(this), this);
        }
     
        public void onDisable() {
            getLogger().info("The plugin has been disabled.");
        }
    }
    
    Insert this in your events class:
    Code:
        PrisonAPI plugin;
     
        public Events(PrisonAPI plugin) {
            this.plugin = plugin;
        }
     
  11. Offline

    Mattz

    Sorry for the late reply.

    Hmm, this didn't make any difference... I've never had this problem before, which is strange. I compared the plugin with another one that works, and there's no difference...
     
  12. Offline

    Mattz

    Anyone see something that I don't?
     
Thread Status:
Not open for further replies.

Share This Page