How can I make an event occur when a specific block breaks

Discussion in 'Plugin Development' started by RedKaneChironic, Sep 19, 2020.

  1. Offline

    RedKaneChironic

    I would like to make a summoned block of stone have different properties. How can I make it so that when this block breaks, an event gets triggered? Here is my current code

    Code:
    package io.github.mysixsenses.helloworld;
    
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin{
    
        boolean check(Command a, String b) {
            return a.getName().equalsIgnoreCase(b);
        }
        boolean checkPlayer(CommandSender player) {
            return player instanceof Player;
        }
        @Override
        public void onEnable() {
            getLogger().info("onEnable has been invoked for the Hello World Plugin!");
        }
      
        @Override
        public void onDisable() {
            getLogger().info("onDisable has been invoked for the Hello World Plugin!");
        }
        //onCommand
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (check(cmd, "basic")) {// If the player typed /basic then do the following...
                sender.sendMessage("Hello World!");
                return true;
            } else if (check(cmd, "basic2")) {
                if (!(checkPlayer(sender))) {
                    sender.sendMessage("This command can only be run by a player.");
                } else {
                    Player player = (Player) sender;
                    player.sendMessage("Hello World!");
                }
                return true;
            } else if(check(cmd, "summonitem")) {
                if (!(checkPlayer(sender))) {
                    sender.sendMessage("This command can only be run by a player.");
                } else {
                    Player player = (Player) sender;
                    // Get the player's location.
                    Location loc = player.getLocation();
                    // Sets location to five above where it used to be. Note that this doesn't change the player's position.
                    loc.setY(loc.getY() + 5);
                    // Gets the block at the new location.
                    Block b = loc.getBlock();
                    // Sets the block to type id 1 (stone).
                    b.setType(Material.STONE);
                }
                return true;
            }
            return false;
        }
    }
    
    Currently, my only idea is to add every modified stone into a list with all the stone and check if it's in the modified stone group when OnBlockBreak is called.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting. ok
     
    Last edited: Sep 20, 2020
  2. Offline

    KarimAKL

    @RedKaneChironic Listen to the BlockBreakEvent, then check if the block broken is your specific block.
     

Share This Page