Interrupting a Timer Set By an Event

Discussion in 'Plugin Development' started by Shad, May 28, 2013.

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

    Shad

    Hi, I'm looking to make a timer that is interrupted when the player looks or moves around, from outside forces or on their own, that was set after they began breaking a block. If the timer is completed, then whatever I want happens.

    I'm not really sure on how I can do the timer that is interrupted by a player movement, any help would be greatly appreciated.
     
  2. Offline

    Jogy34

    You can create a delayed task. When you create it an integer is returned. This is the task id. You can store this with the player's name and then if something happens you can just cancel the task. I think that should work.
     
    Shad likes this.
  3. Offline

    Shad

    Hello, after coming back to this I realize I still have some problems. Particularly, I'm having problems with cancelling the task with a PlayerMoveEvent. I'm not really sure on how I'd go about doing this, and again, any help would be greatly appreciated!
     
  4. Offline

    Garris0n

    Shad Save the id of the task in a HashMap with the player name and cancel it if they move.
     
  5. You could maybe try using the wait(100) method on your
    timer to suspend it on movement. Where 100 is 1/10 second in
    milliseconds. As this will just wait for 1/10 of a second every time
    that the PlayerMoveEvent() is called.

    That'd be my best guess, give it a shot, and see if it works.
    Add some output lines to see if the task you want to delay
    is being delayed/
     
  6. Offline

    Shad

    Garris0n
    My problem is more how to cancel it when they move. Like the actual code to do so, I have no idea how to go about it.

    InteritumStudios
    I don't think that's what I want really, unless I'm misunderstanding what you're saying.
     
  7. Offline

    CubieX

    Use the "BlockDamageEvent" to detect when a player starts to break a block.
    There you do what Garris0n and Jogy34 told you.
    You put the players name on a HashMap and add the TaskID of the BukkitTask of your .runTaskLater() scheduler as value.
    Here You also save the current players location to a "Location" object.
    (Use another HashMap for that, with the players name as key and the Location object as value)

    And in the "PlayerMoveEvent" you check if the players name is present in the first HashMap and if so, check if his current location is identical to the location he had when he begun to break the block. (which is saved in the second hashMap)
    If not, use your stored TaskID for that players timer, to cancel the scheduler
    and then remove the players name from both HashMaps.

    Finally, in the schedulers run() method, you also remove the player from both HashMaps and do
    whatever you like to do if the timer expired.

    And regarding what @InteritumStudios said:
    I think he did not fully understand what you want to achieve.
    But generally: As long as the used scheduler is a synchronous one
    (which you will be using here -> .runTaskLater() is a sync task), never use "wait()" in it,
    as this will freeze the whole main server thread.
     
    Shad likes this.
  8. Offline

    Shad

    CubieX
    Thanks a lot, I got it working beautifully in no time and you explained it so a simpleton like me could understand perfectly.
     
Thread Status:
Not open for further replies.

Share This Page