mirror of
https://github.com/CarmJos/UltraDepository.git
synced 2026-06-04 08:38:25 +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.Main;
|
||||||
import cc.carm.plugin.ultradepository.configuration.depository.Depository;
|
import cc.carm.plugin.ultradepository.configuration.depository.Depository;
|
||||||
import cc.carm.plugin.ultradepository.configuration.depository.DepositoryItem;
|
import cc.carm.plugin.ultradepository.configuration.depository.DepositoryItem;
|
||||||
import cc.carm.plugin.ultradepository.storage.DataStorage;
|
|
||||||
import cc.carm.plugin.ultradepository.util.DateIntUtil;
|
import cc.carm.plugin.ultradepository.util.DateIntUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
@@ -18,15 +17,13 @@ public class UserData {
|
|||||||
|
|
||||||
public final UUID userUUID;
|
public final UUID userUUID;
|
||||||
|
|
||||||
DataStorage storage;
|
|
||||||
Map<String, DepositoryData> depositories;
|
Map<String, DepositoryData> depositories;
|
||||||
|
|
||||||
int date;
|
int date;
|
||||||
|
|
||||||
public UserData(UUID userUUID, DataStorage storage,
|
public UserData(UUID userUUID,
|
||||||
Map<String, DepositoryData> depositories, int date) {
|
Map<String, DepositoryData> depositories, int date) {
|
||||||
this.userUUID = userUUID;
|
this.userUUID = userUUID;
|
||||||
this.storage = storage;
|
|
||||||
this.depositories = depositories;
|
this.depositories = depositories;
|
||||||
this.date = date;
|
this.date = date;
|
||||||
}
|
}
|
||||||
@@ -145,11 +142,6 @@ public class UserData {
|
|||||||
.forEach(DepositoryItemData::clearSold);
|
.forEach(DepositoryItemData::clearSold);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void save() throws Exception {
|
|
||||||
this.storage.saveUserData(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<String, Map<String, Map<String, Integer>>> serializeToMap() {
|
public Map<String, Map<String, Map<String, Integer>>> serializeToMap() {
|
||||||
Map<String, Map<String, Map<String, Integer>>> values = new LinkedHashMap<>();
|
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.Main;
|
||||||
import cc.carm.plugin.ultradepository.data.UserData;
|
import cc.carm.plugin.ultradepository.data.UserData;
|
||||||
|
import cc.carm.plugin.ultradepository.storage.DataStorage;
|
||||||
import cc.carm.plugin.ultradepository.util.DateIntUtil;
|
import cc.carm.plugin.ultradepository.util.DateIntUtil;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -29,11 +30,24 @@ public class UserManager {
|
|||||||
|
|
||||||
public @NotNull UserData loadData(@NotNull UUID userUUID) {
|
public @NotNull UserData loadData(@NotNull UUID userUUID) {
|
||||||
try {
|
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) {
|
} catch (Exception e) {
|
||||||
Main.error("无法正常加载玩家数据,玩家操作将不会被保存,请检查数据配置!");
|
Main.error("无法正常加载玩家数据,玩家操作将不会被保存,请检查数据配置!");
|
||||||
Main.error("Could not load user's data, please check the data configuration!");
|
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) {
|
public void saveData(UserData data) {
|
||||||
try {
|
try {
|
||||||
data.save();
|
long start = System.currentTimeMillis();
|
||||||
Main.debug(" 玩家 " + data.getUserUUID() + " 数据已保存。");
|
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) {
|
} catch (Exception e) {
|
||||||
Main.error("无法正常保存玩家数据,请检查数据配置!");
|
Main.error("无法正常保存玩家数据,请检查数据配置!");
|
||||||
Main.error("Could not save user's data, please check the data configuration!");
|
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 cc.carm.plugin.ultradepository.data.UserData;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@@ -12,7 +13,7 @@ public interface DataStorage {
|
|||||||
|
|
||||||
void shutdown();
|
void shutdown();
|
||||||
|
|
||||||
@NotNull
|
@Nullable
|
||||||
UserData loadData(@NotNull UUID uuid) throws Exception;
|
UserData loadData(@NotNull UUID uuid) throws Exception;
|
||||||
|
|
||||||
void saveUserData(@NotNull UserData data) 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.ConfigurationSection;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -45,18 +46,16 @@ public class FileStorage implements DataStorage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull UserData loadData(@NotNull UUID uuid) {
|
public @Nullable UserData loadData(@NotNull UUID uuid) {
|
||||||
long start = System.currentTimeMillis();
|
|
||||||
Main.debug("正通过 FileStorage 加载 " + uuid + " 的用户数据...");
|
|
||||||
File userDataFile = new File(getDataContainer(), uuid + ".yml");
|
File userDataFile = new File(getDataContainer(), uuid + ".yml");
|
||||||
if (!userDataFile.exists()) {
|
if (!userDataFile.exists()) {
|
||||||
Main.debug("当前文件夾内不存在玩家 " + uuid + " 的数据,视作新档。");
|
Main.debug("当前文件夾内不存在玩家 " + uuid + " 的数据,视作新档。");
|
||||||
return new UserData(uuid, this, new HashMap<>(), DateIntUtil.getCurrentDate());
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
YamlConfiguration userDataConfig = YamlConfiguration.loadConfiguration(userDataFile);
|
YamlConfiguration userDataConfig = YamlConfiguration.loadConfiguration(userDataFile);
|
||||||
int dateInt = userDataConfig.getInt("date", DateIntUtil.getCurrentDate());
|
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");
|
ConfigurationSection depositoriesSection = userDataConfig.getConfigurationSection("depositories");
|
||||||
if (depositoriesSection != null) {
|
if (depositoriesSection != null) {
|
||||||
@@ -88,33 +87,15 @@ public class FileStorage implements DataStorage {
|
|||||||
if (!depositoryData.getContents().isEmpty()) userData.setDepository(depositoryData);
|
if (!depositoryData.getContents().isEmpty()) userData.setDepository(depositoryData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Main.debug("通过 FileStorage 加载 " + uuid + " 的用户数据完成,"
|
|
||||||
+ "耗时 " + (System.currentTimeMillis() - start) + "ms。");
|
|
||||||
|
|
||||||
return userData;
|
return userData;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveUserData(@NotNull UserData data) throws IOException {
|
public void saveUserData(@NotNull UserData data) throws IOException {
|
||||||
long start = System.currentTimeMillis();
|
|
||||||
Main.debug("正通过 FileStorage 保存 " + data.getUserUUID() + " 的用户数据...");
|
|
||||||
|
|
||||||
YamlConfiguration userDataConfig = new YamlConfiguration();
|
YamlConfiguration userDataConfig = new YamlConfiguration();
|
||||||
userDataConfig.set("date", data.getDateInt());
|
userDataConfig.set("date", data.getDateInt());
|
||||||
|
userDataConfig.createSection("depositories", data.serializeToMap());
|
||||||
try {
|
userDataConfig.save(new File(getDataContainer(), data.getUserUUID() + ".yml"));
|
||||||
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。"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import com.google.gson.JsonElement;
|
|||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.sql.Date;
|
import java.sql.Date;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@@ -117,35 +118,29 @@ public class MySQLStorage implements DataStorage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull UserData loadData(@NotNull UUID uuid) throws Exception {
|
public @Nullable UserData loadData(@NotNull UUID uuid) throws Exception {
|
||||||
long start = System.currentTimeMillis();
|
|
||||||
Main.debug("正通过 MySQLStorage 加载 " + uuid + " 的用户数据...");
|
|
||||||
try (SQLQuery query = createAction(uuid).execute()) {
|
try (SQLQuery query = createAction(uuid).execute()) {
|
||||||
ResultSet resultSet = query.getResultSet();
|
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 (resultSet == null || !resultSet.next()) return null;
|
||||||
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());
|
String dataJSON = resultSet.getString("data");
|
||||||
if (contentsData != null) data.setDepository(contentsData);
|
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 data;
|
||||||
return new UserData(uuid, this, new HashMap<>(), DateIntUtil.getCurrentDate());
|
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
throw new Exception(exception);
|
throw new Exception(exception);
|
||||||
}
|
}
|
||||||
@@ -153,25 +148,10 @@ public class MySQLStorage implements DataStorage {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveUserData(@NotNull UserData data) throws Exception {
|
public void saveUserData(@NotNull UserData data) throws Exception {
|
||||||
long start = System.currentTimeMillis();
|
getSQLManager().createReplace(SQLTables.USER_DATA.getName())
|
||||||
Main.debug("正通过 MySQLStorage 保存 " + data.getUserUUID() + " 的用户数据...");
|
.setColumnNames("uuid", "data", "day")
|
||||||
|
.setParams(data.getUserUUID(), GSON.toJson(data.serializeToMap()), data.getDate())
|
||||||
try {
|
.execute();
|
||||||
|
|
||||||
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。");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private SQLManager getSQLManager() {
|
private SQLManager getSQLManager() {
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@@ -7,10 +6,17 @@ public class GsonMapTest {
|
|||||||
|
|
||||||
private static final Gson GSON = new Gson();
|
private static final Gson GSON = new Gson();
|
||||||
|
|
||||||
@Test
|
@org.junit.Test
|
||||||
public void test() {
|
public void test() {
|
||||||
System.out.println(this.getClass().getSimpleName());
|
System.out.println(this.getClass().getSimpleName());
|
||||||
|
|
||||||
|
|
||||||
|
List<Test> tests = new ArrayList<>();
|
||||||
|
tests.add(new Test1());
|
||||||
|
tests.add(new Test2());
|
||||||
|
|
||||||
|
tests.stream().map(test -> test.getClass().getSimpleName()).forEach(System.out::println);
|
||||||
|
|
||||||
Map<String, Map<String, Map<String, Integer>>> values = new LinkedHashMap<>();
|
Map<String, Map<String, Map<String, Integer>>> values = new LinkedHashMap<>();
|
||||||
|
|
||||||
|
|
||||||
@@ -38,5 +44,29 @@ public class GsonMapTest {
|
|||||||
System.out.println(jsonValues);
|
System.out.println(jsonValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface Test {
|
||||||
|
|
||||||
|
void load();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Test1 implements Test {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load() {
|
||||||
|
System.out.println("test1");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Test2 implements Test {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load() {
|
||||||
|
System.out.println("test2");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user