SYSTEM TIMERS
My First ACTUAL Tutorial
Step 1 (Uno): Create a class, and name it "Timer"
Step 2 (Dos): Copy and paste this into it:
Code:
public class Timer {
private int timeToRun; // time to run (in seconds)
private long startTime; // the time the timer started at
private boolean activeTimer; // is the timer currently active?
/**
* constructor sets up the amount of time to run and converts input from seconds to ms, gets the starting time of the timer, and sets the timer as active
* @param timeToRun The amout of time for the timer to run for (in seconds)
**/
public Timer(int timeToRun)
{
this.timeToRun = timeToRun*1000;
startTime = getStartTime();
activeTimer = true;
}
/**
* default constructor which initializes the variables to 0 and false(for the boolean)
**/
public Timer()
{
timeToRun = 0;
startTime = 0;
activeTimer = false;
}
/**
* determines the starting time of the timer
* @return the starting time of the timer in general
**/
private long getStartTime()
{
return System.currentTimeMillis();
}
/**
* calculates the amount of time that has passed since the start of the timer
* @return the amout of time has passed since the timer started
**/
private long timeSinceStart()
{
return System.currentTimeMillis() - startTime;
}
/**
* @return whether the timer is active or not
**/
public boolean isActive()
{
return activeTimer;
}
/**
* set whether the timer is active or not
* @param activeTimer is the timer active?
**/
public void setActive(boolean activeTimer)
{
this.activeTimer = activeTimer;
}
/**
* set the amount of time for the timer to run and gets the starting time of the timer
* @param timeToRun The amout of time for the timer to run for (in seconds)
**/
public void setTimeToRun(int timeToRun)
{
this.timeToRun = timeToRun*1000;
startTime = getStartTime();
}
/**
* @return how much time is left for the timer overall(in seconds)
**/
public int getTimeLeft()
{
return misc.round((double)((timeToRun - timeSinceStart())/1000));
}
/**
* determines whether or not the timer is finished running
* @return if the timer is finished running properly
**/
public boolean stop()
{
if (timeSinceStart() >= timeToRun)
return true;
return false;
}
}
Step 3 (Tres): Declare these variables, and the method given:
Code:
private String news[] = // actual news messages
{
"Message 1",
"Message 2",
"Message 3",
"Message 4",
"You can keep on going if you'd like."
};
private int newsIndex = 0; // index of the news array to print
private Timer newsTimer = new Timer(20); // news timer to print every 20 seconds
// sends the news message to player
public void news() {
sendMessage("[NEWS]"+news[newsIndex]);
newsIndex++;
if (newsIndex >= news.length)
newsIndex = 0;
}
// end news
Step 4 (Cuatro): We're going to add the processing of the timer, so, in your process method; add this:
Code:
// news processing
if (newsTimer.stop()) {
news();
newsTimer.setTimeToRun(20);
}
// ends the news process
Now, this honestly lasts as long as you have set it to.
Here's a quick example:
Timer:
Code:
private Timer sayHey = new Timer(30); // a 30 second timer
And then the process for running:
Code:
// if the timer is done and if it is active
if (sayHey.stop() && sayHey.isActive())
{
System.out.println("Hey!");
sayHi.setActive(false); // set active to false
}
This will keep the action from being done constantly when you don't want it to because technically stop() will return true until you set time again.
And if you wanted it to run again, you could say..
Code:
sayHey.setTimeToRun(30);
sayHey.setActive(true);