Search in sources :

Example 6 with PrerequisiteWriter

use of pcgen.persistence.lst.output.prereq.PrerequisiteWriter in project pcgen by PCGen.

the class AdddomainsToken method unparse.

@Override
public String[] unparse(LoadContext context, PCClassLevel level) {
    AssociatedChanges<CDOMReference<Domain>> changes = context.getListContext().getChangesInList(getTokenName(), level, PCClass.ALLOWED_DOMAINS);
    Collection<CDOMReference<Domain>> removedItems = changes.getRemoved();
    if (removedItems != null && !removedItems.isEmpty() || changes.includesGlobalClear()) {
        context.addWriteMessage(getTokenName() + " does not support .CLEAR");
        return null;
    }
    MapToList<CDOMReference<Domain>, AssociatedPrereqObject> mtl = changes.getAddedAssociations();
    if (mtl == null || mtl.isEmpty()) {
        return null;
    }
    PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
    Set<String> set = new TreeSet<>();
    for (CDOMReference<Domain> domain : mtl.getKeySet()) {
        for (AssociatedPrereqObject assoc : mtl.getListFor(domain)) {
            StringBuilder sb = new StringBuilder(domain.getLSTformat(false));
            List<Prerequisite> prereqs = assoc.getPrerequisiteList();
            Prerequisite prereq;
            if (prereqs == null || prereqs.isEmpty()) {
                prereq = null;
            } else if (prereqs.size() == 1) {
                prereq = prereqs.get(0);
            } else {
                context.addWriteMessage("Added Domain from " + getTokenName() + " had more than one " + "Prerequisite: " + prereqs.size());
                return null;
            }
            if (prereq != null) {
                sb.append('[');
                StringWriter swriter = new StringWriter();
                try {
                    prereqWriter.write(swriter, prereq);
                } catch (PersistenceLayerException e) {
                    context.addWriteMessage("Error writing Prerequisite: " + e);
                    return null;
                }
                sb.append(swriter.toString());
                sb.append(']');
            }
            set.add(sb.toString());
        }
    }
    return new String[] { StringUtil.join(set, Constants.DOT) };
}
Also used : PrerequisiteWriter(pcgen.persistence.lst.output.prereq.PrerequisiteWriter) PersistenceLayerException(pcgen.persistence.PersistenceLayerException) StringWriter(java.io.StringWriter) TreeSet(java.util.TreeSet) Domain(pcgen.core.Domain) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 7 with PrerequisiteWriter

use of pcgen.persistence.lst.output.prereq.PrerequisiteWriter in project pcgen by PCGen.

the class CampaignLoader method validatePrereqs.

/**
	 * Check that all prerequisites specified in the PCC file are 
	 * supported. Any unsupported prereqs will be reported as LST 
	 * errors. This is a recursive function allowing it to 
	 * check nested prereqs.
	 * 
	 * @param prereqList The prerequisites to be checked.
	 */
private void validatePrereqs(List<Prerequisite> prereqList) {
    if (prereqList == null || prereqList.isEmpty()) {
        return;
    }
    for (Prerequisite prereq : prereqList) {
        if (prereq.isCharacterRequired()) {
            final PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
            ArrayList<Prerequisite> displayList = new ArrayList<>();
            displayList.add(prereq);
            String lstString = prereqWriter.getPrerequisiteString(displayList, Constants.TAB);
            Logging.log(Logging.LST_ERROR, "Prereq " + prereq.getKind() + " is not supported in PCC files. Prereq was " + lstString + " in " + campaign.getSourceURI() + ". Prereq will be ignored.");
        } else {
            validatePrereqs(prereq.getPrerequisites());
        }
    }
}
Also used : PrerequisiteWriter(pcgen.persistence.lst.output.prereq.PrerequisiteWriter) ArrayList(java.util.ArrayList) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 8 with PrerequisiteWriter

use of pcgen.persistence.lst.output.prereq.PrerequisiteWriter in project pcgen by PCGen.

the class CustomData method writePurchaseModeConfiguration.

/**
	 * Write custom purchase mode config
	 */
public static void writePurchaseModeConfiguration() {
    ensureCustomDirExists();
    final BufferedWriter bw = getPurchaseModeWriter();
    final SortedMap<Integer, PointBuyCost> pbStatCosts = SettingsHandler.getGame().getPointBuyStatCostMap();
    if (bw == null || pbStatCosts == null) {
        return;
    }
    try {
        bw.write("#");
        bw.newLine();
        bw.write(AUTO_GEN_WARN_LINE_1);
        bw.newLine();
        bw.write(AUTO_GEN_WARN_LINE_2);
        bw.newLine();
        bw.write("#");
        bw.newLine();
        bw.write("# Point-buy ability score costs");
        bw.newLine();
        bw.write("#");
        bw.newLine();
        if (!pbStatCosts.isEmpty()) {
            for (Integer statValue : pbStatCosts.keySet()) {
                final PointBuyCost pbc = pbStatCosts.get(statValue);
                bw.write("STAT:" + statValue.toString() + "\t\tCOST:" + Integer.toString(pbc.getBuyCost()));
                final int iCount = pbc.getPrerequisiteCount();
                if (iCount != 0) {
                    final StringWriter writer = new StringWriter();
                    for (Prerequisite prereq : pbc.getPrerequisiteList()) {
                        final PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
                        try {
                            writer.write("\t");
                            prereqWriter.write(writer, prereq);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }
                    bw.write(writer.toString());
                }
                bw.newLine();
            }
        }
        bw.write("#");
        bw.newLine();
        bw.write("# Point-buy methods");
        bw.newLine();
        bw.write("#");
        bw.newLine();
        for (PointBuyMethod pbm : SettingsHandler.getGame().getModeContext().getReferenceContext().getConstructedCDOMObjects(PointBuyMethod.class)) {
            bw.write("METHOD:" + pbm.getDisplayName() + "\t\tPOINTS:" + pbm.getPointFormula());
            bw.newLine();
        }
    } catch (IOException e) {
        Logging.errorPrint("Error in writePurchaseModeConfiguration", e);
    } finally {
        try {
            bw.close();
        } catch (IOException ex) {
            Logging.errorPrint("Error in writePurchaseModeConfiguration while closing", ex);
        }
    }
}
Also used : StringWriter(java.io.StringWriter) PrerequisiteWriter(pcgen.persistence.lst.output.prereq.PrerequisiteWriter) IOException(java.io.IOException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 9 with PrerequisiteWriter

use of pcgen.persistence.lst.output.prereq.PrerequisiteWriter in project pcgen by PCGen.

the class EquipToken method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    List<String> list = new ArrayList<>();
    PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
    Changes<ChooseSelectionActor<?>> listChanges = context.getObjectContext().getListChanges(obj, ListKey.NEW_CHOOSE_ACTOR);
    Changes<QualifiedObject<CDOMReference<Equipment>>> changes = context.getObjectContext().getListChanges(obj, ListKey.EQUIPMENT);
    Collection<QualifiedObject<CDOMReference<Equipment>>> added = changes.getAdded();
    HashMapToList<List<Prerequisite>, CDOMReference<Equipment>> m = new HashMapToList<>();
    if (added != null) {
        for (QualifiedObject<CDOMReference<Equipment>> qo : added) {
            m.addToListFor(qo.getPrerequisiteList(), qo.getRawObject());
        }
    }
    Collection<ChooseSelectionActor<?>> listAdded = listChanges.getAdded();
    if (listAdded != null && !listAdded.isEmpty()) {
        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;
                }
            }
        }
    }
    for (List<Prerequisite> prereqs : m.getKeySet()) {
        List<CDOMReference<Equipment>> eq = m.getListFor(prereqs);
        WeightedCollection<CDOMReference<Equipment>> refs = new WeightedCollection<>(ReferenceUtilities.REFERENCE_SORTER);
        refs.addAll(eq);
        String ab = ReferenceUtilities.joinLstFormat(refs, Constants.PIPE);
        if (prereqs != null && !prereqs.isEmpty()) {
            if (prereqs.size() > 1) {
                context.addWriteMessage("Error: " + obj.getClass().getSimpleName() + " had more than one Prerequisite for " + getFullName());
                return null;
            }
            Prerequisite p = prereqs.get(0);
            StringWriter swriter = new StringWriter();
            try {
                prereqWriter.write(swriter, p);
            } catch (PersistenceLayerException e) {
                context.addWriteMessage("Error writing Prerequisite: " + e);
                return null;
            }
            ab = ab + '|' + swriter.toString();
        }
        list.add(ab);
    }
    if (list.isEmpty()) {
        // Empty indicates no Token
        return null;
    }
    return list.toArray(new String[list.size()]);
}
Also used : PrerequisiteWriter(pcgen.persistence.lst.output.prereq.PrerequisiteWriter) ArrayList(java.util.ArrayList) ChooseSelectionActor(pcgen.cdom.base.ChooseSelectionActor) PersistenceLayerException(pcgen.persistence.PersistenceLayerException) HashMapToList(pcgen.base.util.HashMapToList) Equipment(pcgen.core.Equipment) StringWriter(java.io.StringWriter) QualifiedObject(pcgen.core.QualifiedObject) WeightedCollection(pcgen.base.util.WeightedCollection) ArrayList(java.util.ArrayList) HashMapToList(pcgen.base.util.HashMapToList) List(java.util.List) CDOMReference(pcgen.cdom.base.CDOMReference) Prerequisite(pcgen.core.prereq.Prerequisite)

Aggregations

Prerequisite (pcgen.core.prereq.Prerequisite)9 PrerequisiteWriter (pcgen.persistence.lst.output.prereq.PrerequisiteWriter)9 StringWriter (java.io.StringWriter)7 CDOMReference (pcgen.cdom.base.CDOMReference)6 PersistenceLayerException (pcgen.persistence.PersistenceLayerException)6 ArrayList (java.util.ArrayList)5 TreeSet (java.util.TreeSet)4 AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)4 DoubleKeyMapToList (pcgen.base.util.DoubleKeyMapToList)2 ChooseSelectionActor (pcgen.cdom.base.ChooseSelectionActor)2 Domain (pcgen.core.Domain)2 QualifiedObject (pcgen.core.QualifiedObject)2 Spell (pcgen.core.spell.Spell)2 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 List (java.util.List)1 HashMapToList (pcgen.base.util.HashMapToList)1 WeightedCollection (pcgen.base.util.WeightedCollection)1 ClassSpellList (pcgen.cdom.list.ClassSpellList)1 DomainSpellList (pcgen.cdom.list.DomainSpellList)1