1
mirror of https://github.com/CarmJos/UltraDepository.git synced 2026-06-05 00:58:22 +08:00

Compare commits

..

4 Commits

Author SHA1 Message Date
carm f001ac0010 [v1.2.3] 版本修复
- [A] 采用新版 EasyPlugin ,修改SQL语句使其更加优雅。
2022-01-08 01:05:59 +08:00
carm 988fe600ce [v1.2.2] 版本修复
- [F] 修复 getDateInt() 可能报错的问题。
2022-01-07 00:51:15 +08:00
carm 52df1863f4 [v1.2.1] 版本更新
- [U] 采用最新版本 EasyPlugin
- [U] 保留插件配置文件原本的注解
2022-01-06 16:21:48 +08:00
carm 4c46af55d0 [v1.2.1] 版本更新
- [U] 采用最新版本 EasyPlugin
- [U] 保留插件配置文件原本的注解
2022-01-06 16:21:29 +08:00
10 changed files with 141 additions and 114 deletions
+27 -17
View File
@@ -9,13 +9,13 @@
<maven.compiler.target>11</maven.compiler.target> <maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding> <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<easyplugin.version>1.1.2</easyplugin.version> <easyplugin.version>1.2.0</easyplugin.version>
</properties> </properties>
<groupId>cc.carm.plugin</groupId> <groupId>cc.carm.plugin</groupId>
<artifactId>ultradepository</artifactId> <artifactId>ultradepository</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<version>1.2.0</version> <version>1.2.3</version>
<name>UltraDepository</name> <name>UltraDepository</name>
<description>超级仓库插件,支持设定不同物品的存储仓库。</description> <description>超级仓库插件,支持设定不同物品的存储仓库。</description>
@@ -92,39 +92,28 @@
<dependencies> <dependencies>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>2.2.1</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-main</artifactId> <artifactId>easyplugin-main</artifactId>
<version>${easyplugin.version}</version> <optional>true</optional>
<scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-configuration</artifactId> <artifactId>easyplugin-configuration</artifactId>
<version>${easyplugin.version}</version> <optional>true</optional>
<scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-gui</artifactId> <artifactId>easyplugin-gui</artifactId>
<version>${easyplugin.version}</version> <optional>true</optional>
<scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-database</artifactId> <artifactId>easyplugin-database</artifactId>
<version>${easyplugin.version}</version> <optional>true</optional>
<scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
@@ -141,6 +130,13 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>2.2.1</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>com.github.MilkBowl</groupId> <groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId> <artifactId>VaultAPI</artifactId>
@@ -157,6 +153,20 @@
</dependencies> </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> <build>
<plugins> <plugins>
<plugin> <plugin>
@@ -38,28 +38,25 @@ public class UltraDepository extends EasyPlugin {
@Override @Override
protected void load() { protected void load() {
instance = this; instance = this;
log("加载配置文件...");
ConfigManager.initConfig();
GUI.initialize(this);
} }
@Override @Override
protected boolean initialize() { protected boolean initialize() {
log("初始化存储方式..."); log("加载配置文件...");
StorageMethod storageMethod = PluginConfig.STORAGE_METHOD.get(); if (!ConfigManager.initialize()) {
if (storageMethod == null) { log("初始化配置文件失败,放弃加载。");
log("初始化存储方式失败,放弃加载");
return false; return false;
} }
storage = storageMethod.createStorage(); log("初始化存储方式...");
StorageMethod storageMethod = PluginConfig.STORAGE_METHOD.getOptional().orElse(StorageMethod.YAML);
log(" 正在使用 " + storageMethod.name() + " 进行数据存储"); log(" 正在使用 " + storageMethod.name() + " 进行数据存储");
storage = storageMethod.createStorage();
if (!storage.initialize()) { if (!storage.initialize()) {
error("存储初始化失败,请检查配置文件。"); error("初始化存储失败,请检查配置文件。");
storage.shutdown();
return false; return false;
} }
@@ -79,6 +76,7 @@ public class UltraDepository extends EasyPlugin {
log("加载仓库管理器..."); log("加载仓库管理器...");
depositoryManager = new DepositoryManager(); depositoryManager = new DepositoryManager();
getDepositoryManager().loadDepositories(); getDepositoryManager().loadDepositories();
GUI.initialize(this);
log("注册监听器..."); log("注册监听器...");
regListener(new UserListener()); regListener(new UserListener());
@@ -117,7 +117,7 @@ public class UserData {
public Date getDate() { public Date getDate() {
return new Date(DateIntUtil.getDateMillis(getDateInt())); return DateIntUtil.getDate(getDateInt());
} }
public int getDateInt() { public int getDateInt() {
@@ -9,12 +9,17 @@ public class ConfigManager {
private static FileConfig pluginConfiguration; private static FileConfig pluginConfiguration;
private static FileConfig messageConfiguration; private static FileConfig messageConfiguration;
public static void initConfig() { public static boolean initialize() {
try {
pluginConfiguration = new FileConfig(UltraDepository.getInstance(), "config.yml"); pluginConfiguration = new FileConfig(UltraDepository.getInstance(), "config.yml");
messageConfiguration = new FileConfig(UltraDepository.getInstance(), "messages.yml"); messageConfiguration = new FileConfig(UltraDepository.getInstance(), "messages.yml");
FileConfig.pluginConfiguration = () -> pluginConfiguration; FileConfig.pluginConfiguration = () -> pluginConfiguration;
FileConfig.messageConfiguration = () -> messageConfiguration; FileConfig.messageConfiguration = () -> messageConfiguration;
return true;
} catch (Exception ex) {
return false;
}
} }
public static FileConfig getPluginConfig() { public static FileConfig getPluginConfig() {
@@ -26,13 +31,21 @@ public class ConfigManager {
} }
public static void reload() { public static void reload() {
try {
getPluginConfig().reload(); getPluginConfig().reload();
getMessageConfig().reload(); getMessageConfig().reload();
} catch (Exception ex) {
ex.printStackTrace();
}
} }
public static void saveConfig() { public static void saveConfig() {
try {
getPluginConfig().save(); getPluginConfig().save();
getMessageConfig().save(); getMessageConfig().save();
} catch (Exception ex) {
ex.printStackTrace();
}
} }
} }
@@ -27,7 +27,7 @@ public interface DataStorage {
* @return 正确ID数据 * @return 正确ID数据
* @since v1.1.6 * @since v1.1.6
*/ */
default String getFixedTypeID(String typeID) { static String getFixedTypeID(String typeID) {
String trueID = typeID; String trueID = typeID;
if (typeID.contains(":")) { if (typeID.contains(":")) {
try { try {
@@ -30,8 +30,8 @@ public class JSONStorage implements DataStorage {
private File dataContainer; private File dataContainer;
public static final Gson GSON = new Gson(); protected static final Gson GSON = new Gson();
public static final JsonParser PARSER = new JsonParser(); protected static final JsonParser PARSER = new JsonParser();
@Override @Override
public boolean initialize() { public boolean initialize() {
@@ -66,18 +66,9 @@ public class JSONStorage implements DataStorage {
JsonObject dataObject = dataElement.getAsJsonObject(); JsonObject dataObject = dataElement.getAsJsonObject();
int dateInt = dataObject.get("date").getAsInt(); int dateInt = dataObject.get("date").getAsInt();
JsonObject repositoriesObject = dataObject.getAsJsonObject("depositories");
UserData userData = new UserData(uuid, new HashMap<>(), dateInt); UserData userData = new UserData(uuid, new HashMap<>(), dateInt);
for (Map.Entry<String, JsonElement> entry : repositoriesObject.entrySet()) { loadDepositoriesInto(userData, dataObject.getAsJsonObject("depositories"));
Depository depository = UltraDepository.getDepositoryManager().getDepository(entry.getKey());
if (depository == null) continue;
DepositoryData contentsData = parseContentsData(depository, userData, entry.getValue());
if (contentsData != null) userData.setDepository(contentsData);
}
return userData; return userData;
} }
@@ -86,7 +77,7 @@ public class JSONStorage implements DataStorage {
public void saveUserData(@NotNull UserData data) throws Exception { public void saveUserData(@NotNull UserData data) throws Exception {
JsonObject dataObject = new JsonObject(); JsonObject dataObject = new JsonObject();
dataObject.addProperty("date", data.getDateInt()); 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")); FileWriter writer = new FileWriter(new File(getDataContainer(), data.getUserUUID() + ".json"));
writer.write(GSON.toJson(dataObject)); writer.write(GSON.toJson(dataObject));
@@ -94,18 +85,36 @@ public class JSONStorage implements DataStorage {
writer.close(); writer.close();
} }
protected DepositoryData parseContentsData(@NotNull Depository source, public static JsonElement saveDepositoriesToJson(UserData data) {
@NotNull UserData owner, return GSON.toJsonTree(data.serializeToMap());
@NotNull JsonElement contentsElement) {
return contentsElement.isJsonObject() ? parseContentsData(source, owner, contentsElement.getAsJsonObject()) : null;
} }
protected DepositoryData parseContentsData(@NotNull Depository source, 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 UserData owner,
@NotNull JsonObject contentsObject) { @NotNull JsonElement contentsElement) {
if (!contentsElement.isJsonObject()) return null;
JsonObject contentsObject = contentsElement.getAsJsonObject();
DepositoryData data = DepositoryData.emptyContents(source, owner); DepositoryData data = DepositoryData.emptyContents(source, owner);
for (Map.Entry<String, JsonElement> entry : contentsObject.entrySet()) { 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; if (item == null) continue;
DepositoryItemData itemData = parseItemData(item, data, entry.getValue()); DepositoryItemData itemData = parseItemData(item, data, entry.getValue());
@@ -115,15 +124,13 @@ public class JSONStorage implements DataStorage {
return data; return data;
} }
protected DepositoryItemData parseItemData(@NotNull DepositoryItem source,
public static DepositoryItemData parseItemData(@NotNull DepositoryItem source,
@NotNull DepositoryData owner, @NotNull DepositoryData owner,
@NotNull JsonElement itemElement) { @NotNull JsonElement itemElement) {
return itemElement.isJsonObject() ? parseItemData(source, owner, itemElement.getAsJsonObject()) : null; if (!itemElement.isJsonObject()) return null;
} JsonObject itemObject = itemElement.getAsJsonObject();
protected DepositoryItemData parseItemData(@NotNull DepositoryItem source,
@NotNull DepositoryData owner,
@NotNull JsonObject itemObject) {
int amount = itemObject.has("amount") ? itemObject.get("amount").getAsInt() : 0; int amount = itemObject.has("amount") ? itemObject.get("amount").getAsInt() : 0;
int sold = itemObject.has("sold") ? itemObject.get("sold").getAsInt() : 0; int sold = itemObject.has("sold") ? itemObject.get("sold").getAsInt() : 0;
if (amount == 0 && sold == 0) return null; if (amount == 0 && sold == 0) return null;
@@ -5,15 +5,9 @@ import cc.carm.lib.easyplugin.database.DatabaseTable;
import cc.carm.lib.easyplugin.database.EasySQL; import cc.carm.lib.easyplugin.database.EasySQL;
import cc.carm.lib.easyplugin.database.api.SQLManager; 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.PreparedQueryAction;
import cc.carm.lib.easyplugin.database.api.action.query.SQLQuery;
import cc.carm.plugin.ultradepository.UltraDepository; 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.data.UserData;
import cc.carm.plugin.ultradepository.util.DateIntUtil; 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.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -21,39 +15,37 @@ import java.sql.Date;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
public class MySQLStorage extends JSONStorage { public class MySQLStorage extends JSONStorage {
private static final ConfigValue<String> DRIVER_NAME = new ConfigValue<>( 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<>( 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<>( 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<>( 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( private static final ConfigValue<String> TABLE_NAME = new ConfigValue<>(
"ub_data", "storage.mysql.table", String.class,
new String[]{ "ud_data"
"`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();
SQLManager sqlManager; SQLManager sqlManager;
DatabaseTable userDataTable;
@Override @Override
public boolean initialize() { public boolean initialize() {
@@ -71,7 +63,17 @@ public class MySQLStorage extends JSONStorage {
try { try {
UltraDepository.getInstance().log(" 创建插件所需表..."); 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) { } catch (SQLException exception) {
UltraDepository.getInstance().error("无法创建插件所需的表,请检查数据库权限。"); UltraDepository.getInstance().error("无法创建插件所需的表,请检查数据库权限。");
UltraDepository.getInstance().error("Could not create necessary tables, please check the database privileges."); UltraDepository.getInstance().error("Could not create necessary tables, please check the database privileges.");
@@ -90,38 +92,25 @@ public class MySQLStorage extends JSONStorage {
@Override @Override
public @Nullable UserData loadData(@NotNull UUID uuid) throws Exception { public @Nullable UserData loadData(@NotNull UUID uuid) throws Exception {
try (SQLQuery query = createAction(uuid).execute()) { return createAction(uuid).executeFunction((query) -> {
ResultSet resultSet = query.getResultSet(); ResultSet resultSet = query.getResultSet();
if (resultSet == null || !resultSet.next()) return null; if (resultSet == null || !resultSet.next()) return null;
String dataJSON = resultSet.getString("data");
Date date = resultSet.getDate("day"); Date date = resultSet.getDate("day");
UserData data = new UserData(uuid, new HashMap<>(), DateIntUtil.getDateInt(date)); UserData data = new UserData(uuid, new HashMap<>(), DateIntUtil.getDateInt(date));
JsonElement dataElement = PARSER.parse(dataJSON); loadDepositoriesInto(data, PARSER.parse(resultSet.getString("data")));
if (dataElement.isJsonObject()) {
for (Map.Entry<String, JsonElement> entry : dataElement.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);
}
}
return data; return data;
});
} catch (Exception exception) {
throw new Exception(exception);
}
} }
@Override @Override
public void saveUserData(@NotNull UserData data) throws Exception { public void saveUserData(@NotNull UserData data) throws Exception {
getSQLManager().createReplace(USER_TABLE.getTableName()) getSQLManager().createReplace(getUserDataTable().getTableName())
.setColumnNames("uuid", "data", "day") .setColumnNames("uuid", "data", "day")
.setParams(data.getUserUUID(), GSON.toJson(data.serializeToMap()), data.getDate()) .setParams(data.getUserUUID(), serializeDepositories(data), data.getDate())
.execute(); .execute();
} }
@@ -129,8 +118,12 @@ public class MySQLStorage extends JSONStorage {
return sqlManager; return sqlManager;
} }
public DatabaseTable getUserDataTable() {
return userDataTable;
}
private PreparedQueryAction createAction(UUID uuid) { private PreparedQueryAction createAction(UUID uuid) {
return USER_TABLE.createQuery(sqlManager) return getUserDataTable().createQuery(getSQLManager())
.addCondition("uuid", uuid.toString()) .addCondition("uuid", uuid.toString())
.setLimit(1).build(); .setLimit(1).build();
} }
@@ -73,7 +73,7 @@ public class YAMLStorage implements DataStorage {
for (String itemTypeID : depositorySection.getKeys(false)) { 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; if (item == null) continue;
ConfigurationSection itemSection = depositorySection.getConfigurationSection(itemTypeID); ConfigurationSection itemSection = depositorySection.getConfigurationSection(itemTypeID);
@@ -22,10 +22,15 @@ public class DateIntUtil {
} }
public static long getDateMillis(int dateInt) { public static long getDateMillis(int dateInt) {
return getDate(dateInt).getTime();
}
public static Date getDate(int dateInt) {
try { try {
return getFormat().parse(Integer.toString(dateInt)).getTime(); long millis = getFormat().parse(Integer.toString(dateInt)).getTime();
} catch (ParseException e) { return new java.sql.Date(millis);
return System.currentTimeMillis(); } catch (ParseException | NumberFormatException e) {
return new Date(System.currentTimeMillis());
} }
} }
+1
View File
@@ -29,6 +29,7 @@ storage:
# 数据库驱动路径 # 数据库驱动路径
driver: "com.mysql.jdbc.Driver" driver: "com.mysql.jdbc.Driver"
url: "jdbc:mysql://127.0.0.1:3306/<db-name>" url: "jdbc:mysql://127.0.0.1:3306/<db-name>"
table: "ud_data" # 插件表名,允许自定义
username: "username" username: "username"
password: "password" password: "password"