Search in sources :

Example 6 with WeaponProfProvider

use of pcgen.cdom.helper.WeaponProfProvider in project pcgen by PCGen.

the class WeaponProfToken method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    List<String> list = new ArrayList<>();
    Changes<ChooseSelectionActor<?>> listChanges = context.getObjectContext().getListChanges(obj, ListKey.NEW_CHOOSE_ACTOR);
    Changes<WeaponProfProvider> changes = context.getObjectContext().getListChanges(obj, ListKey.WEAPONPROF);
    QualifiedObject<Boolean> deityweap = context.getObjectContext().getObject(obj, ObjectKey.HAS_DEITY_WEAPONPROF);
    Collection<WeaponProfProvider> added = changes.getAdded();
    Collection<ChooseSelectionActor<?>> listAdded = listChanges.getAdded();
    boolean foundAny = false;
    boolean foundOther = false;
    if (listAdded != null && !listAdded.isEmpty()) {
        foundOther = true;
        for (ChooseSelectionActor<?> cra : listAdded) {
            if (cra.getSource().equals(getTokenName())) {
                try {
                    list.add(cra.getLstFormat());
                } catch (PersistenceLayerException e) {
                    context.addWriteMessage("Error writing Prerequisite: " + e);
                    return null;
                }
            }
        }
    }
    if (deityweap != null && deityweap.getRawObject()) {
        foundOther = true;
        StringBuilder sb = new StringBuilder();
        sb.append("DEITYWEAPONS");
        if (deityweap.hasPrerequisites()) {
            sb.append('|');
            sb.append(context.getPrerequisiteString(deityweap.getPrerequisiteList()));
        }
        list.add(sb.toString());
    }
    if (added != null) {
        for (WeaponProfProvider wpp : added) {
            if (!wpp.isValid()) {
                context.addWriteMessage("Non-sensical " + "WeaponProfProvider in " + getFullName() + ": Had invalid contents");
                return null;
            }
            StringBuilder sb = new StringBuilder(wpp.getLstFormat());
            List<Prerequisite> prereqs = wpp.getPrerequisiteList();
            if (prereqs != null && !prereqs.isEmpty()) {
                if (prereqs.size() > 1) {
                    context.addWriteMessage("Error: " + obj.getClass().getSimpleName() + " had more than one Prerequisite for " + getFullName());
                    return null;
                }
                sb.append('|');
                sb.append(context.getPrerequisiteString(prereqs));
            }
            String lstFormat = sb.toString();
            boolean isUnconditionalAll = Constants.LST_ALL.equals(lstFormat);
            foundAny |= isUnconditionalAll;
            foundOther |= !isUnconditionalAll;
            list.add(lstFormat);
        }
    }
    if (foundAny && foundOther) {
        context.addWriteMessage("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + list);
        return null;
    }
    if (list.isEmpty()) {
        // Empty indicates no Token
        return null;
    }
    return list.toArray(new String[list.size()]);
}
Also used : ArrayList(java.util.ArrayList) ChooseSelectionActor(pcgen.cdom.base.ChooseSelectionActor) WeaponProfProvider(pcgen.cdom.helper.WeaponProfProvider) PersistenceLayerException(pcgen.persistence.PersistenceLayerException) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 7 with WeaponProfProvider

use of pcgen.cdom.helper.WeaponProfProvider in project pcgen by PCGen.

the class AutoWeaponProfFacet method dataAdded.

/**
	 * Processes an added CDOMObject to extract WeaponProf objects which are
	 * granted by AUTO:WEAPONPROF. These extracted WeaponProf objects are added
	 * to the Player Character.
	 * 
	 * Triggered when one of the Facets to which AutoWeaponProfFacet listens
	 * fires a DataFacetChangeEvent to indicate a CDOMObject was added to a
	 * Player Character.
	 * 
	 * @param dfce
	 *            The DataFacetChangeEvent containing the information about the
	 *            change
	 * 
	 * @see pcgen.cdom.facet.event.DataFacetChangeListener#dataAdded(pcgen.cdom.facet.event.DataFacetChangeEvent)
	 */
@Override
public void dataAdded(DataFacetChangeEvent<CharID, CDOMObject> dfce) {
    CDOMObject cdo = dfce.getCDOMObject();
    List<WeaponProfProvider> weaponProfs = cdo.getListFor(ListKey.WEAPONPROF);
    if (weaponProfs != null) {
        addAll(dfce.getCharID(), weaponProfs, cdo);
    }
}
Also used : CDOMObject(pcgen.cdom.base.CDOMObject) WeaponProfProvider(pcgen.cdom.helper.WeaponProfProvider)

Example 8 with WeaponProfProvider

use of pcgen.cdom.helper.WeaponProfProvider in project pcgen by PCGen.

the class WeaponProfToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
    String weaponProfs;
    // Do not initialize, null is significant!
    Prerequisite prereq = null;
    boolean isPre = false;
    if (value.indexOf("[") == -1) {
        // Supported version of PRExxx using |.  Needs to be at the front of the
        // Parsing code because many objects expect the pre to have been determined
        // Ahead of time.  Until deprecated code is removed, it will have to stay
        // like this.
        weaponProfs = value;
        StringTokenizer tok = new StringTokenizer(weaponProfs, Constants.PIPE);
        while (tok.hasMoreTokens()) {
            String token = tok.nextToken();
            if (PreParserFactory.isPreReqString(token)) {
                if (isPre) {
                    String errorText = "Invalid " + getTokenName() + ": " + value + "  PRExxx must be at the END of the Token";
                    Logging.errorPrint(errorText);
                    return new ParseResult.Fail(errorText, context);
                }
                prereq = getPrerequisite(token);
                if (prereq == null) {
                    return new ParseResult.Fail("Error generating Prerequisite " + prereq + " in " + getFullName(), context);
                }
                int preStart = value.indexOf(token) - 1;
                weaponProfs = value.substring(0, preStart);
                isPre = true;
                ParseResult fail = checkForLoopPrereqs(prereq, context);
                if (fail != null) {
                    return fail;
                }
            }
        }
    } else {
        return new ParseResult.Fail("Use of [] for Prerequisites has been removed. " + "Please use | based standard", context);
    }
    ParseResult pr = checkForIllegalSeparator('|', weaponProfs);
    if (!pr.passed()) {
        return pr;
    }
    boolean foundAny = false;
    boolean foundOther = false;
    StringTokenizer tok = new StringTokenizer(weaponProfs, Constants.PIPE);
    WeaponProfProvider wpp = new WeaponProfProvider();
    while (tok.hasMoreTokens()) {
        String token = tok.nextToken();
        if (Constants.LST_PERCENT_LIST.equals(token)) {
            foundOther = true;
            ChooseSelectionActor<WeaponProf> cra;
            if (prereq == null) {
                cra = this;
            } else {
                ConditionalSelectionActor<WeaponProf> cca = new ConditionalSelectionActor<>(this);
                cca.addPrerequisite(prereq);
                cra = cca;
            }
            context.getObjectContext().addToList(obj, ListKey.NEW_CHOOSE_ACTOR, cra);
        } else if ("DEITYWEAPONS".equals(token)) {
            foundOther = true;
            context.getObjectContext().put(obj, ObjectKey.HAS_DEITY_WEAPONPROF, new QualifiedObject<>(Boolean.TRUE, prereq));
        } else {
            if (Constants.LST_ALL.equalsIgnoreCase(token)) {
                foundAny = true;
                CDOMGroupRef<WeaponProf> allRef = context.getReferenceContext().getCDOMAllReference(WEAPONPROF_CLASS);
                wpp.addWeaponProfAll(allRef);
            } else {
                foundOther = true;
                if (token.startsWith(Constants.LST_TYPE_DOT) || token.startsWith(Constants.LST_TYPE_EQUAL)) {
                    CDOMGroupRef<WeaponProf> rr = TokenUtilities.getTypeReference(context, WEAPONPROF_CLASS, token.substring(5));
                    if (rr == null) {
                        return ParseResult.INTERNAL_ERROR;
                    }
                    wpp.addWeaponProfType(rr);
                } else {
                    CDOMSingleRef<WeaponProf> ref = context.getReferenceContext().getCDOMReference(WEAPONPROF_CLASS, token);
                    wpp.addWeaponProf(ref);
                }
            }
        }
    }
    if (foundAny && foundOther) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
    }
    if (!wpp.isEmpty()) {
        if (prereq != null) {
            wpp.addPrerequisite(prereq);
        }
        context.getObjectContext().addToList(obj, ListKey.WEAPONPROF, wpp);
    }
    return ParseResult.SUCCESS;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) WeaponProf(pcgen.core.WeaponProf) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) WeaponProfProvider(pcgen.cdom.helper.WeaponProfProvider) StringTokenizer(java.util.StringTokenizer) ConditionalSelectionActor(pcgen.cdom.content.ConditionalSelectionActor) QualifiedObject(pcgen.core.QualifiedObject) CDOMGroupRef(pcgen.cdom.reference.CDOMGroupRef) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 9 with WeaponProfProvider

use of pcgen.cdom.helper.WeaponProfProvider in project pcgen by PCGen.

the class WeaponProfTokenTest method loadTypeProf.

@Override
protected void loadTypeProf(String... types) {
    WeaponProfProvider wpp = new WeaponProfProvider();
    CDOMGroupRef<WeaponProf> ref = primaryContext.getReferenceContext().getCDOMTypeReference(WeaponProf.class, types);
    wpp.addWeaponProfType(ref);
    primaryProf.addToListFor(ListKey.WEAPONPROF, wpp);
}
Also used : WeaponProf(pcgen.core.WeaponProf) WeaponProfProvider(pcgen.cdom.helper.WeaponProfProvider)

Example 10 with WeaponProfProvider

use of pcgen.cdom.helper.WeaponProfProvider in project pcgen by PCGen.

the class WeaponProfTokenTest method testUnparseNullInList.

@Test
public void testUnparseNullInList() throws PersistenceLayerException {
    WeaponProfProvider wpp = new WeaponProfProvider();
    wpp.addWeaponProf(null);
    primaryProf.addToListFor(ListKey.WEAPONPROF, wpp);
    try {
        getToken().unparse(primaryContext, primaryProf);
        fail();
    } catch (NullPointerException e) {
    // Yep!
    }
}
Also used : WeaponProfProvider(pcgen.cdom.helper.WeaponProfProvider) Test(org.junit.Test)

Aggregations

WeaponProfProvider (pcgen.cdom.helper.WeaponProfProvider)10 WeaponProf (pcgen.core.WeaponProf)4 Test (org.junit.Test)3 Prerequisite (pcgen.core.prereq.Prerequisite)2 ArrayList (java.util.ArrayList)1 StringTokenizer (java.util.StringTokenizer)1 AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)1 CDOMListObject (pcgen.cdom.base.CDOMListObject)1 CDOMObject (pcgen.cdom.base.CDOMObject)1 ChooseSelectionActor (pcgen.cdom.base.ChooseSelectionActor)1 ConditionalSelectionActor (pcgen.cdom.content.ConditionalSelectionActor)1 VariableKey (pcgen.cdom.enumeration.VariableKey)1 ArmorProfProvider (pcgen.cdom.helper.ArmorProfProvider)1 ShieldProfProvider (pcgen.cdom.helper.ShieldProfProvider)1 CDOMGroupRef (pcgen.cdom.reference.CDOMGroupRef)1 CDOMSingleRef (pcgen.cdom.reference.CDOMSingleRef)1 QualifiedObject (pcgen.core.QualifiedObject)1 BonusObj (pcgen.core.bonus.BonusObj)1 PersistenceLayerException (pcgen.persistence.PersistenceLayerException)1 ParseResult (pcgen.rules.persistence.token.ParseResult)1