package net.sourceforge.pain.logic.event;
import net.sourceforge.pain.*;
import net.sourceforge.pain.data.*;
import net.sourceforge.pain.data.prototype.*;
import net.sourceforge.pain.data.type.*;
import net.sourceforge.pain.db.*;
import net.sourceforge.pain.logic.fn.*;
import net.sourceforge.pain.logic.fn.factory.*;
import net.sourceforge.pain.util.*;
import java.util.*;
/**
* PAiN Date: 14.04.2003 Time: 0:38:36
*/
public class ResetEvent extends AbstractEvent {
/** warn: single thread model!
*/
private static final Set tmp_passedSpaces = new HashSet();
private final static String DEFAULT_RESET_MESSAGE = "The earth rumbles with the after affects of another experience gone away...";
public Object execute(Object param) throws Exception {
ResetGroup g = (ResetGroup) param;
String resetMessage = g.getResetMessage();
Log.debug("ResetPlugin:Making reset on " + g.getGroupInfo());
if (resetMessage == null) {
resetMessage = DEFAULT_RESET_MESSAGE;
}
Set resets = g.getResets();
PainDB db = Core.getDB();
for (Iterator it = resets.iterator(); it.hasNext();) {
final Reset r = (Reset) it.next();
final SpaceReset sr = (SpaceReset) r.getRole(SpaceReset.class);
final Space space = sr.getLocation();
if (!tmp_passedSpaces.contains(space)) {
tmp_passedSpaces.add(space);
MessageOutFn.outSpace(space, resetMessage);
}
if (r.getLastResettedObject() != null) {
continue;
}
// single reset done in transaction.
// it will not agget to others if fail.
DbTransaction t = new DbTransaction() {
public Object execute(Object[] params) throws Exception {
// too simple initial impl: only space resets supported (do not worry: ROM has no other resets types :)) )
Prototype p = r.getResettedPrototype();
final LogicalObject obj = GlobalFactory.createObject(p);
final Located l = (Located) obj.getRole(Located.class);
if (l == null) {
throw new RuntimeException("Something wrong: space reset resets not located object!:" + obj);
}
RelocateFn.addToSpace(space, l);
r.setLastResettedObject(obj);
return null;
}
};
try {
db.execute(t);
} catch (Exception e) {
Log.error("ResetPlugin: something wrong!", e);
}
}
tmp_passedSpaces.clear();
return param;
}
}