From fafccd1e95132d44efc5848d70105c34b795c258 Mon Sep 17 00:00:00 2001
From: yaohunya <31456652@qq.com>
Date: Wed, 31 Jul 2024 06:12:01 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=89=88?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 6 +-
.../java/com/yaohun/questsystem/Main.java | 157 ++++++++++++++-
.../com/yaohun/questsystem/api/QuestAPI.java | 152 +++++++++++++++
.../yaohun/questsystem/data/PlayerData.java | 179 ++++++++++++++++++
.../yaohun/questsystem/data/QuestData.java | 76 ++++++++
.../questsystem/data/database/MegumiSQL.java | 13 ++
.../questsystem/data/database/SqlManager.java | 104 ++++++++++
.../questsystem/listener/JoinEvent.java | 38 ++++
.../yaohun/questsystem/manage/DataManage.java | 145 ++++++++++++++
.../com/yaohun/questsystem/util/SqlUtil.java | 115 +++++++++++
.../com/yaohun/questsystem/util/TimeType.java | 5 +
src/main/resources/config.yml | 9 +
src/main/resources/plugin.yml | 6 +-
src/main/resources/quest.yml | 15 ++
14 files changed, 1015 insertions(+), 5 deletions(-)
create mode 100644 src/main/java/com/yaohun/questsystem/api/QuestAPI.java
create mode 100644 src/main/java/com/yaohun/questsystem/data/PlayerData.java
create mode 100644 src/main/java/com/yaohun/questsystem/data/QuestData.java
create mode 100644 src/main/java/com/yaohun/questsystem/data/database/MegumiSQL.java
create mode 100644 src/main/java/com/yaohun/questsystem/data/database/SqlManager.java
create mode 100644 src/main/java/com/yaohun/questsystem/listener/JoinEvent.java
create mode 100644 src/main/java/com/yaohun/questsystem/manage/DataManage.java
create mode 100644 src/main/java/com/yaohun/questsystem/util/SqlUtil.java
create mode 100644 src/main/java/com/yaohun/questsystem/util/TimeType.java
create mode 100644 src/main/resources/config.yml
create mode 100644 src/main/resources/quest.yml
diff --git a/pom.xml b/pom.xml
index 2c1f130..693564b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -22,9 +22,9 @@
- org.spigotmc
- spigot
- 1.18.2
+ com.google.code.gson
+ gson
+ 2.8.8
\ 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 ef44872..8d34cd7 100644
--- a/src/main/java/com/yaohun/questsystem/Main.java
+++ b/src/main/java/com/yaohun/questsystem/Main.java
@@ -1,19 +1,174 @@
package com.yaohun.questsystem;
+import com.yaohun.questsystem.api.QuestAPI;
+import com.yaohun.questsystem.data.PlayerData;
+import com.yaohun.questsystem.data.QuestData;
+import com.yaohun.questsystem.data.database.SqlManager;
+import com.yaohun.questsystem.listener.JoinEvent;
+import com.yaohun.questsystem.manage.DataManage;
+import com.yaohun.questsystem.util.SqlUtil;
+import com.yaohun.questsystem.util.TimeType;
+import me.Demon.DemonPlugin.DemonAPI;
+import org.bukkit.Bukkit;
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandSender;
+import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
+import java.util.List;
+
public class Main extends JavaPlugin {
public static Main plugin;
+ public static DataManage dataManage;
+ public static SqlManager sqlManager;
+ public static SqlUtil sqlUtil;
@Override
public void onEnable() {
plugin = this;
+ DemonAPI.sendConsoleMessage("§f[§6!§f] §aQuestSystem (1.0) §f开始加载");
+ LinkMySqlData();
+ saveDefaultConfig();
+ dataManage = new DataManage();
+ for (Player player : Bukkit.getOnlinePlayers()){
+ Main.dataManage.loadPlayerData(player);
+ }
+ getServer().getPluginManager().registerEvents(new JoinEvent(),this);
+ DemonAPI.sendConsoleMessage("§f[§6!§f] §aQuestSystem §f加载完成,祝你使用愉快!");
+ DemonAPI.sendConsoleMessage("§f[§6!§f] §b极光像素工作室出品");
+ }
+ 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);
+ sqlManager.createTable();
}
@Override
public void onDisable() {
- super.onDisable();
+ for (Player player : Bukkit.getOnlinePlayers()){
+ dataManage.savePlayerData(player);
+ }
+ 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任务数据:");
+ 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());
+ }
+ 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());
+ }
+ 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());
+ }
+ }
+ if (Command.equalsIgnoreCase("quest")) {
+ Player player = (Player) sender;
+ if(args.length == 1 && args[0].equalsIgnoreCase("save")) {
+ if (dataManage.getPlayerData(player) != null) {
+ dataManage.savePlayerData(player);
+ }
+ DemonAPI.sendMessage(sender,"数据已保存至数据库.");
+ }
+ /*if(args.length == 2 && args[0].equalsIgnoreCase("js")) {
+ String questKey = args[1];
+ // 判断这个任务是否存在
+ QuestData questData = dataManage.getQuestData(questKey);
+ if(questData == null){
+ DemonAPI.sendMessage(sender,"任务: "+questKey+" 不存在");
+ return true;
+ }
+ PlayerData playerData = dataManage.getPlayerData(player);
+ // 判断玩家是否已接受任务
+ if(playerData.isAcceptQuest(questData)){
+ DemonAPI.sendMessage(sender,"任务: "+questKey+" 已接受");
+ return true;
+ }
+ // 判断玩家是否已完成任务
+ if(playerData.isFinished(questData)){
+ DemonAPI.sendMessage(sender,"任务: "+questKey+" 已完成");
+ return true;
+ }
+ playerData.acceptQuest(questData);
+ DemonAPI.sendMessage(sender,"任务: "+questKey+" 已接受");
+ }
+ if(args.length == 2 && args[0].equalsIgnoreCase("add")) {
+ String questKey = args[1];
+ // 判断这个任务是否存在
+ QuestData questData = dataManage.getQuestData(questKey);
+ if(questData == null){
+ DemonAPI.sendMessage(sender,"任务: "+questKey+" 不存在");
+ return true;
+ }
+ PlayerData playerData = dataManage.getPlayerData(player);
+ // 判断玩家是否已接受任务
+ if(!playerData.isAcceptQuest(questData)){
+ DemonAPI.sendMessage(sender,"任务: "+questKey+" 未接受");
+ return true;
+ }
+ playerData.addQuestAmount(questData);
+ DemonAPI.sendMessage(sender,"任务: "+questKey+" 当前进度: "+playerData.getQuestAmount(questData)+"/"+questData.getNeedAmount()+"["+(playerData.getProgress(questData) * 100)+"%]");
+ }
+ if(args.length == 3 && args[0].equalsIgnoreCase("add")) {
+ String questKey = args[1];
+ // 判断这个任务是否存在
+ QuestData questData = dataManage.getQuestData(questKey);
+ if(questData == null){
+ DemonAPI.sendMessage(sender,"任务: "+questKey+" 不存在");
+ return true;
+ }
+ PlayerData playerData = dataManage.getPlayerData(player);
+ // 判断玩家是否已接受任务
+ if(!playerData.isAcceptQuest(questData)){
+ DemonAPI.sendMessage(sender,"任务: "+questKey+" 未接受");
+ return true;
+ }
+ int amount = Integer.parseInt(args[2]);
+ playerData.addQuestAmount(questData,amount);
+ DemonAPI.sendMessage(sender,"任务: "+questKey+" 当前进度: "+playerData.getQuestAmount(questData)+"/"+questData.getNeedAmount()+"["+(playerData.getProgress(questData) * 100)+"%]");
+ }
+ if(args.length == 2 && args[0].equalsIgnoreCase("tj")) {
+ String questKey = args[1];
+ // 判断这个任务是否存在
+ QuestData questData = dataManage.getQuestData(questKey);
+ if(questData == null){
+ DemonAPI.sendMessage(sender,"任务: "+questKey+" 不存在");
+ return true;
+ }
+ PlayerData playerData = dataManage.getPlayerData(player);
+ // 判断玩家是否已接受任务
+ if(!playerData.isAcceptQuest(questData)){
+ DemonAPI.sendMessage(sender,"任务: "+questKey+" 未接受");
+ return true;
+ }
+ // 判断玩家是否已完成任务
+ if(playerData.isFinished(questData)){
+ DemonAPI.sendMessage(sender,"任务: "+questKey+" 已完成");
+ return true;
+ }
+ if (!playerData.canSubmitQuest(questData)) {
+ DemonAPI.sendMessage(sender,"任务: "+questKey+" 任务尚未完成");
+ return true;
+ }
+ playerData.submitQuest(questData);
+ DemonAPI.sendMessage(sender,"任务: "+questKey+" 已提交");
+ }*/
+ }
+ return true;
}
}
diff --git a/src/main/java/com/yaohun/questsystem/api/QuestAPI.java b/src/main/java/com/yaohun/questsystem/api/QuestAPI.java
new file mode 100644
index 0000000..5d13606
--- /dev/null
+++ b/src/main/java/com/yaohun/questsystem/api/QuestAPI.java
@@ -0,0 +1,152 @@
+package com.yaohun.questsystem.api;
+
+import com.yaohun.questsystem.Main;
+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;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Set;
+
+public class QuestAPI {
+
+ public static PlayerData getPlayerData(Player p){
+ DataManage dataManage = Main.dataManage;
+ PlayerData playerData = dataManage.getPlayerData(p);
+ if(playerData == null){
+ return null;
+ }
+ return playerData;
+ }
+
+ /*
+ * 获取玩家的任务列表
+ * */
+ 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){
+ questDataList.add(questData);
+ }
+ }
+ return questDataList;
+ }
+
+ /*
+ * 获取指定任务信息数据
+ * */
+ public static QuestData getQuestData(String questKey){
+ QuestData quest = Main.dataManage.getQuestData(questKey);
+ if(quest == null){
+ System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在.");
+ return null;
+ }
+ return quest;
+ }
+
+ /*
+ * 增加任务进度
+ * */
+ public static void addQuestProgress(Player p,String questKey,int amount){
+ QuestData quest = Main.dataManage.getQuestData(questKey);
+ if(quest == null){
+ System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在.");
+ return;
+ }
+ PlayerData data = Main.dataManage.getPlayerData(p);
+ data.addQuestAmount(quest,amount);
+ }
+
+ /*
+ * 设置任务进度
+ * */
+ public static void setQuestProgress(Player p,String questKey,int amount){
+ QuestData quest = Main.dataManage.getQuestData(questKey);
+ if(quest == null){
+ System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在.");
+ return;
+ }
+ PlayerData data = Main.dataManage.getPlayerData(p);
+ data.setQuestAmount(quest,amount);
+ }
+
+ /*
+ * 强制完成任务进度
+ * */
+ public static void forceCompleteQuest(Player p,String questKey){
+ QuestData quest = Main.dataManage.getQuestData(questKey);
+ if(quest == null){
+ System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在.");
+ return;
+ }
+ PlayerData data = Main.dataManage.getPlayerData(p);
+ data.setQuestAmount(quest,-1);
+ }
+ /*
+ * 强制完成任务进度并触发奖励
+ * */
+ public static void forceCompleteQuestFinish(Player p,String questKey,boolean finish){
+ QuestData quest = Main.dataManage.getQuestData(questKey);
+ if(quest == null){
+ System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在.");
+ return;
+ }
+ PlayerData data = Main.dataManage.getPlayerData(p);
+ data.setQuestAmount(quest,-1);
+ if(finish){
+ quest.sendFinishCommand(p);
+ }
+ }
+
+ /*
+ * 重置任务进度(若已完成也会返回0%)
+ * */
+ public static void resetCompleteQuest(Player p,String questKey){
+ QuestData quest = Main.dataManage.getQuestData(questKey);
+ if(quest == null){
+ System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在.");
+ return;
+ }
+ PlayerData data = Main.dataManage.getPlayerData(p);
+ data.setQuestAmount(quest,0);
+ }
+
+ /*
+ * 删除任务
+ * */
+ public static void deletePlayerDataQuest(Player p,String questKey){
+ QuestData quest = Main.dataManage.getQuestData(questKey);
+ if(quest == null){
+ System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在.");
+ return;
+ }
+ PlayerData data = Main.dataManage.getPlayerData(p);
+ HashMap hashMap = data.getQuestAmount();
+ hashMap.remove(quest);
+ data.setQuestMap(hashMap);
+ }
+
+ /*
+ * 删除所有任务
+ * */
+ 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
new file mode 100644
index 0000000..f787763
--- /dev/null
+++ b/src/main/java/com/yaohun/questsystem/data/PlayerData.java
@@ -0,0 +1,179 @@
+package com.yaohun.questsystem.data;
+
+import org.bukkit.Bukkit;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Set;
+
+public class PlayerData {
+
+ private String name; // 玩家名字
+ private HashMap questAmount;
+
+ public PlayerData(String name){
+ this.name = name;
+ this.questAmount = new 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());
+ }
+ }
+
+ public String getName() {
+ return name;
+ }
+ public HashMap getQuestAmount() {
+ return questAmount;
+ }
+
+ public void setQuestMap(HashMap questMap) {
+ this.questAmount = questMap;
+ }
+
+ public Set getQuests() {
+ return this.questAmount.keySet();
+ }
+ // 获取玩家已完成任务数量
+ public int getFinishedAmount(){
+ int i = 0;
+ for (QuestData quest : getQuests()){
+ if(isFinished(quest)){
+ i++;
+ }
+ }
+ return i;
+ }
+
+ /*
+ * 接受任务
+ * */
+ public void acceptQuest(QuestData quest){
+ if(!isAcceptQuest(quest)){
+ this.questAmount.put(quest,0);
+ }
+ }
+
+ /*
+ * 判断任务是否已接受
+ * */
+ public boolean isAcceptQuest(QuestData quest){
+ if(this.questAmount.get(quest) == null){
+ return false;
+ }
+ return true;
+ }
+ /*
+ * 判断任务是否已完成
+ * */
+ public boolean isFinished(QuestData quest) {
+ // 判断任务是否已接受
+ if(isAcceptQuest(quest)){
+ // 判断任务是否已提交
+ 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()){
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /*
+ * 获取玩家当前任务进度
+ * */
+ 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, int addAmount) {
+ // 若玩家未接受任务直接返回
+ if (!isAcceptQuest(quest)) {
+ return;
+ }
+ // 当玩家当前任务进度为0时直接返回
+ int amount = this.questAmount.getOrDefault(quest, 0);
+ if (amount <= -1) {
+ return;
+ }
+ int new_amount = amount + addAmount;
+ int quest_amount = quest.getNeedAmount();
+ if (new_amount > quest_amount) {
+ int need_Amount = quest_amount - amount; // 得出 玩家还需要多少积分完成任务
+ amount = amount + need_Amount;
+ this.questAmount.put(quest, amount);
+ return;
+ }
+ this.questAmount.put(quest, new_amount);
+ }
+ /*设置任务进度*/
+ public void setQuestAmount(QuestData quest, int amount) {
+ // 若玩家未接受任务直接返回
+ if (!isAcceptQuest(quest)) {
+ return;
+ }
+ this.questAmount.put(quest, amount);
+ }
+
+ /*
+ * 获取任务的进度
+ * */
+ public double getProgress(QuestData quest) {
+ if (!isAcceptQuest(quest)) {
+ return 0.0D;
+ }
+ double amount = this.questAmount.get(quest);
+ double d = amount / quest.getNeedAmount();
+ if (d < 0.0D) {
+ d = 0.0D;
+ }else if (d > 1.0D) {
+ d = 1.0D;
+ }
+ return d;
+ }
+ public void reload(List questDataList) {
+ HashMap newData = new HashMap<>();
+ for (QuestData newQuest : questDataList) {
+ for (QuestData oldQuest : this.questAmount.keySet()) {
+ if (newQuest.getQuestKey().equalsIgnoreCase(oldQuest.getQuestKey())) {
+ newData.put(newQuest, this.questAmount.get(oldQuest));
+ }
+ }
+ }
+ this.questAmount = newData;
+ }
+}
diff --git a/src/main/java/com/yaohun/questsystem/data/QuestData.java b/src/main/java/com/yaohun/questsystem/data/QuestData.java
new file mode 100644
index 0000000..b2c706d
--- /dev/null
+++ b/src/main/java/com/yaohun/questsystem/data/QuestData.java
@@ -0,0 +1,76 @@
+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;
+
+import java.util.List;
+
+public class QuestData {
+
+ private String questKey; // 任务名
+ private String name; // 任务名
+ private TimeType timeType; // 任务时间
+ private String type; // 任务类型
+ private String target;
+ private int needAmount;
+ private List finish;
+ private List description;
+
+ public QuestData(String questKey,FileConfiguration yml){
+ this.questKey = questKey;
+ String cycle = yml.getString(questKey+".timeType","longTerm");
+ if(cycle.equalsIgnoreCase("everyday")){
+ timeType = TimeType.EVERYDAY;
+ }else if(cycle.equalsIgnoreCase("weekly")){
+ timeType = TimeType.WEEKLY;
+ }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");
+ }
+
+ public String getQuestKey() {
+ return questKey;
+ }
+
+ public TimeType getTimeType() {
+ return timeType;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public String getTarget() {
+ return target;
+ }
+
+ public int getNeedAmount() {
+ return needAmount;
+ }
+
+ public List getDescription() {
+ return description;
+ }
+
+ public void sendFinishCommand(Player player){
+ if(player == null){return;}
+ DemonAPI.ConsoleOutCommandList(player,getFinish());
+ }
+
+ public List getFinish() {
+ return finish;
+ }
+}
diff --git a/src/main/java/com/yaohun/questsystem/data/database/MegumiSQL.java b/src/main/java/com/yaohun/questsystem/data/database/MegumiSQL.java
new file mode 100644
index 0000000..d4a9d1d
--- /dev/null
+++ b/src/main/java/com/yaohun/questsystem/data/database/MegumiSQL.java
@@ -0,0 +1,13 @@
+package com.yaohun.questsystem.data.database;
+
+import java.sql.Connection;
+
+public abstract class MegumiSQL {
+ public abstract void openConnection();
+
+ public abstract boolean checkConnection();
+
+ public abstract Connection getConnection();
+
+ public abstract void closeConnection();
+}
diff --git a/src/main/java/com/yaohun/questsystem/data/database/SqlManager.java b/src/main/java/com/yaohun/questsystem/data/database/SqlManager.java
new file mode 100644
index 0000000..4441ab1
--- /dev/null
+++ b/src/main/java/com/yaohun/questsystem/data/database/SqlManager.java
@@ -0,0 +1,104 @@
+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+"(" +
+ " 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();
+ boolean butt = true;
+ String select = "SELECT * FROM "+table+" WHERE name = '%name%'";
+ try {
+ ResultSet resultSet = getSQL().querySQL(select.replace("%name%", name));
+ while (resultSet.next()) {
+ butt = false;
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ // 若数据库没有name的数据则会创建数据
+ 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+" 数据库数据已创建.");
+ }
+ getSQL().closeConnection();
+ }
+
+ public String getNowTimeString(){
+ // 获取当前时间戳
+ Instant now = Instant.now();
+ // 将时间戳转换为本地日期时间
+ LocalDateTime dateTime = LocalDateTime.ofInstant(now, ZoneId.systemDefault());
+ // 定义日期时间格式
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm");
+ return dateTime.format(formatter);
+ }
+
+ // 获取玩家任务数据
+ public String getQuestData(String name) {
+ String select = "SELECT * FROM "+table+" WHERE name = '%name%'";
+ try {
+ getSQL().openConnection();
+ ResultSet resultSet = getSQL().querySQL(select.replace("%name%", name));
+ while (resultSet.next()) {
+ String string = resultSet.getString("data");
+ 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 " +
+ "`data` = '%data%'," +
+ "`savetime` = '%savetime%' WHERE `"+table+"`.`name` = '%name%'";
+ getSQL().openConnection();
+ getSQL().updateSQL(set.replace("%name%", name).
+ replace("%data%", jsonData).
+ replace("%savetime%",getNowTimeString()));
+ getSQL().closeConnection();
+ }
+
+ private SqlUtil getSQL() {
+ return Main.sqlUtil;
+ }
+}
diff --git a/src/main/java/com/yaohun/questsystem/listener/JoinEvent.java b/src/main/java/com/yaohun/questsystem/listener/JoinEvent.java
new file mode 100644
index 0000000..c0e0b4a
--- /dev/null
+++ b/src/main/java/com/yaohun/questsystem/listener/JoinEvent.java
@@ -0,0 +1,38 @@
+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;
+import org.bukkit.event.player.PlayerJoinEvent;
+import org.bukkit.event.player.PlayerKickEvent;
+import org.bukkit.event.player.PlayerQuitEvent;
+import org.bukkit.scheduler.BukkitRunnable;
+
+public class JoinEvent implements Listener {
+
+ @EventHandler
+ public void onJoin(PlayerJoinEvent e){
+ Player p = e.getPlayer();
+ new BukkitRunnable() {
+ @Override
+ public void run() {
+ Main.dataManage.loadPlayerData(p);
+ }
+ }.runTaskLater(Main.plugin,20L);
+ }
+
+ @EventHandler
+ public void onQuit(PlayerQuitEvent e){
+ Player p = e.getPlayer();
+ Main.dataManage.savePlayerData(p);
+ }
+
+ @EventHandler
+ 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
new file mode 100644
index 0000000..252e013
--- /dev/null
+++ b/src/main/java/com/yaohun/questsystem/manage/DataManage.java
@@ -0,0 +1,145 @@
+package com.yaohun.questsystem.manage;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+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.*;
+
+public class DataManage {
+
+ private HashMap dataHashMap = new HashMap<>();
+ private HashMap questDataMap;
+
+ private ArrayList questDataArrayList = new ArrayList<>();
+
+ 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)) {
+ questDataArrayList.add(questData);
+ }
+ }
+ this.questDataMap = dataHashMap;
+ DemonAPI.sendConsoleMessage("§f[§a!§f] §f任务列表 §8> §6"+questDataMap.size()+" 个");
+ }
+
+ public ArrayList getQuestDataArrayList() {
+ return questDataArrayList;
+ }
+
+ public HashMap getQuestDataMap() {
+ return questDataMap;
+ }
+ 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){
+ JsonObject jsonObject = gson.fromJson(sqlJson, JsonObject.class);
+ JsonArray questsArray = jsonObject.getAsJsonArray("quest");
+ // 创建 HashMap 来存储结果
+ HashMap questData = new HashMap<>();
+ // 遍历 JSON 数组并将数据存储到 HashMap 中
+ for (JsonElement element : questsArray) {
+ JsonObject questObject = element.getAsJsonObject();
+ String key = questObject.get("key").getAsString();
+ QuestData quest = getQuestData(key);
+ if (quest != null) {
+ int amount = questObject.get("amount").getAsInt();
+ questData.put(quest, amount);
+ } else {
+ System.out.println("[错误 - 任务] " + key + " 不存在无法载入.");
+ }
+ }
+ // 判断玩家数据库中是否已存在数据
+ Main.sqlManager.createPlayerData(name);
+ // 创建初始化数据
+ PlayerData playerData = new PlayerData(name, questData);
+ this.dataHashMap.put(player, playerData);
+ }else{
+ // 判断玩家数据库中是否已存在数据
+ Main.sqlManager.createPlayerData(name);
+ // 创建初始化数据
+ PlayerData playerData = new PlayerData(name);
+ this.dataHashMap.put(player, playerData);
+ }
+ player.sendMessage("§f[§e!§f]§7玩家 §6"+name+" §7任务数据已载入.");
+ }
+
+ /*
+ * 删除玩家数据
+ * */
+ 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){
+ 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();
+ int amount = data.getQuestAmount(quest);
+ JsonObject questObject = new JsonObject();
+ questObject.addProperty("key", questKey);
+ questObject.addProperty("amount", amount);
+ questArray.add(questObject);
+ }
+ jsonObject.add("quest", questArray);
+ String jsonString = jsonObject.toString();
+ Main.sqlManager.SavePlayerData(player.getName(),jsonString);
+ }
+
+ public PlayerData getPlayerData(Player player){
+ // 获取玩家是否在数据库中
+ if(this.dataHashMap.get(player) == null){
+ String name = player.getName();
+ 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
new file mode 100644
index 0000000..d07fe83
--- /dev/null
+++ b/src/main/java/com/yaohun/questsystem/util/SqlUtil.java
@@ -0,0 +1,115 @@
+package com.yaohun.questsystem.util;
+
+
+import com.yaohun.questsystem.data.database.MegumiSQL;
+import org.bukkit.Bukkit;
+
+import java.sql.*;
+
+public class SqlUtil extends MegumiSQL {
+ private final String hostname;
+ private final String port;
+ private final String database;
+ private final String username;
+ private final String password;
+ private Connection connection;
+
+ public SqlUtil(String hostname, String port, String database, String username, String password) {
+ this.hostname = hostname;
+ this.port = port;
+ this.database = database;
+ this.username = username;
+ this.password = password;
+ this.connection = null;
+ }
+
+ public static String getMinecraftVersion() {
+ String packageName = Bukkit.getServer().getClass().getPackage().getName();
+ String version = packageName.substring(packageName.lastIndexOf('.') + 1);
+ return version;
+ }
+
+ @Override
+ public void openConnection() {
+ try {
+ String mcVersion = getMinecraftVersion();
+ if(mcVersion.contains("1_20")){
+ Class.forName("com.mysql.cj.jdbc.Driver"); // 1.20.4的mysql路径
+ }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);
+ } catch (SQLException e) {
+ System.out.println("[日志 - 错误] 连接数据库失败!");
+ this.connection = null;
+ } catch (ClassNotFoundException e) {
+ System.out.println("[日志 - 错误] 未找到JDBC驱动程序");
+ this.connection = null;
+ }
+ }
+
+ @Override
+ public boolean checkConnection() {
+ return this.connection != null;
+ }
+
+ @Override
+ public Connection getConnection() {
+ return this.connection;
+ }
+
+ @Override
+ public void closeConnection() {
+ if (this.connection != null) {
+ try {
+ this.connection.close();
+ this.connection = null;
+ } catch (SQLException e) {
+ System.out.println("[日志 - 错误] 关闭数据库连接失败!");
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public ResultSet querySQL(String query) {
+ Connection conn;
+ if (checkConnection())
+ conn = getConnection();
+ else
+ return null;
+ Statement stat = null;
+ try {
+ stat = conn.createStatement();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ ResultSet result = null;
+ try {
+ if (stat != null)
+ result = stat.executeQuery(query);
+ else
+ return null;
+ } catch (SQLException e) {
+ e.printStackTrace();
+ return null;
+ }
+ return result;
+ }
+
+ public boolean updateSQL(String data) {
+ Connection conn;
+ if (checkConnection())
+ conn = getConnection();
+ else
+ return false;
+ Statement stat = null;
+ try {
+ stat = conn.createStatement();
+ stat.executeUpdate(data);
+ return true;
+ } catch (SQLException e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+}
diff --git a/src/main/java/com/yaohun/questsystem/util/TimeType.java b/src/main/java/com/yaohun/questsystem/util/TimeType.java
new file mode 100644
index 0000000..3eb10fa
--- /dev/null
+++ b/src/main/java/com/yaohun/questsystem/util/TimeType.java
@@ -0,0 +1,5 @@
+package com.yaohun.questsystem.util;
+
+public enum TimeType {
+ EVERYDAY,WEEKLY,LONGTERM
+}
diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml
new file mode 100644
index 0000000..a558cfb
--- /dev/null
+++ b/src/main/resources/config.yml
@@ -0,0 +1,9 @@
+SqlData:
+ Yaohun:
+ everyday:
+ # -1 = 已完成
+ # 0 = 进行中
+ # 100 = 已满足要求
+ questKey: -1
+ weekly: 1111
+ longTerm: 1111
\ No newline at end of file
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index 182e5b7..2adb32a 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -1,5 +1,9 @@
name: QuestSystem
main: com.yaohun.questsystem.Main
version: 1.0
+api-version: 1.18
+softdepend:
+ - DemonAPI
commands:
- quest:
\ No newline at end of file
+ quest:
+ questinfo:
\ No newline at end of file
diff --git a/src/main/resources/quest.yml b/src/main/resources/quest.yml
new file mode 100644
index 0000000..1ffd2b1
--- /dev/null
+++ b/src/main/resources/quest.yml
@@ -0,0 +1,15 @@
+# 任务序号
+breakBlock:
+ name: "任务·破坏方块100次"
+ type: break
+ content:
+ Name: "air"
+ amount: 100
+ description:
+ - "XXXXXXXXXXXXXXXXXX"
+ - "XXXXXXXXXXXXXXXXXX"
+ - "XXXXXXXXXXXXXXXXXX"
+ - "XXXXXXXXXXXXXXXXXX"
+ - "XXXXXXXXXXXXXXXXXX"
+ finish:
+ - "console:points give %player% 1"
\ No newline at end of file