use of pcgen.cdom.reference.CDOMSingleRef in project pcgen by PCGen.
the class KitDeity method testApply.
@Override
public boolean testApply(Kit aKit, PlayerCharacter aPC, List<String> warnings) {
domainsToAdd = null;
theDeity = theDeityRef.get();
if (!aPC.canSelectDeity(theDeity)) {
warnings.add("DEITY: Cannot select deity \"" + theDeity.getDisplayName() + "\"");
return false;
}
aPC.setDeity(theDeity);
if (theDomains == null || theDomains.isEmpty()) {
// nothing else to do.
return true;
}
if (aPC.getMaxCharacterDomains() <= 0) {
warnings.add("DEITY: Not allowed to choose a domain");
return true;
}
int numberOfChoices;
if (choiceCount == null) {
numberOfChoices = theDomains.size();
} else {
numberOfChoices = choiceCount.resolve(aPC, "").intValue();
}
//
if (numberOfChoices > theDomains.size()) {
numberOfChoices = theDomains.size();
}
if (numberOfChoices == 0) {
// No choices allowed, we are done.
return true;
}
List<CDOMSingleRef<Domain>> xs;
if (numberOfChoices == theDomains.size()) {
xs = theDomains;
} else {
//
while (true) {
xs = Globals.getChoiceFromList("Choose Domains", theDomains, new ArrayList<>(), numberOfChoices, aPC);
if (!xs.isEmpty()) {
break;
}
}
}
//
for (CDOMSingleRef<Domain> ref : xs) {
Domain domain = ref.get();
if (!domain.qualifies(aPC, domain)) {
warnings.add("DEITY: Not qualified for domain \"" + domain.getDisplayName() + "\"");
continue;
}
if (aPC.getDomainCount() >= aPC.getMaxCharacterDomains()) {
warnings.add("DEITY: No more allowed domains");
return false;
}
if (!aPC.hasDefaultDomainSource()) {
warnings.add("DEITY: Cannot add domain \"" + domain.getDisplayName() + "\" as the character does not have a domain " + "source yet.");
return false;
}
if (domainsToAdd == null) {
domainsToAdd = new ArrayList<>();
}
domainsToAdd.add(domain);
aPC.addDomain(domain);
DomainApplication.applyDomain(aPC, domain);
}
aPC.calcActiveBonuses();
return true;
}
use of pcgen.cdom.reference.CDOMSingleRef in project pcgen by PCGen.
the class DomainToken method parseTokenWithSeparator.
@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCClass pcc, String value) {
StringTokenizer pipeTok = new StringTokenizer(value, Constants.PIPE);
boolean first = true;
String tok = pipeTok.nextToken();
if (looksLikeAPrerequisite(tok)) {
return new ParseResult.Fail("Cannot have only PRExxx subtoken in " + getTokenName() + ": " + value, context);
}
List<QualifiedObject<CDOMSingleRef<Domain>>> toAdd = new ArrayList<>();
boolean foundClear = false;
while (true) {
if (Constants.LST_DOT_CLEAR.equals(tok)) {
if (!first) {
return new ParseResult.Fail(" Non-sensical " + getTokenName() + ": .CLEAR was not the first list item", context);
}
context.getObjectContext().removeList(pcc, ListKey.DOMAIN);
foundClear = true;
} else {
CDOMSingleRef<Domain> domain = context.getReferenceContext().getCDOMReference(DOMAIN_CLASS, tok);
QualifiedObject<CDOMSingleRef<Domain>> qo = new QualifiedObject<>(domain);
toAdd.add(qo);
context.getObjectContext().addToList(pcc, ListKey.DOMAIN, qo);
}
first = false;
if (!pipeTok.hasMoreTokens()) {
// No prereqs, so we're done
return ParseResult.SUCCESS;
}
tok = pipeTok.nextToken();
if (looksLikeAPrerequisite(tok)) {
break;
}
}
if (foundClear) {
return new ParseResult.Fail("Cannot use PREREQs when using .CLEAR in " + getTokenName(), context);
}
while (true) {
Prerequisite prereq = getPrerequisite(tok);
if (prereq == null) {
return new ParseResult.Fail(" (Did you put feats after the " + "PRExxx tags in " + getTokenName() + ":?)", context);
}
for (PrereqObject pro : toAdd) {
pro.addPrerequisite(prereq);
}
if (!pipeTok.hasMoreTokens()) {
break;
}
tok = pipeTok.nextToken();
}
return ParseResult.SUCCESS;
}
use of pcgen.cdom.reference.CDOMSingleRef in project pcgen by PCGen.
the class DomainTokenTest method testUnparseSinglePre.
@Test
public void testUnparseSinglePre() throws PersistenceLayerException {
Domain wp1 = construct(primaryContext, "TestWP1");
CDOMDirectSingleRef<Domain> ref = CDOMDirectSingleRef.getRef(wp1);
PreParserFactory prereqParser = PreParserFactory.getInstance();
Prerequisite prereq = prereqParser.parse("PRERACE:1,Dwarf");
assertNotNull(prereq);
QualifiedObject<CDOMSingleRef<Domain>> qo = new QualifiedObject<>(ref, prereq);
primaryProf.addToListFor(ListKey.DOMAIN, qo);
String[] unparsed = getToken().unparse(primaryContext, primaryProf);
expectSingle(unparsed, getLegalValue() + "|PRERACE:1,Dwarf");
}
use of pcgen.cdom.reference.CDOMSingleRef in project pcgen by PCGen.
the class QualifyToken method unparse.
@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
Changes<Qualifier> changes = context.getObjectContext().getListChanges(obj, ListKey.QUALIFY);
if (changes == null || changes.isEmpty()) {
return null;
}
Collection<Qualifier> quals = changes.getAdded();
HashMapToList<String, CDOMSingleRef<?>> map = new HashMapToList<>();
for (Qualifier qual : quals) {
Class<? extends Loadable> cl = qual.getQualifiedClass();
String s = StringPClassUtil.getStringFor(cl);
CDOMSingleRef<?> ref = qual.getQualifiedReference();
String key = s;
if (ref instanceof CategorizedCDOMReference) {
Category<?> cat = ((CategorizedCDOMReference<?>) ref).getCDOMCategory();
key += '=' + cat.getKeyName();
}
map.addToListFor(key, ref);
}
Set<CDOMSingleRef<?>> set = new TreeSet<>(ReferenceUtilities.REFERENCE_SORTER);
Set<String> returnSet = new TreeSet<>();
for (String key : map.getKeySet()) {
set.clear();
set.addAll(map.getListFor(key));
StringBuilder sb = new StringBuilder();
sb.append(key).append(Constants.PIPE).append(ReferenceUtilities.joinLstFormat(set, Constants.PIPE));
returnSet.add(sb.toString());
}
return returnSet.toArray(new String[returnSet.size()]);
}
use of pcgen.cdom.reference.CDOMSingleRef in project pcgen by PCGen.
the class ForwardRefToken method unparse.
@Override
public String[] unparse(LoadContext context, Campaign obj) {
Changes<Qualifier> changes = context.getObjectContext().getListChanges(obj, ListKey.FORWARDREF);
if (changes == null || changes.isEmpty()) {
return null;
}
Collection<Qualifier> quals = changes.getAdded();
HashMapToList<String, CDOMSingleRef<?>> map = new HashMapToList<>();
for (Qualifier qual : quals) {
Class<? extends Loadable> cl = qual.getQualifiedClass();
String s = StringPClassUtil.getStringFor(cl);
CDOMSingleRef<?> ref = qual.getQualifiedReference();
String key = s;
if (ref instanceof CategorizedCDOMReference) {
String cat = ((CategorizedCDOMReference<?>) ref).getLSTCategory();
if (Constants.FEAT_CATEGORY.equals(cat)) {
key = Constants.FEAT_CATEGORY;
} else {
key += '=' + cat;
}
}
map.addToListFor(key, ref);
}
Set<CDOMSingleRef<?>> set = new TreeSet<>(ReferenceUtilities.REFERENCE_SORTER);
Set<String> returnSet = new TreeSet<>();
for (String key : map.getKeySet()) {
set.clear();
set.addAll(map.getListFor(key));
StringBuilder sb = new StringBuilder();
sb.append(key).append(Constants.PIPE).append(ReferenceUtilities.joinLstFormat(set, Constants.COMMA));
returnSet.add(sb.toString());
}
return returnSet.toArray(new String[returnSet.size()]);
}
Aggregations