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() };
}
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;
}
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");
}
Aggregations