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

完成配置部分,增添数个变量,以便GUI使用。

This commit is contained in:
Carm Jos 2021-12-29 01:31:51 +08:00
parent 10ca626304
commit c71041cb13
15 changed files with 330 additions and 229 deletions

View File

@ -51,35 +51,39 @@
<summary>展开查看所有子指令</summary>
```text
# sell <背包ID> <物品ID> <数量>
# open [仓库ID]
@ 玩家指令 (UltraDepository.use)
- 打开对应仓库的界面。
# sell <仓库ID> <物品ID> <数量>
@ 玩家指令 (UltraDepository.Command.Sell)
- 售出对应数量的对应物品。
- 该指令受到玩家每日售出数量的限制。
# sellAll [背包ID] [物品ID]
# sellAll [仓库ID] [物品ID]
@ 玩家指令 (UltraDepository.Command.SellAll)
- 售出所有相关物品。
- 该指令受到玩家每日售出数量的限制。
# info <玩家> [背包ID] [物品ID]
# info <玩家> [仓库ID] [物品ID]
@ 管理指令 (UltraDepository.admin)
- 得到玩家的相关物品信息。
# add <玩家> <背包ID> <物品ID> <数量>
# add <玩家> <仓库ID> <物品ID> <数量>
@ 管理指令 (UltraDepository.admin)
- 为玩家添加对应背包中对于物品的数量。
- 为玩家添加对应仓库中对于物品的数量。
# remove <玩家> <背包ID> <物品ID> <数量>
# remove <玩家> <仓库ID> <物品ID> <数量>
@ 管理指令 (UltraDepository.admin)
- 为玩家减少对应背包中对于物品的数量。
- 为玩家减少对应仓库中对于物品的数量。
# sell <玩家> [背包ID] [物品ID] [数量]
# sell <玩家> [仓库ID] [物品ID] [数量]
@ 管理指令 (UltraDepository.admin)
- 为玩家售出相关物品。
- 若不填写数量,则售出所有对应背包的对应物品。
- 若不填写物品,则售出对应背包内所有物品。
- 若不填写背包,则售出所有背包内所有物品。
- 若不填写数量,则售出所有对应仓库的对应物品。
- 若不填写物品,则售出对应仓库内所有物品。
- 若不填写仓库,则售出所有仓库内所有物品。
- 该指令受到玩家每日售出数量的限制。
```
@ -93,24 +97,31 @@
<summary>展开查看所有变量</summary>
```text
# %UltraDepository_amount_<背包ID>_<物品ID>%
- 得到对应背包内对应物品的数量
# %UltraDepository_amount_<仓库ID>_<物品ID>%
- 得到对应仓库内对应物品的数量
# %UltraDepository_price_<背包ID>_<物品ID>%
- 得到对应背包内对应物品的价格
# %UltraDepository_price_<仓库ID>_<物品ID>%
- 得到对应仓库内对应物品的价格
# %UltraDepository_sold_<背包ID>_<物品ID>%
- 得到对应背包内对应物品的今日售出数量
# %UltraDepository_sold_<仓库ID>_<物品ID>%
- 得到对应仓库内对应物品的今日售出数量
# %UltraDepository_limit_<背包ID>_<物品ID>%
- 得到对应背包内对应物品的每日售出限制
# %UltraDepository_limit_<仓库ID>_<物品ID>%
- 得到对应仓库内对应物品的每日售出限制
# %UltraDepository_remain_<背包ID>_<物品ID>%
- 得到对应背包内对应物品的剩余可售出数量
# %UltraDepository_remain_<仓库ID>_<物品ID>%
- 得到对应仓库内对应物品的剩余可售出数量
- $剩余可售出数量 = $每日售出限制 - $今日售出数量
# %UltraDepository_capacity_<背包ID>%
- 得到对应背包的容量
# %UltraDepository_capacity_<仓库ID>%
- 得到对应仓库的容量
# %UltraDepository_used_<仓库ID>%
- 得到已使用的仓库容量
# %UltraDepository_usable_<仓库ID>%
- 得到剩余可使用的仓库容量
```
</details>

View File

@ -4,8 +4,8 @@ import cc.carm.plugin.ultradepository.configuration.PluginConfig;
import cc.carm.plugin.ultradepository.hooker.PAPIExpansion;
import cc.carm.plugin.ultradepository.listener.CollectListener;
import cc.carm.plugin.ultradepository.listener.UserListener;
import cc.carm.plugin.ultradepository.manager.DepositoryManager;
import cc.carm.plugin.ultradepository.manager.ConfigManager;
import cc.carm.plugin.ultradepository.manager.DepositoryManager;
import cc.carm.plugin.ultradepository.manager.EconomyManager;
import cc.carm.plugin.ultradepository.manager.UserManager;
import cc.carm.plugin.ultradepository.storage.DataStorage;
@ -72,9 +72,9 @@ public class Main extends JavaPlugin {
log(" &7[-] 检测到未安装Vault关闭出售功能。");
}
log("加载背包管理器...");
log("加载仓库管理器...");
depositoryManager = new DepositoryManager();
getDepositoryManager().loadDepositories();
log("注册监听器...");
regListener(new UserListener());
@ -100,7 +100,7 @@ public class Main extends JavaPlugin {
long startTime = System.currentTimeMillis();
log("保存现有用户数据...");
getUserManager().saveAll();
log("释放存储源...");
getStorage().shutdown();

View File

@ -11,18 +11,23 @@ import java.util.Objects;
public class Depository {
final String identifier;
private final @NotNull String identifier;
String name;
GUIConfiguration guiConfiguration;
DepositoryCapacity capacity;
private final @NotNull String name;
private final @NotNull GUIConfiguration guiConfiguration;
private final @NotNull DepositoryCapacity capacity;
Map<String, DepositoryItem> items;
private Map<String, DepositoryItem> items;
public Depository(@NotNull String identifier, @NotNull String name,
@NotNull GUIConfiguration guiConfiguration,
@NotNull DepositoryCapacity capacity) {
this(identifier, name, guiConfiguration, capacity, new HashMap<>());
}
public Depository(String identifier, String name,
GUIConfiguration guiConfiguration,
DepositoryCapacity capacity,
public Depository(@NotNull String identifier, @NotNull String name,
@NotNull GUIConfiguration guiConfiguration,
@NotNull DepositoryCapacity capacity,
Map<String, DepositoryItem> items) {
this.identifier = identifier;
this.name = name;
@ -66,24 +71,24 @@ public class Depository {
}
public static Depository loadFrom(String identifier, FileConfiguration configuration) {
return new Depository(
identifier, configuration.getString("name"),
Depository depository = new Depository(identifier, configuration.getString("name", identifier),
GUIConfiguration.readConfiguration(configuration.getConfigurationSection("gui")),
new DepositoryCapacity(
configuration.getInt("capacity.default", 0),
configuration.getStringList("capacity.permissions")
),
readItems(configuration.getConfigurationSection("items"))
)
);
depository.items = readItems(depository, configuration.getConfigurationSection("items"));
return depository;
}
private static Map<String, DepositoryItem> readItems(ConfigurationSection section) {
private static Map<String, DepositoryItem> readItems(Depository depository, ConfigurationSection section) {
if (section == null) return new HashMap<>();
Map<String, DepositoryItem> items = new HashMap<>();
for (String key : section.getKeys(false)) {
ConfigurationSection itemSection = section.getConfigurationSection(key);
if (itemSection != null) {
items.put(key, DepositoryItem.readFrom(key, itemSection));
items.put(key, DepositoryItem.readFrom(depository, key, itemSection));
}
}
return items;

View File

@ -13,6 +13,8 @@ import java.util.Objects;
public class DepositoryItem {
final Depository depository;
final @NotNull Material material;
final int data;
@ -24,9 +26,11 @@ public class DepositoryItem {
@Nullable String name;
@Nullable List<String> lore;
public DepositoryItem(@NotNull Material material, int data,
public DepositoryItem(@NotNull Depository depository,
@NotNull Material material, int data,
int slot, double price, int limit,
@Nullable String name, @Nullable List<String> lore) {
this.depository = depository;
this.material = material;
this.data = data;
this.slot = slot;
@ -36,6 +40,10 @@ public class DepositoryItem {
this.lore = lore;
}
public Depository getDepository() {
return depository;
}
public @NotNull String getTypeID() {
return getMaterial().name() + ":" + getData();
}
@ -92,7 +100,7 @@ public class DepositoryItem {
return Objects.hash(material, data);
}
public static DepositoryItem readFrom(String typeID, ConfigurationSection section) {
public static DepositoryItem readFrom(Depository depository, String typeID, ConfigurationSection section) {
try {
Material material;
int data = 0;
@ -106,7 +114,7 @@ public class DepositoryItem {
if (material == null) throw new NullPointerException(typeID);
return new DepositoryItem(
material, data,
depository, material, data,
section.getInt("slot", 0),
section.getDouble("price", 0),
section.getInt("limit", 0),

View File

@ -1,32 +1,58 @@
package cc.carm.plugin.ultradepository.data;
import cc.carm.plugin.ultradepository.configuration.depository.Depository;
import cc.carm.plugin.ultradepository.configuration.depository.DepositoryItem;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.Objects;
public class DepositoryData {
private final Map<@NotNull String, @NotNull ItemData> contents;
final UserData owner;
final Depository source;
private final Map<@NotNull String, @NotNull DepositoryItemData> contents;
public DepositoryData(Map<@NotNull String, @NotNull ItemData> contents) {
public DepositoryData(@NotNull Depository source, @NotNull UserData owner,
Map<@NotNull String, @NotNull DepositoryItemData> contents) {
this.owner = owner;
this.source = source;
this.contents = contents;
}
public @NotNull Map<String, ItemData> getContents() {
public String getIdentifier() {
return getSource().getIdentifier();
}
public Depository getSource() {
return source;
}
public @NotNull Map<String, DepositoryItemData> getContents() {
return this.contents;
}
public @Nullable ItemData getItemData(@NotNull String itemType) {
return getContents().get(itemType);
public @Nullable DepositoryItemData getItemData(@NotNull String itemType) {
DepositoryItem item = getSource().getItems().get(itemType);
if (item != null) {
return getContents().putIfAbsent(item.getTypeID(), DepositoryItemData.emptyItemData(item, this));
} else {
return null;
}
}
public static DepositoryData emptyContents(Set<String> itemTypes) {
return new DepositoryData(itemTypes.stream().collect(Collectors.toMap(
item -> item, item -> ItemData.emptyItemData(), (a, b) -> b
)));
public @NotNull DepositoryItemData getItemData(@NotNull DepositoryItem item) {
return Objects.requireNonNull(getContents().putIfAbsent(item.getTypeID(), DepositoryItemData.emptyItemData(item, this)));
}
public int getUsedCapacity() {
return getContents().values().stream().mapToInt(DepositoryItemData::getAmount).sum();
}
public static DepositoryData emptyContents(Depository source, UserData owner) {
return new DepositoryData(source, owner, new HashMap<>());
}
}

View File

@ -0,0 +1,62 @@
package cc.carm.plugin.ultradepository.data;
import cc.carm.plugin.ultradepository.configuration.depository.DepositoryItem;
public class DepositoryItemData {
final DepositoryItem source;
final DepositoryData owner;
int amount;
int sold;
public DepositoryItemData(DepositoryItem source, DepositoryData owner,
int amount, int sold) {
this.owner = owner;
this.source = source;
this.amount = amount;
this.sold = sold;
}
public DepositoryData getOwner() {
return owner;
}
public DepositoryItem getSource() {
return source;
}
public int getAmount() {
return amount;
}
public int getSold() {
return sold;
}
public void setAmount(int amount) {
this.amount = Math.max(0, amount);
}
public void setSold(int sold) {
this.sold = Math.max(0, sold);
}
public void clearSold() {
this.sold = 0;
}
public static DepositoryItemData emptyItemData(DepositoryItem source, DepositoryData owner) {
return new DepositoryItemData(source, owner, 0, 0);
}
@Override
public String toString() {
return "UBItemData{" +
"amount=" + amount +
", sold=" + sold +
'}';
}
}

View File

@ -1,44 +0,0 @@
package cc.carm.plugin.ultradepository.data;
public class ItemData {
int amount;
int sold;
public ItemData(int amount, int sold) {
this.amount = amount;
this.sold = sold;
}
public int getAmount() {
return amount;
}
public int getSold() {
return sold;
}
public void setAmount(int amount) {
this.amount = Math.max(0, amount);
}
public void setSold(int sold) {
this.sold = Math.max(0, sold);
}
public void clearSold() {
this.sold = 0;
}
public static ItemData emptyItemData() {
return new ItemData(0, 0);
}
@Override
public String toString() {
return "UBItemData{" +
"amount=" + amount +
", sold=" + sold +
'}';
}
}

View File

@ -1,15 +1,15 @@
package cc.carm.plugin.ultradepository.data;
import cc.carm.plugin.ultradepository.Main;
import cc.carm.plugin.ultradepository.configuration.depository.DepositoryItem;
import cc.carm.plugin.ultradepository.configuration.depository.Depository;
import cc.carm.plugin.ultradepository.configuration.depository.DepositoryItem;
import cc.carm.plugin.ultradepository.storage.DataStorage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.sql.Date;
import java.util.Map;
import java.util.Set;
import java.util.Objects;
import java.util.UUID;
public class UserData {
@ -17,15 +17,15 @@ public class UserData {
public final UUID userUUID;
DataStorage storage;
Map<String, DepositoryData> backpacks;
Map<String, DepositoryData> depositories;
Date day;
public UserData(UUID userUUID, DataStorage storage,
Map<String, DepositoryData> backpacks, Date day) {
Map<String, DepositoryData> depositories, Date day) {
this.userUUID = userUUID;
this.storage = storage;
this.backpacks = backpacks;
this.depositories = depositories;
this.day = day;
}
@ -34,105 +34,88 @@ public class UserData {
return this.userUUID;
}
public @NotNull Map<String, DepositoryData> getBackpacks() {
return this.backpacks;
public @NotNull Map<String, DepositoryData> getDepositories() {
return this.depositories;
}
public @Nullable DepositoryData getBackpackData(String backpackID) {
Depository configuration = Main.getDepositoryManager().getDepository(backpackID);
if (configuration == null) return null;
return getBackpackData(configuration);
public void setDepository(DepositoryData data) {
this.depositories.put(data.getIdentifier(), data);
}
public @NotNull DepositoryData getBackpackData(Depository backpack) {
if (!getBackpacks().containsKey(backpack.getIdentifier())) {
getBackpacks().put(backpack.getIdentifier(), DepositoryData.emptyContents(backpack.getItems().keySet()));
}
return getBackpacks().get(backpack.getIdentifier());
public @Nullable DepositoryData getDepositoryData(String depositoryID) {
Depository depository = Main.getDepositoryManager().getDepository(depositoryID);
if (depository == null) return null;
return getDepositoryData(depository);
}
public @NotNull Set<String> getBackpackIDs() {
return getBackpacks().keySet();
public @NotNull DepositoryData getDepositoryData(Depository depository) {
return Objects.requireNonNull(getDepositories().putIfAbsent(
depository.getIdentifier(),
DepositoryData.emptyContents(depository, this))
);
}
public @Nullable ItemData getItemData(@NotNull String backpackID, @NotNull String typeID) {
DepositoryData data = getBackpackData(backpackID);
public @Nullable DepositoryItemData getItemData(@NotNull String depositoryID, @NotNull String typeID) {
DepositoryData data = getDepositoryData(depositoryID);
if (data == null) return null;
if (!Main.getDepositoryManager().hasItem(backpackID, typeID)) return null;
ItemData itemData = data.getItemData(typeID);
if (itemData == null) {
itemData = ItemData.emptyItemData();
data.getContents().put(typeID, itemData);
}
return itemData;
return data.getItemData(typeID);
}
public @NotNull ItemData getItemData(@NotNull Depository backpack, @NotNull DepositoryItem itemType) {
DepositoryData data = getBackpackData(backpack);
ItemData itemData = data.getItemData(itemType.getTypeID());
if (itemData == null) {
itemData = ItemData.emptyItemData();
data.getContents().put(itemType.getTypeID(), itemData);
}
return itemData;
public @NotNull DepositoryItemData getItemData(@NotNull DepositoryItem itemType) {
return getDepositoryData(itemType.getDepository()).getItemData(itemType);
}
public @Nullable Integer getItemAmount(@NotNull String backpackID, @NotNull String typeID) {
ItemData data = getItemData(backpackID, typeID);
public @Nullable Integer getItemAmount(@NotNull String depositoryID, @NotNull String typeID) {
DepositoryItemData data = getItemData(depositoryID, typeID);
if (data == null) return null;
return data.getAmount();
}
public @Nullable Integer getItemSold(@NotNull String backpackID, @NotNull String typeID) {
public @Nullable Integer getItemSold(@NotNull String depositoryID, @NotNull String typeID) {
checkoutDate();
ItemData data = getItemData(backpackID, typeID);
DepositoryItemData data = getItemData(depositoryID, typeID);
if (data == null) return null;
return data.getSold();
}
public @Nullable Integer setItemAmount(@NotNull String backpackID, @NotNull String typeID, int amount) {
ItemData data = getItemData(backpackID, typeID);
public @Nullable Integer setItemAmount(@NotNull String depositoryID, @NotNull String typeID, int amount) {
DepositoryItemData data = getItemData(depositoryID, typeID);
if (data == null) return null;
data.setAmount(amount);
return amount;
}
public @Nullable Integer setItemSold(@NotNull String backpackID, @NotNull String typeID, int soldAmount) {
ItemData data = getItemData(backpackID, typeID);
public @Nullable Integer setItemSold(@NotNull String depositoryID, @NotNull String typeID, int soldAmount) {
DepositoryItemData data = getItemData(depositoryID, typeID);
if (data == null) return null;
data.setSold(soldAmount);
return soldAmount;
}
public @Nullable Integer addItemAmount(@NotNull String backpackID, @NotNull String typeID, int amount) {
Integer current = getItemAmount(backpackID, typeID);
public @Nullable Integer addItemAmount(@NotNull String depositoryID, @NotNull String typeID, int amount) {
Integer current = getItemAmount(depositoryID, typeID);
if (current == null) return null;
return setItemAmount(backpackID, typeID, current + amount);
return setItemAmount(depositoryID, typeID, current + amount);
}
public @Nullable Integer addItemSold(@NotNull String backpackID, @NotNull String typeID, int amount) {
Integer current = getItemSold(backpackID, typeID);
public @Nullable Integer addItemSold(@NotNull String depositoryID, @NotNull String typeID, int amount) {
Integer current = getItemSold(depositoryID, typeID);
if (current == null) return null;
return setItemSold(backpackID, typeID, current + amount);
return setItemSold(depositoryID, typeID, current + amount);
}
public @Nullable Integer removeItemAmount(@NotNull String backpackID, @NotNull String typeID, int amount) {
return addItemAmount(backpackID, typeID, -amount);
public @Nullable Integer removeItemAmount(@NotNull String depositoryID, @NotNull String typeID, int amount) {
return addItemAmount(depositoryID, typeID, -amount);
}
public @Nullable Integer removeItemSold(@NotNull String backpackID, @NotNull String typeID, int amount) {
return addItemSold(backpackID, typeID, -amount);
public @Nullable Integer removeItemSold(@NotNull String depositoryID, @NotNull String typeID, int amount) {
return addItemSold(depositoryID, typeID, -amount);
}
@ -149,9 +132,9 @@ public class UserData {
public void checkoutDate() {
if (isCurrentDay()) return;
this.day = new Date(System.currentTimeMillis()); //更新日期
getBackpacks().values().stream()
getDepositories().values().stream()
.flatMap(value -> value.getContents().values().stream())
.forEach(ItemData::clearSold);
.forEach(DepositoryItemData::clearSold);
}

View File

@ -1,6 +1,10 @@
package cc.carm.plugin.ultradepository.hooker;
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.data.DepositoryItemData;
import cc.carm.plugin.ultradepository.data.UserData;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@ -15,7 +19,9 @@ public class PAPIExpansion extends PlaceholderExpansion {
"%UltraDepository_sold_<BackpackID>_<ItemTypeID>%",
"%UltraDepository_price_<BackpackID>_<ItemTypeID>%",
"%UltraDepository_remain_<BackpackID>_<ItemTypeID>%",
"%UltraDepository_capacity_<BackpackID>%"
"%UltraDepository_capacity_<BackpackID>%",
"%UltraDepository_used_<BackpackID>%",
"%UltraDepository_remain_<BackpackID>%"
);
Main main;
@ -55,45 +61,65 @@ public class PAPIExpansion extends PlaceholderExpansion {
String[] args = identifier.split("_");
if (args.length < 1) {
return "参数不足";
return "Error Params";
}
UserData data = Main.getUserManager().getData(player);
switch (args[0].toLowerCase()) {
case "amount": {
if (args.length < 3) return "参数不足";
Integer amount = Main.getUserManager().getData(player).getItemAmount(args[2], args[3]);
if (amount == null) return "仓库或物品不存在";
if (args.length < 3) return "Error Params";
Integer amount = data.getItemAmount(args[1], args[2]);
if (amount == null) return "Depository or Item not exists";
else return amount.toString();
}
case "sold": {
if (args.length < 3) return "参数不足";
Integer sold = Main.getUserManager().getData(player).getItemSold(args[2], args[3]);
if (sold == null) return "仓库或物品不存在";
if (args.length < 3) return "Error Params";
Integer sold = data.getItemSold(args[1], args[2]);
if (sold == null) return "Depository or Item not exists";
else return sold.toString();
}
case "remain": {
if (args.length < 3) return "参数不足";
Integer sold = Main.getUserManager().getData(player).getItemSold(args[2], args[3]);
if (sold == null) return "仓库或物品不存在";
Integer limit = Main.getDepositoryManager().getItemSellLimit(args[2], args[3]);
if (limit == null) return "仓库或物品不存在";
return Integer.toString(limit - sold);
if (args.length < 2) return "Error Params";
Depository depository = Main.getDepositoryManager().getDepository(args[1]);
if (depository == null) return "Depository not exists";
DepositoryItem item = depository.getItems().get(args[2]);
if (item == null) return "Depository Item not exists";
int limit = item.getLimit();
DepositoryItemData itemData = data.getItemData(item);
return Integer.toString(limit - itemData.getSold());
}
case "limit": {
if (args.length < 3) return "参数不足";
Integer limit = Main.getDepositoryManager().getItemSellLimit(args[2], args[3]);
if (limit == null) return "仓库或物品不存在";
if (args.length < 3) return "Error Params";
Integer limit = Main.getDepositoryManager().getItemSellLimit(args[1], args[2]);
if (limit == null) return "Depository or Item not exists";
else return limit.toString();
}
case "price": {
if (args.length < 3) return "参数不足";
Double price = Main.getDepositoryManager().getItemPrice(args[2], args[3]);
if (price == null) return "仓库或物品不存在";
if (args.length < 3) return "Error Params";
Double price = Main.getDepositoryManager().getItemPrice(args[1], args[2]);
if (price == null) return "Depository or Item not exists";
else return price.toString();
}
case "capacity": {
if (args.length < 2) return "Error Params";
Depository depository = Main.getDepositoryManager().getDepository(args[1]);
if (depository == null) return "Depository not exists";
return Integer.toString(depository.getCapacity().getPlayerCapacity(player));
}
case "used": {
if (args.length < 2) return "Error Params";
Depository depository = Main.getDepositoryManager().getDepository(args[1]);
if (depository == null) return "Depository not exists";
return Integer.toString(data.getDepositoryData(depository).getUsedCapacity());
}
case "usable": {
if (args.length < 2) return "Error Params";
Depository depository = Main.getDepositoryManager().getDepository(args[1]);
if (depository == null) return "Depository not exists";
int used = data.getDepositoryData(depository).getUsedCapacity();
return Integer.toString(depository.getCapacity().getPlayerCapacity(player) - used);
}
case "version": {
return getVersion();
}

View File

@ -43,17 +43,8 @@ public class UserListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public void onQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
String playerName = player.getName();
UUID playerUUID = player.getUniqueId();
UserData userData = Main.getUserManager().getData(player);
Main.getScheduler().runAsync(() -> {
try {
userData.save();
Main.debug(" 玩家 " + playerName + " 数据已保存并卸载。");
} catch (Exception ignored) {
}
Main.getUserManager().getDataCache().remove(playerUUID);
});
Main.getScheduler().runAsync(() -> Main.getUserManager().unloadData(playerUUID, true));
}
}

View File

@ -38,6 +38,8 @@ public class DepositoryManager {
}
public void loadDepositories() {
long start = System.currentTimeMillis();
Main.log(" 开始加载仓库配置...");
File folder = new File(Main.getInstance().getDataFolder(), "depositories");
if (!folder.exists()) {
folder.mkdir();
@ -56,11 +58,14 @@ public class DepositoryManager {
String identifier = fileName.substring(0, fileName.lastIndexOf("."));
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file);
Depository depository = Depository.loadFrom(identifier, configuration);
if (depository != null) {
if (depository.getItems().size() > 0) {
data.put(identifier, depository);
} else {
Main.error(" 仓库 " + depository.getName() + " 未配置任何物品,请检查相关配置!");
}
}
this.depositories = data;
Main.log(" 仓库配置加载完成,共加载 " + data.size() + " 个仓库,耗时 " + (System.currentTimeMillis() - start) + "ms 。");
}
public @NotNull HashMap<@NotNull String, @NotNull Depository> getDepositories() {

View File

@ -37,6 +37,28 @@ public class UserManager {
}
}
public void unloadData(UUID uuid, boolean save) {
UserData data = getData(uuid);
if (data == null) return;
if (save) saveData(data);
dataCache.remove(uuid);
}
public void saveData(UserData data) {
try {
data.save();
Main.debug(" 玩家 " + data.getUserUUID() + " 数据已保存。");
} catch (Exception e) {
Main.error("无法正常保存玩家数据,请检查数据配置!");
Main.error("Could not save user's data, please check the data configuration!");
e.printStackTrace();
}
}
public void saveAll() {
dataCache.values().forEach(this::saveData);
}
public boolean isCollectEnabled(Player player) {
return player.hasPermission("UltraDepository.use") &&
player.hasPermission("UltraDepository.auto") &&

View File

@ -5,10 +5,12 @@ import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.easysql.api.action.query.PreparedQueryAction;
import cc.carm.lib.easysql.api.action.query.SQLQuery;
import cc.carm.plugin.ultradepository.Main;
import cc.carm.plugin.ultradepository.data.DepositoryData;
import cc.carm.plugin.ultradepository.data.ItemData;
import cc.carm.plugin.ultradepository.configuration.PluginConfig;
import cc.carm.plugin.ultradepository.configuration.depository.Depository;
import cc.carm.plugin.ultradepository.configuration.depository.DepositoryItem;
import cc.carm.plugin.ultradepository.configuration.values.ConfigValue;
import cc.carm.plugin.ultradepository.data.DepositoryData;
import cc.carm.plugin.ultradepository.data.DepositoryItemData;
import cc.carm.plugin.ultradepository.data.UserData;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
@ -120,21 +122,23 @@ public class MySQLStorage implements DataStorage {
Main.debug("正通过 MySQLStorage 加载 " + uuid + " 的用户数据...");
try (SQLQuery query = createAction(uuid).execute()) {
ResultSet resultSet = query.getResultSet();
Map<String, DepositoryData> dataMap = new HashMap<>();
if (resultSet != null && resultSet.next()) {
String dataJSON = resultSet.getString("data");
Date date = resultSet.getDate("day");
UserData data = new UserData(uuid, this, new HashMap<>(), date);
JsonElement dataElement = PARSER.parse(dataJSON);
if (dataElement.isJsonObject()) {
dataElement.getAsJsonObject().entrySet().forEach(entry -> {
String backpackID = entry.getKey();
DepositoryData contentsData = parseContentsData(entry.getValue());
if (contentsData != null) dataMap.put(backpackID, contentsData);
});
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 new UserData(uuid, this, dataMap, date);
return data;
}
Main.debug("当前库内不存在玩家 " + uuid + " 的数据,视作新档。");
return new UserData(uuid, this, new HashMap<>(), new Date(System.currentTimeMillis()));
@ -150,7 +154,7 @@ public class MySQLStorage implements DataStorage {
JsonObject dataObject = new JsonObject();
data.getBackpacks().forEach((id, contents) -> dataObject.add(id, serializeContentsData(contents)));
data.getDepositories().forEach((id, contents) -> dataObject.add(id, serializeContentsData(contents)));
try {
getSQLManager().createReplace(SQLTables.USER_DATA.getName())
@ -179,45 +183,47 @@ public class MySQLStorage implements DataStorage {
.setLimit(1).build();
}
private DepositoryData parseContentsData(JsonElement contentsElement) {
return contentsElement.isJsonObject() ? parseContentsData(contentsElement.getAsJsonObject()) : null;
private DepositoryData parseContentsData(Depository source, UserData owner, JsonElement contentsElement) {
return contentsElement.isJsonObject() ? parseContentsData(source, owner, contentsElement.getAsJsonObject()) : null;
}
private DepositoryData parseContentsData(JsonObject contentsObject) {
Map<String, ItemData> contents = new HashMap<>();
private DepositoryData parseContentsData(Depository source, UserData owner, JsonObject contentsObject) {
DepositoryData data = DepositoryData.emptyContents(source, owner);
for (Map.Entry<String, JsonElement> entry : contentsObject.entrySet()) {
String itemType = entry.getKey();
ItemData data = parseItemData(entry.getValue());
contents.put(itemType, data);
DepositoryItem item = source.getItems().get(entry.getKey());
if (item == null) continue;
DepositoryItemData itemData = parseItemData(item, data, entry.getValue());
if (itemData != null) data.getContents().put(item.getTypeID(), itemData);
}
return new DepositoryData(contents);
return data;
}
private ItemData parseItemData(JsonElement itemElement) {
return itemElement.isJsonObject() ? parseItemData(itemElement.getAsJsonObject()) : null;
private DepositoryItemData parseItemData(DepositoryItem source, DepositoryData owner, JsonElement itemElement) {
return itemElement.isJsonObject() ? parseItemData(source, owner, itemElement.getAsJsonObject()) : null;
}
private ItemData parseItemData(JsonObject itemObject) {
if (!itemObject.has("amount") || !itemObject.has("sold")) {
return ItemData.emptyItemData();
} else {
return new ItemData(itemObject.get("amount").getAsInt(), itemObject.get("sold").getAsInt());
}
private DepositoryItemData parseItemData(DepositoryItem source, DepositoryData owner, JsonObject itemObject) {
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;
else return new DepositoryItemData(source, owner, amount, sold);
}
@Nullable
private JsonObject serializeContentsData(@Nullable DepositoryData contentsData) {
if (contentsData == null) return null;
JsonObject contentsObject = new JsonObject();
contentsData.getContents().forEach((typeID, item) -> contentsObject.add(typeID, serializeItemData(item)));
contentsData.getContents().entrySet().stream()
.filter(entry -> entry.getValue().getSold() > 0 || entry.getValue().getAmount() > 0)
.forEach(entry -> contentsObject.add(entry.getKey(), serializeItemData(entry.getValue())));
return contentsObject;
}
@NotNull
private JsonObject serializeItemData(@NotNull ItemData itemData) {
private JsonObject serializeItemData(@NotNull DepositoryItemData itemData) {
JsonObject itemObject = new JsonObject();
itemObject.addProperty("amount", itemData.getAmount());
itemObject.addProperty("sold", itemData.getSold());
if (itemData.getAmount() > 0) itemObject.addProperty("amount", itemData.getAmount());
if (itemData.getSold() > 0) itemObject.addProperty("sold", itemData.getSold());
return itemObject;
}

View File

@ -5,7 +5,7 @@ import cc.carm.plugin.ultradepository.configuration.PluginConfig;
import cc.carm.plugin.ultradepository.configuration.PluginMessages;
import cc.carm.plugin.ultradepository.configuration.depository.Depository;
import cc.carm.plugin.ultradepository.configuration.depository.DepositoryItem;
import cc.carm.plugin.ultradepository.data.ItemData;
import cc.carm.plugin.ultradepository.data.DepositoryItemData;
import cc.carm.plugin.ultradepository.data.UserData;
import cc.carm.plugin.ultradepository.util.ItemStackFactory;
import cc.carm.plugin.ultradepository.util.gui.GUI;
@ -41,7 +41,7 @@ public class DepositoryGUI extends GUI {
private GUIItem createGUIItem(DepositoryItem item) {
ItemStackFactory factory = new ItemStackFactory(item.getDisplayItem());
ItemData itemData = userData.getItemData(depository, item);
DepositoryItemData itemData = userData.getItemData(item);
List<String> additionalLore = PluginConfig.General.ADDITIONAL_LORE.get(player, new Object[]{
item.getName(), itemData.getAmount(), item.getPrice(), itemData.getSold(), item.getLimit()
});

View File

@ -5,7 +5,7 @@ import cc.carm.plugin.ultradepository.configuration.PluginConfig;
import cc.carm.plugin.ultradepository.configuration.PluginMessages;
import cc.carm.plugin.ultradepository.configuration.depository.Depository;
import cc.carm.plugin.ultradepository.configuration.depository.DepositoryItem;
import cc.carm.plugin.ultradepository.data.ItemData;
import cc.carm.plugin.ultradepository.data.DepositoryItemData;
import cc.carm.plugin.ultradepository.data.UserData;
import cc.carm.plugin.ultradepository.util.ItemStackFactory;
import cc.carm.plugin.ultradepository.util.gui.GUI;
@ -23,7 +23,7 @@ public class SellItemGUI extends GUI {
final Player player;
final UserData userData;
final ItemData itemData;
final DepositoryItemData itemData;
final Depository configuration;
final DepositoryItem item;
@ -31,7 +31,7 @@ public class SellItemGUI extends GUI {
int currentAmount;
public SellItemGUI(Player player, UserData userData, ItemData itemData,
public SellItemGUI(Player player, UserData userData, DepositoryItemData itemData,
Depository configuration, DepositoryItem item) {
super(GUIType.FOUR_BY_NINE, PluginConfig.General.SellGUI.TITLE.get(player, new String[]{
configuration.getName(), item.getName()
@ -181,7 +181,7 @@ public class SellItemGUI extends GUI {
return Math.min(getRemainAmount(), getSellLimit() - getSoldAmount()) - getCurrentAmount();
}
public static void open(Player player, UserData userData, ItemData itemData,
public static void open(Player player, UserData userData, DepositoryItemData itemData,
Depository configuration, DepositoryItem item) {
if (!Main.getEconomyManager().isInitialized()) return;
SellItemGUI gui = new SellItemGUI(player, userData, itemData, configuration, item);