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

完成用户数据部分与变量部分

This commit is contained in:
2021-12-26 10:39:41 +08:00
parent abf82e1a91
commit 5bcea5f24b
51 changed files with 1644 additions and 351 deletions
@@ -1,4 +1,17 @@
package cc.carm.plugin.ultrabackpack.api;
import cc.carm.plugin.ultrabackpack.api.manager.UBUserManager;
public class UltraBackpackAPI {
private static UBUserManager userManager;
public static void initialize(UBUserManager userManager) {
UltraBackpackAPI.userManager = userManager;
}
public static UBUserManager getUserManager() {
return userManager;
}
}
@@ -1,12 +0,0 @@
package cc.carm.plugin.ultrabackpack.api.configuration.backpack;
import java.util.Map;
public interface BackpackCapacity {
int getDefault();
Map<Integer, String> getPermissions();
}
@@ -1,24 +0,0 @@
package cc.carm.plugin.ultrabackpack.api.configuration.backpack;
import cc.carm.plugin.ultrabackpack.api.configuration.gui.GUIConfiguration;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
public interface BackpackConfiguration {
@NotNull String getIdentifier();
@NotNull String getName();
@NotNull GUIConfiguration getGUIConfiguration();
@NotNull BackpackCapacity getCapacity();
int getCapacityFor(Player player);
@NotNull Map<String, BackpackItem> getItems();
}
@@ -1,23 +0,0 @@
package cc.carm.plugin.ultrabackpack.api.configuration.backpack;
import org.bukkit.Material;
import java.util.List;
public interface BackpackItem {
Material getMaterial();
int getData();
int getSoldLimit();
int getSoldPrice();
int getDisplaySlot();
String getDisplayName();
List<String> getDisplayLore();
}
@@ -1,27 +0,0 @@
package cc.carm.plugin.ultrabackpack.api.configuration.gui;
import org.bukkit.event.inventory.ClickType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface GUIAction {
enum ActionType {
COMMAND,
CONSOLE,
MESSAGE,
SOUND,
CLOSE
}
@Nullable ClickType getClickType();
@NotNull ActionType getActionType();
@NotNull String getActionContent();
void executeAction();
}
@@ -1,14 +0,0 @@
package cc.carm.plugin.ultrabackpack.api.configuration.gui;
import java.util.Map;
public interface GUIConfiguration {
String getTitle();
int getLines();
Map<Integer, GUIItemDetail> getItems();
}
@@ -1,15 +0,0 @@
package cc.carm.plugin.ultrabackpack.api.configuration.gui;
import org.bukkit.inventory.ItemStack;
import java.util.List;
public interface GUIItemDetail {
ItemStack getIcon();
List<GUIAction> getActions();
}
@@ -1,25 +1,27 @@
package cc.carm.plugin.ultrabackpack.api.data;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
public class UBContentsData {
private final Map<String, UBItemData> contents;
private final Map<@NotNull String, @NotNull UBItemData> contents;
public UBContentsData(Map<String, UBItemData> contents) {
public UBContentsData(Map<@NotNull String, @NotNull UBItemData> contents) {
this.contents = contents;
}
public Map<String, UBItemData> getContents() {
public @NotNull Map<String, UBItemData> getContents() {
return this.contents;
}
public UBItemData getItemData(String itemType) {
public @Nullable UBItemData getItemData(@NotNull String itemType) {
return getContents().get(itemType);
}
public static UBContentsData emptyContents() {
return new UBContentsData(new HashMap<>());
}
@@ -18,6 +18,14 @@ public class UBItemData {
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;
}
@@ -16,15 +16,29 @@ public interface UBUserData {
@NotNull Set<String> getBackpackIDs();
int getItemAmount(String backpackID, String typeID);
@Nullable UBItemData getItemData(@NotNull String backpackID, @NotNull String typeID);
int getItemSold(String backpackID, String typeID);
@Nullable Integer getItemAmount(@NotNull String backpackID, @NotNull String typeID);
Date getDataDay();
@Nullable Integer getItemSold(@NotNull String backpackID, @NotNull String typeID);
@Nullable Integer setItemAmount(@NotNull String backpackID, @NotNull String typeID, int amount);
@Nullable Integer setItemSold(@NotNull String backpackID, @NotNull String typeID, int amount);
@Nullable Integer addItemAmount(@NotNull String backpackID, @NotNull String typeID, int amount);
@Nullable Integer addItemSold(@NotNull String backpackID, @NotNull String typeID, int amount);
@Nullable Integer removeItemAmount(@NotNull String backpackID, @NotNull String typeID, int amount);
@Nullable Integer removeItemSold(@NotNull String backpackID, @NotNull String typeID, int amount);
Date getDate();
boolean isCurrentDay();
void updateDate();
void checkoutDate();
void save() throws Exception;
@@ -16,7 +16,7 @@ public interface UBUserManager {
UBUserData getData(@NotNull Player player);
@NotNull
UBUserData loaData(@NotNull UUID userUUID);
UBUserData loadData(@NotNull UUID userUUID);
}
@@ -1,18 +0,0 @@
package cc.carm.plugin.ultrabackpack.api.storage;
import cc.carm.plugin.ultrabackpack.api.data.UBUserData;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
public interface UBStorage {
boolean initialize();
@NotNull
UBUserData loadData(@NotNull UUID uuid) throws Exception;
void saveUserData(@NotNull UBUserData data) throws Exception;
}
@@ -0,0 +1,33 @@
package cc.carm.plugin.ultrabackpack.api.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ColorParser {
public static String parse(String text) {
text = parseHexColor(text);
return parseColor(text);
}
public static String parseColor(final String text) {
return text.replaceAll("&", "§").replace("§§", "&");
}
public static String parseHexColor(String text) {
Pattern pattern = Pattern.compile("&\\((&?#[0-9a-fA-F]{6})\\)");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
String hexColor = text.substring(matcher.start() + 2, matcher.end() - 1);
hexColor = hexColor.replace("&", "");
StringBuilder bukkitColorCode = new StringBuilder('§' + "x");
for (int i = 1; i < hexColor.length(); i++) {
bukkitColorCode.append('§').append(hexColor.charAt(i));
}
text = text.replaceAll("&\\(" + hexColor + "\\)", bukkitColorCode.toString().toLowerCase());
matcher.reset(text);
}
return text;
}
}
@@ -0,0 +1,139 @@
package cc.carm.plugin.ultrabackpack.api.util;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ItemStackFactory {
ItemStack item;
private ItemStackFactory() {
}
public ItemStackFactory(ItemStack is) {
this.item = is.clone();
}
public ItemStackFactory(Material type) {
this(type, 1);
}
public ItemStackFactory(Material type, int amount) {
this(type, amount, (short) 0);
}
public ItemStackFactory(Material type, int amount, short data) {
this.item = new ItemStack(type, amount, data);
}
public ItemStackFactory(Material type, int amount, int data) {
this(type, amount, (short) data);
}
public ItemStack toItemStack() {
return this.item;
}
public ItemStackFactory setType(Material type) {
this.item.setType(type);
return this;
}
public ItemStackFactory setDurability(int i) {
this.item.setDurability((short) i);
return this;
}
public ItemStackFactory setAmount(int a) {
this.item.setAmount(a);
return this;
}
public ItemStackFactory setDisplayName(@NotNull String name) {
ItemMeta im = this.item.getItemMeta();
if (im != null) {
im.setDisplayName(ColorParser.parse(name));
this.item.setItemMeta(im);
}
return this;
}
public ItemStackFactory setLore(@NotNull List<String> loreList) {
ItemMeta im = this.item.getItemMeta();
if (im != null) {
im.setLore(
loreList.stream()
.map(ColorParser::parse)
.collect(Collectors.toList())
);
this.item.setItemMeta(im);
}
return this;
}
public ItemStackFactory addLore(@NotNull String s) {
ItemMeta im = this.item.getItemMeta();
if (im != null) {
List<String> lore = im.getLore() != null ? im.getLore() : new ArrayList<>();
lore.add(ColorParser.parse(s));
im.setLore(lore);
this.item.setItemMeta(im);
}
return this;
}
public ItemStackFactory addEnchant(@NotNull Enchantment enchant, int level, boolean ignoreLevelRestriction) {
ItemMeta im = this.item.getItemMeta();
if (im != null) {
im.addEnchant(enchant, level, ignoreLevelRestriction);
this.item.setItemMeta(im);
}
return this;
}
public ItemStackFactory removeEnchant(@NotNull Enchantment enchant) {
ItemMeta im = this.item.getItemMeta();
if (im != null) {
im.removeEnchant(enchant);
this.item.setItemMeta(im);
}
return this;
}
public ItemStackFactory addFlag(@NotNull ItemFlag flag) {
ItemMeta im = this.item.getItemMeta();
if (im != null) {
im.addItemFlags(flag);
this.item.setItemMeta(im);
}
return this;
}
public ItemStackFactory removeFlag(@NotNull ItemFlag flag) {
ItemMeta im = this.item.getItemMeta();
if (im != null) {
im.removeItemFlags(flag);
this.item.setItemMeta(im);
}
return this;
}
public ItemStackFactory setUnbreakable(boolean unbreakable) {
ItemMeta im = this.item.getItemMeta();
if (im != null) {
im.setUnbreakable(unbreakable);
this.item.setItemMeta(im);
}
return this;
}
}
@@ -0,0 +1,82 @@
package cc.carm.plugin.ultrabackpack.api.util;
import cc.carm.plugin.ultrabackpack.api.util.ColorParser;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import java.util.*;
public class MessageUtil {
public static boolean hasPlaceholderAPI() {
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
}
public static void send(@Nullable CommandSender sender, List<String> messages) {
if (messages == null || messages.isEmpty() || sender == null) return;
for (String s : messages) {
sender.sendMessage(ColorParser.parse(s));
}
}
public static void send(@Nullable CommandSender sender, String... messages) {
send(sender, Arrays.asList(messages));
}
public static void sendWithPlaceholders(CommandSender sender, String... messages) {
sendWithPlaceholders(sender, Arrays.asList(messages));
}
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages) {
if (messages == null || messages.isEmpty() || sender == null) return;
send(sender, setPlaceholders(sender, messages));
}
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String param, Object value) {
sendWithPlaceholders(sender, messages, new String[]{param}, new Object[]{value});
}
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String[] params, Object[] values) {
sendWithPlaceholders(sender, setCustomParams(messages, params, values));
}
public static List<String> setPlaceholders(@Nullable CommandSender sender, List<String> messages) {
if (messages == null || messages.isEmpty() || sender == null) return messages;
if (hasPlaceholderAPI() && sender instanceof Player) {
return PlaceholderAPI.setPlaceholders((Player) sender, messages);
} else {
return messages;
}
}
public static List<String> setCustomParams(List<String> messages, String param, Object value) {
return setCustomParams(messages, new String[]{param}, new Object[]{value});
}
public static List<String> setCustomParams(List<String> messages, String[] params, Object[] values) {
if (params.length != values.length) return messages;
HashMap<String, Object> paramsMap = new HashMap<>();
for (int i = 0; i < params.length; i++) {
paramsMap.put(params[i], values[i]);
}
return setCustomParams(messages, paramsMap);
}
public static List<String> setCustomParams(List<String> messages, HashMap<String, Object> params) {
List<String> list = new ArrayList<>();
for (String message : messages) {
String afterMessage = message;
for (Map.Entry<String, Object> entry : params.entrySet()) {
afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString());
}
list.add(afterMessage);
}
return list;
}
}