Search in sources :

Example 1 with RemoteModifier

use of pcgen.cdom.content.RemoteModifier in project pcgen by PCGen.

the class RemoteModifierFacet method dataAdded.

@Override
public void dataAdded(DataFacetChangeEvent<CharID, VarScoped> dfce) {
    CharID id = dfce.getCharID();
    VarScoped vs = dfce.getCDOMObject();
    /*
		 * If this can have local variables, find what may have been modified by
		 * previous objects
		 */
    for (RemoteModifier<?> rm : getSet(id)) {
        VarScoped src = get(id, rm);
        ScopeInstance inst = scopeFacet.get(id, src);
        processAdd(id, rm, vs, inst);
        if (vs instanceof Equipment) {
            Equipment e = (Equipment) vs;
            for (EquipmentHead head : e.getEquipmentHeads()) {
                processAdd(id, rm, head, inst);
            }
        }
    }
    /*
		 * Look at what newly added object can modify on others
		 */
    if (vs instanceof CDOMObject) {
        ScopeInstance inst = scopeFacet.get(id, vs);
        List<RemoteModifier<?>> list = ((CDOMObject) vs).getListFor(ListKey.REMOTE_MODIFIER);
        if (list != null) {
            Set<? extends VarScoped> targets = varScopedFacet.getSet(id);
            for (RemoteModifier<?> rm : list) {
                set(id, rm, vs);
                //Apply to existing as necessary
                for (VarScoped obj : targets) {
                    processAdd(id, rm, obj, inst);
                    if (obj instanceof Equipment) {
                        Equipment e = (Equipment) obj;
                        for (EquipmentHead head : e.getEquipmentHeads()) {
                            processAdd(id, rm, head, inst);
                        }
                    }
                }
            }
        }
    }
}
Also used : ScopeInstance(pcgen.base.formula.base.ScopeInstance) EquipmentHead(pcgen.cdom.inst.EquipmentHead) Equipment(pcgen.core.Equipment) CDOMObject(pcgen.cdom.base.CDOMObject) RemoteModifier(pcgen.cdom.content.RemoteModifier) VarScoped(pcgen.base.formula.base.VarScoped) CharID(pcgen.cdom.enumeration.CharID)

Example 2 with RemoteModifier

use of pcgen.cdom.content.RemoteModifier in project pcgen by PCGen.

the class RemoteModifierFacet method dataRemoved.

@Override
public void dataRemoved(DataFacetChangeEvent<CharID, VarScoped> dfce) {
    CharID id = dfce.getCharID();
    VarScoped vs = dfce.getCDOMObject();
    /*
		 * If this can have local variables, find what had been modified by
		 * previous objects
		 */
    for (RemoteModifier<?> rm : getSet(id)) {
        VarScoped src = get(id, rm);
        ScopeInstance inst = scopeFacet.get(id, src);
        processRemove(id, rm, vs, inst);
        if (vs instanceof Equipment) {
            Equipment e = (Equipment) vs;
            for (EquipmentHead head : e.getEquipmentHeads()) {
                processRemove(id, rm, head, inst);
            }
        }
    }
    /*
		 * Look at what newly added object can modify on others
		 */
    if (vs instanceof CDOMObject) {
        ScopeInstance inst = scopeFacet.get(id, vs);
        List<RemoteModifier<?>> list = ((CDOMObject) vs).getListFor(ListKey.REMOTE_MODIFIER);
        if (list != null) {
            Set<? extends VarScoped> targets = varScopedFacet.getSet(id);
            for (RemoteModifier<?> rm : list) {
                remove(id, rm);
                //RemoveFrom existing as necessary
                for (VarScoped obj : targets) {
                    processRemove(id, rm, obj, inst);
                    if (obj instanceof Equipment) {
                        Equipment e = (Equipment) obj;
                        for (EquipmentHead head : e.getEquipmentHeads()) {
                            processRemove(id, rm, head, inst);
                        }
                    }
                }
            }
        }
    }
}
Also used : ScopeInstance(pcgen.base.formula.base.ScopeInstance) EquipmentHead(pcgen.cdom.inst.EquipmentHead) Equipment(pcgen.core.Equipment) CDOMObject(pcgen.cdom.base.CDOMObject) RemoteModifier(pcgen.cdom.content.RemoteModifier) VarScoped(pcgen.base.formula.base.VarScoped) CharID(pcgen.cdom.enumeration.CharID)

Example 3 with RemoteModifier

use of pcgen.cdom.content.RemoteModifier in project pcgen by PCGen.

the class ModifyOtherLst method continueParsing.

private <GT extends VarScoped> ParseResult continueParsing(LoadContext context, CDOMObject obj, String value, ParsingSeparator sep) {
    ScopeInstance scopeInst = context.getActiveScope();
    @SuppressWarnings("unchecked") final LegalScope scope = scopeInst.getLegalScope();
    final String groupingName = sep.next();
    ObjectGrouping group;
    if (groupingName.startsWith("GROUP=")) {
        final String groupName = groupingName.substring(6);
        group = new ObjectGrouping() {

            @Override
            public boolean contains(VarScoped item) {
                return (item instanceof CDOMObject) && ((CDOMObject) item).containsInList(ListKey.GROUP, groupName);
            }

            @Override
            public LegalScope getScope() {
                return scope;
            }

            @Override
            public String getIdentifier() {
                return "GROUP=" + groupName;
            }
        };
    } else if ("ALL".equals(groupingName)) {
        group = new ObjectGrouping() {

            @Override
            public boolean contains(VarScoped cdo) {
                return true;
            }

            @Override
            public LegalScope getScope() {
                return scope;
            }

            @Override
            public String getIdentifier() {
                return "ALL";
            }
        };
    } else {
        group = new ObjectGrouping() {

            @Override
            public boolean contains(VarScoped cdo) {
                return cdo.getKeyName().equalsIgnoreCase(groupingName);
            }

            @Override
            public LegalScope getScope() {
                return scope;
            }

            @Override
            public String getIdentifier() {
                return groupingName;
            }
        };
    }
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " needed 3rd argument: " + value, context);
    }
    String varName = sep.next();
    if (!context.getVariableContext().isLegalVariableID(scope, varName)) {
        return new ParseResult.Fail(getTokenName() + " found invalid var name: " + varName + " Modified on " + obj.getClass().getSimpleName() + ' ' + obj.getKeyName(), context);
    }
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " needed 4th argument: " + value, context);
    }
    String modType = sep.next();
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " needed 5th argument: " + value, context);
    }
    String modValue = sep.next();
    //Defaults to zero
    int priorityNumber = 0;
    if (sep.hasNext()) {
        String priority = sep.next();
        if (priority.length() < 10) {
            return new ParseResult.Fail(getTokenName() + " was expecting PRIORITY= but got " + priority + " in " + value, context);
        }
        if ("PRIORITY=".equalsIgnoreCase(priority.substring(0, 9))) {
            try {
                priorityNumber = Integer.parseInt(priority.substring(9));
            } catch (NumberFormatException e) {
                return new ParseResult.Fail(getTokenName() + " requires Priority to be an integer: " + priority.substring(9) + " was not an integer");
            }
            if (priorityNumber < 0) {
                return new ParseResult.Fail(getTokenName() + " Priority requires an integer >= 0. " + priorityNumber + " was not positive");
            }
        } else {
            return new ParseResult.Fail(getTokenName() + " was expecting PRIORITY=x but got " + priority + " in " + value, context);
        }
        if (sep.hasNext()) {
            return new ParseResult.Fail(getTokenName() + " had too many arguments: " + value, context);
        }
    }
    PCGenModifier<?> modifier;
    try {
        FormatManager<?> format = context.getVariableContext().getVariableFormat(scope, varName);
        modifier = context.getVariableContext().getModifier(modType, modValue, priorityNumber, scope, format);
    } catch (IllegalArgumentException iae) {
        return new ParseResult.Fail(getTokenName() + " Modifier " + modType + " had value " + modValue + " but it was not valid: " + iae.getMessage(), context);
    }
    VarModifier<?> vm = new VarModifier<>(varName, scope, modifier);
    RemoteModifier<?> rm = new RemoteModifier<>(group, vm);
    context.getObjectContext().addToList(obj, ListKey.REMOTE_MODIFIER, rm);
    return ParseResult.SUCCESS;
}
Also used : ScopeInstance(pcgen.base.formula.base.ScopeInstance) ParseResult(pcgen.rules.persistence.token.ParseResult) RemoteModifier(pcgen.cdom.content.RemoteModifier) VarScoped(pcgen.base.formula.base.VarScoped) LegalScope(pcgen.base.formula.base.LegalScope) VarModifier(pcgen.cdom.content.VarModifier) ObjectGrouping(pcgen.cdom.base.ObjectGrouping) CDOMObject(pcgen.cdom.base.CDOMObject)

Example 4 with RemoteModifier

use of pcgen.cdom.content.RemoteModifier in project pcgen by PCGen.

the class ModifyOtherLst method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    Changes<RemoteModifier<?>> changes = context.getObjectContext().getListChanges(obj, ListKey.REMOTE_MODIFIER);
    if (changes.hasRemovedItems()) {
        Logging.errorPrint(getTokenName() + " does not support removed items");
        return null;
    }
    if (changes.includesGlobalClear()) {
        Logging.errorPrint(getTokenName() + " does not support .CLEAR");
        return null;
    }
    Collection<RemoteModifier<?>> added = changes.getAdded();
    List<String> modifiers = new ArrayList<>();
    if (added != null && !added.isEmpty()) {
        for (RemoteModifier<?> rm : added) {
            VarModifier<?> vm = rm.getVarModifier();
            StringBuilder sb = new StringBuilder();
            ObjectGrouping og = rm.getGrouping();
            sb.append(og.getScope().getName());
            sb.append(Constants.PIPE);
            sb.append(og.getIdentifier());
            sb.append(Constants.PIPE);
            sb.append(vm.getVarName());
            sb.append(Constants.PIPE);
            sb.append(unparseModifier(vm));
            modifiers.add(sb.toString());
        }
    }
    if (modifiers.isEmpty()) {
        //Legal
        return null;
    }
    return modifiers.toArray(new String[modifiers.size()]);
}
Also used : ObjectGrouping(pcgen.cdom.base.ObjectGrouping) RemoteModifier(pcgen.cdom.content.RemoteModifier) ArrayList(java.util.ArrayList)

Aggregations

RemoteModifier (pcgen.cdom.content.RemoteModifier)4 ScopeInstance (pcgen.base.formula.base.ScopeInstance)3 VarScoped (pcgen.base.formula.base.VarScoped)3 CDOMObject (pcgen.cdom.base.CDOMObject)3 ObjectGrouping (pcgen.cdom.base.ObjectGrouping)2 CharID (pcgen.cdom.enumeration.CharID)2 EquipmentHead (pcgen.cdom.inst.EquipmentHead)2 Equipment (pcgen.core.Equipment)2 ArrayList (java.util.ArrayList)1 LegalScope (pcgen.base.formula.base.LegalScope)1 VarModifier (pcgen.cdom.content.VarModifier)1 ParseResult (pcgen.rules.persistence.token.ParseResult)1