Break Block Event

Discussion in 'Plugin Development' started by RexTheMon, Oct 3, 2015.

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

    RexTheMon

    Hey guys I need some help with this plugin that I am developing. Problem: it wont broadcast a message when someone breaks a Diamond Block.

    Here is my EventHandler:
    Code:
    package com.rtmplays.main.event;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    
    public class BlockBreak implements Listener {
       
        @EventHandler
        public void onBLockBreak(BlockBreakEvent event) {
            Player player = event.getPlayer();
           
            Block block = event.getBlock();
            Material material = block.getType();
           
            if(material == Material.DIAMOND_BLOCK) {
                Bukkit.broadcastMessage(player.getName() + ", Found a Diamond!");
            }
        }
    
    }
    
    This is my main class:
    Code:
    package com.rtmplays.main;
    
    import java.util.logging.Logger;
    
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.rtmplays.main.event.BlockBreak;
    
    public class Main extends JavaPlugin {
      
        public void onEnable() {
            PluginDescriptionFile pdfFile = getDescription();
             Logger logger = getLogger();
           
             registerEvents();
          
            logger.info("******************************");
            logger.info("Enabling.. " + pdfFile.getName() + " Version: " + pdfFile.getVersion());
            logger.info("By RexTheMon");
            logger.info("******************************");
        }
      
        public void onDisable() {
            PluginDescriptionFile pdfFile = getDescription();
             Logger logger = getLogger();
          
            logger.info("******************************");
            logger.info("Disabling.. " + pdfFile.getName() + " Version: " + pdfFile.getVersion());
            logger.info("By RexTheMon");
            logger.info("******************************");
        }
      
        public void registerEvents() {
            PluginManager pm = getServer().getPluginManager();
          
            pm.registerEvents(new BlockBreak(), this);
        }
    }
    
     
  2. Offline

    AeroLiquid

    Do you receive any errors in the console or in game?
     
  3. Offline

    SuperSniper

    @RexTheMon You're trying to make it to where it broadcasts if it's a Diamond BLOCK, right? Not a Diamond ORE? Im just asking because it says "Found a diamond", which makes me thing that you're trying to make it to where you have to break the ORE. If you are, change it to
    Material.DIAMOND_ORE

    If not, I don't really know what's wrong :p

    You can try not using lots of variables though, just do

    Code:
    @EventHandler
    public void onBreak(BlockBreakEvent e) {
    Player player = e.getPlayer();
    if(e.getBlock().getType() == Material.DIAMOND_BLOCK) {
    Bukkit.broadcastMessage(player.getName()+", Found a Diamond!");
    
     
  4. Offline

    Gonmarte

    @RexTheMon Do you have any error in the console? if yes paste here the stack trace. In the broadcast message you should put Diamond Block instead of Diamond if you want a Diamond Block. If you want a Diamond_Ore and you are testing the plugin breaking a diamong ore but in fact you coded a diamond block you need to change it!
     
  5. Offline

    boomboompower

    @RexTheMon you messed up the registerEvents thing in the onEnable, it should be
    Code:
    getServer().getPluginManager().registerEvents(this, this);
    Also why don't you just have it in 1 class file? Its small enough not to get lost right?
     
  6. @boomboompower
    No No No No No.
    "this" refers to the same instance, only if the plugin itself is a listener and you want to register the events for that class you use "this", else you need to use the instance of the listener
     
  7. Offline

    RexTheMon

    I am trying to make it so when you break a Diamond Ore it broadcasts a message to the whole server.

    Also I do not have any console errors.

    @boomboompower I know

    @bwfcwalshy I have been learning from the TheBCBroz. The tutorial he was doing made me want to make a plugin!

    @SuperSniper Thanks! I will try Diamond_Ore instead.
     
  8. He teaches really bad practices. Read the thread I posted and look around the forum. There are 1000's of posts on why he is bad.
     
  9. Offline

    DoggyCode™

    Try:
    Code:
    if(e.getBlock().getType().equals(Material.DIAMOND_BLOCK)){
    instead of
    Code:
    if(e.getBlock().getType()==Material.DIAMOND_BLOCK){
     
  10. Offline

    teej107

    That won't make a difference
     
  11. Offline

    DoggyCode™

    Could checking for the instance of the Block's state make a difference instead? And I would use ".equals(...)", it's more preferable.
     
  12. Offline

    RexTheMon

    Thanks everyone I have fixed it, yay!

    Instead of TheBCBroz who should I watch, answers?
     
  13. @RexTheMon If you insist on watching someone try PogoStick29dev otherwise if you don't mind reading the best tutorials are the official ones.
     
    oceantheskatr and RexTheMon like this.
  14. Offline

    RexTheMon

    I do watch him. Lol!

    Whats a good place to put my plugins for use, so they will get out there fast?

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
Thread Status:
Not open for further replies.

Share This Page