How do I cancel block break event

Discussion in 'Plugin Development' started by xTeCnOxShAdOwZz, Apr 10, 2014.

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

    xTeCnOxShAdOwZz

    Hello, I am trying to make a plugin that allows a certain pick axe to destroy bedrock. The problem I am facing is that the block break event is triggered the moment a player clicks, so if the player stops mining it, the block will carry on being destroyed (time taken to destroy configured in the config.yml) and will then break. I want the bedrock to not be destroyed if the player stops actually mining it. Here is what I have so far (this is the only part of the code that affects the block being broken, so I won't paste the rest of the code. Also this is my first ever go at coding, so don't take the mick ;) )

    @EventHandler(priority=EventPriority.LOW)
    public void onPlayerInteract(PlayerInteractEvent event)
    {
    final Block block = event.getClickedBlock();
    final Player p = event.getPlayer();
    if ((event.getAction() == Action.RIGHT_CLICK_AIR) ||
    (event.getAction() == Action.RIGHT_CLICK_BLOCK) ||
    (event.getAction() == Action.PHYSICAL)) {
    return;
    }
    if (this.bedrockMiners.contains(p.getName()))
    {
    ItemStack item = p.getItemInHand();
    if ((item.getType() == this.reqItem.getType()) &&
    (event.getItem().getItemMeta().getDisplayName().equals(ChatColor.DARK_AQUA + getConfig().getString("Name"))) &&
    (event.getClickedBlock().getType().equals(Material.BEDROCK)) &&
    (block.getLocation().getY() >= this.minBreakY)) {
    Bukkit.getScheduler().scheduleSyncDelayedTask(this,
    new Runnable()
    {
    public void run()
    {
    if (p.getLocation().distance(block.getLocation()) < 6.0D)
    {

    block.setType(Material.AIR);
    block.getLocation().getBlock()
    .setType(Material.AIR);
    p.playSound(p.getLocation(), Sound.ENDERMAN_TELEPORT, 10, 1);
    p.getItemInHand().setDurability(
    (short) (p.getItemInHand().getDurability() + 40));
    ItemStack bedrock = new ItemStack(
    Material.BEDROCK, 1);
    if (Main.this.dropBedrock) {
    p.getWorld().dropItemNaturally(
    block.getLocation(),
    bedrock);
    }
    }
    }
    }, this.time * 20);
    }
    }
    }
    }
     
  2. xTeCnOxShAdOwZz Didn't really look at the code but I'm a bit confused - you mentioned BlockBreakEvent but you're using PlayerInteractEvent?
     
  3. Offline

    xTeCnOxShAdOwZz

    Sorry, that's what I meant, the playerInteractEvent
     
  4. xTeCnOxShAdOwZz I don't believe it's possible to check whether a player stops mining or not.

    EDIT: Could look into packets of course. Might be something in there.
     
  5. Offline

    xTeCnOxShAdOwZz

    well, couldn't I check at the end of the delay to see whether the item is still bedrock, if it isn't bedrock, don't do anything. if it is bedrock, set it to air? this is what I want, but I don't know the syntax
     
Thread Status:
Not open for further replies.

Share This Page