Search in sources :

Example 16 with PlayerCharacter

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;
}
Also used : PlayerCharacter(pcgen.core.PlayerCharacter)

Example 17 with PlayerCharacter

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.");
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) Entry(java.util.Map.Entry) PlayerCharacter(pcgen.core.PlayerCharacter) Map(java.util.Map) HashMap(java.util.HashMap)

Example 18 with PlayerCharacter

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());
}
Also used : Description(pcgen.core.Description) PlayerCharacter(pcgen.core.PlayerCharacter) ObjectWrapperFacet(pcgen.cdom.facet.ObjectWrapperFacet) PlayerCharacterTrackingFacet(pcgen.cdom.facet.PlayerCharacterTrackingFacet)

Example 19 with PlayerCharacter

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);
        }
    }
}
Also used : TempBonusInfo(pcgen.core.BonusManager.TempBonusInfo) PlayerCharacter(pcgen.core.PlayerCharacter) BonusObj(pcgen.core.bonus.BonusObj) Equipment(pcgen.core.Equipment) CDOMObject(pcgen.cdom.base.CDOMObject) Map(java.util.Map)

Example 20 with PlayerCharacter

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);
        }
    }
}
Also used : CNAbility(pcgen.cdom.content.CNAbility) Ability(pcgen.core.Ability) CNAbility(pcgen.cdom.content.CNAbility) PlayerCharacter(pcgen.core.PlayerCharacter) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) CharID(pcgen.cdom.enumeration.CharID)

Aggregations

PlayerCharacter (pcgen.core.PlayerCharacter)438 PreParserFactory (pcgen.persistence.lst.prereq.PreParserFactory)89 PCClass (pcgen.core.PCClass)61 LoadContext (pcgen.rules.context.LoadContext)58 Ability (pcgen.core.Ability)54 Equipment (pcgen.core.Equipment)41 PCTemplate (pcgen.core.PCTemplate)38 EquipSet (pcgen.core.character.EquipSet)32 BonusObj (pcgen.core.bonus.BonusObj)29 Race (pcgen.core.Race)28 CharID (pcgen.cdom.enumeration.CharID)25 ExportHandler (pcgen.io.ExportHandler)24 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)16 PreClassTester (plugin.pretokens.test.PreClassTester)16 CDOMObject (pcgen.cdom.base.CDOMObject)15 Skill (pcgen.core.Skill)14 CNAbility (pcgen.cdom.content.CNAbility)13 URI (java.net.URI)11 URISyntaxException (java.net.URISyntaxException)11