Search in sources :

Example 1 with SerializerMissingKeyException

use of me.deecaad.core.file.SerializerMissingKeyException 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 2 with SerializerMissingKeyException

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

the class Explosion method serialize.

@Override
@Nonnull
public Explosion serialize(SerializeData data) throws SerializerException {
    // We don't need to get the values here since we add them to the map
    // later. We should still make sure these are positive numbers, though.
    data.of("Explosion_Type_Data.Yield").assertPositive();
    data.of("Explosion_Type_Data.Angle").assertPositive();
    data.of("Explosion_Type_Data.Height").assertPositive();
    data.of("Explosion_Type_Data.Width").assertPositive();
    data.of("Explosion_Type_Data.Radius").assertPositive();
    data.of("Rays").assertPositive();
    // We always want at least one.
    if (!data.config.contains(data.key + ".Explosion_Type_Data")) {
        throw new SerializerMissingKeyException(data.serializer, "Explosion_Type_Data", data.of("Explosion_Type_Data").getLocation());
    }
    Map<String, Object> typeData = data.config.getConfigurationSection(data.key + ".Explosion_Type_Data").getValues(false);
    // most people will not understand hat it means. Vanilla MC uses 16.
    if (!typeData.containsKey("Rays"))
        typeData.put("Rays", 16);
    ExplosionExposure exposure;
    ExplosionShape shape;
    try {
        exposure = ExposureFactory.getInstance().get(data.of("Explosion_Exposure").get("DEFAULT"), typeData);
        shape = ShapeFactory.getInstance().get(data.of("Explosion_Shape").get("DEFAULT"), typeData);
    } catch (SerializerException ex) {
        // We need to manually set the file and path, since the Factory
        // class does not get enough information to fill it.
        ex.setLocation(data.of("Explosion_Type_Data").getLocation());
        throw ex;
    }
    BlockDamage blockDamage = data.of("Block_Damage").serialize(BlockDamage.class);
    RegenerationData regeneration = data.of("Regeneration").serialize(RegenerationData.class);
    // to make when copying/pasting and deleting chunks of config.
    if ((blockDamage == null || !blockDamage.isBreakBlocks()) && regeneration != null) {
        throw data.exception(null, "Found an Explosion that defines 'Regeneration' when 'Block_Damage' cannot break blocks!", "This happens when 'Block_Damage.Break_Blocks: false' or when 'Block_Damage' was not added AND you tried to add 'Regeneration'");
    }
    // This is a required argument to determine when a projectile using this
    // explosion should explode (onEntityHit, onBlockHit, after delay, etc.)
    Detonation detonation = data.of("Detonation").assertExists().serialize(Detonation.class);
    Double blockChance = data.of("Block_Damage.Spawn_Falling_Block_Chance").serializeNonStandardSerializer(new ChanceSerializer());
    if (blockChance == null)
        blockChance = 0.0;
    boolean isKnockback = !data.of("Disable_Vanilla_Knockback").getBool(false);
    // These 4 options are all nullable and not required for an explosion
    // to occur. It is very interesting when they are all used together :p
    ClusterBomb clusterBomb = data.of("Cluster_Bomb").serialize(ClusterBomb.class);
    AirStrike airStrike = data.of("Airstrike").serialize(AirStrike.class);
    Flashbang flashbang = data.of("Flashbang").serialize(Flashbang.class);
    Mechanics mechanics = data.of("Mechanics").serialize(Mechanics.class);
    return new Explosion(shape, exposure, blockDamage, regeneration, detonation, blockChance, isKnockback, clusterBomb, airStrike, flashbang, mechanics);
}
Also used : ChanceSerializer(me.deecaad.core.file.serializers.ChanceSerializer) ExplosionExposure(me.deecaad.weaponmechanics.weapon.explode.exposures.ExplosionExposure) SerializerException(me.deecaad.core.file.SerializerException) RegenerationData(me.deecaad.weaponmechanics.weapon.explode.regeneration.RegenerationData) ExplosionShape(me.deecaad.weaponmechanics.weapon.explode.shapes.ExplosionShape) Mechanics(me.deecaad.weaponmechanics.mechanics.Mechanics) WeaponMechanics(me.deecaad.weaponmechanics.WeaponMechanics) SerializerMissingKeyException(me.deecaad.core.file.SerializerMissingKeyException) Nonnull(javax.annotation.Nonnull)

Aggregations

SerializerMissingKeyException (me.deecaad.core.file.SerializerMissingKeyException)2 Nonnull (javax.annotation.Nonnull)1 SerializerException (me.deecaad.core.file.SerializerException)1 SerializerOptionsException (me.deecaad.core.file.SerializerOptionsException)1 SerializerTypeException (me.deecaad.core.file.SerializerTypeException)1 ChanceSerializer (me.deecaad.core.file.serializers.ChanceSerializer)1 WeaponMechanics (me.deecaad.weaponmechanics.WeaponMechanics)1 Mechanics (me.deecaad.weaponmechanics.mechanics.Mechanics)1 ExplosionExposure (me.deecaad.weaponmechanics.weapon.explode.exposures.ExplosionExposure)1 RegenerationData (me.deecaad.weaponmechanics.weapon.explode.regeneration.RegenerationData)1 ExplosionShape (me.deecaad.weaponmechanics.weapon.explode.shapes.ExplosionShape)1