use of pcgen.core.PlayerCharacter in project pcgen by PCGen.
the class CharacterFacadeImpl method getExportCharacter.
/**
* Retrieve a copy of the current character suitable for export. This
* attempts to minimise the expensive cloning function, by returning the
* previously cloned character if the base character has not changed in
* the meantime.
* @return A copy of the current character.
*/
private synchronized PlayerCharacter getExportCharacter() {
PlayerCharacter exportPc = lastExportChar;
if (exportPc == null || theCharacter.getSerial() != lastExportCharSerial) {
// Calling preparePCForOutput will mark export character as modified, so compare original character serial when checking for real changes
// Get serial at beginning so we can detect if a change occurs during clone and preparePCForOutput
lastExportCharSerial = theCharacter.getSerial();
exportPc = theCharacter.clone();
// Get the PC all up to date, (equipment and active bonuses etc)
exportPc.preparePCForOutput();
lastExportChar = exportPc;
// It is possible another thread changed PC during export; log for now, the next export will rebuild
int countSerialChanges = theCharacter.getSerial() - lastExportCharSerial;
if (countSerialChanges > 0) {
Logging.log(Logging.DEBUG, "Player character " + exportPc.getName() + " changed " + countSerialChanges + " times during export.");
}
}
return exportPc;
}
use of pcgen.core.PlayerCharacter in project pcgen by PCGen.
the class CharacterFacadeImpl method export.
/**
* @see pcgen.core.facade.CharacterFacade#export(pcgen.io.ExportHandler, java.io.BufferedWriter)
*/
@Override
public void export(ExportHandler theHandler, BufferedWriter buf) throws ExportException {
final int maxRetries = 3;
for (int i = 0; i < maxRetries; i++) {
try {
Logging.log(Logging.DEBUG, "Starting export at serial " + theCharacter.getSerial() + " to " + theHandler.getTemplateFile());
PlayerCharacter exportPc = getExportCharacter();
//PlayerCharacter exportPc = theCharacter;
theHandler.write(exportPc, buf);
Logging.log(Logging.DEBUG, "Finished export at serial " + theCharacter.getSerial() + " to " + theHandler.getTemplateFile());
return;
} catch (ConcurrentModificationException e) {
Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
for (Entry<Thread, StackTraceElement[]> threadEntry : allStackTraces.entrySet()) {
if (threadEntry.getValue().length > 1) {
StringBuilder sb = new StringBuilder("Thread: " + threadEntry.getKey() + "\n");
for (StackTraceElement elem : threadEntry.getValue()) {
sb.append(" ");
sb.append(elem.toString());
sb.append("\n");
}
Logging.log(Logging.INFO, sb.toString());
}
}
Logging.log(Logging.WARNING, "Retrying export after ConcurrentModificationException", e);
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
Logging.errorPrint("Interrupted sleep - probably closing.");
return;
}
}
}
Logging.errorPrint("Unable to export using " + theHandler.getTemplateFile() + " due to concurrent modifications.");
}
use of pcgen.core.PlayerCharacter in project pcgen by PCGen.
the class DescriptionActor method process.
@Override
public TemplateModel process(CharID id, PObject d) throws TemplateModelException {
List<Description> theBenefits = d.getListFor(listKey);
if (theBenefits == null) {
return FacetLibrary.getFacet(ObjectWrapperFacet.class).wrap(id, Constants.EMPTY_STRING);
}
PlayerCharacterTrackingFacet charStore = SpringHelper.getBean(PlayerCharacterTrackingFacet.class);
PlayerCharacter aPC = charStore.getPC(id);
final StringBuilder buf = new StringBuilder(250);
boolean needSpace = false;
for (final Description desc : theBenefits) {
final String str = desc.getDescription(aPC, Collections.singletonList(d));
if (!str.isEmpty()) {
if (needSpace) {
buf.append(' ');
}
buf.append(str);
needSpace = true;
}
}
return FacetLibrary.getFacet(ObjectWrapperFacet.class).wrap(id, buf.toString());
}
use of pcgen.core.PlayerCharacter in project pcgen by PCGen.
the class TempBonusHelper method removeBonusFromCharacter.
static void removeBonusFromCharacter(PlayerCharacter pc, Equipment aEq, CDOMObject aCreator) {
for (Map.Entry<BonusObj, BonusManager.TempBonusInfo> me : pc.getTempBonusMap().entrySet()) {
BonusObj aBonus = me.getKey();
TempBonusInfo tbi = me.getValue();
Object aC = tbi.source;
if (aCreator != aC) {
continue;
}
Object aT = tbi.target;
if ((aT instanceof Equipment) && (aEq != null)) {
if (aEq.equals(aT)) {
pc.removeTempBonus(aBonus);
pc.removeTempBonusItemList((Equipment) aT);
((Equipment) aT).removeTempBonus(aBonus);
((Equipment) aT).setAppliedName(EMPTY_STRING);
}
} else if ((aT instanceof PlayerCharacter) && (aEq == null)) {
pc.removeTempBonus(aBonus);
}
}
}
use of pcgen.core.PlayerCharacter in project pcgen by PCGen.
the class AbilitySelectionApplication method dataAdded.
@Override
public void dataAdded(DataFacetChangeEvent<CharID, CNAbilitySelection> dfce) {
CharID id = dfce.getCharID();
PlayerCharacter pc = pcFacet.getPC(id);
CNAbilitySelection cnas = dfce.getCDOMObject();
CNAbility cna = cnas.getCNAbility();
Ability ability = cna.getAbility();
String selection = cnas.getSelection();
if (selection != null) {
ChooseInformation<?> chooseInfo = ability.get(ObjectKey.CHOOSE_INFO);
if (chooseInfo != null) {
applySelection(pc, chooseInfo, cna, selection);
}
}
}
Aggregations