1
mirror of https://github.com/CarmJos/UltraDepository.git synced 2026-06-04 16:48:21 +08:00

Compare commits

..

6 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
carm 123ae0b039 [v1.2.0] 版本更新
- `[U]` 采用最新版本 EasyPlugin
- `[U]` 令安静模式的权限默认不生效。
2022-01-06 14:22:42 +08:00
carm dd5793427a 指定只获取storageMethod的name 2022-01-06 00:15:00 +08:00
14 changed files with 160 additions and 131 deletions
+27 -20
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.0</easyplugin.version>
<easyplugin.version>1.2.0</easyplugin.version>
</properties>
<groupId>cc.carm.plugin</groupId>
<artifactId>ultradepository</artifactId>
<packaging>jar</packaging>
<version>1.1.8</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>
@@ -261,9 +271,6 @@
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>LICENSE</include>
</includes>
</resource>
</resources>
</build>
@@ -36,30 +36,27 @@ public class UltraDepository extends EasyPlugin {
}
@Override
public void load() {
protected void load() {
instance = this;
log("加载配置文件...");
ConfigManager.initConfig();
GUI.initialize(this);
}
@Override
public boolean initialize() {
protected boolean initialize() {
log("初始化存储方式...");
StorageMethod storageMethod = PluginConfig.STORAGE_METHOD.get();
if (storageMethod == null) {
log("初始化存储方式失败,放弃加载");
log("加载配置文件...");
if (!ConfigManager.initialize()) {
log("初始化配置文件失败,放弃加载。");
return false;
}
storage = storageMethod.createStorage();
log("初始化存储方式...");
StorageMethod storageMethod = PluginConfig.STORAGE_METHOD.getOptional().orElse(StorageMethod.YAML);
log(" 正在使用 " + storageMethod.name() + " 进行数据存储");
storage = storageMethod.createStorage();
if (!storage.initialize()) {
error("存储初始化失败,请检查配置文件。");
error("初始化存储失败,请检查配置文件。");
storage.shutdown();
return false;
}
@@ -79,6 +76,7 @@ public class UltraDepository extends EasyPlugin {
log("加载仓库管理器...");
depositoryManager = new DepositoryManager();
getDepositoryManager().loadDepositories();
GUI.initialize(this);
log("注册监听器...");
regListener(new UserListener());
@@ -101,7 +99,7 @@ public class UltraDepository extends EasyPlugin {
"active_depositories",
() -> getDepositoryManager().getDepositories().size())
);
metrics.addCustomChart(new SimplePie("storage_method", () -> getStorage().getClass().getSimpleName()));
metrics.addCustomChart(new SimplePie("storage_method", storageMethod::name));
metrics.addCustomChart(new SimplePie("economy_enabled", () -> economyManager.isInitialized() ? "YES" : "NO"));
metrics.addCustomChart(new SimplePie("papi_version", () -> {
Plugin plugin = Bukkit.getPluginManager().getPlugin("PlaceholderAPI");
@@ -113,7 +111,7 @@ public class UltraDepository extends EasyPlugin {
}
@Override
public void shutdown() {
protected void shutdown() {
if (!isInitialized()) return;
log("保存现有用户数据...");
@@ -1,13 +1,14 @@
package cc.carm.plugin.ultradepository.configuration;
import cc.carm.lib.easyplugin.configuration.cast.ConfigStringCast;
import cc.carm.lib.easyplugin.configuration.impl.ConfigSound;
import cc.carm.lib.easyplugin.configuration.impl.ConfigStringCast;
import cc.carm.lib.easyplugin.configuration.message.ConfigMessage;
import cc.carm.lib.easyplugin.configuration.message.ConfigMessageList;
import cc.carm.lib.easyplugin.configuration.values.ConfigValue;
import cc.carm.plugin.ultradepository.manager.ConfigManager;
import cc.carm.plugin.ultradepository.storage.StorageMethod;
import org.bukkit.Material;
import org.bukkit.Sound;
public class PluginConfig {
@@ -44,7 +45,7 @@ public class PluginConfig {
public static class Sounds {
public static final ConfigSound COLLECT = new ConfigSound("sounds.collect");
public static final ConfigSound COLLECT = new ConfigSound("sounds.collect", Sound.ENTITY_VILLAGER_CELEBRATE);
public static final ConfigSound SELL_SUCCESS = new ConfigSound("sounds.sell-success");
public static final ConfigSound SELL_FAIL = new ConfigSound("sounds.sell-fail");
public static final ConfigSound GUI_CLICK = new ConfigSound("sounds.gui-click");
@@ -117,7 +117,7 @@ public class UserData {
public Date getDate() {
return new Date(DateIntUtil.getDateMillis(getDateInt()));
return DateIntUtil.getDate(getDateInt());
}
public int getDateInt() {
@@ -9,12 +9,17 @@ public class ConfigManager {
private static FileConfig pluginConfiguration;
private static FileConfig messageConfiguration;
public static void initConfig() {
pluginConfiguration = new FileConfig(UltraDepository.getInstance(), "config.yml");
messageConfiguration = new FileConfig(UltraDepository.getInstance(), "messages.yml");
public static boolean initialize() {
try {
pluginConfiguration = new FileConfig(UltraDepository.getInstance(), "config.yml");
messageConfiguration = new FileConfig(UltraDepository.getInstance(), "messages.yml");
FileConfig.pluginConfiguration = () -> pluginConfiguration;
FileConfig.messageConfiguration = () -> messageConfiguration;
FileConfig.pluginConfiguration = () -> pluginConfiguration;
FileConfig.messageConfiguration = () -> messageConfiguration;
return true;
} catch (Exception ex) {
return false;
}
}
public static FileConfig getPluginConfig() {
@@ -26,13 +31,21 @@ public class ConfigManager {
}
public static void reload() {
getPluginConfig().reload();
getMessageConfig().reload();
try {
getPluginConfig().reload();
getMessageConfig().reload();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void saveConfig() {
getPluginConfig().save();
getMessageConfig().save();
try {
getPluginConfig().save();
getMessageConfig().save();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
@@ -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 {
@@ -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;
@@ -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();
}
@@ -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);
@@ -42,12 +42,12 @@ public class DepositoryGUI extends GUI {
private GUIItem createGUIItem(DepositoryItem item) {
DepositoryItemData itemData = userData.getItemData(item);
int remain = item.getLimit() - itemData.getSold();
int canSell = item.getLimit() - itemData.getSold();
ItemStackFactory factory = new ItemStackFactory(item.getDisplayItem());
List<String> additionalLore = PluginConfig.General.ADDITIONAL_LORE.get(player, new Object[]{
item.getName(), itemData.getAmount(), item.getPrice(),
itemData.getSold(), remain, item.getLimit()
itemData.getSold(), canSell, item.getLimit()
});
additionalLore.forEach(factory::addLore);
@@ -65,10 +65,12 @@ public class DepositoryGUI extends GUI {
}
if (type == ClickType.LEFT) {
if (remain >= 1) {
if (canSell >= 1) {
SellItemGUI.open(player, userData, itemData, depository, item);
} else {
PluginMessages.ITEM_SOLD_LIMIT.send(player, new Object[]{remain, item.getLimit()});
PluginConfig.Sounds.SELL_FAIL.play(player);
PluginMessages.ITEM_SOLD_LIMIT.send(player, new Object[]{canSell, item.getLimit()});
player.closeInventory();
}
} else if (type == ClickType.RIGHT) {
@@ -11,6 +11,7 @@ import cc.carm.plugin.ultradepository.configuration.depository.Depository;
import cc.carm.plugin.ultradepository.configuration.depository.DepositoryItem;
import cc.carm.plugin.ultradepository.data.DepositoryItemData;
import cc.carm.plugin.ultradepository.data.UserData;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
@@ -73,7 +74,7 @@ public class SellItemGUI extends GUI {
}
private GUIItem getAddItem(int amount) {
ItemStackFactory factory = new ItemStackFactory(Add.TYPE.get());
ItemStackFactory factory = new ItemStackFactory(Add.TYPE.getOptional().orElse(Material.STONE));
factory.setDurability(Add.DATA.get());
factory.setDisplayName(Add.NAME.get(player, new Object[]{
getItemName(), amount
@@ -93,7 +94,7 @@ public class SellItemGUI extends GUI {
}
private GUIItem getRemoveItem(int amount) {
ItemStackFactory factory = new ItemStackFactory(Remove.TYPE.get());
ItemStackFactory factory = new ItemStackFactory(Remove.TYPE.getOptional().orElse(Material.STONE));
factory.setDurability(Remove.DATA.get());
factory.setDisplayName(Remove.NAME.get(player, new Object[]{
getItemName(), amount
@@ -112,7 +113,7 @@ public class SellItemGUI extends GUI {
}
private GUIItem getConfirmItem() {
ItemStackFactory factory = new ItemStackFactory(Confirm.TYPE.get());
ItemStackFactory factory = new ItemStackFactory(Confirm.TYPE.getOptional().orElse(Material.STONE));
factory.setDurability(Confirm.DATA.get());
factory.setDisplayName(Confirm.NAME.get(player, new Object[]{
getItemName(), getCurrentAmount(), getTotalMoney()
@@ -131,7 +132,7 @@ public class SellItemGUI extends GUI {
}
private GUIItem getCancelItem() {
ItemStackFactory factory = new ItemStackFactory(Cancel.TYPE.get());
ItemStackFactory factory = new ItemStackFactory(Cancel.TYPE.getOptional().orElse(Material.STONE));
factory.setDurability(Cancel.DATA.get());
factory.setDisplayName(Cancel.NAME.get());
factory.setLore(Cancel.LORE.get());
@@ -22,10 +22,15 @@ public class DateIntUtil {
}
public static long getDateMillis(int dateInt) {
return getDate(dateInt).getTime();
}
public static Date getDate(int dateInt) {
try {
return getFormat().parse(Integer.toString(dateInt)).getTime();
} catch (ParseException e) {
return System.currentTimeMillis();
long millis = getFormat().parse(Integer.toString(dateInt)).getTime();
return new java.sql.Date(millis);
} catch (ParseException | NumberFormatException e) {
return new Date(System.currentTimeMillis());
}
}
+1
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"
+1
View File
@@ -32,6 +32,7 @@ permissions:
"UltraDepository.silent":
description: "超级仓库的安静模式权限,拥有该权限将不再接收到放入背包的提示。"
default: false
"UltraDepository.auto":
description: "超级仓库的自动收集权限"