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) };
}
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());
}
}
}
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);
}
}
}
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()]);
}
Aggregations