Block drop doesn't work

Discussion in 'Plugin Development' started by baggerboot, Dec 15, 2011.

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

    baggerboot

    I want mob spawners to drop a mob spawner item when somebody breaks them.

    I currently have this, but it doesn't work. I'm new to creating plugins for Bukkit, so it could be that
    I'm doing it completely the wrong way.
    Code:
        public void onBlockBreak(BlockBreakEvent event) {
            Block block = event.getBlock();
            Player player = event.getPlayer();
            World world = ((Entity) player).getWorld();
            if(block.getType() == Material.MOB_SPAWNER){
                ItemStack drop = new ItemStack(block.getType());
                drop.setAmount(1);
                drop.setType(Material.MOB_SPAWNER);
                world.dropItem(block.getLocation(), drop);
            }
        }
     
  2. Offline

    Trc202

    Are you getting any errors at all when you run the plugin?
    Another thing to check is how you register the listener.
     
  3. Offline

    baggerboot

    I am not getting any errors at all. I'll add/change the bold code and run it again. Maybe my weird approach on the item dropping code(the only way I knew to do it) caused some problems.
     
  4. Offline

    Trc202

    How are you registering the event?
     
  5. Offline

    baggerboot

    This is the BlockListener code.

    Code:
    package me.baggerboot.plugins.baggmcplugin;
    
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockListener;
    import org.bukkit.inventory.ItemStack;
    
    public class BMCBlockListener extends BlockListener {
    
        private final BaggmcPlugin plugin;
    
        public BMCBlockListener(BaggmcPlugin plugin) {
            this.plugin = plugin;
        }
    
        @Override
        public void onBlockBreak(BlockBreakEvent event) {
            if(event.isCancelled()) return;
    
            Block block = event.getBlock();
            Player player = event.getPlayer();
            World world = block.getWorld();
    
            if (block.getType() == Material.MOB_SPAWNER) {
                ItemStack drop = new ItemStack(block.getTypeId(), 1);
    
                world.dropItemNaturally(block.getLocation(), drop);
            }
        }
    }
    
    And this is the code for creating a BlockListener object:
    Code:
        private BlockListener blockListener = new BMCBlockListener(this);
     
  6. Offline

    CodeRedstone

    Are you actually registering the event?
     
  7. He wanted to know the code of your main class at the part where you register your events.
     
  8. Offline

    baggerboot

    Ohshi-

    I forgot to register the event at all...
    I've fixed it, I'll check if it works now.

    That solved the problem. As I said, I am still new to creating Bukkit plugins, so I just completely forgot about the event registering.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
    r3Fuze likes this.
Thread Status:
Not open for further replies.

Share This Page