Search in sources :

Example 1 with VariableProcessorEq

use of pcgen.core.VariableProcessorEq in project pcgen by PCGen.

the class GetVarCommand method run.

/**
	 * Runs getvar on the inStack. The parameter is popped
	 * off the {@code inStack}, and the variable's value is
	 * pushed back to the top of {@code inStack}.
	 * @param inStack the jep stack
	 * @throws ParseException
	 */
//Uses JEP, which doesn't use generics
@SuppressWarnings("unchecked")
@Override
public void run(final Stack inStack) throws ParseException {
    // check the stack
    checkStack(inStack);
    // get the parameter from the stack
    final Object param1;
    Object param2 = null;
    //
    if (curNumberOfParameters == 1) {
        param1 = inStack.pop();
    } else if (curNumberOfParameters == 2) {
        param2 = inStack.pop();
        param1 = inStack.pop();
        if (!(param2 instanceof Double)) {
            throw new ParseException("Invalid parameter type");
        }
    } else {
        throw new ParseException("Invalid parameter count");
    }
    if (param1 instanceof String) {
        Float result = null;
        if (parent instanceof PlayerCharacter) {
            final PlayerCharacter character = (PlayerCharacter) parent;
            result = getVariableForCharacter(character, param1);
        } else if (parent instanceof Equipment) {
            boolean bPrimary = true;
            if (param2 != null) {
                bPrimary = (((Double) param2).intValue() != 0);
            }
            result = ((Equipment) parent).getVariableValue((String) param1, "", bPrimary, null);
        } else if (parent instanceof VariableProcessorPC) {
            final VariableProcessorPC vpc = (VariableProcessorPC) parent;
            // check to see if this is just a variable
            result = vpc.lookupVariable((String) param1, variableSource, null);
            if (result == null) {
                result = vpc.getVariableValue(null, (String) param1, variableSource, 0);
            }
        } else if (parent instanceof VariableProcessorEq) {
            VariableProcessorEq veq = (VariableProcessorEq) parent;
            result = veq.getVariableValue(null, (String) param1, variableSource, 0);
        } else if (parent == null) {
            Logging.errorPrint("Ignored request for var " + String.valueOf(param1) + " with no parent.");
        }
        if (result == null) {
            throw new ParseException("Error retreiving variable:" + param1);
        }
        inStack.push(result.doubleValue());
    } else {
        throw new ParseException("Invalid parameter type");
    }
}
Also used : VariableProcessorPC(pcgen.core.VariableProcessorPC) PlayerCharacter(pcgen.core.PlayerCharacter) Equipment(pcgen.core.Equipment) VariableProcessorEq(pcgen.core.VariableProcessorEq) ParseException(org.nfunk.jep.ParseException)

Aggregations

ParseException (org.nfunk.jep.ParseException)1 Equipment (pcgen.core.Equipment)1 PlayerCharacter (pcgen.core.PlayerCharacter)1 VariableProcessorEq (pcgen.core.VariableProcessorEq)1 VariableProcessorPC (pcgen.core.VariableProcessorPC)1