Timer problem.

Discussion in 'Plugin Development' started by halvors, Apr 9, 2011.

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

    halvors

    Hi!

    I'm creating a plugin, but need to add a Timer.

    Code:
    Player player = event.getPlayer();
    int delay = 10;
    
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        public void run() {
            player.sendMessage("10 secounds delay!");
        }
    }, delay);
    
    This do not work because TimerTask() is another class. How can i svole it? just need to run
    Code:
    player.sendMessage("10 secounds delay!");
    
    In the timer
     
  2. Offline

    Sammy

    Don't use the Timer Class, use bukkits scheduler
    Take a look at my tutorial

    Code:
     int OneTime = plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    
                public void run() {
    
     player.sendMessage("10 secounds delay!");
                }
            }, 10 * 20L);
     
  3. Offline

    halvors

    Nice, thanks :D

    But still have the problem that "public void run()" can't get variable player, since the function is in class Runnable()

    What do then?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
  4. Offline

    Sammy

    Declare Player as a global variable

    Do this outside the method
    Code:
    Player player;
    
    Then on the method just do:
    Code:
    player = event.getPlayer();
    
     
  5. Offline

    halvors

    Thanks, will try that. Don't know how i don't was thinking about this myself;)

    Sorry, seems like this is wrong, i have to just delay. Also:

    Code:
    if (delayTask) {
        // Delay 10 secounds
    }
    
    // Code
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
  6. Offline

    eisental

    If you just delay from your main thread you will freeze the whole server for 10 seconds.
     
  7. Offline

    halvors

    My goal is to check if player have done a task, if he has done, i'll prevent him to do it of other 30 secounds.
     
  8. Offline

    eisental

    Then you don't need to schedule anything. Just check next time the player tries to do whatever you want to prevent if 30 seconds have passed already or not using System.currentTimeMillis() for ex.
     
  9. Offline

    halvors

    How? Do i need to save the time or some better functions?
     
  10. Offline

    eisental

    You can store the time in a Map<Player, Long> so each player has its own time value and then calculate the difference between the current time and the time you stored in the map.
     
  11. Offline

    halvors

    Then i have to have 2 maps. I already have a map Map<Player, Player> since it's about pvp events.
     
  12. Offline

    eisental

    You could make another class to store both values

    Code:
    class X {
       Player pvp;
       long lastTimeOfTask;
    }
    
    and use this class for map values, but i would use 2 maps.
     
  13. Offline

    halvors

    Yes, was thinking about it :)

    And just have a List<X>.
     
Thread Status:
Not open for further replies.

Share This Page