use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class DomainsToken method unparse.
@Override
public String[] unparse(LoadContext context, Deity deity) {
CDOMReference<DomainList> dl = Deity.DOMAINLIST;
AssociatedChanges<CDOMReference<Domain>> changes = context.getListContext().getChangesInList(getTokenName(), deity, dl);
List<String> list = new ArrayList<>();
Collection<CDOMReference<Domain>> removedItems = changes.getRemoved();
if (changes.includesGlobalClear()) {
if (removedItems != null && !removedItems.isEmpty()) {
context.addWriteMessage("Non-sensical relationship in " + getTokenName() + ": global .CLEAR and local .CLEAR. performed");
return null;
}
list.add(Constants.LST_DOT_CLEAR);
} else if (removedItems != null && !removedItems.isEmpty()) {
list.add(Constants.LST_DOT_CLEAR_DOT + ReferenceUtilities.joinLstFormat(removedItems, ",.CLEAR.", true));
}
MapToList<CDOMReference<Domain>, AssociatedPrereqObject> mtl = changes.getAddedAssociations();
if (mtl != null && !mtl.isEmpty()) {
MapToList<Set<Prerequisite>, CDOMReference<Domain>> m = new HashMapToList<>();
for (CDOMReference<Domain> ab : mtl.getKeySet()) {
for (AssociatedPrereqObject assoc : mtl.getListFor(ab)) {
m.addToListFor(new HashSet<>(assoc.getPrerequisiteList()), ab);
}
}
Set<String> set = new TreeSet<>();
for (Set<Prerequisite> prereqs : m.getKeySet()) {
Set<CDOMReference<Domain>> domainSet = new TreeSet<>(ReferenceUtilities.REFERENCE_SORTER);
domainSet.addAll(m.getListFor(prereqs));
StringBuilder sb = new StringBuilder(ReferenceUtilities.joinLstFormat(domainSet, Constants.COMMA, true));
if (prereqs != null && !prereqs.isEmpty()) {
sb.append(Constants.PIPE);
sb.append(getPrerequisiteString(context, prereqs));
}
set.add(sb.toString());
}
list.addAll(set);
}
if (list.isEmpty()) {
return null;
}
return list.toArray(new String[list.size()]);
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class DomainsToken method parseTokenWithSeparator.
@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, Deity deity, String value) {
StringTokenizer pipeTok = new StringTokenizer(value, Constants.PIPE);
StringTokenizer commaTok = new StringTokenizer(pipeTok.nextToken(), Constants.COMMA);
CDOMReference<DomainList> dl = Deity.DOMAINLIST;
ArrayList<AssociatedPrereqObject> proList = new ArrayList<>();
boolean first = true;
boolean foundAll = false;
boolean foundOther = false;
boolean foundClear = false;
while (commaTok.hasMoreTokens()) {
String tokString = commaTok.nextToken();
if (looksLikeAPrerequisite(tokString)) {
return new ParseResult.Fail("Invalid " + getTokenName() + ": PRExxx was comma delimited : " + value, context);
}
if (Constants.LST_DOT_CLEAR.equals(tokString)) {
if (!first) {
return new ParseResult.Fail(" Non-sensical " + getTokenName() + ": .CLEAR was not the first list item: " + value, context);
}
context.getListContext().removeAllFromList(getTokenName(), deity, dl);
foundClear = true;
} else if (tokString.startsWith(Constants.LST_DOT_CLEAR_DOT)) {
CDOMReference<Domain> ref;
String clearText = tokString.substring(7);
if (Constants.LST_ALL.equals(clearText) || Constants.LST_ANY.equals(clearText)) {
ref = context.getReferenceContext().getCDOMAllReference(DOMAIN_CLASS);
} else {
ref = context.getReferenceContext().getCDOMReference(DOMAIN_CLASS, clearText);
}
context.getListContext().removeFromList(getTokenName(), deity, dl, ref);
foundClear = true;
} else if (Constants.LST_ALL.equals(tokString) || Constants.LST_ANY.equals(tokString)) {
CDOMGroupRef<Domain> ref = context.getReferenceContext().getCDOMAllReference(DOMAIN_CLASS);
proList.add(context.getListContext().addToList(getTokenName(), deity, dl, ref));
foundAll = true;
} else {
CDOMSingleRef<Domain> ref = context.getReferenceContext().getCDOMReference(DOMAIN_CLASS, tokString);
proList.add(context.getListContext().addToList(getTokenName(), deity, dl, ref));
foundOther = true;
}
first = false;
}
if (foundAll && foundOther) {
return new ParseResult.Fail("Non-sensical " + getTokenName() + ": Contains ALL and a specific reference: " + value, context);
}
while (pipeTok.hasMoreTokens()) {
if (foundClear) {
return new ParseResult.Fail("Cannot use PREREQs when using .CLEAR or .CLEAR. in " + getTokenName(), context);
}
String tokString = pipeTok.nextToken();
Prerequisite prereq = getPrerequisite(tokString);
if (prereq == null) {
return new ParseResult.Fail(" (Did you put items after the " + "PRExxx tags in " + getTokenName() + ":?)", context);
}
for (AssociatedPrereqObject ao : proList) {
ao.addPrerequisite(prereq);
}
}
return ParseResult.SUCCESS;
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class ModifyfeatchoiceToken method parseTokenWithSeparator.
@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, Ability ability, String value) {
StringTokenizer tok = new StringTokenizer(value, Constants.PIPE);
List<CDOMReference<Ability>> refs = new ArrayList<>();
ReferenceManufacturer<Ability> rm = context.getReferenceContext().getManufacturer(ABILITY_CLASS, AbilityCategory.FEAT);
while (tok.hasMoreTokens()) {
String token = tok.nextToken();
CDOMReference<Ability> ref = TokenUtilities.getTypeOrPrimitive(rm, token);
if (ref == null) {
return ParseResult.INTERNAL_ERROR;
}
refs.add(ref);
}
ReferenceChoiceSet<Ability> rcs = new ReferenceChoiceSet<>(refs);
ModifyChoiceDecorator gfd = new ModifyChoiceDecorator(rcs);
ChoiceSet<CNAbility> cs = new ChoiceSet<>(getTokenName(), gfd);
TabInfo ti = context.getReferenceContext().silentlyGetConstructedCDOMObject(TabInfo.class, Tab.ABILITIES.toString());
String singularName = ti.getResolvedName();
if (singularName.endsWith("s")) {
singularName = singularName.substring(0, singularName.length() - 1);
}
cs.setTitle("Select a " + singularName + " to modify");
TransitionChoice<CNAbility> tc = new ConcreteTransitionChoice<>(cs, FormulaFactory.ONE);
tc.setRequired(false);
context.getObjectContext().put(ability, ObjectKey.MODIFY_CHOICE, tc);
tc.setChoiceActor(this);
return ParseResult.SUCCESS;
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class PCTemplateTest method testAddLevelAbility.
/**
* Test the definition and application of abilities.
* @throws PersistenceLayerException
* @throws MalformedURLException
*/
public void testAddLevelAbility() throws PersistenceLayerException, MalformedURLException {
LoadContext context = Globals.getContext();
AbilityCategory cat = context.getReferenceContext().constructCDOMObject(AbilityCategory.class, "TestCat");
// Create some abilities to be added
Ability ab1 = new Ability();
ab1.setName("Ability1");
ab1.setCDOMCategory(cat);
context.getReferenceContext().importObject(ab1);
Ability ab2 = new Ability();
ab2.setName("Ability2");
ab2.setCDOMCategory(cat);
context.getReferenceContext().importObject(ab2);
CampaignSourceEntry source;
try {
source = new CampaignSourceEntry(new Campaign(), new URI("file:/" + getClass().getName() + ".java"));
} catch (URISyntaxException e) {
throw new UnreachableError(e);
}
loader.parseLine(context, null, "Template1 LEVEL:2:ABILITY:TestCat|AUTOMATIC|Ability1 ABILITY:TestCat|AUTOMATIC|Ability2", source);
PCTemplate template = context.getReferenceContext().silentlyGetConstructedCDOMObject(PCTemplate.class, "Template1");
context.getReferenceContext().importObject(ab1);
context.getReferenceContext().importObject(ab2);
CDOMSingleRef<AbilityCategory> acRef = context.getReferenceContext().getCDOMReference(AbilityCategory.class, "TestCat");
assertTrue(context.getReferenceContext().resolveReferences(null));
CDOMReference<AbilityList> autoList = AbilityList.getAbilityListReference(acRef, Nature.AUTOMATIC);
Collection<CDOMReference<Ability>> listMods = template.getListMods(autoList);
assertEquals(1, listMods.size());
Iterator<CDOMReference<Ability>> iterator = listMods.iterator();
CDOMReference<Ability> ref1 = iterator.next();
Collection<Ability> contained1 = ref1.getContainedObjects();
assertEquals(1, contained1.size());
assertTrue(contained1.contains(ab2));
List<PCTemplate> lvlTemplates = template.getSafeListFor(ListKey.LEVEL_TEMPLATES);
assertEquals(1, lvlTemplates.size());
PCTemplate lvl2 = lvlTemplates.get(0);
assertEquals(2, lvl2.get(IntegerKey.LEVEL).intValue());
listMods = lvl2.getListMods(autoList);
assertEquals(1, listMods.size());
iterator = listMods.iterator();
ref1 = iterator.next();
contained1 = ref1.getContainedObjects();
assertEquals(1, contained1.size());
assertTrue(contained1.contains(ab1));
// Add the template to the character
PlayerCharacter pc = getCharacter();
pc.addTemplate(template);
assertFalse("Character should not have ability1.", hasAbility(pc, cat, Nature.AUTOMATIC, ab1));
assertTrue("Character should have ability2.", hasAbility(pc, cat, Nature.AUTOMATIC, ab2));
// Level the character up, testing for when the level tag kicks in
pc.incrementClassLevel(1, testClass);
pc.calcActiveBonuses();
assertFalse("Character should not have ability1.", hasAbility(pc, cat, Nature.AUTOMATIC, ab1));
pc.incrementClassLevel(1, testClass);
pc.calcActiveBonuses();
assertTrue("Character should have ability1.", hasAbility(pc, cat, Nature.AUTOMATIC, ab1));
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class PCTemplateTest method testAddLevelFeatAbility.
/**
* Test the definition and application of abilities of category FEAT.
* @throws PersistenceLayerException
* @throws MalformedURLException
*/
public void testAddLevelFeatAbility() throws PersistenceLayerException, MalformedURLException {
// Create some abilities to be added
LoadContext context = Globals.getContext();
Ability ab1 = new Ability();
ab1.setName("Ability1");
ab1.setCDOMCategory(AbilityCategory.FEAT);
context.getReferenceContext().importObject(ab1);
Ability ab2 = new Ability();
ab2.setName("Ability2");
ab2.setCDOMCategory(AbilityCategory.FEAT);
context.getReferenceContext().importObject(ab2);
CampaignSourceEntry source;
try {
source = new CampaignSourceEntry(new Campaign(), new URI("file:/" + getClass().getName() + ".java"));
} catch (URISyntaxException e) {
throw new UnreachableError(e);
}
loader.parseLine(context, null, "Template1 LEVEL:2:ABILITY:Feat|AUTOMATIC|Ability1 ABILITY:Feat|AUTOMATIC|Ability2", source);
PCTemplate template = context.getReferenceContext().silentlyGetConstructedCDOMObject(PCTemplate.class, "Template1");
context.getReferenceContext().importObject(ab1);
context.getReferenceContext().importObject(ab2);
CDOMSingleRef<AbilityCategory> acRef = context.getReferenceContext().getCDOMReference(AbilityCategory.class, "Feat");
assertTrue(context.getReferenceContext().resolveReferences(null));
CDOMReference<AbilityList> autoList = AbilityList.getAbilityListReference(acRef, Nature.AUTOMATIC);
Collection<CDOMReference<Ability>> listMods = template.getListMods(autoList);
assertEquals(1, listMods.size());
Iterator<CDOMReference<Ability>> iterator = listMods.iterator();
CDOMReference<Ability> ref1 = iterator.next();
Collection<Ability> contained1 = ref1.getContainedObjects();
assertEquals(1, contained1.size());
assertTrue(contained1.contains(ab2));
List<PCTemplate> lvlTemplates = template.getSafeListFor(ListKey.LEVEL_TEMPLATES);
assertEquals(1, lvlTemplates.size());
PCTemplate lvl2 = lvlTemplates.get(0);
assertEquals(2, lvl2.get(IntegerKey.LEVEL).intValue());
listMods = lvl2.getListMods(autoList);
assertEquals(1, listMods.size());
iterator = listMods.iterator();
ref1 = iterator.next();
contained1 = ref1.getContainedObjects();
assertEquals(1, contained1.size());
assertTrue(contained1.contains(ab1));
// Add the template to the character
PlayerCharacter pc = getCharacter();
pc.addTemplate(template);
assertFalse("Character should not have ability1.", hasAbility(pc, AbilityCategory.FEAT, Nature.AUTOMATIC, ab1));
assertTrue("Character should have ability2.", hasAbility(pc, AbilityCategory.FEAT, Nature.AUTOMATIC, ab2));
// Level the character up, testing for when the level tag kicks in
pc.incrementClassLevel(1, testClass);
pc.calcActiveBonuses();
assertFalse("Character should not have ability1.", hasAbility(pc, AbilityCategory.FEAT, Nature.AUTOMATIC, ab1));
pc.incrementClassLevel(1, testClass);
pc.calcActiveBonuses();
assertTrue("Character should have ability1.", hasAbility(pc, AbilityCategory.FEAT, Nature.AUTOMATIC, ab1));
}
Aggregations