package net.sourceforge.pain.plugin.reset;
import net.sourceforge.pain.*;
import net.sourceforge.pain.data.type.*;
import net.sourceforge.pain.plugin.*;
import net.sourceforge.pain.util.*;
import java.util.*;
public final class ResetPlugin extends Plugin implements TimeListener {
public static final int RESETS_GRANULARITY = 10 * Time.PULSE_PER_SCD; // 3 seconds
private int nextCheckTime = 0;
public ResetPlugin() {
}
public void init() throws Exception {
Core.getTime().addListener(this);
}
public void deinit() {
Core.getTime().removeListener(this);
}
public void pulse(int time) {
if (nextCheckTime > time) {
return;
}
nextCheckTime = time + RESETS_GRANULARITY;
Collection resetGroups = Core.getWorld().getResetGroupRegistry().getResetGroups();
for (Iterator it = resetGroups.iterator(); it.hasNext();) {
ResetGroup g = (ResetGroup) it.next();
final int nextResetTime = g.getNextResetTime();
if (nextResetTime > time) {
nextCheckTime = nextResetTime < nextCheckTime ? nextResetTime : nextCheckTime;
} else {
reset(g);
int groupResetPeriod = g.getResetPeriod();
nextCheckTime = groupResetPeriod < nextCheckTime ? groupResetPeriod : nextCheckTime;
g.setNextResetTime(time + groupResetPeriod);
break; // one group per pulse
}
}
}
private static void reset(ResetGroup g) {
try {
Core.processEvent("ResetEvent", g);
} catch (Exception e) {
Log.error("ResetPlugin:", e);
}
}
}