Solved BreakBlockEvent not registering

Discussion in 'Plugin Development' started by Jay23, Jul 29, 2014.

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

    Jay23

    I'm trying see if a player breaks a certain block, and if so, to give them a random chance to receive an item, but it doesn't even register that I've broken a block.

    Here's my main class
    Code:java
    1.  
    2. package coeus.airdrop;
    3.  
    4. import java.util.Random;
    5. import me.confuser.barapi.BarAPI;
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Location;
    9. import org.bukkit.Material;
    10. import org.bukkit.World;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class Airdrop extends JavaPlugin {
    17.  
    18.  
    19. public void onEnable() {
    20. new CrateListener(this);
    21. }
    22.  
    23. public void onDisable() {
    24.  
    25. }
    26.  
    27. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    28. if(cmd.getName().equalsIgnoreCase("airdrop") && sender instanceof Player) {
    29.  
    30. @SuppressWarnings("unused")
    31. Random random = new Random();
    32.  
    33. //int xcoord = (random.nextInt(20000) - 10000);
    34. //int ycoord = 150;
    35. //int zcoord = (random.nextInt(20000) - 10000);
    36. int xcoord = -500;
    37. int zcoord = 460;
    38. int ycoord = 150;
    39.  
    40. World world = Bukkit.getWorld("world");
    41.  
    42. Location airdrop = new Location(world, xcoord, ycoord, zcoord);
    43.  
    44. sender.sendMessage(ChatColor.GOLD + "Airdrop coordinates - X: " + xcoord + " Y: " + ycoord + " Z: " + zcoord);
    45.  
    46. Bukkit.broadcastMessage(ChatColor.GREEN + "There is a new airdrop at X: " + xcoord + " Y: " + ycoord + " Z: " + zcoord);
    47. BarAPI.setMessage((Player)sender, ChatColor.GOLD + "Airdrop located at X: " + xcoord + " Y: " + ycoord + " Z: " + zcoord, 100f);
    48.  
    49. spawnFalling(world, airdrop.add(0, 0, 0));
    50.  
    51. return true;
    52. }
    53. return false;
    54. }
    55.  
    56. @SuppressWarnings("deprecation")
    57. private void spawnFalling(World world, Location airdrop) {
    58. world.spawnFallingBlock(airdrop, Material.PISTON_BASE, (byte)6);
    59. }
    60. }
    61.  


    Here's the CrateListener
    Code:java
    1.  
    2. package coeus.airdrop;
    3.  
    4. import java.util.Random;
    5.  
    6. import org.bukkit.Material;
    7. import org.bukkit.block.Block;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.BlockBreakEvent;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.inventory.PlayerInventory;
    14.  
    15. public class CrateListener implements Listener {
    16.  
    17. public CrateListener(Airdrop plugin) {
    18. }
    19.  
    20. @EventHandler
    21. public void onCrateBreak(BlockBreakEvent event) {
    22.  
    23. Player player = event.getPlayer();
    24. PlayerInventory inventory = player.getInventory();
    25. Block block = event.getBlock();
    26. Random random = new Random();
    27. player.sendMessage("hit");
    28. if(block.getType() == Material.PISTON_BASE) {
    29. int obsidian = (random.nextInt(100));
    30. int diamond = (random.nextInt(100));
    31. int exp = (random.nextInt(100));
    32. int enchanting = (random.nextInt(100));
    33. int brewing = (random.nextInt(100));
    34. int trash = (random.nextInt(100));
    35. int anvil = (random.nextInt(100));
    36. int star = (random.nextInt(100));
    37.  
    38. if(obsidian < 30) {
    39. inventory.addItem(new ItemStack(Material.OBSIDIAN));
    40. }
    41.  
    42. if(diamond < 70) {
    43. inventory.addItem(new ItemStack(Material.DIAMOND));
    44. }
    45.  
    46. if(exp < 40) {
    47. inventory.addItem(new ItemStack(Material.EXP_BOTTLE));
    48. }
    49.  
    50. if(enchanting < 20) {
    51. inventory.addItem(new ItemStack(Material.ENCHANTMENT_TABLE));
    52. }
    53.  
    54. if(brewing < 5) {
    55. inventory.addItem(new ItemStack(Material.BREWING_STAND_ITEM));
    56. }
    57.  
    58. if(trash < 50) {
    59. inventory.addItem(new ItemStack(Material.DEAD_BUSH));
    60. }
    61.  
    62. if(anvil < 10) {
    63. inventory.addItem(new ItemStack(Material.ANVIL));
    64. }
    65.  
    66. if(star < 1) {
    67. inventory.addItem(new ItemStack(Material.NETHER_STAR));
    68. }
    69.  
    70. }
    71. }
    72. }
    73.  


    The entire point of the plugin is when you type /airdrop, it randomly (I've commented the randomizer out so the crate drops right in from of me) places a six-sided piston somewhere in the map at an elevation of 150 blocks that falls to the ground. It then broadcasts a message with the coordinates, and it displays the coordinates on the boss bar at the top of the screen. The command works, and the "crate" is created and falls to the ground below. All displays and text work correctly, but when I go to break the block, it breaks it normally and drops the block. I put in the line

    Code:java
    1.  
    2. player.sendMessage("hit");
    3.  


    under the BlockBreakEvent to see if it continues and runs that line, and sends a message to the player who broke the block, but no luck. Breaking the piston results in just the default breaking action.
     
  2. You've got to register it, you never did.

    In onEnable(), this.getServer().getPluginManager().registerEvents(new CrateListener(this), this);

    Edit: Also, you're not doing anything with your plugin instance object at the moment. (plugin being passed into CrateListener's constructor)
     
    Jay23 likes this.
  3. Offline

    Jay23

    KingFaris11 likes this.
Thread Status:
Not open for further replies.

Share This Page