diff --git a/pom.xml b/pom.xml index 693564b..cd3563e 100644 --- a/pom.xml +++ b/pom.xml @@ -14,17 +14,40 @@ + + papermc-repo + https://repo.papermc.io/repository/maven-public/ + + + sonatype + https://oss.sonatype.org/content/groups/public/ + public https://repo.aurora-pixels.com/repository/public/ + + public-rpg + https://repo.aurora-pixels.com/repository/public-rpg/ + + + io.papermc.paper + paper-api + 1.18.2-R0.1-SNAPSHOT + provided + com.google.code.gson gson 2.8.8 + + me.Demon.DemonPlugin + DemonAPI + 1.2.0 + \ No newline at end of file diff --git a/src/main/java/com/yaohun/questsystem/Main.java b/src/main/java/com/yaohun/questsystem/Main.java index 8d34cd7..a64b496 100644 --- a/src/main/java/com/yaohun/questsystem/Main.java +++ b/src/main/java/com/yaohun/questsystem/Main.java @@ -31,59 +31,61 @@ public class Main extends JavaPlugin { LinkMySqlData(); saveDefaultConfig(); dataManage = new DataManage(); - for (Player player : Bukkit.getOnlinePlayers()){ + for (Player player : Bukkit.getOnlinePlayers()) { Main.dataManage.loadPlayerData(player); } - getServer().getPluginManager().registerEvents(new JoinEvent(),this); + getServer().getPluginManager().registerEvents(new JoinEvent(), this); DemonAPI.sendConsoleMessage("§f[§6!§f] §aQuestSystem §f加载完成,祝你使用愉快!"); DemonAPI.sendConsoleMessage("§f[§6!§f] §b极光像素工作室出品"); } - public void LinkMySqlData(){ + public void LinkMySqlData() { String SQL_Host = "gz-cdb-r9koldtt.sql.tencentcdb.com"; String SQL_Port = "29320"; String SQL_Users = "root"; String SQL_Password = "Pixel@123456"; String SQL_Database = "quest"; sqlManager = new SqlManager(); - sqlUtil = new SqlUtil(SQL_Host,SQL_Port,SQL_Database,SQL_Users,SQL_Password); + sqlUtil = new SqlUtil(SQL_Host, SQL_Port, SQL_Database, SQL_Users, SQL_Password); + sqlUtil.openConnection(); sqlManager.createTable(); } @Override public void onDisable() { - for (Player player : Bukkit.getOnlinePlayers()){ + for (Player player : Bukkit.getOnlinePlayers()) { dataManage.savePlayerData(player); } + sqlUtil.closeConnection(); DemonAPI.sendConsoleMessage("§f[§c!§f] §aQuestSystem §f卸载完成,欢迎下次使用!"); } @Override public boolean onCommand(CommandSender sender, Command command, String Command, String[] args) { if (Command.equalsIgnoreCase("questinfo") && sender instanceof Player player) { - sender.sendMessage("§f[§e!§f]§7查询玩家: "+player.getName()); + sender.sendMessage("§f[§e!§f]§7查询玩家: " + player.getName()); sender.sendMessage("§f[§e!§f]§7任务数据:"); PlayerData data = dataManage.getPlayerData(player); List everydayList = QuestAPI.getPlayerQuestList(player, TimeType.EVERYDAY); - for (QuestData quest : everydayList){ - sender.sendMessage(" [每日] §b"+quest.getQuestKey()+" §f进度: §c"+data.getQuestAmount(quest)+"§f/§a"+quest.getNeedAmount()); + for (QuestData quest : everydayList) { + sender.sendMessage(" [每日] §b" + quest.getQuestKey() + " §f进度: §c" + data.getQuestAmount(quest) + "§f/§a" + quest.getNeedAmount()); } List weeklyList = QuestAPI.getPlayerQuestList(player, TimeType.WEEKLY); - for (QuestData quest : weeklyList){ - sender.sendMessage(" [每周] §b"+quest.getQuestKey()+" §f进度: §c"+data.getQuestAmount(quest)+"§f/§a"+quest.getNeedAmount()); + for (QuestData quest : weeklyList) { + sender.sendMessage(" [每周] §b" + quest.getQuestKey() + " §f进度: §c" + data.getQuestAmount(quest) + "§f/§a" + quest.getNeedAmount()); } List longTermList = QuestAPI.getPlayerQuestList(player, TimeType.LONGTERM); - for (QuestData quest : longTermList){ - sender.sendMessage(" [长期] §b"+quest.getQuestKey()+" §f进度: §c"+data.getQuestAmount(quest)+"§f/§a"+quest.getNeedAmount()); + for (QuestData quest : longTermList) { + sender.sendMessage(" [长期] §b" + quest.getQuestKey() + " §f进度: §c" + data.getQuestAmount(quest) + "§f/§a" + quest.getNeedAmount()); } } if (Command.equalsIgnoreCase("quest")) { Player player = (Player) sender; - if(args.length == 1 && args[0].equalsIgnoreCase("save")) { + if (args.length == 1 && args[0].equalsIgnoreCase("save")) { if (dataManage.getPlayerData(player) != null) { dataManage.savePlayerData(player); } - DemonAPI.sendMessage(sender,"数据已保存至数据库."); + DemonAPI.sendMessage(sender, "数据已保存至数据库."); } /*if(args.length == 2 && args[0].equalsIgnoreCase("js")) { String questKey = args[1]; diff --git a/src/main/java/com/yaohun/questsystem/api/QuestAPI.java b/src/main/java/com/yaohun/questsystem/api/QuestAPI.java index 5d13606..bc830b7 100644 --- a/src/main/java/com/yaohun/questsystem/api/QuestAPI.java +++ b/src/main/java/com/yaohun/questsystem/api/QuestAPI.java @@ -5,7 +5,6 @@ import com.yaohun.questsystem.data.PlayerData; import com.yaohun.questsystem.data.QuestData; import com.yaohun.questsystem.manage.DataManage; import com.yaohun.questsystem.util.TimeType; -import org.bukkit.Bukkit; import org.bukkit.entity.Player; import java.util.ArrayList; @@ -15,31 +14,31 @@ import java.util.Set; public class QuestAPI { - public static PlayerData getPlayerData(Player p){ + public static PlayerData getPlayerData(Player p) { DataManage dataManage = Main.dataManage; PlayerData playerData = dataManage.getPlayerData(p); - if(playerData == null){ + if (playerData == null) { return null; } return playerData; } /* - * 获取玩家的任务列表 - * */ - public static Set getPlayerQuestList(Player p){ + * 获取玩家的任务列表 + * */ + public static Set getPlayerQuestList(Player p) { PlayerData data = Main.dataManage.getPlayerData(p); return data.getQuests(); } /* - * 获取玩家已接受任务 指定周期任务列表 - * */ + * 获取玩家已接受任务 指定周期任务列表 + * */ public static List getPlayerQuestList(Player p, TimeType timeType) { List questDataList = new ArrayList<>(); PlayerData data = Main.dataManage.getPlayerData(p); - for (QuestData questData : data.getQuests()){ - if(questData.getTimeType() == timeType){ + for (QuestData questData : data.getQuests()) { + if (questData.getTimeType() == timeType) { questDataList.add(questData); } } @@ -47,91 +46,92 @@ public class QuestAPI { } /* - * 获取指定任务信息数据 - * */ - public static QuestData getQuestData(String questKey){ + * 获取指定任务信息数据 + * */ + public static QuestData getQuestData(String questKey) { QuestData quest = Main.dataManage.getQuestData(questKey); - if(quest == null){ - System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在."); + if (quest == null) { + System.out.println("[错误 - 任务] 任务: " + questKey + " 配置文件不存在."); return null; } return quest; } /* - * 增加任务进度 - * */ - public static void addQuestProgress(Player p,String questKey,int amount){ + * 增加任务进度 + * */ + public static void addQuestProgress(Player p, String questKey, int amount) { QuestData quest = Main.dataManage.getQuestData(questKey); - if(quest == null){ - System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在."); + if (quest == null) { + System.out.println("[错误 - 任务] 任务: " + questKey + " 配置文件不存在."); return; } PlayerData data = Main.dataManage.getPlayerData(p); - data.addQuestAmount(quest,amount); + data.addQuestAmount(quest, amount); } /* - * 设置任务进度 - * */ - public static void setQuestProgress(Player p,String questKey,int amount){ + * 设置任务进度 + * */ + public static void setQuestProgress(Player p, String questKey, int amount) { QuestData quest = Main.dataManage.getQuestData(questKey); - if(quest == null){ - System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在."); + if (quest == null) { + System.out.println("[错误 - 任务] 任务: " + questKey + " 配置文件不存在."); return; } PlayerData data = Main.dataManage.getPlayerData(p); - data.setQuestAmount(quest,amount); + data.setQuestAmount(quest, amount); } /* - * 强制完成任务进度 - * */ - public static void forceCompleteQuest(Player p,String questKey){ + * 强制完成任务进度 + * */ + public static void forceCompleteQuest(Player p, String questKey) { QuestData quest = Main.dataManage.getQuestData(questKey); - if(quest == null){ - System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在."); + if (quest == null) { + System.out.println("[错误 - 任务] 任务: " + questKey + " 配置文件不存在."); return; } PlayerData data = Main.dataManage.getPlayerData(p); - data.setQuestAmount(quest,-1); + data.setQuestAmount(quest, -1); } + /* - * 强制完成任务进度并触发奖励 - * */ - public static void forceCompleteQuestFinish(Player p,String questKey,boolean finish){ + * 强制完成任务进度并触发奖励 + * */ + public static void forceCompleteQuestFinish(Player p, String questKey, boolean finish) { QuestData quest = Main.dataManage.getQuestData(questKey); - if(quest == null){ - System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在."); + if (quest == null) { + System.out.println("[错误 - 任务] 任务: " + questKey + " 配置文件不存在."); return; } PlayerData data = Main.dataManage.getPlayerData(p); - data.setQuestAmount(quest,-1); - if(finish){ + data.setQuestAmount(quest, -1); + if (finish) { quest.sendFinishCommand(p); } } /* - * 重置任务进度(若已完成也会返回0%) - * */ - public static void resetCompleteQuest(Player p,String questKey){ + * 重置任务进度(若已完成也会返回0%) + * */ + public static void resetCompleteQuest(Player p, String questKey) { QuestData quest = Main.dataManage.getQuestData(questKey); - if(quest == null){ - System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在."); + if (quest == null) { + System.out.println("[错误 - 任务] 任务: " + questKey + " 配置文件不存在."); return; } PlayerData data = Main.dataManage.getPlayerData(p); - data.setQuestAmount(quest,0); + data.setQuestAmount(quest, 0); } /* - * 删除任务 - * */ - public static void deletePlayerDataQuest(Player p,String questKey){ + * 删除任务 + * */ + public static void deletePlayerDataQuest(Player p, String questKey) { QuestData quest = Main.dataManage.getQuestData(questKey); - if(quest == null){ - System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在."); + if (quest == null) { + System.out.println("[错误 - 任务] 任务: " + questKey + " 配置文件不存在."); return; } PlayerData data = Main.dataManage.getPlayerData(p); @@ -141,9 +141,9 @@ public class QuestAPI { } /* - * 删除所有任务 - * */ - public static void deleteAllPlayerDataQuest(Player p){ + * 删除所有任务 + * */ + public static void deleteAllPlayerDataQuest(Player p) { PlayerData data = Main.dataManage.getPlayerData(p); HashMap hashMap = new HashMap<>(); data.setQuestMap(hashMap); diff --git a/src/main/java/com/yaohun/questsystem/data/PlayerData.java b/src/main/java/com/yaohun/questsystem/data/PlayerData.java index f787763..ff5bd33 100644 --- a/src/main/java/com/yaohun/questsystem/data/PlayerData.java +++ b/src/main/java/com/yaohun/questsystem/data/PlayerData.java @@ -9,24 +9,25 @@ import java.util.Set; public class PlayerData { private String name; // 玩家名字 - private HashMap questAmount; + private HashMap questAmount; - public PlayerData(String name){ + public PlayerData(String name) { this.name = name; this.questAmount = new HashMap<>(); } - public PlayerData(String name,HashMap hashMap){ + public PlayerData(String name, HashMap hashMap) { this.name = name; this.questAmount = hashMap; - for (QuestData quest : hashMap.keySet()){ - System.out.println("[调试 - 输出] 任务名: "+quest.getQuestKey()+" 进度: "+getQuestAmount(quest)+"/"+quest.getNeedAmount()); + for (QuestData quest : hashMap.keySet()) { + System.out.println("[调试 - 输出] 任务名: " + quest.getQuestKey() + " 进度: " + getQuestAmount(quest) + "/" + quest.getNeedAmount()); } } public String getName() { return name; } + public HashMap getQuestAmount() { return questAmount; } @@ -38,11 +39,12 @@ public class PlayerData { public Set getQuests() { return this.questAmount.keySet(); } + // 获取玩家已完成任务数量 - public int getFinishedAmount(){ + public int getFinishedAmount() { int i = 0; - for (QuestData quest : getQuests()){ - if(isFinished(quest)){ + for (QuestData quest : getQuests()) { + if (isFinished(quest)) { i++; } } @@ -50,54 +52,57 @@ public class PlayerData { } /* - * 接受任务 - * */ - public void acceptQuest(QuestData quest){ - if(!isAcceptQuest(quest)){ - this.questAmount.put(quest,0); + * 接受任务 + * */ + public void acceptQuest(QuestData quest) { + if (!isAcceptQuest(quest)) { + this.questAmount.put(quest, 0); } } /* - * 判断任务是否已接受 - * */ - public boolean isAcceptQuest(QuestData quest){ - if(this.questAmount.get(quest) == null){ + * 判断任务是否已接受 + * */ + public boolean isAcceptQuest(QuestData quest) { + if (this.questAmount.get(quest) == null) { return false; } return true; } + /* - * 判断任务是否已完成 - * */ + * 判断任务是否已完成 + * */ public boolean isFinished(QuestData quest) { // 判断任务是否已接受 - if(isAcceptQuest(quest)){ + if (isAcceptQuest(quest)) { // 判断任务是否已提交 - if(this.questAmount.get(quest) <= -1){ + if (this.questAmount.get(quest) <= -1) { return true; } } return false; } + /* - * 提交任务 - * */ + * 提交任务 + * */ public void submitQuest(QuestData quest) { // 提交任务后触发命令 quest.sendFinishCommand(Bukkit.getPlayer(this.name)); // 设置任务提交后的状态为 -1 this.questAmount.put(quest, -1); } + /* - * 判断玩家是否达到要求可提交任务 - * */ + * 判断玩家是否达到要求可提交任务 + * */ public boolean canSubmitQuest(QuestData quest) { // 判断玩家是否接受任务 if (isAcceptQuest(quest)) { // 判断玩家是否已达到任务要求数量 int amount = this.questAmount.get(quest); - if(amount >= quest.getNeedAmount()){ + if (amount >= quest.getNeedAmount()) { return true; } } @@ -105,18 +110,18 @@ public class PlayerData { } /* - * 获取玩家当前任务进度 - * */ - public int getQuestAmount(QuestData quest){ - if(this.questAmount.get(quest) == null){ + * 获取玩家当前任务进度 + * */ + public int getQuestAmount(QuestData quest) { + if (this.questAmount.get(quest) == null) { return 0; } return this.questAmount.get(quest); } /*增加任务进度*/ - public void addQuestAmount(QuestData quest){ - addQuestAmount(quest,1); + public void addQuestAmount(QuestData quest) { + addQuestAmount(quest, 1); } /*增加任务进度*/ @@ -140,6 +145,7 @@ public class PlayerData { } this.questAmount.put(quest, new_amount); } + /*设置任务进度*/ public void setQuestAmount(QuestData quest, int amount) { // 若玩家未接受任务直接返回 @@ -150,8 +156,8 @@ public class PlayerData { } /* - * 获取任务的进度 - * */ + * 获取任务的进度 + * */ public double getProgress(QuestData quest) { if (!isAcceptQuest(quest)) { return 0.0D; @@ -160,11 +166,12 @@ public class PlayerData { double d = amount / quest.getNeedAmount(); if (d < 0.0D) { d = 0.0D; - }else if (d > 1.0D) { + } else if (d > 1.0D) { d = 1.0D; } return d; } + public void reload(List questDataList) { HashMap newData = new HashMap<>(); for (QuestData newQuest : questDataList) { diff --git a/src/main/java/com/yaohun/questsystem/data/QuestData.java b/src/main/java/com/yaohun/questsystem/data/QuestData.java index b2c706d..119406f 100644 --- a/src/main/java/com/yaohun/questsystem/data/QuestData.java +++ b/src/main/java/com/yaohun/questsystem/data/QuestData.java @@ -2,7 +2,6 @@ package com.yaohun.questsystem.data; import com.yaohun.questsystem.util.TimeType; import me.Demon.DemonPlugin.DemonAPI; -import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; @@ -19,22 +18,22 @@ public class QuestData { private List finish; private List description; - public QuestData(String questKey,FileConfiguration yml){ + public QuestData(String questKey, FileConfiguration yml) { this.questKey = questKey; - String cycle = yml.getString(questKey+".timeType","longTerm"); - if(cycle.equalsIgnoreCase("everyday")){ + String cycle = yml.getString(questKey + ".timeType", "longTerm"); + if (cycle.equalsIgnoreCase("everyday")) { timeType = TimeType.EVERYDAY; - }else if(cycle.equalsIgnoreCase("weekly")){ + } else if (cycle.equalsIgnoreCase("weekly")) { timeType = TimeType.WEEKLY; - }else if(cycle.equalsIgnoreCase("longTerm")){ + } else if (cycle.equalsIgnoreCase("longTerm")) { timeType = TimeType.LONGTERM; } - this.name = yml.getString(questKey+".name"); - this.type = yml.getString(questKey+".type"); - this.target = yml.getString(questKey+".content.name"); - this.needAmount = yml.getInt(questKey+".content.amount"); - this.finish = yml.getStringList(questKey+".finish"); - this.description = yml.getStringList(questKey+".description"); + this.name = yml.getString(questKey + ".name"); + this.type = yml.getString(questKey + ".type"); + this.target = yml.getString(questKey + ".content.name"); + this.needAmount = yml.getInt(questKey + ".content.amount"); + this.finish = yml.getStringList(questKey + ".finish"); + this.description = yml.getStringList(questKey + ".description"); } public String getQuestKey() { @@ -65,9 +64,11 @@ public class QuestData { return description; } - public void sendFinishCommand(Player player){ - if(player == null){return;} - DemonAPI.ConsoleOutCommandList(player,getFinish()); + public void sendFinishCommand(Player player) { + if (player == null) { + return; + } + DemonAPI.ConsoleOutCommandList(player, getFinish()); } public List getFinish() { diff --git a/src/main/java/com/yaohun/questsystem/data/database/SqlManager.java b/src/main/java/com/yaohun/questsystem/data/database/SqlManager.java index 4441ab1..5747ac7 100644 --- a/src/main/java/com/yaohun/questsystem/data/database/SqlManager.java +++ b/src/main/java/com/yaohun/questsystem/data/database/SqlManager.java @@ -2,41 +2,34 @@ package com.yaohun.questsystem.data.database; import com.yaohun.questsystem.Main; import com.yaohun.questsystem.util.SqlUtil; -import org.bukkit.Bukkit; import java.sql.ResultSet; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; -import java.util.HashMap; public class SqlManager { - public String table = "quest_playerdata"; // 创建数据库表格格式 public void createTable() { // 数据库结构组成 // 玩家名(VARCHAR) 数据(VARCHAR) 数据保存时间(VARCHAR) - String s = "CREATE TABLE IF NOT EXISTS "+table+"(" + + String s = "CREATE TABLE IF NOT EXISTS " + table + "(" + " name VARCHAR(32) NOT NULL," + " data VARCHAR(8000) NOT NULL," + " savetime VARCHAR(256) NOT NULL" + ") ENGINE = InnoDB"; - getSQL().openConnection(); getSQL().updateSQL(s); - getSQL().closeConnection(); } // 创建玩家数据 - public void createPlayerData(String name){ - getSQL().openConnection(); + public void createPlayerData(String name) { boolean butt = true; - String select = "SELECT * FROM "+table+" WHERE name = '%name%'"; - try { - ResultSet resultSet = getSQL().querySQL(select.replace("%name%", name)); + String select = "SELECT * FROM " + table + " WHERE name = '%name%'"; + try (ResultSet resultSet = getSQL().querySQL(select.replace("%name%", name))) { while (resultSet.next()) { butt = false; } @@ -44,19 +37,18 @@ public class SqlManager { e.printStackTrace(); } // 若数据库没有name的数据则会创建数据 - if(butt) { + if (butt) { String set = "INSERT INTO " + table + " (`name`,`data`, `savetime`) " + "VALUES ('%name%','%data%', '%savetime%')"; set = set.replace("%name%", name); set = set.replace("%data%", ""); set = set.replace("%savetime%", getNowTimeString()); getSQL().updateSQL(set); - System.out.println("[调试 - 输出] "+name+" 数据库数据已创建."); + System.out.println("[调试 - 输出] " + name + " 数据库数据已创建."); } - getSQL().closeConnection(); } - public String getNowTimeString(){ + public String getNowTimeString() { // 获取当前时间戳 Instant now = Instant.now(); // 将时间戳转换为本地日期时间 @@ -68,34 +60,28 @@ public class SqlManager { // 获取玩家任务数据 public String getQuestData(String name) { - String select = "SELECT * FROM "+table+" WHERE name = '%name%'"; - try { - getSQL().openConnection(); - ResultSet resultSet = getSQL().querySQL(select.replace("%name%", name)); + String select = "SELECT * FROM " + table + " WHERE name = '%name%'"; + try (ResultSet resultSet = getSQL().querySQL(select.replace("%name%", name))) { while (resultSet.next()) { String string = resultSet.getString("data"); - if(string.length() >= 3) { + if (string.length() >= 3) { System.out.println("[调试 - 输出] " + name + " 数据: " + string); return string; } } } catch (Exception e) { e.printStackTrace(); - } finally { - getSQL().closeConnection(); } return null; } - public void SavePlayerData(String name,String jsonData){ - String set = "UPDATE `"+table+"` SET " + + public void SavePlayerData(String name, String jsonData) { + String set = "UPDATE `" + table + "` SET " + "`data` = '%data%'," + - "`savetime` = '%savetime%' WHERE `"+table+"`.`name` = '%name%'"; - getSQL().openConnection(); + "`savetime` = '%savetime%' WHERE `" + table + "`.`name` = '%name%'"; getSQL().updateSQL(set.replace("%name%", name). replace("%data%", jsonData). - replace("%savetime%",getNowTimeString())); - getSQL().closeConnection(); + replace("%savetime%", getNowTimeString())); } private SqlUtil getSQL() { diff --git a/src/main/java/com/yaohun/questsystem/listener/JoinEvent.java b/src/main/java/com/yaohun/questsystem/listener/JoinEvent.java index c0e0b4a..ba8c711 100644 --- a/src/main/java/com/yaohun/questsystem/listener/JoinEvent.java +++ b/src/main/java/com/yaohun/questsystem/listener/JoinEvent.java @@ -1,7 +1,6 @@ package com.yaohun.questsystem.listener; import com.yaohun.questsystem.Main; -import com.yaohun.questsystem.data.PlayerData; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -13,24 +12,24 @@ import org.bukkit.scheduler.BukkitRunnable; public class JoinEvent implements Listener { @EventHandler - public void onJoin(PlayerJoinEvent e){ + public void onJoin(PlayerJoinEvent e) { Player p = e.getPlayer(); new BukkitRunnable() { @Override public void run() { Main.dataManage.loadPlayerData(p); } - }.runTaskLater(Main.plugin,20L); + }.runTaskLater(Main.plugin, 20L); } @EventHandler - public void onQuit(PlayerQuitEvent e){ + public void onQuit(PlayerQuitEvent e) { Player p = e.getPlayer(); Main.dataManage.savePlayerData(p); } @EventHandler - public void onKick(PlayerKickEvent e){ + public void onKick(PlayerKickEvent e) { Player p = e.getPlayer(); Main.dataManage.savePlayerData(p); } diff --git a/src/main/java/com/yaohun/questsystem/manage/DataManage.java b/src/main/java/com/yaohun/questsystem/manage/DataManage.java index 252e013..e98b0a1 100644 --- a/src/main/java/com/yaohun/questsystem/manage/DataManage.java +++ b/src/main/java/com/yaohun/questsystem/manage/DataManage.java @@ -7,15 +7,15 @@ import com.google.gson.JsonObject; import com.yaohun.questsystem.Main; import com.yaohun.questsystem.data.PlayerData; import com.yaohun.questsystem.data.QuestData; -import com.yaohun.questsystem.util.TimeType; import me.Demon.DemonPlugin.DemonAPI; -import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import java.io.File; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Set; public class DataManage { @@ -24,19 +24,19 @@ public class DataManage { private ArrayList questDataArrayList = new ArrayList<>(); - public DataManage(){ - File file = new File(Main.plugin.getDataFolder(),"quest.yml"); + public DataManage() { + File file = new File(Main.plugin.getDataFolder(), "quest.yml"); FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(file); HashMap dataHashMap = new HashMap<>(); - for (String questKey : fileConfiguration.getKeys(false)){ - QuestData questData = new QuestData(questKey,fileConfiguration); - dataHashMap.put(questKey,questData); - if(!questDataArrayList.contains(questData)) { + for (String questKey : fileConfiguration.getKeys(false)) { + QuestData questData = new QuestData(questKey, fileConfiguration); + dataHashMap.put(questKey, questData); + if (!questDataArrayList.contains(questData)) { questDataArrayList.add(questData); } } this.questDataMap = dataHashMap; - DemonAPI.sendConsoleMessage("§f[§a!§f] §f任务列表 §8> §6"+questDataMap.size()+" 个"); + DemonAPI.sendConsoleMessage("§f[§a!§f] §f任务列表 §8> §6" + questDataMap.size() + " 个"); } public ArrayList getQuestDataArrayList() { @@ -46,23 +46,24 @@ public class DataManage { public HashMap getQuestDataMap() { return questDataMap; } - public QuestData getQuestData(String questKey){ - if(this.questDataMap.get(questKey) == null){ + + public QuestData getQuestData(String questKey) { + if (this.questDataMap.get(questKey) == null) { return null; } return this.questDataMap.get(questKey); } /* - * 载入玩家数据 - * */ + * 载入玩家数据 + * */ public void loadPlayerData(Player player) { String name = player.getName(); String sqlJson = Main.sqlManager.getQuestData(name); // 创建 Gson 实例 Gson gson = new Gson(); // 解析 JSON 数据 - if (sqlJson != null && sqlJson.length() >= 3){ + if (sqlJson != null && sqlJson.length() >= 3) { JsonObject jsonObject = gson.fromJson(sqlJson, JsonObject.class); JsonArray questsArray = jsonObject.getAsJsonArray("quest"); // 创建 HashMap 来存储结果 @@ -84,39 +85,39 @@ public class DataManage { // 创建初始化数据 PlayerData playerData = new PlayerData(name, questData); this.dataHashMap.put(player, playerData); - }else{ + } else { // 判断玩家数据库中是否已存在数据 Main.sqlManager.createPlayerData(name); // 创建初始化数据 PlayerData playerData = new PlayerData(name); this.dataHashMap.put(player, playerData); } - player.sendMessage("§f[§e!§f]§7玩家 §6"+name+" §7任务数据已载入."); + player.sendMessage("§f[§e!§f]§7玩家 §6" + name + " §7任务数据已载入."); } /* - * 删除玩家数据 - * */ - public void deletePlayerData(Player player){ - if(this.dataHashMap.get(player) == null){ + * 删除玩家数据 + * */ + public void deletePlayerData(Player player) { + if (this.dataHashMap.get(player) == null) { return; } this.dataHashMap.remove(player); } /* - * 保存玩家数据 - * */ - public void savePlayerData(Player player){ - if(this.dataHashMap.get(player) == null){ + * 保存玩家数据 + * */ + public void savePlayerData(Player player) { + if (this.dataHashMap.get(player) == null) { return; } PlayerData data = this.dataHashMap.get(player); JsonObject jsonObject = new JsonObject(); JsonArray questArray = new JsonArray(); // 用于存储多个quest对象 - HashMap questAmount = data.getQuestAmount(); - for (QuestData quest : questAmount.keySet()){ - String questKey = quest.getQuestKey(); + HashMap questAmount = data.getQuestAmount(); + for (QuestData quest : questAmount.keySet()) { + String questKey = quest.getQuestKey(); int amount = data.getQuestAmount(quest); JsonObject questObject = new JsonObject(); questObject.addProperty("key", questKey); @@ -125,20 +126,22 @@ public class DataManage { } jsonObject.add("quest", questArray); String jsonString = jsonObject.toString(); - Main.sqlManager.SavePlayerData(player.getName(),jsonString); + Main.sqlManager.SavePlayerData(player.getName(), jsonString); } - public PlayerData getPlayerData(Player player){ + public PlayerData getPlayerData(Player player) { // 获取玩家是否在数据库中 - if(this.dataHashMap.get(player) == null){ + if (this.dataHashMap.get(player) == null) { String name = player.getName(); - this.dataHashMap.put(player,new PlayerData(name)); + this.dataHashMap.put(player, new PlayerData(name)); } return this.dataHashMap.get(player); } + public HashMap getDataHashMap() { return dataHashMap; } + public Set getQuests() { return this.dataHashMap.keySet(); } diff --git a/src/main/java/com/yaohun/questsystem/util/SqlUtil.java b/src/main/java/com/yaohun/questsystem/util/SqlUtil.java index d07fe83..a2065c9 100644 --- a/src/main/java/com/yaohun/questsystem/util/SqlUtil.java +++ b/src/main/java/com/yaohun/questsystem/util/SqlUtil.java @@ -33,9 +33,9 @@ public class SqlUtil extends MegumiSQL { public void openConnection() { try { String mcVersion = getMinecraftVersion(); - if(mcVersion.contains("1_20")){ + if (mcVersion.contains("1_20")) { Class.forName("com.mysql.cj.jdbc.Driver"); // 1.20.4的mysql路径 - }else{ + } else { Class.forName("com.mysql.jdbc.Driver"); // 1.18.2的mysql路径 } this.connection = DriverManager.getConnection("jdbc:mysql://" + this.hostname + ":" + this.port + "/" + this.database + "?useSSL=false", this.username, this.password); @@ -98,13 +98,11 @@ public class SqlUtil extends MegumiSQL { public boolean updateSQL(String data) { Connection conn; - if (checkConnection()) + if (checkConnection()) { conn = getConnection(); - else + } else return false; - Statement stat = null; - try { - stat = conn.createStatement(); + try (Statement stat = conn.createStatement()) { stat.executeUpdate(data); return true; } catch (SQLException e) { diff --git a/src/main/java/com/yaohun/questsystem/util/TimeType.java b/src/main/java/com/yaohun/questsystem/util/TimeType.java index 3eb10fa..e53a4cf 100644 --- a/src/main/java/com/yaohun/questsystem/util/TimeType.java +++ b/src/main/java/com/yaohun/questsystem/util/TimeType.java @@ -1,5 +1,5 @@ package com.yaohun.questsystem.util; public enum TimeType { - EVERYDAY,WEEKLY,LONGTERM + EVERYDAY, WEEKLY, LONGTERM }