Search in sources :

Example 1 with JepCountType

use of pcgen.util.JepCountType in project pcgen by PCGen.

the class CountCommand method run.

/**
	 * Runs count 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 that the count command will process
	 *
	 * @throws ParseException
	 */
@Override
@SuppressWarnings("unchecked")
public //Uses JEP, which doesn't use generics
void run(final Stack inStack) throws ParseException {
    // Grab the character under scrutiny
    final PlayerCharacter pc = getPC();
    if (pc == null) {
        throw new ParseException("Invalid parent (no PC): " + parent.getClass().getName());
    }
    // check the stack
    checkStack(inStack);
    if (1 <= curNumberOfParameters) {
        // move all but the first parameter from the stack into and array of Objects
        final Object[] params = paramStackToArray(inStack, curNumberOfParameters - 1);
        // retrieve the first Object, this should be a String which will map directly to
        // a JepCountEnum, this specifies the type of count to perform
        final Object toCount = inStack.pop();
        if (toCount instanceof String) {
            final JepCountType countEnum = JepCountType.valueOf((String) toCount);
            if (countEnum == null) {
                Logging.errorPrint("Unable to find count type: " + toCount);
            }
            // Count the requested object type.
            final Double result = (Double) countEnum.count(pc, params);
            inStack.push(result);
        } else {
            throw new ParseException("Invalid parameter type");
        }
    } else {
        throw new ParseException("missing parameter, nothing to count");
    }
}
Also used : PlayerCharacter(pcgen.core.PlayerCharacter) ParseException(org.nfunk.jep.ParseException) JepCountType(pcgen.util.JepCountType)

Example 2 with JepCountType

use of pcgen.util.JepCountType in project pcgen by PCGen.

the class CountDistinctCommand method run.

/**
	 * Runs count 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 that the count command will process
	 *
	 * @throws ParseException
	 */
@Override
@SuppressWarnings("unchecked")
public //Uses JEP, which doesn't use generics
void run(final Stack inStack) throws ParseException {
    // Grab the character under scrutiny
    final PlayerCharacter pc = getPC();
    if (pc == null) {
        throw new ParseException("Invalid parent (no PC): " + parent.getClass().getName());
    }
    // check the stack
    checkStack(inStack);
    if (1 <= curNumberOfParameters) {
        // move all but the first parameter from the stack into and array of Objects
        final Object[] params = paramStackToArray(inStack, curNumberOfParameters - 1);
        // retrieve the first Object, this should be a String which will map directly to
        // a JepCountDistinctEnum or JepCountEnum, this specifies the type of count to perform
        final Object toCount = inStack.pop();
        if (toCount instanceof String) {
            JepCountType countEnum = JepCountType.valueOf(toCount + "DISTINCT");
            if (countEnum == null) {
                // Fall back to count
                countEnum = JepCountType.valueOf((String) toCount);
            }
            if (countEnum == null) {
                Logging.errorPrint("Unable to find count type: " + toCount);
            }
            final Double result = (Double) countEnum.count(pc, params);
            inStack.push(result);
        } else {
            throw new ParseException("Invalid parameter type");
        }
    } else {
        throw new ParseException("missing parameter, nothing to count");
    }
}
Also used : PlayerCharacter(pcgen.core.PlayerCharacter) ParseException(org.nfunk.jep.ParseException) JepCountType(pcgen.util.JepCountType)

Aggregations

ParseException (org.nfunk.jep.ParseException)2 PlayerCharacter (pcgen.core.PlayerCharacter)2 JepCountType (pcgen.util.JepCountType)2