1
mirror of https://github.com/CarmJos/UltraDepository.git synced 2024-09-19 11:45:47 +00:00

[v1.2.3] 版本修复

- [A] 采用新版 EasyPlugin ,修改SQL语句使其更加优雅。
This commit is contained in:
Carm Jos 2022-01-08 01:05:59 +08:00
parent 988fe600ce
commit f001ac0010
7 changed files with 103 additions and 91 deletions

44
pom.xml
View File

@ -9,13 +9,13 @@
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<easyplugin.version>1.1.3</easyplugin.version>
<easyplugin.version>1.2.0</easyplugin.version>
</properties>
<groupId>cc.carm.plugin</groupId>
<artifactId>ultradepository</artifactId>
<packaging>jar</packaging>
<version>1.2.2</version>
<version>1.2.3</version>
<name>UltraDepository</name>
<description>超级仓库插件,支持设定不同物品的存储仓库。</description>
@ -92,39 +92,28 @@
<dependencies>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>2.2.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-main</artifactId>
<version>${easyplugin.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-configuration</artifactId>
<version>${easyplugin.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-gui</artifactId>
<version>${easyplugin.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-database</artifactId>
<version>${easyplugin.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
@ -141,6 +130,13 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>2.2.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
@ -157,6 +153,20 @@
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-bom</artifactId>
<version>${easyplugin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>

View File

@ -27,7 +27,7 @@ public interface DataStorage {
* @return 正确ID数据
* @since v1.1.6
*/
default String getFixedTypeID(String typeID) {
static String getFixedTypeID(String typeID) {
String trueID = typeID;
if (typeID.contains(":")) {
try {

View File

@ -30,8 +30,8 @@ public class JSONStorage implements DataStorage {
private File dataContainer;
public static final Gson GSON = new Gson();
public static final JsonParser PARSER = new JsonParser();
protected static final Gson GSON = new Gson();
protected static final JsonParser PARSER = new JsonParser();
@Override
public boolean initialize() {
@ -66,18 +66,9 @@ public class JSONStorage implements DataStorage {
JsonObject dataObject = dataElement.getAsJsonObject();
int dateInt = dataObject.get("date").getAsInt();
JsonObject repositoriesObject = dataObject.getAsJsonObject("depositories");
UserData userData = new UserData(uuid, new HashMap<>(), dateInt);
for (Map.Entry<String, JsonElement> entry : repositoriesObject.entrySet()) {
Depository depository = UltraDepository.getDepositoryManager().getDepository(entry.getKey());
if (depository == null) continue;
DepositoryData contentsData = parseContentsData(depository, userData, entry.getValue());
if (contentsData != null) userData.setDepository(contentsData);
}
loadDepositoriesInto(userData, dataObject.getAsJsonObject("depositories"));
return userData;
}
@ -86,7 +77,7 @@ public class JSONStorage implements DataStorage {
public void saveUserData(@NotNull UserData data) throws Exception {
JsonObject dataObject = new JsonObject();
dataObject.addProperty("date", data.getDateInt());
dataObject.add("depositories", GSON.toJsonTree(data.serializeToMap()));
dataObject.add("depositories", saveDepositoriesToJson(data));
FileWriter writer = new FileWriter(new File(getDataContainer(), data.getUserUUID() + ".json"));
writer.write(GSON.toJson(dataObject));
@ -94,18 +85,36 @@ public class JSONStorage implements DataStorage {
writer.close();
}
protected DepositoryData parseContentsData(@NotNull Depository source,
@NotNull UserData owner,
@NotNull JsonElement contentsElement) {
return contentsElement.isJsonObject() ? parseContentsData(source, owner, contentsElement.getAsJsonObject()) : null;
public static JsonElement saveDepositoriesToJson(UserData data) {
return GSON.toJsonTree(data.serializeToMap());
}
protected DepositoryData parseContentsData(@NotNull Depository source,
@NotNull UserData owner,
@NotNull JsonObject contentsObject) {
public static String serializeDepositories(UserData data) {
return GSON.toJson(saveDepositoriesToJson(data));
}
public static void loadDepositoriesInto(UserData data, JsonElement depositoriesElement) {
if (depositoriesElement == null || !depositoriesElement.isJsonObject()) return;
for (Map.Entry<String, JsonElement> entry : depositoriesElement.getAsJsonObject().entrySet()) {
Depository depository = UltraDepository.getDepositoryManager().getDepository(entry.getKey());
if (depository == null) continue;
DepositoryData contentsData = parseContentsData(depository, data, entry.getValue());
if (contentsData != null) data.setDepository(contentsData);
}
}
public static DepositoryData parseContentsData(@NotNull Depository source,
@NotNull UserData owner,
@NotNull JsonElement contentsElement) {
if (!contentsElement.isJsonObject()) return null;
JsonObject contentsObject = contentsElement.getAsJsonObject();
DepositoryData data = DepositoryData.emptyContents(source, owner);
for (Map.Entry<String, JsonElement> entry : contentsObject.entrySet()) {
DepositoryItem item = source.getItems().get(getFixedTypeID(entry.getKey()));
DepositoryItem item = source.getItems().get(DataStorage.getFixedTypeID(entry.getKey()));
if (item == null) continue;
DepositoryItemData itemData = parseItemData(item, data, entry.getValue());
@ -115,15 +124,13 @@ public class JSONStorage implements DataStorage {
return data;
}
protected DepositoryItemData parseItemData(@NotNull DepositoryItem source,
@NotNull DepositoryData owner,
@NotNull JsonElement itemElement) {
return itemElement.isJsonObject() ? parseItemData(source, owner, itemElement.getAsJsonObject()) : null;
}
protected DepositoryItemData parseItemData(@NotNull DepositoryItem source,
@NotNull DepositoryData owner,
@NotNull JsonObject itemObject) {
public static DepositoryItemData parseItemData(@NotNull DepositoryItem source,
@NotNull DepositoryData owner,
@NotNull JsonElement itemElement) {
if (!itemElement.isJsonObject()) return null;
JsonObject itemObject = itemElement.getAsJsonObject();
int amount = itemObject.has("amount") ? itemObject.get("amount").getAsInt() : 0;
int sold = itemObject.has("sold") ? itemObject.get("sold").getAsInt() : 0;
if (amount == 0 && sold == 0) return null;

View File

@ -5,15 +5,9 @@ import cc.carm.lib.easyplugin.database.DatabaseTable;
import cc.carm.lib.easyplugin.database.EasySQL;
import cc.carm.lib.easyplugin.database.api.SQLManager;
import cc.carm.lib.easyplugin.database.api.action.query.PreparedQueryAction;
import cc.carm.lib.easyplugin.database.api.action.query.SQLQuery;
import cc.carm.plugin.ultradepository.UltraDepository;
import cc.carm.plugin.ultradepository.configuration.depository.Depository;
import cc.carm.plugin.ultradepository.data.DepositoryData;
import cc.carm.plugin.ultradepository.data.UserData;
import cc.carm.plugin.ultradepository.util.DateIntUtil;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -21,39 +15,37 @@ import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class MySQLStorage extends JSONStorage {
private static final ConfigValue<String> DRIVER_NAME = new ConfigValue<>(
"storage.mysql.driver", String.class, "com.mysql.jdbc.Driver"
"storage.mysql.driver", String.class,
"com.mysql.jdbc.Driver"
);
private static final ConfigValue<String> URL = new ConfigValue<>(
"storage.mysql.url", String.class, "jdbc:mysql://127.0.0.1:3306/minecraft"
"storage.mysql.url", String.class,
"jdbc:mysql://127.0.0.1:3306/minecraft"
);
private static final ConfigValue<String> USERNAME = new ConfigValue<>(
"storage.mysql.username", String.class, "username"
"storage.mysql.username", String.class,
"root"
);
private static final ConfigValue<String> PASSWORD = new ConfigValue<>(
"storage.mysql.password", String.class, "password"
"storage.mysql.password", String.class,
"password"
);
private static final DatabaseTable USER_TABLE = new DatabaseTable(
"ub_data",
new String[]{
"`uuid` VARCHAR(36) NOT NULL PRIMARY KEY", // 用户的UUID
"`data` MEDIUMTEXT NOT NULL",// 背包内具体物品
"`day` DATE NOT NULL", // 记录卖出数量的所在天
});
public static final Gson GSON = new Gson();
public static final JsonParser PARSER = new JsonParser();
private static final ConfigValue<String> TABLE_NAME = new ConfigValue<>(
"storage.mysql.table", String.class,
"ud_data"
);
SQLManager sqlManager;
DatabaseTable userDataTable;
@Override
public boolean initialize() {
@ -71,7 +63,17 @@ public class MySQLStorage extends JSONStorage {
try {
UltraDepository.getInstance().log(" 创建插件所需表...");
USER_TABLE.createTable(sqlManager);
this.userDataTable = new DatabaseTable(
TABLE_NAME.getOptional().orElse("ud_data"),
new String[]{
"`uuid` VARCHAR(36) NOT NULL PRIMARY KEY", // 用户的UUID
"`data` MEDIUMTEXT NOT NULL",// 背包内具体物品
"`day` DATE NOT NULL", // 记录卖出数量的所在天
});
getUserDataTable().createTable(sqlManager);
} catch (SQLException exception) {
UltraDepository.getInstance().error("无法创建插件所需的表,请检查数据库权限。");
UltraDepository.getInstance().error("Could not create necessary tables, please check the database privileges.");
@ -90,38 +92,25 @@ public class MySQLStorage extends JSONStorage {
@Override
public @Nullable UserData loadData(@NotNull UUID uuid) throws Exception {
try (SQLQuery query = createAction(uuid).execute()) {
return createAction(uuid).executeFunction((query) -> {
ResultSet resultSet = query.getResultSet();
if (resultSet == null || !resultSet.next()) return null;
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 = UltraDepository.getDepositoryManager().getDepository(entry.getKey());
if (depository == null) continue;
loadDepositoriesInto(data, PARSER.parse(resultSet.getString("data")));
DepositoryData contentsData = parseContentsData(depository, data, entry.getValue());
if (contentsData != null) data.setDepository(contentsData);
}
}
return data;
} catch (Exception exception) {
throw new Exception(exception);
}
});
}
@Override
public void saveUserData(@NotNull UserData data) throws Exception {
getSQLManager().createReplace(USER_TABLE.getTableName())
getSQLManager().createReplace(getUserDataTable().getTableName())
.setColumnNames("uuid", "data", "day")
.setParams(data.getUserUUID(), GSON.toJson(data.serializeToMap()), data.getDate())
.setParams(data.getUserUUID(), serializeDepositories(data), data.getDate())
.execute();
}
@ -129,8 +118,12 @@ public class MySQLStorage extends JSONStorage {
return sqlManager;
}
public DatabaseTable getUserDataTable() {
return userDataTable;
}
private PreparedQueryAction createAction(UUID uuid) {
return USER_TABLE.createQuery(sqlManager)
return getUserDataTable().createQuery(getSQLManager())
.addCondition("uuid", uuid.toString())
.setLimit(1).build();
}

View File

@ -73,7 +73,7 @@ public class YAMLStorage implements DataStorage {
for (String itemTypeID : depositorySection.getKeys(false)) {
DepositoryItem item = depository.getItems().get(getFixedTypeID(itemTypeID));
DepositoryItem item = depository.getItems().get(DataStorage.getFixedTypeID(itemTypeID));
if (item == null) continue;
ConfigurationSection itemSection = depositorySection.getConfigurationSection(itemTypeID);

View File

@ -27,7 +27,8 @@ public class DateIntUtil {
public static Date getDate(int dateInt) {
try {
return (Date) getFormat().parse(Integer.toString(dateInt));
long millis = getFormat().parse(Integer.toString(dateInt)).getTime();
return new java.sql.Date(millis);
} catch (ParseException | NumberFormatException e) {
return new Date(System.currentTimeMillis());
}

View File

@ -29,6 +29,7 @@ storage:
# 数据库驱动路径
driver: "com.mysql.jdbc.Driver"
url: "jdbc:mysql://127.0.0.1:3306/<db-name>"
table: "ud_data" # 插件表名,允许自定义
username: "username"
password: "password"