package com.craftaro.epicenchants.managers; import com.craftaro.epicenchants.objects.Enchant; import com.craftaro.epicenchants.objects.Group; import com.craftaro.epicenchants.EpicEnchants; import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; import java.io.File; import java.util.Collection; import java.util.Collections; import java.util.Optional; import java.util.stream.Collectors; import static com.craftaro.epicenchants.utils.single.ConfigParser.parseEnchant; public class EnchantManager extends Manager { public EnchantManager(EpicEnchants instance) { super(instance); } public Collection getEnchants(Group group) { return Collections.unmodifiableCollection(getValues().stream().filter(s -> s.getGroup().equals(group)).collect(Collectors.toList())); } public Optional getRandomEnchant(Group group) { Collection tierList = getEnchants(group); return tierList.stream().skip((int) (tierList.size() * Math.random())).findFirst(); } public void loadEnchants() { this.instance.getFileManager().getYmlFiles("enchants").forEach(file -> { try { loadEnchant(file); } catch (Exception e) { Bukkit.getConsoleSender().sendMessage("Something went wrong loading the enchant from file " + file.getName()); Bukkit.getConsoleSender().sendMessage("Please check to make sure there are no errors in the file."); e.printStackTrace(); } }); } public void loadEnchant(File file) { Enchant enchant = parseEnchant(this.instance, YamlConfiguration.loadConfiguration(file)); add(enchant.getIdentifier(), enchant); } }