mirror of
https://github.com/CarmJos/UltraDepository.git
synced 2026-06-04 16:48:21 +08:00
将用户数据加载、保存的错误分析和debug消息放在UserManager
This commit is contained in:
@@ -3,7 +3,6 @@ package cc.carm.plugin.ultradepository.data;
|
||||
import cc.carm.plugin.ultradepository.Main;
|
||||
import cc.carm.plugin.ultradepository.configuration.depository.Depository;
|
||||
import cc.carm.plugin.ultradepository.configuration.depository.DepositoryItem;
|
||||
import cc.carm.plugin.ultradepository.storage.DataStorage;
|
||||
import cc.carm.plugin.ultradepository.util.DateIntUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -18,15 +17,13 @@ public class UserData {
|
||||
|
||||
public final UUID userUUID;
|
||||
|
||||
DataStorage storage;
|
||||
Map<String, DepositoryData> depositories;
|
||||
|
||||
int date;
|
||||
|
||||
public UserData(UUID userUUID, DataStorage storage,
|
||||
public UserData(UUID userUUID,
|
||||
Map<String, DepositoryData> depositories, int date) {
|
||||
this.userUUID = userUUID;
|
||||
this.storage = storage;
|
||||
this.depositories = depositories;
|
||||
this.date = date;
|
||||
}
|
||||
@@ -145,11 +142,6 @@ public class UserData {
|
||||
.forEach(DepositoryItemData::clearSold);
|
||||
}
|
||||
|
||||
|
||||
public void save() throws Exception {
|
||||
this.storage.saveUserData(this);
|
||||
}
|
||||
|
||||
public Map<String, Map<String, Map<String, Integer>>> serializeToMap() {
|
||||
Map<String, Map<String, Map<String, Integer>>> values = new LinkedHashMap<>();
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package cc.carm.plugin.ultradepository.manager;
|
||||
|
||||
import cc.carm.plugin.ultradepository.Main;
|
||||
import cc.carm.plugin.ultradepository.data.UserData;
|
||||
import cc.carm.plugin.ultradepository.storage.DataStorage;
|
||||
import cc.carm.plugin.ultradepository.util.DateIntUtil;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -29,11 +30,24 @@ public class UserManager {
|
||||
|
||||
public @NotNull UserData loadData(@NotNull UUID userUUID) {
|
||||
try {
|
||||
return Main.getStorage().loadData(userUUID);
|
||||
long start = System.currentTimeMillis();
|
||||
DataStorage storage = Main.getStorage();
|
||||
Main.debug("正通过 " + storage.getClass().getSimpleName() + " 加载 " + userUUID + " 的用户数据...");
|
||||
UserData data = Main.getStorage().loadData(userUUID);
|
||||
|
||||
if (data == null) {
|
||||
Main.debug("当前还不存在玩家 " + userUUID + " 的数据,视作新档。");
|
||||
return new UserData(userUUID, new HashMap<>(), DateIntUtil.getCurrentDate());
|
||||
}
|
||||
|
||||
Main.debug("通过 " + storage.getClass().getSimpleName() + "加载 " + userUUID + " 的用户数据完成,"
|
||||
+ "耗时 " + (System.currentTimeMillis() - start) + "ms。");
|
||||
|
||||
return data;
|
||||
} catch (Exception e) {
|
||||
Main.error("无法正常加载玩家数据,玩家操作将不会被保存,请检查数据配置!");
|
||||
Main.error("Could not load user's data, please check the data configuration!");
|
||||
return new UserData(userUUID, Main.getStorage(), new HashMap<>(), DateIntUtil.getCurrentDate());
|
||||
return new UserData(userUUID, new HashMap<>(), DateIntUtil.getCurrentDate());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,8 +60,15 @@ public class UserManager {
|
||||
|
||||
public void saveData(UserData data) {
|
||||
try {
|
||||
data.save();
|
||||
Main.debug(" 玩家 " + data.getUserUUID() + " 数据已保存。");
|
||||
long start = System.currentTimeMillis();
|
||||
DataStorage storage = Main.getStorage();
|
||||
|
||||
Main.debug("正通过 " + storage.getClass().getSimpleName() + " 保存 " + data.getUserUUID() + " 的用户数据...");
|
||||
storage.saveUserData(data);
|
||||
|
||||
Main.debug("通过 " + storage.getClass().getSimpleName() + " 保存 " + data.getUserUUID() + " 的用户数据完成," +
|
||||
"耗时 " + (System.currentTimeMillis() - start) + "ms。");
|
||||
|
||||
} catch (Exception e) {
|
||||
Main.error("无法正常保存玩家数据,请检查数据配置!");
|
||||
Main.error("Could not save user's data, please check the data configuration!");
|
||||
|
||||
@@ -2,6 +2,7 @@ package cc.carm.plugin.ultradepository.storage;
|
||||
|
||||
import cc.carm.plugin.ultradepository.data.UserData;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -12,7 +13,7 @@ public interface DataStorage {
|
||||
|
||||
void shutdown();
|
||||
|
||||
@NotNull
|
||||
@Nullable
|
||||
UserData loadData(@NotNull UUID uuid) throws Exception;
|
||||
|
||||
void saveUserData(@NotNull UserData data) throws Exception;
|
||||
|
||||
@@ -11,6 +11,7 @@ import cc.carm.plugin.ultradepository.util.DateIntUtil;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -45,18 +46,16 @@ public class FileStorage implements DataStorage {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull UserData loadData(@NotNull UUID uuid) {
|
||||
long start = System.currentTimeMillis();
|
||||
Main.debug("正通过 FileStorage 加载 " + uuid + " 的用户数据...");
|
||||
public @Nullable UserData loadData(@NotNull UUID uuid) {
|
||||
File userDataFile = new File(getDataContainer(), uuid + ".yml");
|
||||
if (!userDataFile.exists()) {
|
||||
Main.debug("当前文件夾内不存在玩家 " + uuid + " 的数据,视作新档。");
|
||||
return new UserData(uuid, this, new HashMap<>(), DateIntUtil.getCurrentDate());
|
||||
return null;
|
||||
}
|
||||
|
||||
YamlConfiguration userDataConfig = YamlConfiguration.loadConfiguration(userDataFile);
|
||||
int dateInt = userDataConfig.getInt("date", DateIntUtil.getCurrentDate());
|
||||
UserData userData = new UserData(uuid, this, new HashMap<>(), dateInt);
|
||||
UserData userData = new UserData(uuid, new HashMap<>(), dateInt);
|
||||
|
||||
ConfigurationSection depositoriesSection = userDataConfig.getConfigurationSection("depositories");
|
||||
if (depositoriesSection != null) {
|
||||
@@ -88,33 +87,15 @@ public class FileStorage implements DataStorage {
|
||||
if (!depositoryData.getContents().isEmpty()) userData.setDepository(depositoryData);
|
||||
}
|
||||
}
|
||||
Main.debug("通过 FileStorage 加载 " + uuid + " 的用户数据完成,"
|
||||
+ "耗时 " + (System.currentTimeMillis() - start) + "ms。");
|
||||
|
||||
return userData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveUserData(@NotNull UserData data) throws IOException {
|
||||
long start = System.currentTimeMillis();
|
||||
Main.debug("正通过 FileStorage 保存 " + data.getUserUUID() + " 的用户数据...");
|
||||
|
||||
YamlConfiguration userDataConfig = new YamlConfiguration();
|
||||
userDataConfig.set("date", data.getDateInt());
|
||||
|
||||
try {
|
||||
userDataConfig.createSection("depositories", data.serializeToMap());
|
||||
userDataConfig.save(new File(getDataContainer(), data.getUserUUID() + ".yml"));
|
||||
} catch (IOException ioException) {
|
||||
Main.error("在保存玩家 #" + data.getUserUUID() + " 的数据时出现异常。");
|
||||
Main.error("Error occurred when saving #" + data.getUserUUID() + " data.");
|
||||
throw ioException;
|
||||
}
|
||||
|
||||
Main.debug(
|
||||
"通过 FileStorage 保存 " + data.getUserUUID() + " 的用户数据完成," +
|
||||
"耗时 " + (System.currentTimeMillis() - start) + "ms。"
|
||||
);
|
||||
userDataConfig.createSection("depositories", data.serializeToMap());
|
||||
userDataConfig.save(new File(getDataContainer(), data.getUserUUID() + ".yml"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.ResultSet;
|
||||
@@ -117,35 +118,29 @@ public class MySQLStorage implements DataStorage {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull UserData loadData(@NotNull UUID uuid) throws Exception {
|
||||
long start = System.currentTimeMillis();
|
||||
Main.debug("正通过 MySQLStorage 加载 " + uuid + " 的用户数据...");
|
||||
public @Nullable UserData loadData(@NotNull UUID uuid) throws Exception {
|
||||
try (SQLQuery query = createAction(uuid).execute()) {
|
||||
ResultSet resultSet = query.getResultSet();
|
||||
if (resultSet != null && resultSet.next()) {
|
||||
String dataJSON = resultSet.getString("data");
|
||||
Date date = resultSet.getDate("day");
|
||||
UserData data = new UserData(uuid, this, new HashMap<>(), DateIntUtil.getDateInt(date));
|
||||
|
||||
JsonElement dataElement = PARSER.parse(dataJSON);
|
||||
if (dataElement.isJsonObject()) {
|
||||
for (Map.Entry<String, JsonElement> entry : dataElement.getAsJsonObject().entrySet()) {
|
||||
Depository depository = Main.getDepositoryManager().getDepository(entry.getKey());
|
||||
if (depository == null) continue;
|
||||
if (resultSet == null || !resultSet.next()) return null;
|
||||
|
||||
DepositoryData contentsData = parseContentsData(depository, data, entry.getValue());
|
||||
if (contentsData != null) data.setDepository(contentsData);
|
||||
String dataJSON = resultSet.getString("data");
|
||||
Date date = resultSet.getDate("day");
|
||||
UserData data = new UserData(uuid, new HashMap<>(), DateIntUtil.getDateInt(date));
|
||||
|
||||
JsonElement dataElement = PARSER.parse(dataJSON);
|
||||
if (dataElement.isJsonObject()) {
|
||||
for (Map.Entry<String, JsonElement> entry : dataElement.getAsJsonObject().entrySet()) {
|
||||
Depository depository = Main.getDepositoryManager().getDepository(entry.getKey());
|
||||
if (depository == null) continue;
|
||||
|
||||
DepositoryData contentsData = parseContentsData(depository, data, entry.getValue());
|
||||
if (contentsData != null) data.setDepository(contentsData);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Main.debug("通过 MySQLStorage 加载 " + uuid + " 的用户数据完成,"
|
||||
+ "耗时 " + (System.currentTimeMillis() - start) + "ms。");
|
||||
|
||||
return data;
|
||||
}
|
||||
Main.debug("当前库内不存在玩家 " + uuid + " 的数据,视作新档。");
|
||||
return new UserData(uuid, this, new HashMap<>(), DateIntUtil.getCurrentDate());
|
||||
return data;
|
||||
|
||||
} catch (Exception exception) {
|
||||
throw new Exception(exception);
|
||||
}
|
||||
@@ -153,25 +148,10 @@ public class MySQLStorage implements DataStorage {
|
||||
|
||||
@Override
|
||||
public void saveUserData(@NotNull UserData data) throws Exception {
|
||||
long start = System.currentTimeMillis();
|
||||
Main.debug("正通过 MySQLStorage 保存 " + data.getUserUUID() + " 的用户数据...");
|
||||
|
||||
try {
|
||||
|
||||
getSQLManager().createReplace(SQLTables.USER_DATA.getName())
|
||||
.setColumnNames("uuid", "data", "day")
|
||||
.setParams(data.getUserUUID(), GSON.toJson(data.serializeToMap()), data.getDate())
|
||||
.execute();
|
||||
|
||||
} catch (SQLException exception) {
|
||||
Main.error("在保存玩家 #" + data.getUserUUID() + " 的数据时出现异常。");
|
||||
Main.error("Error occurred when saving #" + data.getUserUUID() + " data.");
|
||||
throw new Exception(exception);
|
||||
}
|
||||
|
||||
Main.debug("通过 MySQLStorage 保存 " + data.getUserUUID() + " 的用户数据完成," +
|
||||
"耗时 " + (System.currentTimeMillis() - start) + "ms。");
|
||||
|
||||
getSQLManager().createReplace(SQLTables.USER_DATA.getName())
|
||||
.setColumnNames("uuid", "data", "day")
|
||||
.setParams(data.getUserUUID(), GSON.toJson(data.serializeToMap()), data.getDate())
|
||||
.execute();
|
||||
}
|
||||
|
||||
private SQLManager getSQLManager() {
|
||||
|
||||
Reference in New Issue
Block a user