update to v2.2.0

This commit is contained in:
Xanadu13
2023-08-25 21:58:25 +08:00
committed by GitHub
parent 4913a2c4f8
commit c1211c5155
22 changed files with 1898 additions and 0 deletions
@@ -0,0 +1,172 @@
package pers.xanadu.enderdragon.config;
import org.bukkit.configuration.file.FileConfiguration;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import static pers.xanadu.enderdragon.EnderDragon.plugin;
import static pers.xanadu.enderdragon.config.Lang.error;
public class Config {
public static String version;
public static String lang;
public static boolean debug;
public static boolean advanced_setting_world_env_fix;
public static boolean advanced_setting_save_respawn_status;
public static boolean advanced_setting_glowing_fix;
public static boolean advanced_setting_backslash_split_reward;
public static String damage_visible_mode;
public static int damage_statistics_limit;
public static String special_dragon_jude_mode;
public static boolean crystal_invulnerable;
public static boolean respawn_cd_enable;
public static boolean resist_player_respawn;
public static boolean resist_dragon_breath_gather;
public static boolean hook_plugins_MythicLib;
public static boolean expansion_groovy;
public static String main_gui;
public static String item_format_reward;
public static List<String> dragon_setting_file;
public static List<String> blacklist_worlds;
private static final String pluginFilePath;
public static void reload(FileConfiguration file){
Field[] fields = Config.class.getFields();
for(Field field : fields){
Type type = field.getType();
if(type.equals(java.lang.String.class)){
try{
field.set(null,"");
}catch (Exception ignored){}
}
else if(type.equals(java.util.List.class)){
try{
field.set(null,new ArrayList());
}catch (Exception ignored){}
}
}
char split = file.options().pathSeparator();
Iterator<String> it = file.getKeys(true).iterator();
while (it.hasNext()){
String str = it.next();
try{
if(file.isConfigurationSection(str)) continue;
if(str.startsWith("auto_respawn")) continue;
Config.class.getField(str.replace(split, '_')).set(null, file.get(str));
}catch (Exception e){
error("Config loading error! Key: "+str);
}
}
switch (special_dragon_jude_mode.toLowerCase()){
case "pc" :
case "weight" : break;
default: {
error("Wrong special_dragon_jude_mode type in config.yml! Only \"weight\" or \"pc\" is valid.");
}
}
}
public static void saveResource(String source,String to,boolean replace){
if (source == null || source.equals("")) return;
source = source.replace('\\', '/');
InputStream in = plugin.getResource(source);
if (in == null) return;
File outFile = new File("plugins/EnderDragon", to);
int lastIndex = to.lastIndexOf('/');
File outDir = new File("plugins/EnderDragon", to.substring(0, Math.max(lastIndex, 0)));
if (!outDir.exists()) {
outDir.mkdirs();
}
try {
if (!outFile.exists() || replace) {
OutputStream out = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
}
} catch (IOException ignored) {}
}
public static void copyFile(String source, String dest,boolean replace) {
if (source == null || source.equals("")) return;
source = source.replace('\\', '/');
File inFile = new File("plugins/EnderDragon", source);
File outFile = new File("plugins/EnderDragon", dest);
int lastIndex = dest.lastIndexOf('/');
File outDir = new File("plugins/EnderDragon", dest.substring(0, Math.max(lastIndex, 0)));
if (!outDir.exists()) {
outDir.mkdirs();
}
try {
if (!outFile.exists() || replace) {
InputStream in = new FileInputStream(inFile);
OutputStream out = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
}
} catch (IOException ignored) {
Lang.error("Failed to copy file: "+inFile.getPath()+" -> "+outFile.getPath());
}
}
public static void saveDirectoryResource(String srcDirectory,boolean replace) {
String destDirectory = new File(plugin.getDataFolder(),srcDirectory).getPath();
try {
JarFile jarFile = new JarFile(pluginFilePath);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (!entry.isDirectory() && entry.getName().startsWith(srcDirectory)) {
String relativePath = entry.getName().substring(srcDirectory.length());
File destinationFile = new File(destDirectory, relativePath);
if(!destinationFile.exists() || replace){
destinationFile.getParentFile().mkdirs();
//plugin.saveResource(entry.getName(),false);
try (InputStream input = jarFile.getInputStream(entry);
FileOutputStream output = new FileOutputStream(destinationFile)
) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
}
}
}
}
jarFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String convertInputStreamToString(InputStream inputStream) {
if(inputStream==null) return null;
StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line).append('\n');
}
} catch (Exception e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
static {
pluginFilePath = new File(Config.class.getProtectionDomain().getCodeSource().getLocation().getFile()).getPath();
}
}
@@ -0,0 +1,135 @@
package pers.xanadu.enderdragon.config;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import pers.xanadu.enderdragon.EnderDragon;
import java.io.*;
import static pers.xanadu.enderdragon.EnderDragon.*;
public class FileUpdater {
public static void update() throws IOException {
String respawn_world_name = null;
FileConfiguration config_old = plugin.getConfig();
if("2.1.0".equals(config_old.getString("version"))){
Config.saveResource("config.yml","new/config.yml",true);
File config_new_F = new File(plugin.getDataFolder(),"new/config.yml");
FileConfiguration config_new = YamlConfiguration.loadConfiguration(config_new_F);
config_new.set("lang",config_old.getString("lang"));
config_new.set("damage_visible_mode",config_old.getString("damage_visible_mode"));
config_new.set("damage_statistics.limit",config_old.getInt("damage_statistics.limit"));
String judge_mode = config_old.getString("special_dragon_jude_mode");
if("edge".equals(judge_mode)) judge_mode = "weight";//"edge" is deprecated
config_new.set("special_dragon_jude_mode",judge_mode);
config_new.set("dragon_setting_file",config_old.getStringList("dragon_setting_file"));
// auto_respawn
config_new.set("auto_respawn.task1.enable",config_old.getBoolean("auto_respawn.enable"));
respawn_world_name = config_old.getString("auto_respawn.world_the_end_name");
config_new.set("auto_respawn.task1.world_name",respawn_world_name);
config_new.set("auto_respawn.task1.respawn_time",config_old.getString("auto_respawn.respawn_time"));
config_new.set("crystal_invulnerable",config_old.getBoolean("auto_respawn.invulnerable"));
config_new.set("respawn_cd.enable",config_old.getBoolean("respawn_cd.enable"));
config_new.set("resist_player_respawn",config_old.getBoolean("resist_player_respawn"));
config_new.set("resist_dragon_breath_gather",config_old.getBoolean("resist_dragon_breath_gather"));
config_new.set("main_gui",config_old.getString("main_gui"));
config_new.set("blacklist_worlds",config_old.getStringList("blacklist_worlds"));
//item_format.data -> item_format.reward
config_new.set("item_format.reward",config_old.getString("item_format.data"));
config_new.set("hook_plugins.MythicLib",config_old.getBoolean("hook_plugins.MythicLib"));
config_new.set("debug",config_old.getBoolean("debug"));
config_new.set("advanced_setting.world_env_fix",config_old.getBoolean("advanced_setting.world_env_fix"));
config_new.set("advanced_setting.save_respawn_status",config_old.getBoolean("advanced_setting.save_respawn_status"));
config_new.save(config_new_F);
}
else Lang.error("The version of config.yml is not supported!");
File folder = new File(plugin.getDataFolder(),"setting");
if(folder.exists()){
File[] files = folder.listFiles();
if(files != null){
for(File file : files){
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
String ver = config.getString("version");
if(!"2.1.0".equals(ver)) {
Lang.error("The version of setting/"+file.getName()+" is not supported!");
continue;
}
String name = file.getName();
Config.copyFile("setting/"+name,"new/setting/"+name,true);
}
File new_folder = new File(plugin.getDataFolder(),"new/setting/");
files = new_folder.listFiles();
if(files != null){
for(File file : files){
FileOutputStream out = new FileOutputStream(file,true);
out.write(("\n\n" +
"# the least interval time between injuries (unit:tick, 1tick=0.05s)\n" +
"# only supports integer value\n" +
"no_damage_tick: 10\n" +
"\n" +
"attack:\n" +
" #the damage modify when this dragon attacks player with body or limb (allow double and negative value)\n" +
" damage_modify: 0\n" +
"\n" +
" #the effect give to player when this ender dragon attacks player with body or limb\n" +
" #the format is the same as effect_cloud.potion\n" +
" potion_effect:\n" +
" - ''\n" +
" - ''\n" +
" - ''\n" +
"\n" +
" # the effect give to player when this ender dragon attacks player with body or limb\n" +
" # format: (<type>: seconds)\n" +
" extra_effect:\n" +
" fire: 0\n" +
" # \"freeze\" can only be used in server greater than or equal to version 1.17\n" +
" freeze: 0\n" +
"\n"
).getBytes());
out.close();
FileConfiguration fc = YamlConfiguration.loadConfiguration(file);
fc.set("version","2.2.0");
fc.set("attack.damage_modify",fc.getDouble("attack_damage_modify"));
fc.set("attack_damage_modify",null);
fc.get("attack.potion_effect",fc.getStringList("attack_potion_effect"));
fc.set("attack_potion_effect",null);
fc.save(file);
}
}
}
}
String lang_name = config_old.getString("lang","English") + ".yml";
FileConfiguration lang_old = lang;
if("2.1.0".equals(lang_old.getString("version"))){
Config.saveResource("lang/"+lang_name,"new/lang/"+lang_name,true);
File lang_new_F = new File(plugin.getDataFolder(),"new/lang/"+lang_name);
FileConfiguration lang_new = YamlConfiguration.loadConfiguration(lang_new_F);
lang_old.getKeys(true).forEach(key->{
Object obj = lang_old.get(key);
lang_new.set(key,obj);
});
lang_new.set("version","2.2.0");
lang_new.save(lang_new_F);
}
else Lang.error("The version of "+lang_name+" is not supported!");
FileConfiguration data_old = EnderDragon.data;
if("2.1.0".equals(data_old.getString("version"))){
Config.copyFile("data.yml","new/data.yml",true);
File new_data_file = new File(plugin.getDataFolder(),"new/data.yml");
FileConfiguration new_data = YamlConfiguration.loadConfiguration(new_data_file);
String next_time = new_data.getString("auto_respawn.next_respawn_time");
if(next_time!=null && respawn_world_name!=null){
new_data.set("version","2.2.0");
new_data.set("auto_respawn.task1.world_name",respawn_world_name);
new_data.set("auto_respawn.task1.next_respawn_time",next_time);
new_data.save(new_data_file);
}
}
else Lang.error("The version of data.yml is not supported!");
Lang.info("New config files are generated in plugins/EnderDragon/new.");
Lang.warn("Attention: Please confirm the accuracy before using new config!");
}
}
@@ -0,0 +1,214 @@
package pers.xanadu.enderdragon.config;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import pers.xanadu.enderdragon.util.ColorUtil;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;
import static pers.xanadu.enderdragon.EnderDragon.*;
public class Lang {
public static String version;
public static String plugin_prefix;
public static String plugin_wrong_file_version;
public static String plugin_read_file;
public static String plugin_item_read_error;
public static String plugin_checking_update;
public static String plugin_check_update_fail;
public static String plugin_out_of_date;
public static String plugin_up_to_date;
public static String plugin_disable;
public static String plugin_file_save_error;
public static String command_no_permission;
public static String command_reload_config;
public static String command_only_player;
public static String command_drop_item_add_empty;
public static String command_drop_item_add_invalid_chance;
public static String command_drop_item_add_succeed;
public static String command_drop_item_clear;
public static String command_drop_item_remove_invalid_num;
public static String command_drop_item_remove_fail;
public static String command_drop_item_remove_succeed;
public static String command_respawn_cd_disable;
public static String command_respawn_cd_remove;
public static String command_respawn_cd_removeAll;
public static String command_respawn_cd_retry;
public static String command_respawn_cd_set;
public static String command_respawn_cd_start_already_started;
public static String command_respawn_cd_start_none;
public static String command_respawn_cd_start_succeed;
public static String gui_default_title;
public static String gui_not_found;
public static String gui_item_lore;
public static String gui_item_cmd_lore;
public static String dragon_damage_display;
public static String dragon_player_inv_full;
public static String dragon_no_killer;
public static String dragon_auto_respawn;
public static String dragon_not_found;
public static String dragon_damage_statistics_text;
public static List<String> dragon_damage_statistics_hover_prefix;
public static String dragon_damage_statistics_hover_mt;
public static List<String> dragon_damage_statistics_hover_suffix;
public static String dragon_damage_statistics_hover_exceeds_limit;
public static String world_env_fix_enable;
public static String expansion_groovy_disable;
public static String CommandTips1;
public static String CommandTips2;
public static String CommandTips3;
public static String CommandTips4;
private static final Logger LOGGER = Bukkit.getLogger();
private static final ConsoleCommandSender console = Bukkit.getConsoleSender();
public static void reload(FileConfiguration file){
Field[] fields = Lang.class.getFields();
for(Field field : fields){
Type type = field.getType();
if(type.equals(java.lang.String.class)){
try{
field.set(null,"");
}catch (Exception ignored){}
}
else if(type.equals(java.util.List.class)){
try{
field.set(null,new ArrayList());
}catch (Exception ignored){}
}
}
char split = file.options().pathSeparator();
Iterator<String> it = file.getKeys(true).iterator();
while (it.hasNext()){
String str = it.next();
try{
if(file.isConfigurationSection(str)) continue;
if(file.isString(str)) Lang.class.getField(str.replace(split, '_')).set(null, ColorUtil.parse(file.getString(str)));
else if(file.isList(str)){
List<String> list = file.getStringList(str);
list.forEach(s-> s = ColorUtil.parse(s));
Lang.class.getField(str.replace(split, '_')).set(null, list);
}
}catch (Exception e){
error("Language loading error! Key: "+str);
}
}
CommandTips1 = ColorUtil.parse(lang.getString("CommandTips1","§e/ed reload §a- reload the config"));
CommandTips2 = ColorUtil.parse(lang.getString("CommandTips2","§e/ed respawn §a- respawn a dragon"));
CommandTips3 = ColorUtil.parse(lang.getString("CommandTips3","§e/ed drop gui §a- view the drop_item"));
CommandTips4 = ColorUtil.parse(lang.getString("CommandTips4","§e/ed drop add <name> <chance> §a- add drop_item to one dragon"));
}
public static void sendMessage(String str) {
if(str == null) {
console.sendMessage(Lang.plugin_prefix + "null");
return;
}
if(str.contains("\\n")){
String[] strings = str.split("\\\\n");
for(String s : strings){
sendMessage(s);
}
}
else console.sendMessage(Lang.plugin_prefix + str);
}
public static void info(String str){
if(str == null) {
console.sendMessage("§a[EnderDragon] " + "null");
return;
}
if(str.contains("\\n")){
String[] strings = str.split("\\\\n");
for(String s : strings){
info(s);
}
}
else console.sendMessage("§a[EnderDragon] "+str);
}
public static void warn(String str){
if(str == null) {
console.sendMessage("§e[EnderDragon] " + "null");
return;
}
if(str.contains("\\n")){
String[] strings = str.split("\\\\n");
for(String s : strings){
warn(s);
}
}
else console.sendMessage("§e[EnderDragon] "+str);
}
public static void error(String str){
if(str == null) {
console.sendMessage("§c[EnderDragon] " + "null");
return;
}
if(str.contains("\\n")){
String[] strings = str.split("\\\\n");
for(String s : strings){
error(s);
}
}
else console.sendMessage("§c[EnderDragon] "+str);
}
public static void debug(String str){
if(Config.debug) LOGGER.info(str);
}
public static void sendFeedback(CommandSender sender,String str){
if(str == null) {
sender.sendMessage(Lang.plugin_prefix + "null");
return;
}
if(str.contains("\\n")){
String[] strings = str.split("\\\\n");
for(String s : strings){
sender.sendMessage(Lang.plugin_prefix + s);
}
}
else sender.sendMessage(Lang.plugin_prefix + str);
}
public static void broadcastMSG(String str){
if(str == null) {
Bukkit.broadcastMessage(Lang.plugin_prefix + "null");
return;
}
if(str.contains("\\n")){
String[] strings = str.split("\\\\n");
for(String s : strings){
broadcastMSG(s);
}
}
else Bukkit.broadcastMessage(Lang.plugin_prefix + str);
}
public static void broadcastMSG(List<String> list){
if(list == null) return;
for(String str : list){
Bukkit.broadcastMessage(Lang.plugin_prefix + str);
}
}
public static void runCommands(List<String> list, Player p){
if(list == null) return;
for (String cmd : list) {
if(cmd.equals("")) continue;
if(!cmd.contains("%player%")) plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(),cmd);
else{
if(p != null) plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(),cmd.replaceAll("%player%",p.getName()));
}
}
}
public static void runCommands(List<String> list){
if(list == null) return;
for(String cmd : list){
if(cmd.equals("")) continue;
plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(),cmd);
}
}
}