How do I call a method whenever the world reaches a certain time?

Discussion in 'Plugin Development' started by k9rosie, May 2, 2012.

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

    k9rosie

    I guess the question is more like where should I put the following code:

    Code:java
    1.  
    2. for(Player player : Bukkit.getOnlinePlayers()){
    3. if(player.getLocation().getWorld().getTime() == 14000){
    4. Bukkit.broadcastMessage(ChatColor.AQUA + "Tributes fallen:");
    5. if(players.get(player.getName()) == true){
    6. Bukkit.broadcastMessage(ChatColor.BLUE + player.getName());
    7. }
    8. }
    9.  
    10. }
    11.  


    However, I'm afraid this only works once, so how do I make it so whenever the world reaches the time 14000?
     
  2. Offline

    travja

    So you're making a hunger games plugin? You would put that in an event listener unless you wanted it to come somewhere else. My plugin does PlayerMoveEvent to get the time, so if a player moves and it's tick 14000 it does that same thing.
     
  3. Offline

    k9rosie

    But my problem with that is what if nobody is moving the time tick 14000 happens?
     
  4. Offline

    stelar7

    use a scheduler?
     
  5. Offline

    k9rosie

    I was thinking of this, but this makes me think on why there isn't a TimeChangeEvent
     
  6. Offline

    stelar7

    I think there used to be a WorldTickEvent once (not sure tho)
     
  7. A task should work just fine, make it run every 10 seconds or so... then check if time is bigger than 14000, don't check if it's absolutely equal because it will most likely never trigger.
    You can also check between values if your value will be configurable, like between value and value+500.

    Still, you need to think that code over because looping though all players and checking time over and over and OVER seems a bit too much... if you only need it for one world, define that world and check the timer first and then loop players, otherwise loop worlds first and then go through players...

    travja Why depend on players moving ? That's a bad usage and exploitable.

    EDIT: yeah more posts appeared while I was writing =)
     
    Prgr likes this.
  8. Offline

    k9rosie

    Couldn't I make the scheduler run at every tick? Or would that use a lot of hardware power...
     
  9. Yeah basically codes use CPU power, running it every tick would repeat that code 20 times per second... so unless you *really* need 100% accurancy for that thing, you don't need to run it every tick, you don't need it every second either unless it's critical to the game.
     
  10. Offline

    k9rosie

    The problem with this is that 14000 may be sunset, however, 599999 is day time, and the value 599999 > 14000.
     
  11. Offline

    Prgr

    Had this question awhile ago, I posted my source!
    Check it out,
    http://forums.bukkit.org/threads/tracking-time-resolved.65990/#post-1028669
     
  12. There is, but not in bukkit itself: http://dev.bukkit.org/server-mods/lib24time/#w-get-the-time

    Prgr At your solution you should really change AsyncRepeatingTask to SyncRepeatingTask, see http://wiki.bukkit.org/Scheduler_Programming
    Especially:
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  13. Offline

    k9rosie

    I'm stupid, I just realized that at the beginning of a new day, the time sets itself back down to zero.

    So I got it partially to work but the thing is is that it repeats itself when its night, when I want it to only display once.
     
  14. Offline

    CompuIves

    You can use a boolean for that, that it sets to false the first time it runs and sets to true at a specific time much later (midday or something like that).
     
  15. Offline

    Prgr

    V10lator Do you think it would be better if I changed my code to sync or it would be better for this Hunger Games plugin? Thanks,
     
  16. Prgr both. Never access bukkit methods from an async task (which is another thread). Read the link in my previous post for more information. :)
     
    Prgr likes this.
Thread Status:
Not open for further replies.

Share This Page