a short timer every time a player clicks

Discussion in 'Plugin Development' started by amitlin14, Feb 26, 2013.

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

    amitlin14

    I need to make it so that every time a player clicks a very short timer is executed. The timer's length is 8 ticks.

    How will doing this affect the system? Will it generate huge lag? Will the server crash?
     
  2. use the schedular for this, if you use the schedular, it wont generate any lagg more than its needed to do
     
  3. Offline

    amitlin14

    I cant do what i need with a scheduler, if i could, i would. It can only be done with delays.
     
  4. with the schedular you can schedula a task to executed 8 ticks in the future, if you using a timer from java, you are going to have a bad time
     
  5. Offline

    fireblast709

    Code:
    new BukkitRunnable()
    {
        @Override
        public void run()
        {
            Bukkit.broadcastMessage("Hey look, Im being executed 5 seconds later");
        }
    }.runTaskLater(<plugin instance>, 100L);
     
  6. Offline

    amitlin14

    fireblast709 ferrybig

    Ill explain what im trying to do.

    Im trying to make a sorta mini game, where a player has to click the right mouse button in order to advance. There are several sequences of mouse clicks a player must do, so for example: right click - right click - left click, does X, but in order to see if a player clicked in the appropriate time frame (of 0.2 seconds), i need to do a timer for each click, because if the player did not do the right sequence in the appropriate amount of time, something will be executed.

    That is why i have to use timers, and not just record the time the player clicked, because i need to run a task at the end, if he doesnt.
     
  7. Start the task and if he clicks again before the task executes then cancel it and start another, simple as that.
    And 8 ticks is 0.4 seconds.

    And making tasks doesn't prevent lag, the code is still executed on the main thread but it won't increase lag if that's what you're asking, it's a fast process and assuming your code in the run() is fast too then it won't lag the server.
     
  8. Offline

    fireblast709

    HashMap<String, Long> lastClick. Store the last click in here with the playername. Also run a task, and check if he clicked within that amount of time. If not, do stuff
     
  9. Offline

    amitlin14

  10. You'd need each tick task to constantly check last click time... it's unefficient, do what I suggested but you will need a Map<String, BukkitTask> to cancel the previous task for that specific player.
     
  11. Offline

    amitlin14

    Digi

    Well, as simple as it sounds, how do i know when the player last clicked?

    Do you mean like, store the players name and the task in a hashmap, on an interact event, if the hashmap doesnt contain the key, put it, if it does... then what? cancel the task and put a new one instead? and when the task executes just remove it from the hashmap?

    Is that what you mean?
     
  12. In PlayerInteractEvent check for whatever click conditions you need, then:
    taskid = hashmap get player
    if taskid != null -> cancel task

    taskid = new task for 8 ticks
    hashmap put player name, task id

    In the task you'd want to also get the taskid for the player and check against null then in run() notify the player that he was too slow and reset the whatever you keep track of player clicks.

    You need to prevent the task from executing in order to succesfully complete the combo, the tasks are there to notify the player that he failed and reset the system that keeps track of the clicks.
     
Thread Status:
Not open for further replies.

Share This Page