Search in sources :

Example 1 with SerializeData

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

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

the class AmmoTypes method serialize.

@Override
@Nonnull
public AmmoTypes serialize(SerializeData data) throws SerializerException {
    List<IAmmoType> ammoTypes = new ArrayList<>();
    for (String ammoName : data.config.getConfigurationSection(data.key).getKeys(false)) {
        SerializeData move = data.move(ammoName);
        String symbol = move.of("Symbol").assertType(String.class).get(null);
        // Experience
        int experienceAsAmmoCost = move.of("Experience_As_Ammo_Cost").assertPositive().getInt(-1);
        if (experienceAsAmmoCost != -1) {
            ammoTypes.add(new ExperienceAmmo(ammoName, symbol, experienceAsAmmoCost));
            continue;
        }
        // Money
        int moneyAsAmmoCost = move.of("Money_As_Ammo_Cost").assertPositive().getInt(-1);
        if (moneyAsAmmoCost != -1) {
            ammoTypes.add(new MoneyAmmo(ammoName, symbol, moneyAsAmmoCost));
            continue;
        }
        // Item
        ItemStack bulletItem = move.of("Item_Ammo.Bullet_Item").serializeNonStandardSerializer(new ItemSerializer());
        ItemStack magazineItem = move.of("Item_Ammo.Magazine_Item").serializeNonStandardSerializer(new ItemSerializer());
        if (magazineItem == null && bulletItem == null) {
            throw move.exception(null, "Tried to use ammo without any options? You should use at least one of the ammo types!");
        }
        if (bulletItem != null) {
            CustomTag.AMMO_NAME.setString(bulletItem, ammoName);
        }
        // Not else if since both of these can be used at same time
        if (magazineItem != null) {
            CustomTag.AMMO_NAME.setString(magazineItem, ammoName);
            // Set to indicate that this item is magazine
            CustomTag.AMMO_MAGAZINE.setInteger(magazineItem, 1);
        }
        AmmoConverter ammoConverter = (AmmoConverter) move.of("Item_Ammo.Ammo_Converter_Check").serialize(new AmmoConverter());
        ammoTypes.add(new ItemAmmo(ammoName, symbol, bulletItem, magazineItem, ammoConverter));
    }
    return new AmmoTypes(ammoTypes);
}
Also used : ItemSerializer(me.deecaad.core.file.serializers.ItemSerializer) ArrayList(java.util.ArrayList) SerializeData(me.deecaad.core.file.SerializeData) ItemStack(org.bukkit.inventory.ItemStack) Nonnull(javax.annotation.Nonnull)

Aggregations

SerializeData (me.deecaad.core.file.SerializeData)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Nonnull (javax.annotation.Nonnull)1 ChanceSerializer (me.deecaad.core.file.serializers.ChanceSerializer)1 ItemSerializer (me.deecaad.core.file.serializers.ItemSerializer)1 Material (org.bukkit.Material)1 ItemStack (org.bukkit.inventory.ItemStack)1 NotNull (org.jetbrains.annotations.NotNull)1