Tracking Time? [Resolved]

Discussion in 'Plugin Development' started by Prgr, Mar 18, 2012.

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

    Prgr

    I was wondering what the best way is to go about setting the time to 23000 when the time is over 13000.
    I am going to set my server up to have 3 days and 2 nights that people have to then survive. Any help is appreciated.
    Mainly I want to know if I need to use an event listener, and I want to know which one even when no one is online.
     
  2. Offline

    VeryBIgCorp

    You could use a Timer and schedule it for every 50 milliseconds. Inside of the TimerTask's run method, make a check
    Code:
    if(getServer().getWorld("world").getTime() >	13000)
         getServer().getWorld("world").setTime(23000);
    
    Once I get back to my computer I'll post any other code it you need it.
     
    Prgr likes this.
  3. Offline

    Prgr

    Than
    Thanks for the reply, but what kind of event can I listen for in order to check time frequently like every 50 ticks?
     
  4. Offline

    SgtStud

    Prgr likes this.
  5. Offline

    Prgr

    Thanks Stud I will look into it.
    Do we know of any source code I can look at for this?
    Maybe a lottery plugin source?
     
  6. Offline

    VeryBIgCorp

    Well if you don't want to use a Scheduler (which you should), you just make a timer like this:
    Code:Java
    1.  
    2. TimeCheckTimer tim;
    3. public void onEnable(){
    4. t = new Timer();
    5. tim = new TimeCheckTimer();
    6. t.schedule(tim, 50, 50);
    7. }
    8.  
    9. public void onDisable(){
    10. if(t != null)
    11. t.cancel(); // Make sure this is here to stop the thread
    12. }
    13.  
    14. public class TimeCheckTimer extends TimerTask {
    15. public void run(){
    16. if(getServer().getWorld("world").getTime() > 13000)
    17. getServer().getWorld("world").setTime(23000);
    18. }
    19.  
     
  7. Offline

    Prgr

    Thanks guys I got it working!
    Code:java
    1.  
    2. public class Basic extends JavaPlugin{
    3. @Override
    4. public void onEnable(){
    5. Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new TimeTrack(), 0, 200L);
    6. }
    7. @Override
    8. public void onDisable(){
    9. }
    10. }
    11.  

    Code:java
    1.  
    2. class TimeTrack implements Runnable {
    3. public void run() {
    4. long time = Bukkit.getServer().getWorld("dev").getTime();
    5. if( time > 13000 && time < 23000 ) { //World name is dev, 13000 is the time Sun sets
    6. Bukkit.getServer().getWorld("dev").setTime(23000);// 23000 is the time Sun Rises
    7. }
    8. }
    9. }
    10.  


    My Final Code:
    Code:java
    1.  
    2. public class 3Days2Nights extends JavaPlugin{
    3. @Override
    4. public void onEnable(){
    5. Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new TimeTrack(), 0, 100L);
    6. }
    7. @Override
    8. public void onDisable(){
    9. }
    10. }
    11.  

    Code:java
    1.  
    2. class TimeTrack implements Runnable {
    3. int dayCount = 1;
    4. public void run() {
    5. long time = Bukkit.getServer().getWorld("dev").getTime();
    6. Bukkit.getServer().broadcastMessage(""+dayCount);
    7. Bukkit.getServer().broadcastMessage(""+time);
    8. switch (dayCount){
    9. case 1: //Day 1
    10. if( time > 23000 && time < 24000 ) {
    11. Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "The First Day");
    12. dayCount ++;
    13. }
    14. break;
    15. case 2: //Day 2
    16. if( time > 12000 && time < 23000 ) {
    17. Bukkit.getServer().getWorld("dev").setTime(23900);
    18. Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + "Hump Day");
    19. dayCount ++;
    20. }
    21. break;
    22. case 3: //Day 3
    23. if( time > 12000 && time < 23900 ) {
    24. Bukkit.getServer().getWorld("dev").setTime(23900);
    25. Bukkit.getServer().broadcastMessage(ChatColor.GOLD+ "The Last Day");
    26. dayCount ++;
    27. }
    28. break;
    29. case 4: //Day 4
    30. if(time > 14000 && time < 23900) {
    31. Bukkit.getServer().broadcastMessage(ChatColor.BLUE + "The First Night");
    32. dayCount ++;
    33. }
    34. break;
    35. case 5: //Day 5
    36. if( time > 20000) {
    37. Bukkit.getServer().getWorld("dev").setTime(14000);
    38. Bukkit.getServer().broadcastMessage(ChatColor.DARK_PURPLE + "The Final Night");
    39. dayCount = 1;
    40. }
    41. break;
    42. }
    43. }
    44. }
    45.  


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

    iMint

    Good job, glad you got it working :)
     
  9. Offline

    Prgr

    I am adding different difficulties for each dayCount Now
    First Day: Peaceful
    Hump Day: Easy
    Final Day: Easy
    First Night: Normal
    Final Night: Hard

    I love learning this stuff!
     
    iMint and VeryBIgCorp like this.
Thread Status:
Not open for further replies.

Share This Page