Search in sources :

Example 1 with AssociationKey

use of pcgen.cdom.enumeration.AssociationKey in project pcgen by PCGen.

the class AbstractListContext method remove.

private <BT extends CDOMObject, L extends CDOMList<BT>> void remove(CDOMObject owner, CDOMObject neg, CDOMReference<L> list) {
    ListCommitStrategy commit = getCommitStrategy();
    Collection<CDOMReference<BT>> mods = neg.getListMods(list);
    for (CDOMReference<BT> ref : mods) {
        for (AssociatedPrereqObject assoc : neg.getListAssociations(list, ref)) {
            String token = assoc.getAssociation(AssociationKey.TOKEN);
            AssociatedPrereqObject edge = commit.removeFromList(token, owner, list, ref);
            Collection<AssociationKey<?>> associationKeys = assoc.getAssociationKeys();
            for (AssociationKey<?> ak : associationKeys) {
                setAssoc(assoc, edge, ak);
            }
            edge.addAllPrerequisites(assoc.getPrerequisiteList());
        }
    }
}
Also used : AssociationKey(pcgen.cdom.enumeration.AssociationKey) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Example 2 with AssociationKey

use of pcgen.cdom.enumeration.AssociationKey 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 3 with AssociationKey

use of pcgen.cdom.enumeration.AssociationKey in project pcgen by PCGen.

the class AbstractListContext method add.

private <BT extends CDOMObject, L extends CDOMList<BT>> void add(CDOMObject owner, CDOMObject neg, CDOMReference<L> list) {
    ListCommitStrategy commit = getCommitStrategy();
    Collection<CDOMReference<BT>> mods = neg.getListMods(list);
    for (CDOMReference<BT> ref : mods) {
        for (AssociatedPrereqObject assoc : neg.getListAssociations(list, ref)) {
            String token = assoc.getAssociation(AssociationKey.TOKEN);
            AssociatedPrereqObject edge = commit.addToList(token, owner, list, ref);
            Collection<AssociationKey<?>> associationKeys = assoc.getAssociationKeys();
            for (AssociationKey<?> ak : associationKeys) {
                setAssoc(assoc, edge, ak);
            }
            edge.addAllPrerequisites(assoc.getPrerequisiteList());
        }
    }
}
Also used : AssociationKey(pcgen.cdom.enumeration.AssociationKey) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Example 4 with AssociationKey

use of pcgen.cdom.enumeration.AssociationKey in project pcgen by PCGen.

the class AbstractListContext method cloneInMasterLists.

/**
	 * Create a copy of any associations to the original object and link them 
	 * to the new object. This will scan lists such as ClassSpellLists and 
	 * DomainSpellLists which may link to the original object. For each 
	 * association found, a new association will be created linking to the new object 
	 * and the association will be added to the list.
	 * 
	 * @param <T>    The type of CDOMObject being copied (e.g. Spell, Domain etc)
	 * @param cdoOld The original object being copied. 
	 * @param cdoNew The new object to be linked in.
	 */
@SuppressWarnings("unchecked")
<T extends CDOMObject> void cloneInMasterLists(T cdoOld, T cdoNew) {
    MasterListInterface masterLists = SettingsHandler.getGame().getMasterLists();
    for (CDOMReference ref : masterLists.getActiveLists()) {
        Collection<AssociatedPrereqObject> assocs = masterLists.getAssociations(ref, cdoOld);
        if (assocs != null) {
            for (AssociatedPrereqObject apo : assocs) {
                //					Logging.debugPrint("Found assoc from " + ref + " to "
                //							+ apo.getAssociationKeys() + " / "
                //							+ apo.getAssociation(AssociationKey.OWNER));
                AssociatedPrereqObject newapo = getCommitStrategy().addToMasterList(apo.getAssociation(AssociationKey.TOKEN), cdoNew, ref, cdoNew);
                newapo.addAllPrerequisites(apo.getPrerequisiteList());
                for (AssociationKey assocKey : apo.getAssociationKeys()) {
                    if (assocKey != AssociationKey.TOKEN && assocKey != AssociationKey.OWNER) {
                        newapo.setAssociation(assocKey, apo.getAssociation(assocKey));
                    }
                }
            }
        }
    }
}
Also used : AssociationKey(pcgen.cdom.enumeration.AssociationKey) MasterListInterface(pcgen.cdom.base.MasterListInterface) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Example 5 with AssociationKey

use of pcgen.cdom.enumeration.AssociationKey in project pcgen by PCGen.

the class AbstractListContext method commitDirect.

private <T extends CDOMObject, L extends CDOMList<T>> void commitDirect(CDOMReference<L> list) {
    ListCommitStrategy commit = getCommitStrategy();
    for (OwnerURI ou : edits.positiveMasterMap.getSecondaryKeySet(list)) {
        for (CDOMObject child : edits.positiveMasterMap.getTertiaryKeySet(list, ou)) {
            AssociatedPrereqObject assoc = edits.positiveMasterMap.get(list, ou, child);
            AssociatedPrereqObject edge = commit.addToMasterList(assoc.getAssociation(AssociationKey.TOKEN), ou.owner, list, (T) child);
            Collection<AssociationKey<?>> associationKeys = assoc.getAssociationKeys();
            for (AssociationKey<?> ak : associationKeys) {
                setAssoc(assoc, edge, ak);
            }
            edge.addAllPrerequisites(assoc.getPrerequisiteList());
        }
    }
}
Also used : AssociationKey(pcgen.cdom.enumeration.AssociationKey) CDOMObject(pcgen.cdom.base.CDOMObject) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Aggregations

AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)6 AssociationKey (pcgen.cdom.enumeration.AssociationKey)6 CDOMReference (pcgen.cdom.base.CDOMReference)5 CDOMObject (pcgen.cdom.base.CDOMObject)3 Formula (pcgen.base.formula.Formula)2 DoubleKeyMap (pcgen.base.util.DoubleKeyMap)2 Prerequisite (pcgen.core.prereq.Prerequisite)2 Spell (pcgen.core.spell.Spell)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 ParsingSeparator (pcgen.base.text.ParsingSeparator)1 TripleKeyMap (pcgen.base.util.TripleKeyMap)1 MasterListInterface (pcgen.cdom.base.MasterListInterface)1 Ungranted (pcgen.cdom.base.Ungranted)1 ParseResult (pcgen.rules.persistence.token.ParseResult)1