use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class LocalSkillCostFacetTest method testAddBadSource.
@Test
public void testAddBadSource() {
Skill t1 = getObject();
PCClassLevel pcl = new PCClassLevel();
DataFacetChangeEvent<CharID, CDOMObject> dfce = new DataFacetChangeEvent<>(id, pcl, new Object(), DataFacetChangeEvent.DATA_ADDED);
ListKey<CDOMReference<Skill>> lk = ListKey.LOCALCSKILL;
pcl.addToListFor(lk, CDOMDirectSingleRef.getRef(t1));
try {
getFacet().dataAdded(dfce);
fail();
} catch (IllegalArgumentException e) {
// Yep!
}
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class LocalSkillCostFacetTest method testRemoveSecondSource.
@Test
public void testRemoveSecondSource() {
Skill t1 = getObject();
assertFalse(getFacet().contains(id, class1, SkillCost.CLASS, t1));
addCost(id, class1, t1, SkillCost.CLASS);
PCClassLevel pcl = new PCClassLevel();
pcl.put(ObjectKey.PARENT, class1);
DataFacetChangeEvent<CharID, CDOMObject> dfce = new DataFacetChangeEvent<>(id, pcl, new Object(), DataFacetChangeEvent.DATA_ADDED);
ListKey<CDOMReference<Skill>> lk = ListKey.LOCALCSKILL;
pcl.addToListFor(lk, CDOMDirectSingleRef.getRef(t1));
getFacet().dataAdded(dfce);
assertTrue(getFacet().contains(id, class1, SkillCost.CLASS, t1));
removeCosts(id, pcl);
assertTrue(getFacet().contains(id, class1, SkillCost.CLASS, t1));
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class SpelllevelLst method subParse.
private <CL extends Loadable & CDOMList<Spell>> boolean subParse(LoadContext context, CDOMObject obj, Class<CL> tagType, String tokString, String spellString, List<Prerequisite> prereqs) {
int equalLoc = tokString.indexOf(Constants.EQUALS);
if (equalLoc == -1) {
Logging.errorPrint("Expected an = in SPELLLEVEL " + "definition: " + tokString);
return false;
}
String casterString = tokString.substring(0, equalLoc);
String spellLevel = tokString.substring(equalLoc + 1);
Integer splLevel;
try {
splLevel = Integer.decode(spellLevel);
} catch (NumberFormatException nfe) {
Logging.errorPrint("Expected a number for SPELLLEVEL, found: " + spellLevel);
return false;
}
if (isEmpty(casterString) || hasIllegalSeparator(',', casterString)) {
return false;
}
StringTokenizer clTok = new StringTokenizer(casterString, Constants.COMMA);
List<CDOMReference<? extends CDOMList<Spell>>> slList = new ArrayList<>();
while (clTok.hasMoreTokens()) {
String classString = clTok.nextToken();
CDOMReference<CL> ref;
if (classString.startsWith("SPELLCASTER.")) {
/*
* This is actually a TYPE
*/
ref = context.getReferenceContext().getCDOMTypeReference(tagType, classString.substring(12));
} else {
ref = context.getReferenceContext().getCDOMReference(tagType, classString);
}
slList.add(ref);
}
if (hasIllegalSeparator(',', spellString)) {
return false;
}
StringTokenizer spTok = new StringTokenizer(spellString, ",");
while (spTok.hasMoreTokens()) {
String spellName = spTok.nextToken();
CDOMReference<Spell> sp = context.getReferenceContext().getCDOMReference(Spell.class, spellName);
for (CDOMReference<? extends CDOMList<Spell>> sl : slList) {
AssociatedPrereqObject tpr = context.getListContext().addToList(getTokenName(), obj, sl, sp);
tpr.setAssociation(AssociationKey.SPELL_LEVEL, splLevel);
tpr.addAllPrerequisites(prereqs);
}
}
return true;
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class SpellsLst method parseNonEmptyToken.
/**
* {@literal
* SPELLS:<spellbook name>|[<optional parameters, pipe deliminated>] |<spell
* name>[,<formula for DC>] |<spell name2>[,<formula2 for DC>] |PRExxx
* |PRExxx
*
* CASTERLEVEL=<formula> Casterlevel of spells TIMES=<formula> Cast Times
* per day, -1=At Will
*}
* @param sourceLine
* Line from the LST file without the SPELLS:
* @return spells list
*/
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String sourceLine) {
if (obj instanceof Ungranted) {
return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Ungranted object type: " + obj.getClass().getSimpleName(), context);
}
if ((sourceLine == null) || sourceLine.isEmpty()) {
return new ParseResult.Fail("Argument in " + getTokenName() + " cannot be empty", context);
}
if (sourceLine.equals(Constants.LST_DOT_CLEAR_ALL)) {
context.getListContext().removeAllFromList(getTokenName(), obj, Spell.SPELLS);
return ParseResult.SUCCESS;
}
ParsingSeparator sep = new ParsingSeparator(sourceLine, '|');
sep.addGroupingPair('[', ']');
sep.addGroupingPair('(', ')');
String spellBook = sep.next();
if (spellBook.isEmpty()) {
return new ParseResult.Fail("SpellBook in " + getTokenName() + " cannot be empty", context);
}
// Formula casterLevel = null;
String casterLevel = null;
String times = null;
String timeunit = null;
if (!sep.hasNext()) {
return new ParseResult.Fail(getTokenName() + ": minimally requires a Spell Name", context);
}
String token = sep.next();
while (true) {
if (token.startsWith("TIMES=")) {
if (times != null) {
return new ParseResult.Fail("Found two TIMES entries in " + getTokenName() + ": invalid: " + sourceLine, context);
}
times = token.substring(6);
if (times.isEmpty()) {
return new ParseResult.Fail("Error in Times in " + getTokenName() + ": argument was empty", context);
}
if (!sep.hasNext()) {
return new ParseResult.Fail(getTokenName() + ": minimally requires " + "a Spell Name (after TIMES=)", context);
}
token = sep.next();
} else if (token.startsWith("TIMEUNIT=")) {
if (timeunit != null) {
return new ParseResult.Fail("Found two TIMEUNIT entries in " + getTokenName() + ": invalid: " + sourceLine, context);
}
timeunit = token.substring(9);
if (timeunit.isEmpty()) {
return new ParseResult.Fail("Error in TimeUnit in " + getTokenName() + ": argument was empty", context);
}
if (!sep.hasNext()) {
return new ParseResult.Fail(getTokenName() + ": minimally requires " + "a Spell Name (after TIMEUNIT=)", context);
}
token = sep.next();
} else if (token.startsWith("CASTERLEVEL=")) {
if (casterLevel != null) {
return new ParseResult.Fail("Found two CASTERLEVEL entries in " + getTokenName() + ": invalid: " + sourceLine, context);
}
casterLevel = token.substring(12);
if (casterLevel.isEmpty()) {
return new ParseResult.Fail("Error in Caster Level in " + getTokenName() + ": argument was empty", context);
}
if (!sep.hasNext()) {
return new ParseResult.Fail(getTokenName() + ": minimally requires a " + "Spell Name (after CASTERLEVEL=)", context);
}
token = sep.next();
} else {
break;
}
}
if (times == null) {
times = "1";
}
if (token.isEmpty()) {
return new ParseResult.Fail("Spell arguments may not be empty in " + getTokenName() + ": " + sourceLine, context);
}
if (token.charAt(0) == ',') {
return new ParseResult.Fail(getTokenName() + " Spell arguments may not start with , : " + token, context);
}
if (token.charAt(token.length() - 1) == ',') {
return new ParseResult.Fail(getTokenName() + " Spell arguments may not end with , : " + token, context);
}
if (token.indexOf(",,") != -1) {
return new ParseResult.Fail(getTokenName() + " Spell arguments uses double separator ,, : " + token, context);
}
/*
* CONSIDER This is currently order enforcing the reference fetching to
* match the integration tests that we perform, and their current
* behavior. Not sure if this is really tbe best solution?
*
* See CDOMObject.
*/
DoubleKeyMap<CDOMReference<Spell>, AssociationKey<?>, Object> dkm = new DoubleKeyMap<>(LinkedHashMap.class, HashMap.class);
while (true) {
if (token.isEmpty()) {
return new ParseResult.Fail("Spell arguments may not end with comma or pipe in " + getTokenName() + ": " + sourceLine, context);
}
int commaLoc = token.indexOf(',');
String name = commaLoc == -1 ? token : token.substring(0, commaLoc);
CDOMReference<Spell> spell = context.getReferenceContext().getCDOMReference(Spell.class, name);
dkm.put(spell, AssociationKey.CASTER_LEVEL, casterLevel);
Formula timesFormula = FormulaFactory.getFormulaFor(times);
if (!timesFormula.isValid()) {
return new ParseResult.Fail("Times in " + getTokenName() + " was not valid: " + timesFormula.toString(), context);
}
dkm.put(spell, AssociationKey.TIMES_PER_UNIT, timesFormula);
if (timeunit != null) {
dkm.put(spell, AssociationKey.TIME_UNIT, timeunit);
}
dkm.put(spell, AssociationKey.SPELLBOOK, spellBook);
if (commaLoc != -1) {
dkm.put(spell, AssociationKey.DC_FORMULA, token.substring(commaLoc + 1));
}
if (!sep.hasNext()) {
// No prereqs, so we're done
finish(context, obj, dkm, null);
return ParseResult.SUCCESS;
}
token = sep.next();
if (looksLikeAPrerequisite(token)) {
break;
}
}
List<Prerequisite> prereqs = new ArrayList<>();
while (true) {
Prerequisite prereq = getPrerequisite(token);
if (prereq == null) {
return new ParseResult.Fail(" (Did you put spells after the " + "PRExxx tags in SPELLS:?)", context);
}
prereqs.add(prereq);
if (!sep.hasNext()) {
break;
}
token = sep.next();
}
finish(context, obj, dkm, prereqs);
return ParseResult.SUCCESS;
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class SpellknownLst method subParse.
/**
* Parse the tag contents after the SPELLKNOWN:CLASS| section.
*
* @param context the context under which the tag is being parsed.
* @param obj the obj The object owning the tag.
* @param tagType the type of object the tag creates
* @param tokString the tok string The string defining the caster type/class and spell level.
* @param spellString the spell string The string containing the spell name(s)
* @param prereqs the prereqs The prerequisites to be applied.
*
* @return true, if successful
*/
private <CL extends Loadable & CDOMList<Spell>> boolean subParse(LoadContext context, CDOMObject obj, Class<CL> tagType, String tokString, String spellString, List<Prerequisite> prereqs) {
int equalLoc = tokString.indexOf(Constants.EQUALS);
if (equalLoc == -1) {
Logging.errorPrint("Expected an = in SPELLKNOWN " + "definition: " + tokString);
return false;
}
String casterString = tokString.substring(0, equalLoc);
String spellLevel = tokString.substring(equalLoc + 1);
Integer splLevel;
try {
splLevel = Integer.decode(spellLevel);
} catch (NumberFormatException nfe) {
Logging.errorPrint("Expected a number for SPELLKNOWN, found: " + spellLevel);
return false;
}
if (isEmpty(casterString) || hasIllegalSeparator(',', casterString)) {
return false;
}
StringTokenizer clTok = new StringTokenizer(casterString, Constants.COMMA);
List<CDOMReference<? extends CDOMList<Spell>>> slList = new ArrayList<>();
while (clTok.hasMoreTokens()) {
String classString = clTok.nextToken();
CDOMReference<CL> ref;
if (classString.startsWith("SPELLCASTER.")) {
/*
* This is actually a TYPE
*/
ref = context.getReferenceContext().getCDOMTypeReference(tagType, classString.substring(12));
} else {
ref = context.getReferenceContext().getCDOMReference(tagType, classString);
}
slList.add(ref);
}
if (hasIllegalSeparator(',', spellString)) {
return false;
}
StringTokenizer spTok = new StringTokenizer(spellString, ",");
while (spTok.hasMoreTokens()) {
String spellName = spTok.nextToken();
CDOMReference<Spell> sp = context.getReferenceContext().getCDOMReference(Spell.class, spellName);
for (CDOMReference<? extends CDOMList<Spell>> sl : slList) {
AssociatedPrereqObject tpr = context.getListContext().addToList(getTokenName(), obj, sl, sp);
tpr.setAssociation(AssociationKey.SPELL_LEVEL, splLevel);
tpr.setAssociation(AssociationKey.KNOWN, Boolean.TRUE);
tpr.addAllPrerequisites(prereqs);
}
}
return true;
}
Aggregations