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

@ -47,6 +47,7 @@ public class Main extends JavaPlugin {
String SQL_Database = "quest";
sqlManager = new SqlManager();
sqlUtil = new SqlUtil(SQL_Host, SQL_Port, SQL_Database, SQL_Users, SQL_Password);
sqlUtil.openConnection();
sqlManager.createTable();
}
@ -55,6 +56,7 @@ public class Main extends JavaPlugin {
for (Player player : Bukkit.getOnlinePlayers()) {
dataManage.savePlayerData(player);
}
sqlUtil.closeConnection();
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.manage.DataManage;
import com.yaohun.questsystem.util.TimeType;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.util.ArrayList;
@ -96,6 +95,7 @@ public class QuestAPI {
PlayerData data = Main.dataManage.getPlayerData(p);
data.setQuestAmount(quest, -1);
}
/*
* 强制完成任务进度并触发奖励
* */

View File

@ -27,6 +27,7 @@ public class PlayerData {
public String getName() {
return name;
}
public HashMap<QuestData, Integer> getQuestAmount() {
return questAmount;
}
@ -38,6 +39,7 @@ public class PlayerData {
public Set<QuestData> getQuests() {
return this.questAmount.keySet();
}
// 获取玩家已完成任务数量
public int getFinishedAmount() {
int i = 0;
@ -67,6 +69,7 @@ public class PlayerData {
}
return true;
}
/*
* 判断任务是否已完成
* */
@ -80,6 +83,7 @@ public class PlayerData {
}
return false;
}
/*
* 提交任务
* */
@ -89,6 +93,7 @@ public class PlayerData {
// 设置任务提交后的状态为 -1
this.questAmount.put(quest, -1);
}
/*
* 判断玩家是否达到要求可提交任务
* */
@ -140,6 +145,7 @@ public class PlayerData {
}
this.questAmount.put(quest, new_amount);
}
/*设置任务进度*/
public void setQuestAmount(QuestData quest, int amount) {
// 若玩家未接受任务直接返回
@ -165,6 +171,7 @@ public class PlayerData {
}
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;
@ -66,7 +65,9 @@ public class QuestData {
}
public void sendFinishCommand(Player player) {
if(player == null){return;}
if (player == null) {
return;
}
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.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";
// 创建数据库表格格式
@ -25,18 +22,14 @@ public class SqlManager {
" 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));
try (ResultSet resultSet = getSQL().querySQL(select.replace("%name%", name))) {
while (resultSet.next()) {
butt = false;
}
@ -53,7 +46,6 @@ public class SqlManager {
getSQL().updateSQL(set);
System.out.println("[调试 - 输出] " + name + " 数据库数据已创建.");
}
getSQL().closeConnection();
}
public String getNowTimeString() {
@ -69,9 +61,7 @@ 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));
try (ResultSet resultSet = getSQL().querySQL(select.replace("%name%", name))) {
while (resultSet.next()) {
String string = resultSet.getString("data");
if (string.length() >= 3) {
@ -81,8 +71,6 @@ public class SqlManager {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
getSQL().closeConnection();
}
return null;
}
@ -91,11 +79,9 @@ public class SqlManager {
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() {

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;

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

View File

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