Skip to content

Commit

Permalink
Merge pull request #16 from iXanadu13/V2.5.0
Browse files Browse the repository at this point in the history
V2.5.0
  • Loading branch information
iXanadu13 authored Mar 18, 2024
2 parents f11a906 + ac5ed15 commit b759530
Show file tree
Hide file tree
Showing 15 changed files with 456 additions and 309 deletions.
7 changes: 7 additions & 0 deletions src/main/java/pers/xanadu/enderdragon/EnderDragon.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public void onEnable() {
finish = true;
}
public static void reloadAll(){
ItemManager.getLegacy().set(false);
WorldManager.reload();
GlowManager.reload();
EnderDragon.getInstance().loadFiles();
Expand All @@ -128,6 +129,12 @@ public void run() {
this.cancel();
DragonManager.reload();
GuiManager.loadGui();
if (ItemManager.getLegacy().get()){
Lang.error("I'm sorry that data_type 'nbt' and 'advanced' probably will be disabled in 1.20.5+");
Lang.error("It is recommended to migrate item configuration to bukkit format for compatibility.");
Lang.error("You can use /ed migrate to generate new config.");
}

runnable.runTaskTimer(plugin,0,1);
}
}.runTaskTimer(plugin, 1, 5);
Expand Down
135 changes: 102 additions & 33 deletions src/main/java/pers/xanadu/enderdragon/config/FileUpdater.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pers.xanadu.enderdragon.config;

import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
Expand Down Expand Up @@ -130,45 +131,113 @@ public static void update() throws IOException {
// Lang.warn("Attention: Please confirm the accuracy before using new config!");
}
public static void migrate(){
File folder = new File(plugin.getDataFolder(),"reward");
if (folder.exists()){
File[] files = folder.listFiles(f -> f.getName().endsWith(".yml"));
if (files == null || files.length == 0) {
Lang.warn("No files need to be migrated!");
return;
}
for (File file : files){
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
Config.copyFile(file,"new/reward/"+file.getName(),true);
File gen_file = new File(plugin.getDataFolder(),"new/reward/"+file.getName());
YamlConfiguration new_cfg = YamlConfiguration.loadConfiguration(gen_file);
if(Config.advanced_setting_backslash_split_reward) new_cfg.options().pathSeparator('\\');
List<String> gen = new ArrayList<>();
List<String> list = config.getStringList("list");
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);
if (reward == null) continue;
gen.add(saveReward(reward));
}catch (InvalidConfigurationException e){
Lang.error(Lang.plugin_item_read_error + str);
boolean b1 = handleRewards();
boolean b2 = handleGuiItems();
if (b1 || b2) Lang.info("New reward configuration files are generated in plugins/EnderDragon/migrate.");
else Lang.warn("No files need to be migrated!");
}
private static boolean handleGuiItems(){
File folder = new File(plugin.getDataFolder(),"gui");
if (!folder.exists()) return false;
File[] files = folder.listFiles(f -> f.getName().endsWith(".yml"));
if (files == null || files.length == 0) {
return false;
}
for (File file : files){
Config.copyFile(file,"new/gui/"+file.getName(),true);
File gen_file = new File(plugin.getDataFolder(),"new/gui/"+file.getName());
YamlConfiguration cfg = YamlConfiguration.loadConfiguration(gen_file);
cfg.getKeys(false).forEach(key->{
ConfigurationSection gui = cfg.getConfigurationSection(key);
if (gui == null) return;
ConfigurationSection Items = gui.getConfigurationSection("Items");
if (Items == null) return;
Items.getKeys(false).forEach(ch -> {
ConfigurationSection slot = Items.getConfigurationSection(ch);
if (slot == null) return;
if (!slot.contains("data")) return;
ItemStack itemStack = readItemStack(slot,"data");
ItemManager.save2section_simple(itemStack,slot,"data");
if (!slot.contains("data_disable")) return;
itemStack = readItemStack(slot,"data_disable");
ItemManager.save2section_simple(itemStack,slot,"data_disable");
});
Items.getKeys(false).forEach(ch -> {
ConfigurationSection slot = Items.getConfigurationSection(ch);
if (slot == null) return;
if (slot.contains("data_type")){
slot.set("data_type","simple");
}
}
new_cfg.set("list",gen);
else if (slot.contains("data") || slot.contains("data_disable")) {
slot.set("data_type","simple");
}
});
});
try{
cfg.save(gen_file);
}catch (IOException e){
e.printStackTrace();
}
}
return true;
}
private static ItemStack readItemStack(ConfigurationSection section,final String path){
ItemStack itemStack;
final String type = section.getString("data_type","");
switch (type){
case "nbt":
itemStack = ItemManager.readFromNBT(section,path);
ItemManager.getLegacy().compareAndSet(false,true);
break;
case "advanced":
itemStack = ItemManager.readFromAdvData(section,path);
ItemManager.getLegacy().compareAndSet(false,true);
break;
case "simple":
itemStack = ItemManager.readFromSimple(section,path);
break;
default:
itemStack = ItemManager.readFromBukkit(section,path);
}
return itemStack;
}
private static boolean handleRewards(){
File folder = new File(plugin.getDataFolder(),"reward");
if (!folder.exists()) return false;
File[] files = folder.listFiles(f -> f.getName().endsWith(".yml"));
if (files == null || files.length == 0) {
return false;
}
for (File file : files){
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
Config.copyFile(file,"new/reward/"+file.getName(),true);
File gen_file = new File(plugin.getDataFolder(),"new/reward/"+file.getName());
YamlConfiguration new_cfg = YamlConfiguration.loadConfiguration(gen_file);
if(Config.advanced_setting_backslash_split_reward) new_cfg.options().pathSeparator('\\');
List<String> gen = new ArrayList<>();
List<String> list = config.getStringList("list");
for(String str : list){
YamlConfiguration yml = new YamlConfiguration();
if(Config.advanced_setting_backslash_split_reward) yml.options().pathSeparator('\\');
try{
new_cfg.save(gen_file);
}catch (IOException e){
e.printStackTrace();
yml.loadFromString(str);
Reward reward = ItemManager.readAsReward(yml);
if (reward == null) continue;
gen.add(saveReward(reward));
}catch (InvalidConfigurationException e){
Lang.error(Lang.plugin_item_read_error + str);
}
}
Lang.info("New reward config files are generated in plugins/EnderDragon/migrate.");
new_cfg.set("list",gen);
try{
new_cfg.save(gen_file);
}catch (IOException e){
e.printStackTrace();
}
}
else Lang.warn("No files need to be migrated!");
return true;
}
public static String saveReward(Reward reward){
private static String saveReward(Reward reward){
Chance chance = reward.getChance();
ItemStack item = reward.getItem();
double value = chance.getValue();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pers/xanadu/enderdragon/gui/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void setPage(int a) {
this.page = a;
for(int i=0;i<this.slots.size();i++){
GUISlot slot = this.slots.get(i);
if (slot instanceof ItemSlot) {
if (slot instanceof ItemSlot || slot instanceof DragonSlot) {
this.inv.setItem(i, new ItemStack(Material.AIR));
}
}
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/pers/xanadu/enderdragon/gui/GUISlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ public static GUISlot parse(ConfigurationSection section){
return new EmptySlot();
}
protected enum DataType{
DEFAULT,NBT,ADVANCED
DEFAULT,NBT,ADVANCED,SIMPLE;
public static DataType fromString(String data_type){
if (data_type == null) return DEFAULT;
switch (data_type){
case "nbt": {
return NBT;
}
case "advanced": {
return ADVANCED;
}
case "simple": {
return SIMPLE;
}
default: {
return DEFAULT;
}
}
}
}
}
39 changes: 22 additions & 17 deletions src/main/java/pers/xanadu/enderdragon/gui/slot/TipSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
import org.bukkit.inventory.meta.ItemMeta;
import pers.xanadu.enderdragon.gui.GUISlot;
import pers.xanadu.enderdragon.gui.GUISlotType;
import pers.xanadu.enderdragon.manager.ItemManager;

import static pers.xanadu.enderdragon.manager.ItemManager.*;

public class TipSlot extends GUISlot {
private ItemStack item;
private final ItemStack item;
protected boolean hasDisableMode;
protected ItemStack itemOnDisable;

public TipSlot(GUISlotType slotType, Material material, String str) {
super(slotType);
this.item = new ItemStack(material);
ItemMeta meta = this.item.getItemMeta();
assert meta != null;
meta.setDisplayName(str);
this.item.setItemMeta(meta);
}
Expand All @@ -28,22 +30,25 @@ public TipSlot(GUISlotType slotType, ConfigurationSection section) {
this.hasDisableMode = true;
}
else this.hasDisableMode = false;
if(hasDisableMode){
if(data_type == DataType.NBT) this.itemOnDisable = readFromNBT(section,"data_disable");
else this.itemOnDisable = readFromBukkit(section,"data_disable");
}
String data_type = section.getString("data_type");
if("nbt".equals(data_type)) {
this.data_type = DataType.NBT;
this.item = readFromNBT(section,"data");
}
else if("advanced".equals(data_type)){
this.data_type = DataType.ADVANCED;
this.item = readFromAdvData(section,"data");
}
else {
this.data_type = DataType.DEFAULT;
this.item = readFromBukkit(section,"data");
this.data_type = DataType.fromString(section.getString("data_type"));
switch (data_type) {
case NBT:
ItemManager.getLegacy().compareAndSet(false,true);
this.item = readFromNBT(section,"data");
if(hasDisableMode) this.itemOnDisable = readFromNBT(section,"data_disable");
break;
case ADVANCED:
ItemManager.getLegacy().compareAndSet(false,true);
this.item = readFromAdvData(section,"data");
if(hasDisableMode) this.itemOnDisable = readFromAdvData(section,"data_disable");
break;
case SIMPLE:
this.item = readFromSimple(section,"data");
if(hasDisableMode) this.itemOnDisable = readFromSimple(section,"data_disable");
break;
default:
this.item = readFromBukkit(section,"data");
if(hasDisableMode) this.itemOnDisable = readFromBukkit(section,"data_disable");
}
}

Expand Down
90 changes: 42 additions & 48 deletions src/main/java/pers/xanadu/enderdragon/manager/DragonManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,56 +54,50 @@ public class DragonManager {
private static final Pattern pattern_attacker_top = Pattern.compile("%attacker_top_(\\d+)%");

public static void reload(){
new BukkitRunnable(){
@Override
public void run(){
dragons.clear();
mp.clear();
dragon_names.clear();
sum = 0;
if(Config.dragon_setting_file == null){
Lang.error("\"dragon_setting_file\" in config.yml is empty!");
Lang.warn("Plugin will use the default config...");
Config.dragon_setting_file = new ArrayList<>();
Config.dragon_setting_file.add("default:5");
Config.dragon_setting_file.add("special:5");
}
for(String str : Config.dragon_setting_file){
String[] s = str.split(":");
if(s.length != 2){
Lang.error("\"dragon_setting_file\" in config.yml error! Key: " + str);
continue;
}
String path = "setting/" + s[0] + ".yml";
int edge = -1;
try {
edge = Integer.parseInt(s[1]);
} catch (NumberFormatException ignored){}
if(edge < 0) {
Lang.error("\"dragon_setting_file\" in config.yml error! Key: " + str);
continue;
}
File file = new File(plugin.getDataFolder(),path);
if(!file.exists()) {
try{
plugin.saveResource("setting/"+file.getName(),false);
file = new File(plugin.getDataFolder(),path);
}catch (Exception ignored){
Lang.error("Not Found setting/" + s[0] + ".yml ,skipped it.");
continue;
}
}
FileConfiguration fc = YamlConfiguration.loadConfiguration(file);
if(!Version.setting_dragon.equals(fc.getString("version"))){
Lang.warn(Lang.plugin_wrong_file_version.replace("{file_name}", file.getName()));
}
readSettingFile(fc,edge);
dragons.clear();
mp.clear();
dragon_names.clear();
sum = 0;
if(Config.dragon_setting_file == null){
Lang.error("\"dragon_setting_file\" in config.yml is empty!");
Lang.warn("Plugin will use the default config...");
Config.dragon_setting_file = new ArrayList<>();
Config.dragon_setting_file.add("default:5");
Config.dragon_setting_file.add("special:5");
}
for(String str : Config.dragon_setting_file){
String[] s = str.split(":");
if(s.length != 2){
Lang.error("\"dragon_setting_file\" in config.yml error! Key: " + str);
continue;
}
String path = "setting/" + s[0] + ".yml";
int edge = -1;
try {
edge = Integer.parseInt(s[1]);
} catch (NumberFormatException ignored){}
if(edge < 0) {
Lang.error("\"dragon_setting_file\" in config.yml error! Key: " + str);
continue;
}
File file = new File(plugin.getDataFolder(),path);
if(!file.exists()) {
try{
plugin.saveResource("setting/"+file.getName(),false);
file = new File(plugin.getDataFolder(),path);
}catch (Exception ignored){
Lang.error("Not Found setting/" + s[0] + ".yml ,skipped it.");
continue;
}
dragons.sort((o1, o2) -> o2.priority - o1.priority);
RewardManager.reload();
}
}.runTaskAsynchronously(plugin);

FileConfiguration fc = YamlConfiguration.loadConfiguration(file);
if(!Version.setting_dragon.equals(fc.getString("version"))){
Lang.warn(Lang.plugin_wrong_file_version.replace("{file_name}", file.getName()));
}
readSettingFile(fc,edge);
}
dragons.sort((o1, o2) -> o2.priority - o1.priority);
RewardManager.reload();
}
public static MyDragon judge(){
if(Config.special_dragon_jude_mode.equalsIgnoreCase("weight")){
Expand Down
Loading

0 comments on commit b759530

Please sign in to comment.