Search in sources :

Example 1 with ChanceSerializer

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

the class BlockDamage method serialize.

@Override
@NotNull
public BlockDamage serialize(SerializeData data) throws SerializerException {
    boolean isBreakBlocks = data.of("Break_Blocks").getBool(false);
    int damage = data.of("Damage_Per_Hit").assertPositive().getInt(1);
    int defaultBlockDurability = data.of("Default_Block_Durability").assertPositive().getInt(1);
    boolean isBlacklist = data.of("Blacklist").getBool(false);
    Double dropChance = data.of("Drop_Broken_Block_Chance").serializeNonStandardSerializer(new ChanceSerializer());
    SerializeData.ConfigListAccessor accessor = data.ofList("Block_List").addArgument(Material.class, true);
    // be used to define the extra int tag.
    if (!isBlacklist)
        accessor.addArgument(int.class, false);
    // This does the bulk of our validation.
    List<String[]> strings = accessor.assertExists().assertList().get();
    if (!isBlacklist && strings.isEmpty()) {
        throw data.exception(null, "'Block_Damage' found that cannot break any blocks!", "This happens when you use 'Blacklist: false' and an empty 'Block_List'");
    }
    Map<Material, Integer> blockList = new HashMap<>(strings.size());
    for (String[] split : strings) {
        List<Material> materials = EnumUtil.parseEnums(Material.class, split[0]);
        int durability = split.length > 1 ? Integer.parseInt(split[1]) : damage;
        materials.forEach(material -> blockList.put(material, durability));
    }
    strings = data.ofList("Shots_To_Break_Blocks").addArgument(Material.class, true).addArgument(int.class, true).assertList().get();
    Map<Material, Integer> shotsToBreak = new HashMap<>(strings.size());
    if (isBlacklist) {
        for (String[] split : strings) {
            List<Material> materials = EnumUtil.parseEnums(Material.class, split[0]);
            int durability = Integer.parseInt(split[1]);
            materials.forEach(material -> shotsToBreak.put(material, durability));
        }
    } else if (!strings.isEmpty()) {
        throw data.exception(null, "Found 'Block_Damage' that uses 'Shots_To_Break_Blocks' when 'Blacklist: false'", "'Shots_To_Break_Blocks' should only be used when 'Blacklist: true'", "Instead, copy and paste your values from 'Shots_To_Break_Blocks' to 'Block_List'");
    }
    if (dropChance == null || NumberUtil.equals(dropChance, 0.0)) {
        dropChance = 0.0;
    }
    return new BlockDamage(isBreakBlocks, damage, defaultBlockDurability, isBlacklist, dropChance, blockList, shotsToBreak);
}
Also used : HashMap(java.util.HashMap) ChanceSerializer(me.deecaad.core.file.serializers.ChanceSerializer) SerializeData(me.deecaad.core.file.SerializeData) Material(org.bukkit.Material) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ChanceSerializer

use of me.deecaad.core.file.serializers.ChanceSerializer 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

ChanceSerializer (me.deecaad.core.file.serializers.ChanceSerializer)2 HashMap (java.util.HashMap)1 Nonnull (javax.annotation.Nonnull)1 SerializeData (me.deecaad.core.file.SerializeData)1 SerializerException (me.deecaad.core.file.SerializerException)1 SerializerMissingKeyException (me.deecaad.core.file.SerializerMissingKeyException)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 Material (org.bukkit.Material)1 NotNull (org.jetbrains.annotations.NotNull)1