Help with a BlockBreakEvent Listener

Discussion in 'Plugin Development' started by MrJoe223, Jun 23, 2013.

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

    MrJoe223

    Hey guys, I've been having a bit of a problem with the following code:

    Code:
    package main;
     
    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.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class AutoSmelt extends JavaPlugin implements Listener {
       
        void OnEnable() {
           
            //PluginManager pm = getServer().getPluginManager();
           
            //I've also done v with out the "Bukkit." before it.
           
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
           
        }
       
        void OnDisable() {
           
        }
       
        @EventHandler(priority = EventPriority.NORMAL)
        public void onBlockBreak(BlockBreakEvent event) {
            Player player = event.getPlayer();
           
            if(player.hasPermission("autosmelt.smelt")) {
               
                Material ore = event.getBlock().getType();
               
                if(ore == Material.IRON_ORE) {
                    Block smelted = event.getBlock();
                    smelted.setType(Material.AIR);
                    PlayerInventory inventory = player.getInventory();
                    inventory.addItem(new ItemStack[] { new ItemStack(Material.IRON_INGOT, 1)});
                } else if(ore == Material.GOLD_ORE) {
                    Block smelted2 = event.getBlock();
                    smelted2.setType(Material.AIR);
                    PlayerInventory inventory = player.getInventory();
                    inventory.addItem(new ItemStack[] { new ItemStack(Material.GOLD_INGOT, 1)});
                }
                                    
            }     
     
        }
       
    }
    
    The plugin loads fine, however, when I break either Iron Ore or Gold Ore, it does none of the things specified in the code. I added(but deleted for ease of reading) some .sendmessage()s at different points in the execution to try and find the source of the problem within the onBlockBreak() method, but was unable to find the source of the problem.

    I'm using Eclipse, referencing the 1.5.2 API. It's not coming up with any errors in the code. I added(using the same notation as above) a onPlayerLogin() method, with corresponding event, but that did not function either. As you can see from the //comment above, I had previously used a separate class for the listener, with correct syntax in the RegisterEvents() method.

    If any of you guys have had this problem before, or can spot an error in my code, your input would be greatly appreciated. :)
     
  2. Offline

    Kodfod

    MrJoe223

    Instead of using the material, use the numerical ID. That Might help. (I've always had to use the numerical id's.)
    Code:
    event.getBlock().getTypeId();
     
  3. Offline

    MrJoe223

    Kodfod
    Many thanks for the quick response.

    I edited the code as so:
    Code:java
    1. @EventHandler(priority = EventPriority.NORMAL)
    2. public void onBlockBreak(BlockBreakEvent event) {
    3. Player player = event.getPlayer();
    4.  
    5. if(player.hasPermission("autosmelt.smelt")) {
    6.  
    7. int ore = event.getBlock().getTypeId();
    8.  
    9. if(ore == 15) {
    10. Block smelted = event.getBlock();
    11. smelted.setType(Material.AIR);
    12. PlayerInventory inventory = player.getInventory();
    13. inventory.addItem(new ItemStack[] { new ItemStack(Material.IRON_INGOT, 1)});
    14. } else if(ore == 14) {
    15. Block smelted2 = event.getBlock();
    16. smelted2.setType(Material.AIR);
    17. PlayerInventory inventory = player.getInventory();
    18. inventory.addItem(new ItemStack[] { new ItemStack(Material.GOLD_INGOT, 1)});
    19. }
    20.  
    21.  
    22. }


    However, it is still acting the same way as before, allowing me to mine the gold/iron ore at will. Is there a problem in my listener?

    I placed .sendMessages() around the plugin, and determined that there's a problem with my listener. E.g. other stuff works. If anyone knows what's wrong, that would be great.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
  4. Offline

    pointfit

    Weird, I would like to know also why this isn't working...
     
  5. Offline

    Frazz86

    Are you calling it onEnable?
     
    pointfit likes this.
  6. Offline

    felixfritz

    Instead of setting the block to air, what happens if you just cancel the event? (event.setCancelled(true); )
     
  7. Offline

    MrJoe223

    Thanks for spotting that. It works now. >.<



    Then the gold ore would remain unmined. By setting it to air, I remove the goldore.
     
Thread Status:
Not open for further replies.

Share This Page