use of pcgen.cdom.content.KnownSpellIdentifier in project pcgen by PCGen.
the class KnownspellsTokenTest method testUnparseTypeLevel.
@Test
public void testUnparseTypeLevel() throws PersistenceLayerException {
CDOMGroupRef<Spell> cool = primaryContext.getReferenceContext().getCDOMTypeReference(Spell.class, "Cool");
primaryProf.addToListFor(ListKey.KNOWN_SPELLS, new KnownSpellIdentifier(cool, 4));
String[] sap = getToken().unparse(primaryContext, primaryProf);
expectSingle(sap, "TYPE=Cool,LEVEL=4");
}
use of pcgen.cdom.content.KnownSpellIdentifier in project pcgen by PCGen.
the class KnownspellsToken method parseTokenWithSeparator.
@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCClass pcc, String value) {
StringTokenizer pipeTok = new StringTokenizer(value, Constants.PIPE);
boolean firstToken = true;
while (pipeTok.hasMoreTokens()) {
String totalFilter = pipeTok.nextToken();
if (Constants.LST_DOT_CLEAR_ALL.equals(totalFilter)) {
if (!firstToken) {
return new ParseResult.Fail("Non-sensical situation was " + "encountered while parsing " + getTokenName() + ": When used, .CLEARALL must be the first argument", context);
}
context.getObjectContext().removeList(pcc, ListKey.KNOWN_SPELLS);
continue;
}
ParseResult pr = checkForIllegalSeparator(',', totalFilter);
if (!pr.passed()) {
return pr;
}
StringTokenizer commaTok = new StringTokenizer(totalFilter, Constants.COMMA);
/*
* This is a rather interesting situation - this takes items that
* are ALLOWED and converts them to GRANTS. Therefore, this must be
* done as a post-manufacturing run on the Graph.
*
* As there is no guarantee when the factory is added that the list
* is complete, this resolution of known MUST be performed as a
* query against the PC, not stored in the graph as Grants edges.
*/
// must satisfy all elements in a comma delimited list
Integer levelLim = null;
CDOMReference<Spell> sp = null;
while (commaTok.hasMoreTokens()) {
String filterString = commaTok.nextToken();
if (filterString.startsWith("LEVEL=")) {
if (levelLim != null) {
return new ParseResult.Fail("Cannot have more than one Level limit in " + getTokenName() + ": " + value, context);
}
// the desired spellLevel
try {
levelLim = Integer.valueOf(filterString.substring(6));
if (levelLim < 0) {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addErrorMessage("Invalid Number in " + getTokenName() + ": " + value);
cpr.addErrorMessage(" Level must be >= 0");
return cpr;
}
} catch (NumberFormatException e) {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addErrorMessage("Invalid Number in " + getTokenName() + ": " + value);
cpr.addErrorMessage(" Level must be " + "a non-negative integer");
return cpr;
}
} else {
if (sp != null) {
return new ParseResult.Fail("Cannot have more than one Type/Spell limit in " + getTokenName() + ": " + value, context);
}
sp = TokenUtilities.getTypeOrPrimitive(context, SPELL_CLASS, filterString);
if (sp == null) {
return new ParseResult.Fail(" encountered Invalid limit in " + getTokenName() + ": " + value, context);
}
}
firstToken = false;
}
if (sp == null) {
/*
* There is no need to check for an invalid construction here
* (meaning levelLim is null as well) as that was implicitly
* checked by ensuring || did not occur.
*/
sp = context.getReferenceContext().getCDOMAllReference(SPELL_CLASS);
}
KnownSpellIdentifier ksi = new KnownSpellIdentifier(sp, levelLim);
context.getObjectContext().addToList(pcc, ListKey.KNOWN_SPELLS, ksi);
}
return ParseResult.SUCCESS;
}
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