I have the following code, but I want it to run once every second, so if questNumber changes, then currentQuestStats will also change.
Editor's note: pastebin link with code that used to be here no longer works.
I have the following code, but I want it to run once every second, so if questNumber changes, then currentQuestStats will also change.
Editor's note: pastebin link with code that used to be here no longer works.
use ScheduledExecutorService
which added in JAVA 5, you must import following classes.
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
you can use it like below:
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// TODO add your code here:
}
}, 0, 1, TimeUnit.SECONDS);
Answer
checkQuestNumber()
in QuestsManagement
class.QuestsManagement
currentQuestStats
with this value (§e§lCurrent quest: Kill 1 zombie
).currentQuestStats
and questNumber
private and encapsulate them.here is your QuestsManagement
after those changes
public class QuestsManagement {
private String currentQuestStats = "§e§lCurrent quest: Kill 1 zombie";
private int questNumber = 1;
public String getCurrentQuestStats() {
return currentQuestStats;
}
public int getQuestNumber() {
return questNumber;
}
QuestsManagement() {
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
checkQuestNumber();
}
}, 0, 1, TimeUnit.SECONDS);
}
void checkQuestNumber() {
if (questNumber == 1) {
currentQuestStats = "§e§lCurrent quest: Kill 1 zombie";
// change manual!
questNumber = 2;
} else if (questNumber == 2) {
currentQuestStats = "§e§lCurrent quest: Walk 100 blocks";
// change manual!
questNumber = 1;
}
}
}
create an instance of QuestsManagement
and use getCurrentQuestStats()
method
QuestsManagement m = new QuestsManagement();
player.addChatMessage("Test" + QuestsManagement.getCurrentQuestStats());
Do it using multithreading. Here's the code:
public class QuestsManagement implements Runnable{
public String currentQuestStats = "";
public int questNumber = 1;
Thread t;
@Override
public void run() {
try {
if(questNumber == 1) {
currentQuestStats = "§e§lCurrent quest: Kill 1 zombie";
if(questNumber == 2) {
currentQuestStats = "§e§lCurrent quest: Walk 100 blocks";
}
}
Thread.sleep(1000);
}
catch(Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) {
Thread t = new Thread(new QuestsManagement());
t.start();
//do something else here... whatever you want to do
}
}
First of all, you need to add all of the code you want to run to a method. I will assume you will call it update()
.
What you then want to do is add something like the following to your main program loop:
long nextTick = currentTimeMillis() + 1000 ;//Set time of first game tick
QuestsManagement quest = new QuestsManagement();
//Main Loop
while (true) {
/*
Do some other stuff
*/
if (System. currentTimeMillis() >= nextTick) {//Check to see if it is time or past time to update
nextTick += 1000//Set next tick to one second in the future
quest.update();
}
}
Please note that you are missing the update()
method, the constructor, and any getters and setters. I would also refrain from making your variables public.
Well, there are multiple answers possible to this question. Java provides various ways to perform time operations. One approach is to use Timer class.
public class QuestsManagement {
public String currentQuestStats = "";
public int questNumber = 1;
static Timer t;
TimerTask task;
public static void main(String[] args) {
t = new Timer();
task = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
if(questNumber == 1) {
currentQuestStats = "§e§lCurrent quest: Kill 1 zombie";
if(questNumber == 2) {
currentQuestStats = "§e§lCurrent quest: Walk 100 blocks";
}
} ;
t.scheduleAtFixedRate(task , 1000, 1000 );
}}
import following
import java.util.Timer;
import java.util.TimerTask;