Code:
package org.maggot.server.model.world.cycleEvents;
/**
*
* @author Daniel
*/
public abstract class CycleEvent {
/**
* Called after the alloted number or cycles have passed.
*/
public abstract void execute(CycleEventContainer container);
/**
* Called on event stop.
*/
public abstract void stop();
}
Code:
package org.maggot.server.model.world.cycleEvents;
/**
*
* @author Daniel
*/
public class CycleEventContainer {
/**
* The container for the methods executed when the event is finished.
*/
private final CycleEvent event;
/**
* The number of ticks that should happen before the event is executed.
*/
private final int endCycleCount;
/**
* The amount of cycles that have passed since the event was first
* "registered".
*/
private int currentCycleCount;
/**
* Whether or not the event should be killed.
*/
private boolean killEvent;
public CycleEventContainer(CycleEvent event, int endCycleCount) {
currentCycleCount = 0;
this.event = event;
this.endCycleCount = endCycleCount;
}
/**
* Executes the event.
*/
public void execute() {
event.execute(this);
}
/**
* Stops the event from recurring.
*/
public void stop() {
killEvent = true;
event.stop();
}
/**
* Increases the current tick count by 1 and checks to see if the CycleEvent is
* to be executed.
* @return Whether or not the event shoult be executed.
*/
public boolean executeEvent() {
if(++currentCycleCount >= endCycleCount) {
currentCycleCount = 0;
return true;
}
return false;
}
/**
* Whether or not to remove the event from the list.
* @return
*/
public boolean removeEvent() {
return killEvent;
}
}
Code:
package org.maggot.server.model.world.cycleEvents;
import java.util.LinkedList;
/**
*
* @author Daniel
*/
public class CycleEventManager {
/**
* An ArrayList containing all information needed to run the events.
*/
private LinkedList<CycleEventContainer> currentEvents;
/**
* An ArrayList containing all the containers to be cleaned up after the
* cycle is finished.
*/
private LinkedList<CycleEventContainer> cleanupQueue;
/**
* Initializes class.
*/
public CycleEventManager() {
currentEvents = new LinkedList<CycleEventContainer>();
cleanupQueue = new LinkedList<CycleEventContainer>();
}
/**
*
* @param event The event to add to the event list.
* @param cycles The number of cycles that have to pass until the event is
* executed.
*/
public void addEvent(CycleEvent event, int cycles) {
currentEvents.add(new CycleEventContainer(event, cycles));
}
/**
* Called every 600ms (unless otherwise specified) and loops through all
* current events and decides whether or not to execute it.
*/
public void process() {
for(CycleEventContainer processedEvent : currentEvents) {
if(processedEvent == null) {
System.out.println("Prevented damaged event crash!");
return;
}
if(processedEvent.executeEvent()) {
processedEvent.execute();
}
if(processedEvent.removeEvent()) {
queueContainerCleanup(processedEvent);
}
}
cleanupFinishedEvents();
}
/**
* Adds a container to the cleaning (delete) queue.
* @param processedEvent The container to clean up.
*/
private void queueContainerCleanup(CycleEventContainer processedEvent) {
cleanupQueue.add(processedEvent);
}
/**
* Removes finished events from the event list.
*/
private void cleanupFinishedEvents() {
for(CycleEventContainer currentDeletion : cleanupQueue)
currentEvents.remove(currentDeletion);
cleanupQueue.clear();
}
}
Works 100%, no bugs!
Supports unlimited loop.
Example:
Code:
World.getWorld().getCycleEventManager().addEvent(new CycleEvent() {
@Override
public void execute(CycleEventContainer container) {
System.out.println("HI");
container.stop();
}
@Override
public void stop() {
System.out.println("Event killed.");
}
}, 5);
After 5 cycles (3 seconds), the event executes.