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

@ -47,6 +47,7 @@ public class Main extends JavaPlugin {
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();
} }
@ -55,6 +56,7 @@ public class Main extends JavaPlugin {
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卸载完成,欢迎下次使用!");
} }

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;
@ -96,6 +95,7 @@ public class QuestAPI {
PlayerData data = Main.dataManage.getPlayerData(p); PlayerData data = Main.dataManage.getPlayerData(p);
data.setQuestAmount(quest, -1); data.setQuestAmount(quest, -1);
} }
/* /*
* 强制完成任务进度并触发奖励 * 强制完成任务进度并触发奖励
* */ * */

View File

@ -27,6 +27,7 @@ public class PlayerData {
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,6 +39,7 @@ 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;
@ -67,6 +69,7 @@ public class PlayerData {
} }
return true; return true;
} }
/* /*
* 判断任务是否已完成 * 判断任务是否已完成
* */ * */
@ -80,6 +83,7 @@ public class PlayerData {
} }
return false; return false;
} }
/* /*
* 提交任务 * 提交任务
* */ * */
@ -89,6 +93,7 @@ public class PlayerData {
// 设置任务提交后的状态为 -1 // 设置任务提交后的状态为 -1
this.questAmount.put(quest, -1); this.questAmount.put(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) {
// 若玩家未接受任务直接返回 // 若玩家未接受任务直接返回
@ -165,6 +171,7 @@ public class PlayerData {
} }
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;
@ -66,7 +65,9 @@ public class QuestData {
} }
public void sendFinishCommand(Player player) { public void sendFinishCommand(Player player) {
if(player == null){return;} if (player == null) {
return;
}
DemonAPI.ConsoleOutCommandList(player, getFinish()); DemonAPI.ConsoleOutCommandList(player, getFinish());
} }

View File

@ -2,18 +2,15 @@ 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";
// 创建数据库表格格式 // 创建数据库表格格式
@ -25,18 +22,14 @@ public class SqlManager {
" 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;
} }
@ -53,7 +46,6 @@ public class SqlManager {
getSQL().updateSQL(set); getSQL().updateSQL(set);
System.out.println("[调试 - 输出] " + name + " 数据库数据已创建."); System.out.println("[调试 - 输出] " + name + " 数据库数据已创建.");
} }
getSQL().closeConnection();
} }
public String getNowTimeString() { public String getNowTimeString() {
@ -69,9 +61,7 @@ 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) {
@ -81,8 +71,6 @@ public class SqlManager {
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally {
getSQL().closeConnection();
} }
return null; return null;
} }
@ -91,11 +79,9 @@ public class SqlManager {
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;

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 {
@ -46,6 +46,7 @@ public class DataManage {
public HashMap<String, QuestData> getQuestDataMap() { public HashMap<String, QuestData> getQuestDataMap() {
return questDataMap; return questDataMap;
} }
public QuestData getQuestData(String questKey) { public QuestData getQuestData(String questKey) {
if (this.questDataMap.get(questKey) == null) { if (this.questDataMap.get(questKey) == null) {
return null; return null;
@ -136,9 +137,11 @@ public class DataManage {
} }
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

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