use of pcgen.cdom.enumeration.Type in project pcgen by PCGen.
the class TypeLst method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject cdo, String value) {
if (value.startsWith(Constants.LST_DOT_CLEAR)) {
context.getObjectContext().removeList(cdo, ListKey.TYPE);
if (value.length() == 6) {
return ParseResult.SUCCESS;
} else if (value.charAt(6) == '.') {
value = value.substring(7);
if (isEmpty(value)) {
return new ParseResult.Fail(getTokenName() + "started with .CLEAR. but expected to have a Type after .: " + value, context);
}
} else {
return new ParseResult.Fail(getTokenName() + "started with .CLEAR but expected next character to be .: " + value, context);
}
}
ParseResult pr = checkForIllegalSeparator('.', value);
if (!pr.passed()) {
return pr;
}
StringTokenizer aTok = new StringTokenizer(value, Constants.DOT);
boolean bRemove = false;
boolean bAdd = false;
while (aTok.hasMoreTokens()) {
final String aType = aTok.nextToken();
if ("ADD".equals(aType)) {
if (bRemove) {
return new ParseResult.Fail("Non-sensical use of .REMOVE.ADD. in " + getTokenName() + ": " + value, context);
}
bRemove = false;
bAdd = true;
} else if ("REMOVE".equals(aType)) {
if (bAdd) {
return new ParseResult.Fail("Non-sensical use of .ADD.REMOVE. in " + getTokenName() + ": " + value, context);
}
bRemove = true;
} else if ("CLEAR".equals(aType)) {
return new ParseResult.Fail("Non-sensical use of .CLEAR in " + getTokenName() + ": " + value, context);
} else if (bRemove) {
Type type = Type.getConstant(aType);
context.getObjectContext().removeFromList(cdo, ListKey.TYPE, type);
bRemove = false;
} else {
Type type = Type.getConstant(aType);
// We want to exclude any duplicates from the type list
Changes<Type> listChanges = context.getObjectContext().getListChanges(cdo, ListKey.TYPE);
if (listChanges.getAdded() == null || !listChanges.getAdded().contains(type)) {
context.getObjectContext().addToList(cdo, ListKey.TYPE, type);
}
bAdd = false;
}
}
if (bRemove) {
return new ParseResult.Fail(getTokenName() + "ended with REMOVE, so didn't have any Type to remove: " + value, context);
}
if (bAdd) {
return new ParseResult.Fail(getTokenName() + "ended with ADD, so didn't have any Type to add: " + value, context);
}
return ParseResult.SUCCESS;
}
use of pcgen.cdom.enumeration.Type in project pcgen by PCGen.
the class TypeToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CompanionMod mod, String value) {
//TODO Check for "." and warn?
Type type = Type.getConstant(value);
context.getObjectContext().addToList(mod, ListKey.TYPE, type);
final Category<CompanionMod> cat = context.getReferenceContext().constructNowIfNecessary(COMPANIONLIST_CLASS, value);
if (cat == null) {
return new ParseResult.Fail("Cannot find Companion List: " + value, context);
}
context.getReferenceContext().reassociateCategory(cat, mod);
return ParseResult.SUCCESS;
}
use of pcgen.cdom.enumeration.Type in project pcgen by PCGen.
the class AlttypeToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, Equipment eq, String value) {
EquipmentHead head = eq.getEquipmentHead(2);
if (value.startsWith(Constants.LST_DOT_CLEAR)) {
context.getObjectContext().removeList(head, ListKey.TYPE);
if (value.length() == 6) {
return ParseResult.SUCCESS;
} else if (value.charAt(6) == '.') {
value = value.substring(7);
if (isEmpty(value)) {
return new ParseResult.Fail(getTokenName() + "started with .CLEAR. but expected to have a Type after .: " + value, context);
}
} else {
return new ParseResult.Fail(getTokenName() + "started with .CLEAR but expected next character to be .: " + value, context);
}
}
ParseResult pr = checkForIllegalSeparator('.', value);
if (!pr.passed()) {
return pr;
}
StringTokenizer aTok = new StringTokenizer(value, Constants.DOT);
boolean bRemove = false;
boolean bAdd = false;
while (aTok.hasMoreTokens()) {
final String aType = aTok.nextToken();
if ("ADD".equals(aType)) {
if (bRemove) {
return new ParseResult.Fail("Non-sensical use of .REMOVE.ADD. in " + getTokenName() + ": " + value, context);
}
bRemove = false;
bAdd = true;
} else if ("REMOVE".equals(aType)) {
if (bAdd) {
return new ParseResult.Fail("Non-sensical use of .ADD.REMOVE. in " + getTokenName() + ": " + value, context);
}
bRemove = true;
} else if ("CLEAR".equals(aType)) {
return new ParseResult.Fail("Non-sensical use of .CLEAR in " + getTokenName() + ": " + value, context);
} else if (bRemove) {
Type type = Type.getConstant(aType);
context.getObjectContext().removeFromList(head, ListKey.TYPE, type);
bRemove = false;
} else {
Type type = Type.getConstant(aType);
context.getObjectContext().addToList(head, ListKey.TYPE, type);
bAdd = false;
}
}
if (bRemove) {
return new ParseResult.Fail(getTokenName() + "ended with REMOVE, so didn't have any Type to remove: " + value, context);
}
if (bAdd) {
return new ParseResult.Fail(getTokenName() + "ended with ADD, so didn't have any Type to add: " + value, context);
}
return ParseResult.SUCCESS;
}
use of pcgen.cdom.enumeration.Type in project pcgen by PCGen.
the class AlttypeToken method unparse.
@Override
public String[] unparse(LoadContext context, Equipment eq) {
EquipmentHead head = eq.getEquipmentHead(2);
Changes<Type> changes = context.getObjectContext().getListChanges(head, ListKey.TYPE);
if (changes == null || changes.isEmpty()) {
return null;
}
StringBuilder sb = new StringBuilder();
Collection<?> added = changes.getAdded();
boolean globalClear = changes.includesGlobalClear();
if (globalClear) {
sb.append(Constants.LST_DOT_CLEAR);
}
if (added != null && !added.isEmpty()) {
if (globalClear) {
sb.append(Constants.DOT);
}
sb.append(StringUtil.join(added, Constants.DOT));
}
Collection<Type> removed = changes.getRemoved();
if (removed != null && !removed.isEmpty()) {
if (sb.length() > 0) {
sb.append(Constants.DOT);
}
sb.append("REMOVE.");
sb.append(StringUtil.join(removed, Constants.DOT));
}
if (sb.length() == 0) {
context.addWriteMessage(getTokenName() + " was expecting non-empty changes to include " + "added items or global clear");
return null;
}
return new String[] { sb.toString() };
}
use of pcgen.cdom.enumeration.Type in project pcgen by PCGen.
the class Equipment method typeList.
/**
* Returns a list of the types of this item.
*
* @param bPrimary
* if true return the types if the primary head, otherwise
* return the types of the secondary head
* @return a list of the types of this item.
*/
private List<String> typeList(final boolean bPrimary) {
if (bPrimary && usePrimaryCache) {
return typeListCachePrimary;
}
if (!bPrimary && useSecondaryCache) {
return typeListCacheSecondary;
}
// Use the primary type(s) if none defined for secondary
List<Type> initializingList = getEquipmentHead(2).getListFor(ListKey.TYPE);
if (bPrimary || (initializingList == null) || initializingList.isEmpty()) {
initializingList = getTrueTypeList(false);
} else if (!isDouble()) {
return new ArrayList<>();
}
Set<String> calculatedTypeList = new LinkedHashSet<>();
if (initializingList != null) {
for (Type t : initializingList) {
calculatedTypeList.add(t.getComparisonString());
}
}
final Collection<String> modTypeList = new ArrayList<>();
//
// Add in all type modfiers from "ADDTYPE" modifier
//
EquipmentModifier aEqMod = getEqModifierKeyed("ADDTYPE", bPrimary);
if (aEqMod != null) {
for (String aType : getAssociationList(aEqMod)) {
aType = aType.toUpperCase();
if (!calculatedTypeList.contains(aType)) {
modTypeList.add(aType);
}
}
}
/*
* CONSIDER I think there is a weird order of operations issue nere, need to check
* if it existed way back, e.g. SVN 6206. The issue is if a Type is introduced by a
* MOD, then the ChangeArmorType system doesn't seem to be able to grab/modify it
* Is that correct? - thpr 10/3/08
*/
//
// Add in all of the types from each EquipmentModifier
// currently applied to this piece of equipment
//
final List<EquipmentModifier> eqModList = getEqModifierList(bPrimary);
for (EquipmentModifier eqMod : eqModList) {
//
// If we've just replaced the armor type, then make sure it is
// not in the equipment modifier list
//
Set<String> newTypeList = new LinkedHashSet<>(calculatedTypeList);
for (ChangeArmorType cat : eqMod.getSafeListFor(ListKey.ARMORTYPE)) {
List<String> tempTypeList = cat.applyProcessor(newTypeList);
LinkedHashSet<String> tempTypeSet = new LinkedHashSet<>(tempTypeList);
boolean noMatch = newTypeList.size() != tempTypeList.size() || newTypeList.equals(tempTypeSet);
newTypeList = tempTypeSet;
if (!noMatch) {
break;
}
}
Collection<String> removedTypeList = new ArrayList<>(calculatedTypeList);
removedTypeList.removeAll(newTypeList);
modTypeList.removeAll(removedTypeList);
calculatedTypeList = newTypeList;
for (String aType : eqMod.getSafeListFor(ListKey.ITEM_TYPES)) {
aType = aType.toUpperCase();
// (melee and ranged)
if (calculatedTypeList.contains("BOTH") && calculatedTypeList.contains("MELEE") && ("RANGED".equals(aType) || "THROWN".equals(aType))) {
continue;
}
if (!calculatedTypeList.contains(aType) && !modTypeList.contains(aType)) {
modTypeList.add(aType);
}
}
}
calculatedTypeList.addAll(modTypeList);
//
// Make sure MAGIC tag is the 1st entry
//
List<String> resultingTypeList = new ArrayList<>(calculatedTypeList);
final int idx = resultingTypeList.indexOf("MAGIC");
if (idx > 0) {
resultingTypeList.remove(idx);
resultingTypeList.add(0, "MAGIC");
}
if (bPrimary) {
typeListCachePrimary = resultingTypeList;
usePrimaryCache = true;
} else {
typeListCacheSecondary = resultingTypeList;
useSecondaryCache = true;
}
return resultingTypeList;
}
Aggregations