Files
EnderDragon/src/main/java/pers/xanadu/enderdragon/manager/RewardManager.java
T
2023-08-25 22:00:26 +08:00

134 lines
5.0 KiB
Java

package pers.xanadu.enderdragon.manager;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import pers.xanadu.enderdragon.config.Config;
import pers.xanadu.enderdragon.config.Lang;
import pers.xanadu.enderdragon.reward.Reward;
import pers.xanadu.enderdragon.reward.Chance;
import pers.xanadu.enderdragon.util.MyDragon;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import static pers.xanadu.enderdragon.EnderDragon.*;
public class RewardManager {
public static void reload(){
for(MyDragon dragon : DragonManager.dragons){
dragon.datum.clear();
File file = getRewardFile(dragon.unique_name);
if(file == null) return;
FileConfiguration data = loadConfiguration(file);
String path = "list";
List<String> list = data.getStringList(path);
// if list == null ?
for(String str : list){
YamlConfiguration yml = new YamlConfiguration();
if(Config.advanced_setting_backslash_split_reward) yml.options().pathSeparator('\\');
try{
yml.loadFromString(str);
Reward reward = ItemManager.readAsReward(yml);
dragon.datum.add(reward);
}catch (InvalidConfigurationException e){
Lang.error(Lang.plugin_item_read_error + str);
}
}
}
}
public static void addItem(String key,Reward reward){
addItem(key,reward.getItem(),reward.getChance());
}
public static void addItem(String key, ItemStack item, Chance chance){
MyDragon dragon = DragonManager.mp.get(key);
if(dragon == null) return;
File file = getRewardFile(dragon.unique_name);
if(file == null) return;
FileConfiguration data = loadConfiguration(file);
String path = "list";
List<String> list = data.getStringList(path);
Reward reward = new Reward(item,chance);
list.add(reward.toString());
data.set(path,list);
try {
data.save(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
dragon.datum.add(reward);
}
public static void clearItem(String key){
MyDragon dragon = DragonManager.mp.get(key);
if(dragon == null) return;
File file = getRewardFile(dragon.unique_name);
if(file == null) return;
FileConfiguration data = loadConfiguration(file);
String path = "list";
data.set(path,"");
try {
data.save(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
dragon.datum.clear();
}
public static boolean removeItem(String key,int idx){
MyDragon dragon = DragonManager.mp.get(key);
if(dragon == null) return false;
File file = getRewardFile(dragon.unique_name);
if(file == null) return false;
FileConfiguration data = loadConfiguration(file);
String path = "list";
List<String> list = data.getStringList(path);
try{
list.remove(idx);
data.set(path,list);
data.save(file);
dragon.datum.remove(idx);
return true;
}catch (IndexOutOfBoundsException e){
Lang.error("Index out of bound!");
}catch (IOException e) {
throw new RuntimeException(e);
}
return false;
}
private static File getRewardFile(String key){
String file_path = "reward/" + key + ".yml";
File file = new File(plugin.getDataFolder(),file_path);
if(!file.exists()) {
try{
YamlConfiguration yml = new YamlConfiguration();
if(Config.advanced_setting_backslash_split_reward) yml.options().pathSeparator('\\');
yml.set("version","2.1.0");
yml.set("list","");
yml.save(file);
}catch (IOException e){
Lang.error("Not Found "+file_path+" ,skipped it.");
return null;
}
}
return new File(plugin.getDataFolder(),file_path);
}
private static YamlConfiguration loadConfiguration(@NotNull File file) {
YamlConfiguration config = new YamlConfiguration();
if(Config.advanced_setting_backslash_split_reward) config.options().pathSeparator('\\');
try {
config.load(file);
} catch (FileNotFoundException ex) {
} catch (IOException | InvalidConfigurationException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
}
return config;
}
}