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>
<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>
<id>public</id>
<url>https://repo.aurora-pixels.com/repository/public/</url>
</repository>
<repository>
<id>public-rpg</id>
<url>https://repo.aurora-pixels.com/repository/public-rpg/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.18.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>me.Demon.DemonPlugin</groupId>
<artifactId>DemonAPI</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</project>

View File

@ -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<QuestData> 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<QuestData> 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<QuestData> 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];

View File

@ -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<QuestData> getPlayerQuestList(Player p){
* 获取玩家的任务列表
* */
public static Set<QuestData> getPlayerQuestList(Player p) {
PlayerData data = Main.dataManage.getPlayerData(p);
return data.getQuests();
}
/*
* 获取玩家已接受任务 指定周期任务列表
* */
* 获取玩家已接受任务 指定周期任务列表
* */
public static List<QuestData> getPlayerQuestList(Player p, TimeType timeType) {
List<QuestData> 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<QuestData, Integer> hashMap = new HashMap<>();
data.setQuestMap(hashMap);

View File

@ -9,24 +9,25 @@ import java.util.Set;
public class PlayerData {
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.questAmount = new HashMap<>();
}
public PlayerData(String name,HashMap<QuestData,Integer> hashMap){
public PlayerData(String name, HashMap<QuestData, Integer> 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<QuestData, Integer> getQuestAmount() {
return questAmount;
}
@ -38,11 +39,12 @@ public class PlayerData {
public Set<QuestData> 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<QuestData> questDataList) {
HashMap<QuestData, Integer> newData = new HashMap<>();
for (QuestData newQuest : questDataList) {

View File

@ -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<String> finish;
private List<String> 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<String> getFinish() {

View File

@ -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() {

View File

@ -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);
}

View File

@ -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<QuestData> 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<String, QuestData> 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<QuestData> getQuestDataArrayList() {
@ -46,23 +46,24 @@ public class DataManage {
public HashMap<String, QuestData> 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<QuestData,Integer> questAmount = data.getQuestAmount();
for (QuestData quest : questAmount.keySet()){
String questKey = quest.getQuestKey();
HashMap<QuestData, Integer> 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<Player, PlayerData> getDataHashMap() {
return dataHashMap;
}
public Set<Player> getQuests() {
return this.dataHashMap.keySet();
}

View File

@ -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) {

View File

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