Search in sources :

Example 1 with SerializerTypeException

use of me.deecaad.core.file.SerializerTypeException in project MechanicsMain by WeaponMechanics.

the class ChanceSerializer method serialize.

@NotNull
@Override
public Double serialize(SerializeData data) throws SerializerException {
    Object value = data.of().assertExists().get();
    double chance = 0.0;
    // For example, 10.5% = 0.105
    if (value.toString().contains("%")) {
        String str = value.toString().trim();
        // those up.
        if (str.startsWith("%")) {
            chance = Double.parseDouble(str.substring(1)) / 100.0;
        } else if (str.endsWith("%")) {
            chance = Double.parseDouble(str.substring(0, str.length() - 1)) / 100.0;
        } else {
            throw data.exception(null, "Chance input had a '%' in the middle when it should have been on the end", SerializerException.forValue(value));
        }
    } else // Consider all numbers to be valid
    if (value instanceof Number) {
        chance = (double) value;
    } else // After checking for numbers, and percentages, there is nothing else
    // we can do except yell at the user for being stupid.
    {
        throw new SerializerTypeException(data.serializer, Number.class, value.getClass(), value, data.of().getLocation());
    }
    if (chance < 0.0 || chance > 1.0) {
        throw new SerializerRangeException(data.serializer, 0.0, chance, 1.0, data.of().getLocation()).addMessage("When using percentages, make sure to stay between 0% and 100%");
    }
    return chance;
}
Also used : SerializerTypeException(me.deecaad.core.file.SerializerTypeException) SerializerRangeException(me.deecaad.core.file.SerializerRangeException) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with SerializerTypeException

use of me.deecaad.core.file.SerializerTypeException in project MechanicsMain by WeaponMechanics.

the class Factory method get.

/**
 * Returns a constructed object of who inherits from <code>T</code>,
 * constructed from the given <code>arguments</code>. In order to add a
 * new type, use {@link #set(String, Arguments)}.
 *
 * <p>The given <code>arguments</code> <i>MUST</i> explicitly contain
 * <i>ALL</i> objects defined by the {@link Arguments#arguments}. If an
 * argument is missing, a {@link SerializerException} is thrown. The given
 * objects are type-casted to their expected type. Ensure that the
 * constructors for your defined arguments exist.
 *
 * @param key The non-null, non-case-sensitive name of the class to instantiate.
 * @param arguments The non-null map of arguments.
 * @return Instantiated object.
 * @throws InternalError If no "good" constructor exists.
 * @throws SerializerException invalid key OR missing argument OR invalid argument type.
 */
public final T get(String key, Map<String, Object> arguments) throws SerializerException {
    key = key.trim().toUpperCase(Locale.ROOT);
    Arguments args = map.get(key);
    if (args == null) {
        String name = StringUtil.splitCapitalLetters(getClass().getSimpleName())[0];
        throw new SerializerOptionsException(name, clazz.getSimpleName(), getOptions(), key, "FILL_ME");
    }
    // Pull only the values that we need from the mapped arguments. The
    // order of the arguments will match the order defined by the
    // Arguments class.
    Object[] objects = new Object[args.arguments.length];
    for (int i = 0; i < args.arguments.length; i++) {
        String argument = args.arguments[i];
        Class<?> clazz = args.argumentTypes[i];
        if (!arguments.containsKey(argument)) {
            String name = StringUtil.splitCapitalLetters(args.manufacturedType.getSimpleName())[0];
            throw new SerializerMissingKeyException(name, argument, "FILL_ME").addMessage("You specified: " + arguments);
        }
        // The Integer.class should be allowed to be assigned to a Double.class
        if (clazz != null && !clazz.isAssignableFrom(arguments.get(argument).getClass())) {
            try {
                if (clazz == double.class)
                    objects[i] = Double.parseDouble(arguments.get(argument).toString());
                else if (clazz == int.class)
                    objects[i] = Integer.parseInt(arguments.get(argument).toString());
                else if (clazz == boolean.class)
                    objects[i] = Boolean.parseBoolean(arguments.get(argument).toString());
                else
                    throw new NumberFormatException();
            } catch (NumberFormatException ex) {
                String name = StringUtil.splitCapitalLetters(args.manufacturedType.getSimpleName())[0];
                throw new SerializerTypeException(name, clazz, arguments.get(argument).getClass(), arguments.get(argument), "FILL_ME");
            }
        } else {
            objects[i] = arguments.get(argument);
        }
    }
    return ReflectionUtil.newInstance(args.manufacturedType, objects);
}
Also used : SerializerTypeException(me.deecaad.core.file.SerializerTypeException) SerializerOptionsException(me.deecaad.core.file.SerializerOptionsException) SerializerMissingKeyException(me.deecaad.core.file.SerializerMissingKeyException)

Example 3 with SerializerTypeException

use of me.deecaad.core.file.SerializerTypeException in project MechanicsMain by WeaponMechanics.

the class ModifySpreadWhen method getModifierHandler.

private NumberModifier getModifierHandler(SerializeData.ConfigAccessor data) throws SerializerException {
    String value = Objects.toString(data.get(null), null);
    if (value == null)
        return null;
    try {
        boolean percentage = value.endsWith("%");
        double number = Double.parseDouble(value.split("%")[0]);
        if (!percentage) {
            number *= 0.01;
        }
        return new NumberModifier(number, percentage);
    } catch (NumberFormatException e) {
        throw new SerializerTypeException(this, Double.class, null, value, data.getLocation());
    }
}
Also used : SerializerTypeException(me.deecaad.core.file.SerializerTypeException) NumberModifier(me.deecaad.weaponmechanics.weapon.shoot.NumberModifier)

Aggregations

SerializerTypeException (me.deecaad.core.file.SerializerTypeException)3 SerializerMissingKeyException (me.deecaad.core.file.SerializerMissingKeyException)1 SerializerOptionsException (me.deecaad.core.file.SerializerOptionsException)1 SerializerRangeException (me.deecaad.core.file.SerializerRangeException)1 NumberModifier (me.deecaad.weaponmechanics.weapon.shoot.NumberModifier)1 NotNull (org.jetbrains.annotations.NotNull)1