Why not just use Threads instead of event managers? Is EventManager something you have created yourselves?
I recommend you read the following and implement your event handlers better:
Lesson: Writing Event Listeners
http://download.oracle.com/javase/tutorial/uiswing/events/index.html
Another solution would be to create the new Event() outside of the singleton and just pass it to addevent. Like so.
public void doubleBaubleCheck(int a) {
Event ev = new Event() {
public void execute(EventContainer g) {
Client c = (Client) PlayerHandler.players[a];
// ... your other shit here
}
}, 500);
EventManager.getSingleton().addEvent(ev);
}
This may run into a similar issue though..
Option 3
Because you're probably looking for Timers more than EventManagers. Take a look at this:
http://download.oracle.com/javase/1.4.2/docs/api/java/util/Timer.html
Yes, I wrote the EventManager:
A simple interface for an Event.
public interface Event {
/**
* Called when the event is executed.
*
* @param container
* The event container, so the event can dynamically change the
* tick time etc.
*/
public void execute(EventContainer container);
}
Holds extra data for an event (for example the tick time etc).
public class EventContainer {
/**
* The tick time in milliseconds.
*/
private int tick;
/**
* The actual event.
*/
private Event event;
/**
* A flag which specifies if the event is running;
*/
private boolean isRunning;
/**
* When this event was last run.
*/
private long lastRun;
/**
* The event container.
*
* @param evt
* @param tick
*/
protected EventContainer(Event evt, int tick) {
this.tick = tick;
this.event = evt;
this.isRunning = true;
this.lastRun = System.currentTimeMillis();
// can be changed to 0 if you want events to run straight away
}
/**
* Executes the event!
*/
public void execute() {
this.lastRun = System.currentTimeMillis();
this.event.execute(this);
}
/**
* Gets the last run time.
*
* @return
*/
public long getLastRun() {
return this.lastRun;
}
/**
* Returns the tick time.
*
* @return
*/
public int getTick() {
return this.tick;
}
/**
* Returns the is running flag.
*
* @return
*/
public boolean isRunning() {
return this.isRunning;
}
/**
* Stops this event.
*/
public void stop() {
System.out.println("EventManager: EventManaged command has been executed.");
this.isRunning = false;
}
}
Manages events which will be run in the future. Has its own thread since some
events may need to be ran faster than the cycle time in the main thread.
import java.util.ArrayList;
import java.util.List;
public class EventManager implements Runnable {
/**
* A reference to the singleton;
*/
private static EventManager singleton = null;
/**
* The waitFor variable is multiplied by this before the call to wait() is
* made. We do this because other events may be executed after waitFor is
* set (and take time). We may need to modify this depending on event count?
* Some proper tests need to be done.
*/
private static final double WAIT_FOR_FACTOR = 0.5;
/**
* Gets the event manager singleton. If there is no singleton, the singleton
* is created.
*
* @return The event manager singleton.
*/
public static EventManager getSingleton() {
if (singleton == null) {
singleton = new EventManager();
singleton.thread = new Thread(singleton);
singleton.thread.start();
}
return singleton;
}
/**
* Initialises the event manager (if it needs to be).
*/
public static void initialise() {
getSingleton();
}
/**
* A list of events that are being executed.
*/
private List<EventContainer> events;
/**
* The event manager thread. So we can interrupt it and end it nicely on
* shutdown.
*/
private Thread thread;
/**
* Initialise the event manager.
*/
private EventManager() {
events = new ArrayList<EventContainer>();
}
/**
* Adds an event.
*
* @param event
* The event to add.
* @param tick
* The tick time.
*/
public synchronized void addEvent(Event event, int tick) {
events.add(new EventContainer(event, tick));
notify();
}
@Override
/**
* Processes events. Works kinda like newer versions of cron.
*/
public synchronized void run() {
long waitFor = -1;
List<EventContainer> remove = new ArrayList<EventContainer>();
while (true) {
// reset wait time
waitFor = -1;
// process all events
for (EventContainer container : events) {
if (container.isRunning()) {
if ((System.currentTimeMillis() - container.getLastRun()) >= container
.getTick()) {
container.execute();
}
if (container.getTick() < waitFor || waitFor == -1) {
waitFor = container.getTick();
}
} else {
// add to remove list
remove.add(container);
}
}
// remove events that have completed
for (EventContainer container : remove) {
events.remove(container);
}
remove.clear();
// no events running
try {
if (waitFor == -1) {
wait(); // wait with no timeout
} else {
// an event is running, wait for that time or until a new
// event is added
int decimalWaitFor = (int) (Math.ceil(waitFor
* WAIT_FOR_FACTOR));
wait(decimalWaitFor);
}
} catch (InterruptedException e) {
break; // stop running
}
}
}
/**
* Shuts the event manager down.
*/
public void shutdown() {
this.thread.interrupt();
}
}