Search in sources :

Example 11 with KnownSpellIdentifier

use of pcgen.cdom.content.KnownSpellIdentifier in project pcgen by PCGen.

the class SpellsToken method unparse.

@Override
public String[] unparse(LoadContext context, KitSpells kitSpell) {
    StringBuilder sb = new StringBuilder();
    String spellBook = kitSpell.getSpellBook();
    String globalSpellbook = Globals.getDefaultSpellBook();
    if (spellBook != null && !globalSpellbook.equals(spellBook)) {
        sb.append("SPELLBOOK=").append(spellBook);
    }
    CDOMSingleRef<PCClass> castingClass = kitSpell.getCastingClass();
    if (castingClass != null) {
        if (sb.length() != 0) {
            sb.append(Constants.PIPE);
        }
        sb.append(Constants.LST_CLASS_EQUAL).append(castingClass.getLSTformat(false));
    }
    Collection<KnownSpellIdentifier> spells = kitSpell.getSpells();
    if (spells != null) {
        boolean needPipe = sb.length() > 0;
        for (KnownSpellIdentifier ksi : spells) {
            if (needPipe) {
                sb.append(Constants.PIPE);
            }
            needPipe = true;
            Collection<List<CDOMSingleRef<Ability>>> abilities = kitSpell.getAbilities(ksi);
            for (List<CDOMSingleRef<Ability>> abils : abilities) {
                StringBuilder spell = new StringBuilder();
                spell.append(StringUtil.replaceAll(ksi.getLSTformat(), Constants.LST_TYPE_EQUAL, Constants.LST_TYPE_DOT));
                if (abils != null && !abils.isEmpty()) {
                    spell.append('[');
                    spell.append(ReferenceUtilities.joinLstFormat(abils, "]["));
                    spell.append(']');
                }
                Integer count = kitSpell.getSpellCount(ksi, abils);
                if (count != 1) {
                    spell.append('=').append(count);
                }
                sb.append(spell);
            }
        }
    }
    if (sb.length() == 0) {
        return null;
    }
    return new String[] { sb.toString() };
}
Also used : Ability(pcgen.core.Ability) KnownSpellIdentifier(pcgen.cdom.content.KnownSpellIdentifier) PCClass(pcgen.core.PCClass) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) ArrayList(java.util.ArrayList) List(java.util.List)

Example 12 with KnownSpellIdentifier

use of pcgen.cdom.content.KnownSpellIdentifier in project pcgen by PCGen.

the class SpellsToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, KitSpells kitSpell, String value) {
    StringTokenizer aTok = new StringTokenizer(value, Constants.PIPE);
    ComplexParseResult pr = new ComplexParseResult();
    while (aTok.hasMoreTokens()) {
        String field = aTok.nextToken();
        if (field.startsWith("SPELLBOOK=")) {
            if (kitSpell.getSpellBook() != null) {
                return new ParseResult.Fail("Cannot reset SPELLBOOK in SPELLS: " + value, context);
            }
            String spellBook = field.substring(10);
            if (spellBook.isEmpty()) {
                return new ParseResult.Fail("Cannot set SPELLBOOK " + "to empty value in SPELLS: " + value, context);
            }
            kitSpell.setSpellBook(spellBook);
        } else if (field.startsWith(Constants.LST_CLASS_EQUAL)) {
            if (kitSpell.getCastingClass() != null) {
                return new ParseResult.Fail("Cannot reset CLASS" + " in SPELLS: " + value, context);
            }
            String className = field.substring(6);
            if (className.isEmpty()) {
                return new ParseResult.Fail("Cannot set CLASS " + "to empty value in SPELLS: " + value, context);
            } else if (className.equalsIgnoreCase("Default")) {
                pr.addWarningMessage("Use of Default for CLASS= in KIT " + "SPELLS line is unnecessary: Ignoring");
            } else {
                kitSpell.setCastingClass(context.getReferenceContext().getCDOMReference(PCClass.class, className));
            }
        } else {
            int count = 1;
            int equalLoc = field.indexOf(Constants.EQUALS);
            if (equalLoc != -1) {
                String countStr = field.substring(equalLoc + 1);
                try {
                    count = Integer.parseInt(countStr);
                } catch (NumberFormatException e) {
                    return new ParseResult.Fail("Expected an Integer COUNT," + " but found: " + countStr + " in " + value, context);
                }
                field = field.substring(0, equalLoc);
            }
            if (field.isEmpty()) {
                return new ParseResult.Fail("Expected an Spell in SPELLS" + " but found: " + value, context);
            }
            StringTokenizer subTok = new StringTokenizer(field, "[]");
            String filterString = subTok.nextToken();
            // must satisfy all elements in a comma delimited list
            CDOMReference<Spell> sp = null;
            sp = TokenUtilities.getTypeOrPrimitive(context, SPELL_CLASS, filterString);
            if (sp == null) {
                return new ParseResult.Fail("  encountered Invalid limit in " + getTokenName() + ": " + value, context);
            }
            KnownSpellIdentifier ksi = new KnownSpellIdentifier(sp, null);
            ArrayList<CDOMSingleRef<Ability>> featList = new ArrayList<>();
            while (subTok.hasMoreTokens()) {
                String featName = subTok.nextToken();
                CDOMSingleRef<Ability> feat = context.getReferenceContext().getCDOMReference(ABILITY_CLASS, AbilityCategory.FEAT, featName);
                featList.add(feat);
            }
            kitSpell.addSpell(ksi, featList, count);
        }
    }
    if (kitSpell.getSpellBook() == null) {
        kitSpell.setSpellBook(Globals.getDefaultSpellBook());
    }
    return pr;
}
Also used : Ability(pcgen.core.Ability) ComplexParseResult(pcgen.rules.persistence.token.ComplexParseResult) ParseResult(pcgen.rules.persistence.token.ParseResult) KnownSpellIdentifier(pcgen.cdom.content.KnownSpellIdentifier) ArrayList(java.util.ArrayList) ComplexParseResult(pcgen.rules.persistence.token.ComplexParseResult) PCClass(pcgen.core.PCClass) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) StringTokenizer(java.util.StringTokenizer) CDOMReference(pcgen.cdom.base.CDOMReference)

Example 13 with KnownSpellIdentifier

use of pcgen.cdom.content.KnownSpellIdentifier in project pcgen by PCGen.

the class KnownspellsTokenTest method testUnparseLevel.

@Test
public void testUnparseLevel() throws PersistenceLayerException {
    CDOMGroupRef<Spell> all = primaryContext.getReferenceContext().getCDOMAllReference(Spell.class);
    primaryProf.addToListFor(ListKey.KNOWN_SPELLS, new KnownSpellIdentifier(all, 4));
    String[] sap = getToken().unparse(primaryContext, primaryProf);
    expectSingle(sap, "LEVEL=4");
}
Also used : KnownSpellIdentifier(pcgen.cdom.content.KnownSpellIdentifier) Spell(pcgen.core.spell.Spell) Test(org.junit.Test)

Aggregations

KnownSpellIdentifier (pcgen.cdom.content.KnownSpellIdentifier)13 Spell (pcgen.core.spell.Spell)9 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 CDOMSingleRef (pcgen.cdom.reference.CDOMSingleRef)4 Ability (pcgen.core.Ability)4 List (java.util.List)3 PCClass (pcgen.core.PCClass)3 StringTokenizer (java.util.StringTokenizer)2 CDOMList (pcgen.cdom.base.CDOMList)2 CDOMReference (pcgen.cdom.base.CDOMReference)2 ComplexParseResult (pcgen.rules.persistence.token.ComplexParseResult)2 ParseResult (pcgen.rules.persistence.token.ParseResult)2 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 Formula (pcgen.base.formula.Formula)1 TreeMapToList (pcgen.base.util.TreeMapToList)1 PCClassLevel (pcgen.cdom.inst.PCClassLevel)1 CharacterSpell (pcgen.core.character.CharacterSpell)1