This commit is contained in:
YuTian 2024-08-06 14:01:55 +08:00
parent 8803564676
commit f4b37b12b5
10 changed files with 210 additions and 191 deletions

23
pom.xml
View File

@ -14,17 +14,40 @@
</properties> </properties>
<repositories> <repositories>
<repository>
<id>papermc-repo</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<repository> <repository>
<id>public</id> <id>public</id>
<url>https://repo.aurora-pixels.com/repository/public/</url> <url>https://repo.aurora-pixels.com/repository/public/</url>
</repository> </repository>
<repository>
<id>public-rpg</id>
<url>https://repo.aurora-pixels.com/repository/public-rpg/</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.18.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>2.8.8</version> <version>2.8.8</version>
</dependency> </dependency>
<dependency>
<groupId>me.Demon.DemonPlugin</groupId>
<artifactId>DemonAPI</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -31,59 +31,61 @@ public class Main extends JavaPlugin {
LinkMySqlData(); LinkMySqlData();
saveDefaultConfig(); saveDefaultConfig();
dataManage = new DataManage(); dataManage = new DataManage();
for (Player player : Bukkit.getOnlinePlayers()){ for (Player player : Bukkit.getOnlinePlayers()) {
Main.dataManage.loadPlayerData(player); 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] §aQuestSystem §f加载完成,祝你使用愉快!");
DemonAPI.sendConsoleMessage("§f[§6!§f] §b极光像素工作室出品"); DemonAPI.sendConsoleMessage("§f[§6!§f] §b极光像素工作室出品");
} }
public void LinkMySqlData(){ public void LinkMySqlData() {
String SQL_Host = "gz-cdb-r9koldtt.sql.tencentcdb.com"; String SQL_Host = "gz-cdb-r9koldtt.sql.tencentcdb.com";
String SQL_Port = "29320"; String SQL_Port = "29320";
String SQL_Users = "root"; String SQL_Users = "root";
String SQL_Password = "Pixel@123456"; String SQL_Password = "Pixel@123456";
String SQL_Database = "quest"; String SQL_Database = "quest";
sqlManager = new SqlManager(); 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(); sqlManager.createTable();
} }
@Override @Override
public void onDisable() { public void onDisable() {
for (Player player : Bukkit.getOnlinePlayers()){ for (Player player : Bukkit.getOnlinePlayers()) {
dataManage.savePlayerData(player); dataManage.savePlayerData(player);
} }
sqlUtil.closeConnection();
DemonAPI.sendConsoleMessage("§f[§c!§f] §aQuestSystem §f卸载完成,欢迎下次使用!"); DemonAPI.sendConsoleMessage("§f[§c!§f] §aQuestSystem §f卸载完成,欢迎下次使用!");
} }
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String Command, String[] args) { public boolean onCommand(CommandSender sender, Command command, String Command, String[] args) {
if (Command.equalsIgnoreCase("questinfo") && sender instanceof Player player) { 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任务数据:"); sender.sendMessage("§f[§e!§f]§7任务数据:");
PlayerData data = dataManage.getPlayerData(player); PlayerData data = dataManage.getPlayerData(player);
List<QuestData> everydayList = QuestAPI.getPlayerQuestList(player, TimeType.EVERYDAY); List<QuestData> everydayList = QuestAPI.getPlayerQuestList(player, TimeType.EVERYDAY);
for (QuestData quest : everydayList){ for (QuestData quest : everydayList) {
sender.sendMessage(" [每日] §b"+quest.getQuestKey()+" §f进度: §c"+data.getQuestAmount(quest)+"§f/§a"+quest.getNeedAmount()); sender.sendMessage(" [每日] §b" + quest.getQuestKey() + " §f进度: §c" + data.getQuestAmount(quest) + "§f/§a" + quest.getNeedAmount());
} }
List<QuestData> weeklyList = QuestAPI.getPlayerQuestList(player, TimeType.WEEKLY); List<QuestData> weeklyList = QuestAPI.getPlayerQuestList(player, TimeType.WEEKLY);
for (QuestData quest : weeklyList){ for (QuestData quest : weeklyList) {
sender.sendMessage(" [每周] §b"+quest.getQuestKey()+" §f进度: §c"+data.getQuestAmount(quest)+"§f/§a"+quest.getNeedAmount()); sender.sendMessage(" [每周] §b" + quest.getQuestKey() + " §f进度: §c" + data.getQuestAmount(quest) + "§f/§a" + quest.getNeedAmount());
} }
List<QuestData> longTermList = QuestAPI.getPlayerQuestList(player, TimeType.LONGTERM); List<QuestData> longTermList = QuestAPI.getPlayerQuestList(player, TimeType.LONGTERM);
for (QuestData quest : longTermList){ for (QuestData quest : longTermList) {
sender.sendMessage(" [长期] §b"+quest.getQuestKey()+" §f进度: §c"+data.getQuestAmount(quest)+"§f/§a"+quest.getNeedAmount()); sender.sendMessage(" [长期] §b" + quest.getQuestKey() + " §f进度: §c" + data.getQuestAmount(quest) + "§f/§a" + quest.getNeedAmount());
} }
} }
if (Command.equalsIgnoreCase("quest")) { if (Command.equalsIgnoreCase("quest")) {
Player player = (Player) sender; 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) { if (dataManage.getPlayerData(player) != null) {
dataManage.savePlayerData(player); dataManage.savePlayerData(player);
} }
DemonAPI.sendMessage(sender,"数据已保存至数据库."); DemonAPI.sendMessage(sender, "数据已保存至数据库.");
} }
/*if(args.length == 2 && args[0].equalsIgnoreCase("js")) { /*if(args.length == 2 && args[0].equalsIgnoreCase("js")) {
String questKey = args[1]; String questKey = args[1];

View File

@ -5,7 +5,6 @@ import com.yaohun.questsystem.data.PlayerData;
import com.yaohun.questsystem.data.QuestData; import com.yaohun.questsystem.data.QuestData;
import com.yaohun.questsystem.manage.DataManage; import com.yaohun.questsystem.manage.DataManage;
import com.yaohun.questsystem.util.TimeType; import com.yaohun.questsystem.util.TimeType;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.ArrayList; import java.util.ArrayList;
@ -15,10 +14,10 @@ import java.util.Set;
public class QuestAPI { public class QuestAPI {
public static PlayerData getPlayerData(Player p){ public static PlayerData getPlayerData(Player p) {
DataManage dataManage = Main.dataManage; DataManage dataManage = Main.dataManage;
PlayerData playerData = dataManage.getPlayerData(p); PlayerData playerData = dataManage.getPlayerData(p);
if(playerData == null){ if (playerData == null) {
return null; return null;
} }
return playerData; return playerData;
@ -27,7 +26,7 @@ public class QuestAPI {
/* /*
* 获取玩家的任务列表 * 获取玩家的任务列表
* */ * */
public static Set<QuestData> getPlayerQuestList(Player p){ public static Set<QuestData> getPlayerQuestList(Player p) {
PlayerData data = Main.dataManage.getPlayerData(p); PlayerData data = Main.dataManage.getPlayerData(p);
return data.getQuests(); return data.getQuests();
} }
@ -38,8 +37,8 @@ public class QuestAPI {
public static List<QuestData> getPlayerQuestList(Player p, TimeType timeType) { public static List<QuestData> getPlayerQuestList(Player p, TimeType timeType) {
List<QuestData> questDataList = new ArrayList<>(); List<QuestData> questDataList = new ArrayList<>();
PlayerData data = Main.dataManage.getPlayerData(p); PlayerData data = Main.dataManage.getPlayerData(p);
for (QuestData questData : data.getQuests()){ for (QuestData questData : data.getQuests()) {
if(questData.getTimeType() == timeType){ if (questData.getTimeType() == timeType) {
questDataList.add(questData); questDataList.add(questData);
} }
} }
@ -49,10 +48,10 @@ public class QuestAPI {
/* /*
* 获取指定任务信息数据 * 获取指定任务信息数据
* */ * */
public static QuestData getQuestData(String questKey){ public static QuestData getQuestData(String questKey) {
QuestData quest = Main.dataManage.getQuestData(questKey); QuestData quest = Main.dataManage.getQuestData(questKey);
if(quest == null){ if (quest == null) {
System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在."); System.out.println("[错误 - 任务] 任务: " + questKey + " 配置文件不存在.");
return null; return null;
} }
return quest; return quest;
@ -61,53 +60,54 @@ public class QuestAPI {
/* /*
* 增加任务进度 * 增加任务进度
* */ * */
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); QuestData quest = Main.dataManage.getQuestData(questKey);
if(quest == null){ if (quest == null) {
System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在."); System.out.println("[错误 - 任务] 任务: " + questKey + " 配置文件不存在.");
return; return;
} }
PlayerData data = Main.dataManage.getPlayerData(p); 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); QuestData quest = Main.dataManage.getQuestData(questKey);
if(quest == null){ if (quest == null) {
System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在."); System.out.println("[错误 - 任务] 任务: " + questKey + " 配置文件不存在.");
return; return;
} }
PlayerData data = Main.dataManage.getPlayerData(p); 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); QuestData quest = Main.dataManage.getQuestData(questKey);
if(quest == null){ if (quest == null) {
System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在."); System.out.println("[错误 - 任务] 任务: " + questKey + " 配置文件不存在.");
return; return;
} }
PlayerData data = Main.dataManage.getPlayerData(p); 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); QuestData quest = Main.dataManage.getQuestData(questKey);
if(quest == null){ if (quest == null) {
System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在."); System.out.println("[错误 - 任务] 任务: " + questKey + " 配置文件不存在.");
return; return;
} }
PlayerData data = Main.dataManage.getPlayerData(p); PlayerData data = Main.dataManage.getPlayerData(p);
data.setQuestAmount(quest,-1); data.setQuestAmount(quest, -1);
if(finish){ if (finish) {
quest.sendFinishCommand(p); quest.sendFinishCommand(p);
} }
} }
@ -115,23 +115,23 @@ public class QuestAPI {
/* /*
* 重置任务进度(若已完成也会返回0%) * 重置任务进度(若已完成也会返回0%)
* */ * */
public static void resetCompleteQuest(Player p,String questKey){ public static void resetCompleteQuest(Player p, String questKey) {
QuestData quest = Main.dataManage.getQuestData(questKey); QuestData quest = Main.dataManage.getQuestData(questKey);
if(quest == null){ if (quest == null) {
System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在."); System.out.println("[错误 - 任务] 任务: " + questKey + " 配置文件不存在.");
return; return;
} }
PlayerData data = Main.dataManage.getPlayerData(p); 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); QuestData quest = Main.dataManage.getQuestData(questKey);
if(quest == null){ if (quest == null) {
System.out.println("[错误 - 任务] 任务: "+questKey+" 配置文件不存在."); System.out.println("[错误 - 任务] 任务: " + questKey + " 配置文件不存在.");
return; return;
} }
PlayerData data = Main.dataManage.getPlayerData(p); PlayerData data = Main.dataManage.getPlayerData(p);
@ -143,7 +143,7 @@ public class QuestAPI {
/* /*
* 删除所有任务 * 删除所有任务
* */ * */
public static void deleteAllPlayerDataQuest(Player p){ public static void deleteAllPlayerDataQuest(Player p) {
PlayerData data = Main.dataManage.getPlayerData(p); PlayerData data = Main.dataManage.getPlayerData(p);
HashMap<QuestData, Integer> hashMap = new HashMap<>(); HashMap<QuestData, Integer> hashMap = new HashMap<>();
data.setQuestMap(hashMap); data.setQuestMap(hashMap);

View File

@ -9,24 +9,25 @@ import java.util.Set;
public class PlayerData { public class PlayerData {
private String name; // 玩家名字 private String name; // 玩家名字
private HashMap<QuestData,Integer> questAmount; private HashMap<QuestData, Integer> questAmount;
public PlayerData(String name){ public PlayerData(String name) {
this.name = name; this.name = name;
this.questAmount = new HashMap<>(); this.questAmount = new HashMap<>();
} }
public PlayerData(String name,HashMap<QuestData,Integer> hashMap){ public PlayerData(String name, HashMap<QuestData, Integer> hashMap) {
this.name = name; this.name = name;
this.questAmount = hashMap; this.questAmount = hashMap;
for (QuestData quest : hashMap.keySet()){ for (QuestData quest : hashMap.keySet()) {
System.out.println("[调试 - 输出] 任务名: "+quest.getQuestKey()+" 进度: "+getQuestAmount(quest)+"/"+quest.getNeedAmount()); System.out.println("[调试 - 输出] 任务名: " + quest.getQuestKey() + " 进度: " + getQuestAmount(quest) + "/" + quest.getNeedAmount());
} }
} }
public String getName() { public String getName() {
return name; return name;
} }
public HashMap<QuestData, Integer> getQuestAmount() { public HashMap<QuestData, Integer> getQuestAmount() {
return questAmount; return questAmount;
} }
@ -38,11 +39,12 @@ public class PlayerData {
public Set<QuestData> getQuests() { public Set<QuestData> getQuests() {
return this.questAmount.keySet(); return this.questAmount.keySet();
} }
// 获取玩家已完成任务数量 // 获取玩家已完成任务数量
public int getFinishedAmount(){ public int getFinishedAmount() {
int i = 0; int i = 0;
for (QuestData quest : getQuests()){ for (QuestData quest : getQuests()) {
if(isFinished(quest)){ if (isFinished(quest)) {
i++; i++;
} }
} }
@ -52,34 +54,36 @@ public class PlayerData {
/* /*
* 接受任务 * 接受任务
* */ * */
public void acceptQuest(QuestData quest){ public void acceptQuest(QuestData quest) {
if(!isAcceptQuest(quest)){ if (!isAcceptQuest(quest)) {
this.questAmount.put(quest,0); this.questAmount.put(quest, 0);
} }
} }
/* /*
* 判断任务是否已接受 * 判断任务是否已接受
* */ * */
public boolean isAcceptQuest(QuestData quest){ public boolean isAcceptQuest(QuestData quest) {
if(this.questAmount.get(quest) == null){ if (this.questAmount.get(quest) == null) {
return false; return false;
} }
return true; return true;
} }
/* /*
* 判断任务是否已完成 * 判断任务是否已完成
* */ * */
public boolean isFinished(QuestData quest) { 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 true;
} }
} }
return false; return false;
} }
/* /*
* 提交任务 * 提交任务
* */ * */
@ -89,6 +93,7 @@ public class PlayerData {
// 设置任务提交后的状态为 -1 // 设置任务提交后的状态为 -1
this.questAmount.put(quest, -1); this.questAmount.put(quest, -1);
} }
/* /*
* 判断玩家是否达到要求可提交任务 * 判断玩家是否达到要求可提交任务
* */ * */
@ -97,7 +102,7 @@ public class PlayerData {
if (isAcceptQuest(quest)) { if (isAcceptQuest(quest)) {
// 判断玩家是否已达到任务要求数量 // 判断玩家是否已达到任务要求数量
int amount = this.questAmount.get(quest); int amount = this.questAmount.get(quest);
if(amount >= quest.getNeedAmount()){ if (amount >= quest.getNeedAmount()) {
return true; return true;
} }
} }
@ -107,16 +112,16 @@ public class PlayerData {
/* /*
* 获取玩家当前任务进度 * 获取玩家当前任务进度
* */ * */
public int getQuestAmount(QuestData quest){ public int getQuestAmount(QuestData quest) {
if(this.questAmount.get(quest) == null){ if (this.questAmount.get(quest) == null) {
return 0; return 0;
} }
return this.questAmount.get(quest); return this.questAmount.get(quest);
} }
/*增加任务进度*/ /*增加任务进度*/
public void addQuestAmount(QuestData quest){ public void addQuestAmount(QuestData quest) {
addQuestAmount(quest,1); addQuestAmount(quest, 1);
} }
/*增加任务进度*/ /*增加任务进度*/
@ -140,6 +145,7 @@ public class PlayerData {
} }
this.questAmount.put(quest, new_amount); this.questAmount.put(quest, new_amount);
} }
/*设置任务进度*/ /*设置任务进度*/
public void setQuestAmount(QuestData quest, int amount) { public void setQuestAmount(QuestData quest, int amount) {
// 若玩家未接受任务直接返回 // 若玩家未接受任务直接返回
@ -160,11 +166,12 @@ public class PlayerData {
double d = amount / quest.getNeedAmount(); double d = amount / quest.getNeedAmount();
if (d < 0.0D) { if (d < 0.0D) {
d = 0.0D; d = 0.0D;
}else if (d > 1.0D) { } else if (d > 1.0D) {
d = 1.0D; d = 1.0D;
} }
return d; return d;
} }
public void reload(List<QuestData> questDataList) { public void reload(List<QuestData> questDataList) {
HashMap<QuestData, Integer> newData = new HashMap<>(); HashMap<QuestData, Integer> newData = new HashMap<>();
for (QuestData newQuest : questDataList) { for (QuestData newQuest : questDataList) {

View File

@ -2,7 +2,6 @@ package com.yaohun.questsystem.data;
import com.yaohun.questsystem.util.TimeType; import com.yaohun.questsystem.util.TimeType;
import me.Demon.DemonPlugin.DemonAPI; import me.Demon.DemonPlugin.DemonAPI;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -19,22 +18,22 @@ public class QuestData {
private List<String> finish; private List<String> finish;
private List<String> description; private List<String> description;
public QuestData(String questKey,FileConfiguration yml){ public QuestData(String questKey, FileConfiguration yml) {
this.questKey = questKey; this.questKey = questKey;
String cycle = yml.getString(questKey+".timeType","longTerm"); String cycle = yml.getString(questKey + ".timeType", "longTerm");
if(cycle.equalsIgnoreCase("everyday")){ if (cycle.equalsIgnoreCase("everyday")) {
timeType = TimeType.EVERYDAY; timeType = TimeType.EVERYDAY;
}else if(cycle.equalsIgnoreCase("weekly")){ } else if (cycle.equalsIgnoreCase("weekly")) {
timeType = TimeType.WEEKLY; timeType = TimeType.WEEKLY;
}else if(cycle.equalsIgnoreCase("longTerm")){ } else if (cycle.equalsIgnoreCase("longTerm")) {
timeType = TimeType.LONGTERM; timeType = TimeType.LONGTERM;
} }
this.name = yml.getString(questKey+".name"); this.name = yml.getString(questKey + ".name");
this.type = yml.getString(questKey+".type"); this.type = yml.getString(questKey + ".type");
this.target = yml.getString(questKey+".content.name"); this.target = yml.getString(questKey + ".content.name");
this.needAmount = yml.getInt(questKey+".content.amount"); this.needAmount = yml.getInt(questKey + ".content.amount");
this.finish = yml.getStringList(questKey+".finish"); this.finish = yml.getStringList(questKey + ".finish");
this.description = yml.getStringList(questKey+".description"); this.description = yml.getStringList(questKey + ".description");
} }
public String getQuestKey() { public String getQuestKey() {
@ -65,9 +64,11 @@ public class QuestData {
return description; return description;
} }
public void sendFinishCommand(Player player){ public void sendFinishCommand(Player player) {
if(player == null){return;} if (player == null) {
DemonAPI.ConsoleOutCommandList(player,getFinish()); return;
}
DemonAPI.ConsoleOutCommandList(player, getFinish());
} }
public List<String> getFinish() { public List<String> getFinish() {

View File

@ -2,41 +2,34 @@ package com.yaohun.questsystem.data.database;
import com.yaohun.questsystem.Main; import com.yaohun.questsystem.Main;
import com.yaohun.questsystem.util.SqlUtil; import com.yaohun.questsystem.util.SqlUtil;
import org.bukkit.Bukkit;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.HashMap;
public class SqlManager { public class SqlManager {
public String table = "quest_playerdata"; public String table = "quest_playerdata";
// 创建数据库表格格式 // 创建数据库表格格式
public void createTable() { public void createTable() {
// 数据库结构组成 // 数据库结构组成
// 玩家名(VARCHAR) 数据(VARCHAR) 数据保存时间(VARCHAR) // 玩家名(VARCHAR) 数据(VARCHAR) 数据保存时间(VARCHAR)
String s = "CREATE TABLE IF NOT EXISTS "+table+"(" + String s = "CREATE TABLE IF NOT EXISTS " + table + "(" +
" name VARCHAR(32) NOT NULL," + " name VARCHAR(32) NOT NULL," +
" data VARCHAR(8000) NOT NULL," + " data VARCHAR(8000) NOT NULL," +
" savetime VARCHAR(256) NOT NULL" + " savetime VARCHAR(256) NOT NULL" +
") ENGINE = InnoDB"; ") ENGINE = InnoDB";
getSQL().openConnection();
getSQL().updateSQL(s); getSQL().updateSQL(s);
getSQL().closeConnection();
} }
// 创建玩家数据 // 创建玩家数据
public void createPlayerData(String name){ public void createPlayerData(String name) {
getSQL().openConnection();
boolean butt = true; boolean butt = true;
String select = "SELECT * FROM "+table+" WHERE name = '%name%'"; String select = "SELECT * FROM " + table + " WHERE name = '%name%'";
try { try (ResultSet resultSet = getSQL().querySQL(select.replace("%name%", name))) {
ResultSet resultSet = getSQL().querySQL(select.replace("%name%", name));
while (resultSet.next()) { while (resultSet.next()) {
butt = false; butt = false;
} }
@ -44,19 +37,18 @@ public class SqlManager {
e.printStackTrace(); e.printStackTrace();
} }
// 若数据库没有name的数据则会创建数据 // 若数据库没有name的数据则会创建数据
if(butt) { if (butt) {
String set = "INSERT INTO " + table + " (`name`,`data`, `savetime`) " + String set = "INSERT INTO " + table + " (`name`,`data`, `savetime`) " +
"VALUES ('%name%','%data%', '%savetime%')"; "VALUES ('%name%','%data%', '%savetime%')";
set = set.replace("%name%", name); set = set.replace("%name%", name);
set = set.replace("%data%", ""); set = set.replace("%data%", "");
set = set.replace("%savetime%", getNowTimeString()); set = set.replace("%savetime%", getNowTimeString());
getSQL().updateSQL(set); getSQL().updateSQL(set);
System.out.println("[调试 - 输出] "+name+" 数据库数据已创建."); System.out.println("[调试 - 输出] " + name + " 数据库数据已创建.");
} }
getSQL().closeConnection();
} }
public String getNowTimeString(){ public String getNowTimeString() {
// 获取当前时间戳 // 获取当前时间戳
Instant now = Instant.now(); Instant now = Instant.now();
// 将时间戳转换为本地日期时间 // 将时间戳转换为本地日期时间
@ -68,34 +60,28 @@ public class SqlManager {
// 获取玩家任务数据 // 获取玩家任务数据
public String getQuestData(String name) { public String getQuestData(String name) {
String select = "SELECT * FROM "+table+" WHERE name = '%name%'"; String select = "SELECT * FROM " + table + " WHERE name = '%name%'";
try { try (ResultSet resultSet = getSQL().querySQL(select.replace("%name%", name))) {
getSQL().openConnection();
ResultSet resultSet = getSQL().querySQL(select.replace("%name%", name));
while (resultSet.next()) { while (resultSet.next()) {
String string = resultSet.getString("data"); String string = resultSet.getString("data");
if(string.length() >= 3) { if (string.length() >= 3) {
System.out.println("[调试 - 输出] " + name + " 数据: " + string); System.out.println("[调试 - 输出] " + name + " 数据: " + string);
return string; return string;
} }
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally {
getSQL().closeConnection();
} }
return null; return null;
} }
public void SavePlayerData(String name,String jsonData){ public void SavePlayerData(String name, String jsonData) {
String set = "UPDATE `"+table+"` SET " + String set = "UPDATE `" + table + "` SET " +
"`data` = '%data%'," + "`data` = '%data%'," +
"`savetime` = '%savetime%' WHERE `"+table+"`.`name` = '%name%'"; "`savetime` = '%savetime%' WHERE `" + table + "`.`name` = '%name%'";
getSQL().openConnection();
getSQL().updateSQL(set.replace("%name%", name). getSQL().updateSQL(set.replace("%name%", name).
replace("%data%", jsonData). replace("%data%", jsonData).
replace("%savetime%",getNowTimeString())); replace("%savetime%", getNowTimeString()));
getSQL().closeConnection();
} }
private SqlUtil getSQL() { private SqlUtil getSQL() {

View File

@ -1,7 +1,6 @@
package com.yaohun.questsystem.listener; package com.yaohun.questsystem.listener;
import com.yaohun.questsystem.Main; import com.yaohun.questsystem.Main;
import com.yaohun.questsystem.data.PlayerData;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -13,24 +12,24 @@ import org.bukkit.scheduler.BukkitRunnable;
public class JoinEvent implements Listener { public class JoinEvent implements Listener {
@EventHandler @EventHandler
public void onJoin(PlayerJoinEvent e){ public void onJoin(PlayerJoinEvent e) {
Player p = e.getPlayer(); Player p = e.getPlayer();
new BukkitRunnable() { new BukkitRunnable() {
@Override @Override
public void run() { public void run() {
Main.dataManage.loadPlayerData(p); Main.dataManage.loadPlayerData(p);
} }
}.runTaskLater(Main.plugin,20L); }.runTaskLater(Main.plugin, 20L);
} }
@EventHandler @EventHandler
public void onQuit(PlayerQuitEvent e){ public void onQuit(PlayerQuitEvent e) {
Player p = e.getPlayer(); Player p = e.getPlayer();
Main.dataManage.savePlayerData(p); Main.dataManage.savePlayerData(p);
} }
@EventHandler @EventHandler
public void onKick(PlayerKickEvent e){ public void onKick(PlayerKickEvent e) {
Player p = e.getPlayer(); Player p = e.getPlayer();
Main.dataManage.savePlayerData(p); Main.dataManage.savePlayerData(p);
} }

View File

@ -7,15 +7,15 @@ import com.google.gson.JsonObject;
import com.yaohun.questsystem.Main; import com.yaohun.questsystem.Main;
import com.yaohun.questsystem.data.PlayerData; import com.yaohun.questsystem.data.PlayerData;
import com.yaohun.questsystem.data.QuestData; import com.yaohun.questsystem.data.QuestData;
import com.yaohun.questsystem.util.TimeType;
import me.Demon.DemonPlugin.DemonAPI; import me.Demon.DemonPlugin.DemonAPI;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.io.File; import java.io.File;
import java.util.*; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class DataManage { public class DataManage {
@ -24,19 +24,19 @@ public class DataManage {
private ArrayList<QuestData> questDataArrayList = new ArrayList<>(); private ArrayList<QuestData> questDataArrayList = new ArrayList<>();
public DataManage(){ public DataManage() {
File file = new File(Main.plugin.getDataFolder(),"quest.yml"); File file = new File(Main.plugin.getDataFolder(), "quest.yml");
FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(file); FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(file);
HashMap<String, QuestData> dataHashMap = new HashMap<>(); HashMap<String, QuestData> dataHashMap = new HashMap<>();
for (String questKey : fileConfiguration.getKeys(false)){ for (String questKey : fileConfiguration.getKeys(false)) {
QuestData questData = new QuestData(questKey,fileConfiguration); QuestData questData = new QuestData(questKey, fileConfiguration);
dataHashMap.put(questKey,questData); dataHashMap.put(questKey, questData);
if(!questDataArrayList.contains(questData)) { if (!questDataArrayList.contains(questData)) {
questDataArrayList.add(questData); questDataArrayList.add(questData);
} }
} }
this.questDataMap = dataHashMap; 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<QuestData> getQuestDataArrayList() { public ArrayList<QuestData> getQuestDataArrayList() {
@ -46,8 +46,9 @@ public class DataManage {
public HashMap<String, QuestData> getQuestDataMap() { public HashMap<String, QuestData> getQuestDataMap() {
return questDataMap; 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 null;
} }
return this.questDataMap.get(questKey); return this.questDataMap.get(questKey);
@ -62,7 +63,7 @@ public class DataManage {
// 创建 Gson 实例 // 创建 Gson 实例
Gson gson = new Gson(); Gson gson = new Gson();
// 解析 JSON 数据 // 解析 JSON 数据
if (sqlJson != null && sqlJson.length() >= 3){ if (sqlJson != null && sqlJson.length() >= 3) {
JsonObject jsonObject = gson.fromJson(sqlJson, JsonObject.class); JsonObject jsonObject = gson.fromJson(sqlJson, JsonObject.class);
JsonArray questsArray = jsonObject.getAsJsonArray("quest"); JsonArray questsArray = jsonObject.getAsJsonArray("quest");
// 创建 HashMap 来存储结果 // 创建 HashMap 来存储结果
@ -84,21 +85,21 @@ public class DataManage {
// 创建初始化数据 // 创建初始化数据
PlayerData playerData = new PlayerData(name, questData); PlayerData playerData = new PlayerData(name, questData);
this.dataHashMap.put(player, playerData); this.dataHashMap.put(player, playerData);
}else{ } else {
// 判断玩家数据库中是否已存在数据 // 判断玩家数据库中是否已存在数据
Main.sqlManager.createPlayerData(name); Main.sqlManager.createPlayerData(name);
// 创建初始化数据 // 创建初始化数据
PlayerData playerData = new PlayerData(name); PlayerData playerData = new PlayerData(name);
this.dataHashMap.put(player, playerData); 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){ public void deletePlayerData(Player player) {
if(this.dataHashMap.get(player) == null){ if (this.dataHashMap.get(player) == null) {
return; return;
} }
this.dataHashMap.remove(player); this.dataHashMap.remove(player);
@ -107,15 +108,15 @@ public class DataManage {
/* /*
* 保存玩家数据 * 保存玩家数据
* */ * */
public void savePlayerData(Player player){ public void savePlayerData(Player player) {
if(this.dataHashMap.get(player) == null){ if (this.dataHashMap.get(player) == null) {
return; return;
} }
PlayerData data = this.dataHashMap.get(player); PlayerData data = this.dataHashMap.get(player);
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
JsonArray questArray = new JsonArray(); // 用于存储多个quest对象 JsonArray questArray = new JsonArray(); // 用于存储多个quest对象
HashMap<QuestData,Integer> questAmount = data.getQuestAmount(); HashMap<QuestData, Integer> questAmount = data.getQuestAmount();
for (QuestData quest : questAmount.keySet()){ for (QuestData quest : questAmount.keySet()) {
String questKey = quest.getQuestKey(); String questKey = quest.getQuestKey();
int amount = data.getQuestAmount(quest); int amount = data.getQuestAmount(quest);
JsonObject questObject = new JsonObject(); JsonObject questObject = new JsonObject();
@ -125,20 +126,22 @@ public class DataManage {
} }
jsonObject.add("quest", questArray); jsonObject.add("quest", questArray);
String jsonString = jsonObject.toString(); 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(); String name = player.getName();
this.dataHashMap.put(player,new PlayerData(name)); this.dataHashMap.put(player, new PlayerData(name));
} }
return this.dataHashMap.get(player); return this.dataHashMap.get(player);
} }
public HashMap<Player, PlayerData> getDataHashMap() { public HashMap<Player, PlayerData> getDataHashMap() {
return dataHashMap; return dataHashMap;
} }
public Set<Player> getQuests() { public Set<Player> getQuests() {
return this.dataHashMap.keySet(); return this.dataHashMap.keySet();
} }

View File

@ -33,9 +33,9 @@ public class SqlUtil extends MegumiSQL {
public void openConnection() { public void openConnection() {
try { try {
String mcVersion = getMinecraftVersion(); String mcVersion = getMinecraftVersion();
if(mcVersion.contains("1_20")){ if (mcVersion.contains("1_20")) {
Class.forName("com.mysql.cj.jdbc.Driver"); // 1.20.4的mysql路径 Class.forName("com.mysql.cj.jdbc.Driver"); // 1.20.4的mysql路径
}else{ } else {
Class.forName("com.mysql.jdbc.Driver"); // 1.18.2的mysql路径 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); 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) { public boolean updateSQL(String data) {
Connection conn; Connection conn;
if (checkConnection()) if (checkConnection()) {
conn = getConnection(); conn = getConnection();
else } else
return false; return false;
Statement stat = null; try (Statement stat = conn.createStatement()) {
try {
stat = conn.createStatement();
stat.executeUpdate(data); stat.executeUpdate(data);
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {

View File

@ -1,5 +1,5 @@
package com.yaohun.questsystem.util; package com.yaohun.questsystem.util;
public enum TimeType { public enum TimeType {
EVERYDAY,WEEKLY,LONGTERM EVERYDAY, WEEKLY, LONGTERM
} }