Sorry about no comments in the code but I blanked when trying to write them :/
Code:
/*
* CycleController
*
* By Fritz (Kataang)
*
* Controls all of the cycle events
*/
package server.cycle;
import java.util.ArrayList;
import server.Config;
import server.util.Misc;
public class CycleController implements Runnable {
public static void add(double cyclesBetweenRunning, double cyclesToRun,
Cycle cycle) {
cycles.add(new CycleContainer(cyclesBetweenRunning, cyclesToRun, cycle));
}
public static void add(double cyclesToRun, Cycle cycle) {
cycles.add(new CycleContainer(cyclesToRun, -1, cycle));
}
public void run() {
while (true) {
for (CycleContainer cycle : cycles) {
try {
if (cycle.lastRun + Config.SERVER_CYCLE
* cycle.cyclesBetweenRunning() <= System
.currentTimeMillis()
&& cycle.cyclesToRun() > cycle.cyclesRan) {
cycle.toDo();
cycle.cyclesRan++;
} else if (cycle.cyclesToRun() <= cycle.cyclesRan) {
toRemove.add(cycle);
}
} catch (Exception e) {
Misc.print("Error during cycle, stopping the cycle.");
}
}
for (CycleContainer cycle : toRemove) {
cycles.remove(cycle);
}
toRemove.clear();
}
}
private static ArrayList<CycleContainer> cycles = new ArrayList<CycleContainer>();
private ArrayList<CycleContainer> toRemove = new ArrayList<CycleContainer>();
}
Code:
/*
* CycleContainer
*
* By Fritz (Kataang)
*
* Contains a cycle
*/
package server.cycle;
import server.util.Misc;
public class CycleContainer {
public CycleContainer(double cyclesBetweenRunning, double cyclesToRun,
Cycle cycle) {
this.cycle = cycle;
this.cyclesBetweenRunning = cyclesBetweenRunning;
this.cyclesToRun = cyclesToRun;
if (cyclesToRun > 0)
cyclesRan = 0;
lastRun = System.currentTimeMillis();
}
public void toDo() {
this.lastRun = System.currentTimeMillis();
cycle.toDo(this);
}
public void end(Cycle cycle) {
cyclesRan = cyclesToRun;
}
public double lastRun() {
return lastRun;
}
public double cyclesToRun() {
return cyclesToRun;
}
public double cyclesBetweenRunning() {
return cyclesBetweenRunning;
}
private Cycle cycle;
private double cyclesToRun, cyclesBetweenRunning;
public double cyclesRan = Double.MIN_VALUE, lastRun;
}
Code:
/*
* Cycle
*
* By Fritz (Kataang)
*
* Interface for a cycle
*/
package server.cycle;
public interface Cycle {
public void toDo(CycleContainer cycleContainer);
}
And in Server.java below
Code:
Connection.initialize();
add
Code:
new Thread(new CycleController()).start();
And in Config.java add
Code:
public static final double SERVER_CYCLE = 600;
__________________________________________________ __________________________________________________ ____________________________________
There are 2 ways you can use this, one, you can run a cycle based event a certain number of times with an interval defined number of cycles between each execution OR you can run a cycle based event with an interval of a defined number of cycles.
Usage #1:
Code:
CycleController.add(4, 10, new Cycle() {
public void toDo(CycleContainer con) {
sendMessage("Hey there!");
}
});
The 4 is the number of cycles between each execution and the 10 defines how many times it will execute the code inside public void toDo()
Code:
CycleController.add(4, 10, new Cycle() {
Code:
public void toDo(CycleContainer con) {
sendMessage("Hey there!");
}
Usage #2:
Code:
CycleController.add(4, new Cycle() {
public void toDo(CycleContainer con) {
sendMessage("Hey there!");
}
});
The 4 is the number of cycles between each execution of the code inside toDo().
Code:
CycleController.add(4, new Cycle() {
Code:
public void toDo(CycleContainer con) {
sendMessage("Hey there!");
}
To end a cycle event, use this inside the toDo() void: