EpicEnchants/src/main/java/com/craftaro/epicenchants/managers/EnchantManager.java

48 lines
1.7 KiB
Java
Raw Normal View History

package com.craftaro.epicenchants.managers;
2019-01-16 13:01:24 +01:00
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;
2019-01-16 13:01:24 +01:00
import java.io.File;
2019-02-19 14:23:20 +01:00
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
2019-01-17 00:16:30 +01:00
import java.util.stream.Collectors;
2019-01-16 13:01:24 +01:00
import static com.craftaro.epicenchants.utils.single.ConfigParser.parseEnchant;
2019-02-19 14:23:20 +01:00
public class EnchantManager extends Manager<String, Enchant> {
public EnchantManager(EpicEnchants instance) {
2019-02-19 14:23:20 +01:00
super(instance);
2019-01-16 13:01:24 +01:00
}
2019-01-17 00:16:30 +01:00
public Collection<Enchant> getEnchants(Group group) {
2019-02-19 14:23:20 +01:00
return Collections.unmodifiableCollection(getValues().stream().filter(s -> s.getGroup().equals(group)).collect(Collectors.toList()));
2019-01-17 00:16:30 +01:00
}
public Optional<Enchant> getRandomEnchant(Group group) {
Collection<Enchant> tierList = getEnchants(group);
2019-01-17 00:16:30 +01:00
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));
2019-02-19 14:23:20 +01:00
add(enchant.getIdentifier(), enchant);
}
2019-02-19 14:23:20 +01:00
}