use of pcgen.cdom.helper.CNAbilitySelection in project pcgen by PCGen.
the class AbstractCNASEnforcingFacet method remove.
public boolean remove(CharID id, CNAbilitySelection cnas, Object source) {
if (cnas == null) {
throw new IllegalArgumentException("Attempt to remove null from list");
}
if (source == null) {
throw new IllegalArgumentException("Attempt to remove object with null source from list");
}
List<List<SourcedCNAS>> list = getList(id);
if (list == null) {
return false;
}
for (Iterator<List<SourcedCNAS>> listIT = list.iterator(); listIT.hasNext(); ) {
List<SourcedCNAS> array = listIT.next();
int length = array.size();
//Iterate backwards, so that we remove harmless items first
for (int j = length - 1; j >= 0; j--) {
SourcedCNAS sc = array.get(j);
if (cnas.equals(sc.cnas) && source.equals(sc.source)) {
//fix the array here
if ((j == 0) && (length == 1)) {
//There is no alternative, remove the array from the list;
listIT.remove();
fireDataFacetChangeEvent(id, cnas, DataFacetChangeEvent.DATA_REMOVED);
return true;
} else {
array.remove(j);
CNAbilitySelection newPrimary = array.get(0).cnas;
//Only fire if the CNAS differs to avoid churn
if (!cnas.equals(newPrimary) && (j == 0)) {
fireDataFacetChangeEvent(id, cnas, DataFacetChangeEvent.DATA_REMOVED);
fireDataFacetChangeEvent(id, newPrimary, DataFacetChangeEvent.DATA_ADDED);
return true;
}
return false;
}
}
}
}
return false;
}
use of pcgen.cdom.helper.CNAbilitySelection in project pcgen by PCGen.
the class AbstractCharacterTestCase method applyAbility.
public static CNAbility applyAbility(PlayerCharacter character, AbilityCategory cat, Ability a, String assoc) {
if (a.getCDOMCategory() == null) {
fail("Attempt to apply an Ability " + a.getKeyName() + " that never received a Category");
}
CNAbility cna = CNAbilityFactory.getCNAbility(cat, Nature.NORMAL, a);
CNAbilitySelection cnas = new CNAbilitySelection(cna, assoc);
character.addAbility(cnas, UserSelection.getInstance(), UserSelection.getInstance());
return cna;
}
use of pcgen.cdom.helper.CNAbilitySelection in project pcgen by PCGen.
the class AbilityTokenTest method testDecodeChoice.
@Test
public void testDecodeChoice() {
try {
pca.decodeChoice(context, "CATEGORY=FEAT|NATURE=NORMAL|ItemName");
fail();
} catch (IllegalArgumentException e) {
// OK
}
Ability item = construct("ItemName");
CNAbilitySelection as = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.NORMAL, item));
assertEquals(as, pca.decodeChoice(context, "CATEGORY=FEAT|NATURE=NORMAL|ItemName"));
}
use of pcgen.cdom.helper.CNAbilitySelection in project pcgen by PCGen.
the class AbilityTokenTest method testWithChoose.
@Test
public void testWithChoose() {
try {
setUpPC();
//Need to make sure we use the character related context
context = Globals.getContext();
context.getReferenceContext().importObject(AbilityCategory.FEAT);
TokenRegistration.register(ADD_TOKEN);
TokenRegistration.register(ADD_ABILITY_TOKEN);
} catch (PersistenceLayerException e1) {
fail("Cannot set up PC");
}
Ability item = construct("ChooseAbility");
Ability parent = construct("Parent");
context.getReferenceContext().constructCDOMObject(Language.class, "Foo");
context.getReferenceContext().constructCDOMObject(Language.class, "Bar");
context.getReferenceContext().constructCDOMObject(Language.class, "Goo");
context.getReferenceContext().constructCDOMObject(Language.class, "Wow");
context.getReferenceContext().constructCDOMObject(Language.class, "Rev");
AbilityCategory ff = context.getReferenceContext().constructCDOMObject(AbilityCategory.class, "Fighter Feat");
ff.setAbilityCategory(CDOMDirectSingleRef.getRef(AbilityCategory.FEAT));
AbilityCategory oc = context.getReferenceContext().constructCDOMObject(AbilityCategory.class, "Some Other Category");
Ability badCA = context.getReferenceContext().constructCDOMObject(Ability.class, "ChooseAbility");
context.getReferenceContext().reassociateCategory(oc, badCA);
try {
assertTrue(context.processToken(item, "CHOOSE", "LANG|Foo|Bar|Goo|Wow|Rev"));
assertTrue(context.processToken(item, "MULT", "Yes"));
assertTrue(context.processToken(badCA, "CHOOSE", "LANG|Foo|Bar|Goo|Wow|Rev"));
assertTrue(context.processToken(badCA, "MULT", "Yes"));
assertTrue(context.processToken(parent, "ADD", "ABILITY|FEAT|NORMAL|ChooseAbility"));
} catch (PersistenceLayerException e) {
e.printStackTrace();
fail();
}
finishLoad(context);
PlayerCharacter pc = new PlayerCharacter();
Object source = UserSelection.getInstance();
CNAbilitySelection badCACAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(oc, Nature.AUTOMATIC, badCA), "Foo");
CNAbilitySelection fooCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.AUTOMATIC, item), "Foo");
CNAbilitySelection barCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.VIRTUAL, item), "Bar");
CNAbilitySelection gooCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.NORMAL, item), "Goo");
CNAbilitySelection wowCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.NORMAL, item), "Wow");
CNAbilitySelection wowFFCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(ff, Nature.NORMAL, item), "Wow");
CNAbilitySelection revCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.NORMAL, item), "Rev");
CNAbilitySelection revFFCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(ff, Nature.NORMAL, item), "Rev");
assertTrue(pca.allow(fooCAS, pc, false));
assertTrue(pca.allow(barCAS, pc, false));
assertTrue(pca.allow(gooCAS, pc, false));
assertTrue(pca.allow(wowCAS, pc, false));
assertTrue(pca.allow(revFFCAS, pc, false));
pc.applyAbility(badCACAS, source);
//Should have had no effect
assertTrue(pca.allow(fooCAS, pc, false));
assertTrue(pca.allow(barCAS, pc, false));
assertTrue(pca.allow(gooCAS, pc, false));
assertTrue(pca.allow(wowCAS, pc, false));
assertTrue(pca.allow(revFFCAS, pc, false));
pc.applyAbility(fooCAS, source);
assertFalse(pca.allow(fooCAS, pc, false));
assertTrue(pca.allow(barCAS, pc, false));
assertTrue(pca.allow(gooCAS, pc, false));
assertTrue(pca.allow(wowCAS, pc, false));
assertTrue(pca.allow(revFFCAS, pc, false));
pc.applyAbility(barCAS, source);
assertFalse(pca.allow(fooCAS, pc, false));
assertFalse(pca.allow(barCAS, pc, false));
assertTrue(pca.allow(gooCAS, pc, false));
assertTrue(pca.allow(wowCAS, pc, false));
assertTrue(pca.allow(revFFCAS, pc, false));
pc.applyAbility(gooCAS, source);
assertFalse(pca.allow(fooCAS, pc, false));
assertFalse(pca.allow(barCAS, pc, false));
assertFalse(pca.allow(gooCAS, pc, false));
assertTrue(pca.allow(wowCAS, pc, false));
assertTrue(pca.allow(revFFCAS, pc, false));
pc.applyAbility(wowFFCAS, source);
assertFalse(pca.allow(fooCAS, pc, false));
assertFalse(pca.allow(barCAS, pc, false));
assertFalse(pca.allow(gooCAS, pc, false));
assertFalse(pca.allow(wowCAS, pc, false));
assertTrue(pca.allow(revFFCAS, pc, false));
pc.applyAbility(revCAS, source);
assertFalse(pca.allow(fooCAS, pc, false));
assertFalse(pca.allow(barCAS, pc, false));
assertFalse(pca.allow(gooCAS, pc, false));
assertFalse(pca.allow(wowCAS, pc, false));
assertFalse(pca.allow(revFFCAS, pc, false));
}
use of pcgen.cdom.helper.CNAbilitySelection in project pcgen by PCGen.
the class AbstractCNASEnforcingFacetTest method testTypeAddMultRemove.
@Test
public void testTypeAddMultRemove() {
Object source1 = new Object();
CNAbilitySelection t1 = getMultObject("English");
CNAbilitySelection t2 = getMultObject("German");
getFacet().add(id, t1, source1);
getFacet().add(id, t2, source1);
getFacet().remove(id, t1, source1);
assertEquals(1, getFacet().getCount(id));
assertFalse(getFacet().isEmpty(id));
Collection<CNAbilitySelection> setofone = getFacet().getSet(id);
assertNotNull(setofone);
assertEquals(1, setofone.size());
assertTrue(setofone.contains(t2));
assertEventCount(2, 1);
}
Aggregations