Search in sources :

Example 26 with CDOMReference

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!
    }
}
Also used : DataFacetChangeEvent(pcgen.cdom.facet.event.DataFacetChangeEvent) Skill(pcgen.core.Skill) CDOMObject(pcgen.cdom.base.CDOMObject) CDOMObject(pcgen.cdom.base.CDOMObject) CharID(pcgen.cdom.enumeration.CharID) CDOMReference(pcgen.cdom.base.CDOMReference) PCClassLevel(pcgen.cdom.inst.PCClassLevel) Test(org.junit.Test)

Example 27 with CDOMReference

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));
}
Also used : DataFacetChangeEvent(pcgen.cdom.facet.event.DataFacetChangeEvent) Skill(pcgen.core.Skill) CDOMObject(pcgen.cdom.base.CDOMObject) CDOMObject(pcgen.cdom.base.CDOMObject) CharID(pcgen.cdom.enumeration.CharID) CDOMReference(pcgen.cdom.base.CDOMReference) PCClassLevel(pcgen.cdom.inst.PCClassLevel) Test(org.junit.Test)

Example 28 with CDOMReference

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;
}
Also used : ArrayList(java.util.ArrayList) Spell(pcgen.core.spell.Spell) StringTokenizer(java.util.StringTokenizer) CDOMList(pcgen.cdom.base.CDOMList) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Example 29 with CDOMReference

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;
}
Also used : AssociationKey(pcgen.cdom.enumeration.AssociationKey) ParseResult(pcgen.rules.persistence.token.ParseResult) ArrayList(java.util.ArrayList) Ungranted(pcgen.cdom.base.Ungranted) Spell(pcgen.core.spell.Spell) Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) CDOMObject(pcgen.cdom.base.CDOMObject) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) DoubleKeyMap(pcgen.base.util.DoubleKeyMap) CDOMReference(pcgen.cdom.base.CDOMReference) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 30 with CDOMReference

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;
}
Also used : ArrayList(java.util.ArrayList) Spell(pcgen.core.spell.Spell) StringTokenizer(java.util.StringTokenizer) CDOMList(pcgen.cdom.base.CDOMList) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Aggregations

CDOMReference (pcgen.cdom.base.CDOMReference)125 ArrayList (java.util.ArrayList)57 StringTokenizer (java.util.StringTokenizer)40 AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)35 ParseResult (pcgen.rules.persistence.token.ParseResult)30 Skill (pcgen.core.Skill)24 ChoiceSet (pcgen.cdom.base.ChoiceSet)22 ConcretePersistentTransitionChoice (pcgen.cdom.base.ConcretePersistentTransitionChoice)21 ReferenceChoiceSet (pcgen.cdom.choiceset.ReferenceChoiceSet)19 Prerequisite (pcgen.core.prereq.Prerequisite)18 Formula (pcgen.base.formula.Formula)16 CDOMObject (pcgen.cdom.base.CDOMObject)15 Spell (pcgen.core.spell.Spell)15 TreeSet (java.util.TreeSet)14 Test (org.junit.Test)14 ClassSkillChoiceActor (pcgen.cdom.helper.ClassSkillChoiceActor)12 CharID (pcgen.cdom.enumeration.CharID)11 ObjectMatchingReference (pcgen.cdom.reference.ObjectMatchingReference)11 ParsingSeparator (pcgen.base.text.ParsingSeparator)10 CDOMList (pcgen.cdom.base.CDOMList)10