waiting time before canceling an event

Discussion in 'Plugin Development' started by bobthefish, Jun 5, 2014.

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

    bobthefish

    Hello, as the title says, im trying to schedule somethign to happen after 1 second. Its not happening and Im not getting any errors, it worked up to line 6
    Code:java
    1. @EventHandler
    2. public void onBreak(final BlockBreakEvent e){
    3. Player p = e.getPlayer();
    4. if(e.getBlock().getType() == Material.LOG){
    5. p.getInventory().addItem(new ItemStack(e.getBlock().getType(), 1));
    6. p.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    7. public void run() {
    8. e.setCancelled(true);
    9. }
    10. }, (20L));
     
  2. Offline

    NathanWolf

    You can't cancel an event after it's already happened, kind of a causality thing.

    If you want to allow the block to break but then restore it after some time, you will need to store the block state and then set it back in your scheduled task.
     
  3. Offline

    bobthefish

    NathanWolf Oh ok, can you by any chance help me on how to do that?
     
  4. Offline

    NathanWolf

    Look at Block.getState - you can save what that returns, then allow the block to break, then call update() (later on) in the stored BlockState to restore it... I think!
     
  5. Offline

    bobthefish

    NathanWolf OK, Ill try it out, Ill let you know how it works out :D

    NathanWolf is this what you had in mind?
    Code:java
    1. @EventHandler
    2. public void onBreak(final BlockBreakEvent e){
    3. Player p = e.getPlayer();
    4. if(e.getBlock().getType() == Material.LOG){
    5. final BlockState w= e.getBlock().getState();
    6. p.getInventory().addItem(new ItemStack(e.getBlock().getType(), 1));
    7. p.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    8. public void run() {
    9. w.update();
    10. }
    11. }, (20L));


    Anyone have an idea?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  6. bobthefish I have a revolutionary idea, have you tried firstly checking whether that works, and secondly checking the JavaDocs for the method you're using?
     
  7. Offline

    NathanWolf


    Yup! Does it work?
     
Thread Status:
Not open for further replies.

Share This Page