Search in sources :

Example 16 with CDOMReference

use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.

the class KnownspellsToken method unparse.

@Override
public String[] unparse(LoadContext context, PCClass pcc) {
    Changes<KnownSpellIdentifier> changes = context.getObjectContext().getListChanges(pcc, ListKey.KNOWN_SPELLS);
    List<String> list = new ArrayList<>();
    if (changes.includesGlobalClear()) {
        list.add(Constants.LST_DOT_CLEAR_ALL);
    }
    Collection<KnownSpellIdentifier> removedItems = changes.getRemoved();
    if (removedItems != null && !removedItems.isEmpty()) {
        context.addWriteMessage(getTokenName() + " does not support .CLEAR.");
        return null;
    }
    Collection<KnownSpellIdentifier> added = changes.getAdded();
    if (added != null && !added.isEmpty()) {
        TreeMapToList<CDOMReference<?>, Integer> map = new TreeMapToList<>(ReferenceUtilities.REFERENCE_SORTER);
        for (KnownSpellIdentifier ksi : added) {
            CDOMReference<Spell> ref = ksi.getSpellReference();
            Integer i = ksi.getSpellLevel();
            map.addToListFor(ref, i);
        }
        for (CDOMReference<?> ref : map.getKeySet()) {
            for (Integer lvl : map.getListFor(ref)) {
                StringBuilder sb = new StringBuilder();
                boolean needComma = false;
                String refString = ref.getLSTformat(false);
                if (!Constants.LST_ALL.equals(refString)) {
                    sb.append(refString);
                    needComma = true;
                }
                if (lvl != null) {
                    if (needComma) {
                        sb.append(',');
                    }
                    sb.append("LEVEL=").append(lvl);
                }
                list.add(sb.toString());
            }
        }
    }
    if (list.isEmpty()) {
        return null;
    }
    return new String[] { StringUtil.join(list, Constants.PIPE) };
}
Also used : KnownSpellIdentifier(pcgen.cdom.content.KnownSpellIdentifier) ArrayList(java.util.ArrayList) Spell(pcgen.core.spell.Spell) TreeMapToList(pcgen.base.util.TreeMapToList) CDOMReference(pcgen.cdom.base.CDOMReference)

Example 17 with CDOMReference

use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.

the class AdddomainsToken method unparse.

@Override
public String[] unparse(LoadContext context, PCClass pcc) {
    AssociatedChanges<CDOMReference<Domain>> changes = context.getListContext().getChangesInList(getTokenName(), pcc, PCClass.ALLOWED_DOMAINS);
    Collection<CDOMReference<Domain>> removedItems = changes.getRemoved();
    if (removedItems != null && !removedItems.isEmpty() || changes.includesGlobalClear()) {
        context.addWriteMessage(getTokenName() + " does not support .CLEAR");
        return null;
    }
    MapToList<CDOMReference<Domain>, AssociatedPrereqObject> mtl = changes.getAddedAssociations();
    if (mtl == null || mtl.isEmpty()) {
        return null;
    }
    PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
    Set<String> set = new TreeSet<>();
    Set<String> noPrereqSet = new TreeSet<>();
    for (CDOMReference<Domain> domain : mtl.getKeySet()) {
        for (AssociatedPrereqObject assoc : mtl.getListFor(domain)) {
            StringBuilder sb = new StringBuilder(domain.getLSTformat(false));
            List<Prerequisite> prereqs = assoc.getPrerequisiteList();
            if (prereqs == null || prereqs.isEmpty()) {
                noPrereqSet.add(sb.toString());
                continue;
            }
            for (Prerequisite prereq : prereqs) {
                sb.append(Constants.PIPE);
                StringWriter swriter = new StringWriter();
                try {
                    prereqWriter.write(swriter, prereq);
                } catch (PersistenceLayerException e) {
                    context.addWriteMessage("Error writing Prerequisite: " + e);
                    return null;
                }
                sb.append(swriter.toString());
            }
            set.add(sb.toString());
        }
    }
    if (!noPrereqSet.isEmpty()) {
        set.add(StringUtil.join(noPrereqSet, Constants.PIPE));
    }
    return set.toArray(new String[set.size()]);
}
Also used : PrerequisiteWriter(pcgen.persistence.lst.output.prereq.PrerequisiteWriter) PersistenceLayerException(pcgen.persistence.PersistenceLayerException) StringWriter(java.io.StringWriter) TreeSet(java.util.TreeSet) Domain(pcgen.core.Domain) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 18 with CDOMReference

use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.

the class CcskillToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCClass obj, String value) {
    boolean first = true;
    boolean foundAny = false;
    boolean foundOther = false;
    StringTokenizer tok = new StringTokenizer(value, Constants.PIPE);
    while (tok.hasMoreTokens()) {
        String tokText = tok.nextToken();
        if (Constants.LST_DOT_CLEAR.equals(tokText)) {
            if (!first) {
                return new ParseResult.Fail("  Non-sensical " + getTokenName() + ": .CLEAR was not the first list item", context);
            }
            context.getObjectContext().removeList(obj, ListKey.LOCALCCSKILL);
        } else if (tokText.startsWith(Constants.LST_DOT_CLEAR_DOT)) {
            String clearText = tokText.substring(7);
            if (Constants.LST_ALL.equals(clearText)) {
                context.getObjectContext().removeFromList(obj, ListKey.LOCALCCSKILL, context.getReferenceContext().getCDOMAllReference(SKILL_CLASS));
            } else {
                CDOMReference<Skill> ref = TokenUtilities.getTypeOrPrimitive(context, SKILL_CLASS, clearText);
                if (ref == null) {
                    return new ParseResult.Fail("  Error was encountered while parsing " + getTokenName(), context);
                }
                context.getObjectContext().removeFromList(obj, ListKey.LOCALCCSKILL, ref);
            }
        } else {
            /*
				 * Note this HAS to be done one-by-one, because the
				 * .clearChildNodeOfClass method above does NOT recognize the
				 * C/CC Skill object and therefore doesn't know how to search
				 * the sublists
				 */
            if (Constants.LST_ALL.equals(tokText)) {
                foundAny = true;
                context.getObjectContext().addToList(obj, ListKey.LOCALCCSKILL, context.getReferenceContext().getCDOMAllReference(SKILL_CLASS));
            } else {
                foundOther = true;
                CDOMReference<Skill> ref = getSkillReference(context, tokText);
                if (ref == null) {
                    return new ParseResult.Fail("  Error was encountered while parsing " + getTokenName(), context);
                }
                context.getObjectContext().addToList(obj, ListKey.LOCALCCSKILL, ref);
            }
        }
        first = false;
    }
    if (foundAny && foundOther) {
        return new ParseResult.Fail("Non-sensical " + getTokenName() + ": Contains ANY and a specific reference: " + value, context);
    }
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) ParseResult(pcgen.rules.persistence.token.ParseResult) CDOMReference(pcgen.cdom.base.CDOMReference)

Example 19 with CDOMReference

use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.

the class AbilityTokenTest method createSingle.

private List<CDOMReference<Ability>> createSingle(String name) {
    List<CDOMReference<Ability>> refs = new ArrayList<>();
    Ability obj = primaryContext.getReferenceContext().constructCDOMObject(Ability.class, name);
    primaryContext.getReferenceContext().reassociateCategory(AbilityCategory.FEAT, obj);
    CDOMDirectSingleRef<Ability> ar = CDOMDirectSingleRef.getRef(obj);
    refs.add(ar);
    if (name.indexOf('(') != -1) {
        List<String> choices = new ArrayList<>();
        AbilityUtilities.getUndecoratedName(name, choices);
        assertEquals(1, choices.size());
        ar.setChoice(choices.get(0));
    }
    return refs;
}
Also used : Ability(pcgen.core.Ability) ArrayList(java.util.ArrayList) CDOMReference(pcgen.cdom.base.CDOMReference)

Example 20 with CDOMReference

use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.

the class ClassSkillsLevelTokenTest method testUnparseSingleRanked.

@Test
public void testUnparseSingleRanked() throws PersistenceLayerException {
    List<CDOMReference<Skill>> refs = new ArrayList<>();
    addSingleRef(refs, "TestWP1");
    ReferenceChoiceSet<Skill> rcs = new ReferenceChoiceSet<>(refs);
    ChoiceSet<Skill> cs = new ChoiceSet<>(getSubToken().getTokenName(), rcs);
    PersistentTransitionChoice<Skill> tc = new ConcretePersistentTransitionChoice<>(cs, FormulaFactory.ONE);
    primaryProf.addToListFor(ListKey.ADD, tc);
    tc.setChoiceActor(new ClassSkillChoiceActor(fighter, 3));
    String[] unparsed = getToken().unparse(primaryContext, primaryProf);
    expectSingle(unparsed, getSubTokenName() + '|' + "TestWP1,AUTORANK=3");
}
Also used : ReferenceChoiceSet(pcgen.cdom.choiceset.ReferenceChoiceSet) ChoiceSet(pcgen.cdom.base.ChoiceSet) ArrayList(java.util.ArrayList) ConcretePersistentTransitionChoice(pcgen.cdom.base.ConcretePersistentTransitionChoice) Skill(pcgen.core.Skill) ClassSkillChoiceActor(pcgen.cdom.helper.ClassSkillChoiceActor) CDOMReference(pcgen.cdom.base.CDOMReference) ReferenceChoiceSet(pcgen.cdom.choiceset.ReferenceChoiceSet) Test(org.junit.Test)

Aggregations

CDOMReference (pcgen.cdom.base.CDOMReference)125 ArrayList (java.util.ArrayList)57 StringTokenizer (java.util.StringTokenizer)40 AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)35 ParseResult (pcgen.rules.persistence.token.ParseResult)30 Skill (pcgen.core.Skill)24 ChoiceSet (pcgen.cdom.base.ChoiceSet)22 ConcretePersistentTransitionChoice (pcgen.cdom.base.ConcretePersistentTransitionChoice)21 ReferenceChoiceSet (pcgen.cdom.choiceset.ReferenceChoiceSet)19 Prerequisite (pcgen.core.prereq.Prerequisite)18 Formula (pcgen.base.formula.Formula)16 CDOMObject (pcgen.cdom.base.CDOMObject)15 Spell (pcgen.core.spell.Spell)15 TreeSet (java.util.TreeSet)14 Test (org.junit.Test)14 ClassSkillChoiceActor (pcgen.cdom.helper.ClassSkillChoiceActor)12 CharID (pcgen.cdom.enumeration.CharID)11 ObjectMatchingReference (pcgen.cdom.reference.ObjectMatchingReference)11 ParsingSeparator (pcgen.base.text.ParsingSeparator)10 CDOMList (pcgen.cdom.base.CDOMList)10