use of pcgen.cdom.choiceset.SpellCasterChoiceSet in project pcgen by PCGen.
the class SpellCasterToken method parseToken.
@Override
public ParseResult parseToken(LoadContext context, CDOMObject obj, String value) {
if (isEmpty(value)) {
return new ParseResult.Fail("Value in " + getFullName() + " may not be empty", context);
}
ParsingSeparator sep = new ParsingSeparator(value, '|');
sep.addGroupingPair('[', ']');
sep.addGroupingPair('(', ')');
String activeValue = sep.next();
Formula count;
if (!sep.hasNext()) {
count = FormulaFactory.ONE;
} else {
count = FormulaFactory.getFormulaFor(activeValue);
if (!count.isValid()) {
return new ParseResult.Fail("Count in " + getTokenName() + " was not valid: " + count.toString(), context);
}
if (count.isStatic() && count.resolveStatic().doubleValue() <= 0) {
return new ParseResult.Fail("Count in " + getFullName() + " must be > 0", context);
}
activeValue = sep.next();
}
if (sep.hasNext()) {
return new ParseResult.Fail(getFullName() + " had too many pipe separated items: " + value, context);
}
ParseResult pr = checkSeparatorsAndNonEmpty(',', activeValue);
if (!pr.passed()) {
return pr;
}
StringTokenizer tok = new StringTokenizer(activeValue, Constants.COMMA);
boolean foundAny = false;
boolean foundOther = false;
List<CDOMReference<PCClass>> groups = new ArrayList<>();
List<CDOMReference<PCClass>> prims = new ArrayList<>();
List<String> spelltypes = new ArrayList<>();
CDOMGroupRef<PCClass> allRef = context.getReferenceContext().getCDOMAllReference(PCCLASS_CLASS);
while (tok.hasMoreTokens()) {
String token = tok.nextToken();
if (Constants.LST_ANY.equalsIgnoreCase(token)) {
foundAny = true;
groups.add(allRef);
} else {
foundOther = true;
if (token.equals("Arcane") || token.equals("Divine") || token.equals("Psionic")) {
spelltypes.add(token);
} else if (token.startsWith(Constants.LST_TYPE_DOT) || token.startsWith(Constants.LST_TYPE_EQUAL)) {
CDOMReference<PCClass> ref = TokenUtilities.getTypeReference(context, PCCLASS_CLASS, token.substring(5));
if (ref == null) {
return new ParseResult.Fail(" Error was encountered while parsing " + getFullName() + ": " + token + " is not a valid reference: " + value, context);
}
groups.add(ref);
} else {
prims.add(context.getReferenceContext().getCDOMReference(PCCLASS_CLASS, token));
}
}
}
if (foundAny && foundOther) {
return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
}
ReferenceChoiceSet<PCClass> grcs = groups.isEmpty() ? null : new ReferenceChoiceSet<>(groups);
ReferenceChoiceSet<PCClass> prcs = prims.isEmpty() ? null : new ReferenceChoiceSet<>(prims);
SelectableSet<PCClass> cs = new SpellCasterChoiceSet(allRef, spelltypes, grcs, prcs);
cs.setTitle("Spell Caster Class Choice");
PersistentTransitionChoice<PCClass> tc = new ConcretePersistentTransitionChoice<>(cs, count);
context.getObjectContext().addToList(obj, ListKey.ADD, tc);
tc.setChoiceActor(this);
return ParseResult.SUCCESS;
}
Aggregations