diff --git a/base/gui/src/main/java/cc/carm/lib/easyplugin/gui/GUI.java b/base/gui/src/main/java/cc/carm/lib/easyplugin/gui/GUI.java index 251f5d2..2361f75 100644 --- a/base/gui/src/main/java/cc/carm/lib/easyplugin/gui/GUI.java +++ b/base/gui/src/main/java/cc/carm/lib/easyplugin/gui/GUI.java @@ -34,7 +34,7 @@ public class GUI { } protected GUIType type; - protected String name; + protected String title; public HashMap items; public Inventory inv; @@ -57,14 +57,13 @@ public class GUI { protected GUIListener listener; - public GUI(GUIType type, String name) { + public GUI(GUIType type, String title) { this.type = type; - this.name = ColorParser.parse(name); + this.title = ColorParser.parse(title); this.items = new HashMap<>(); } - - public HashMap<@NotNull Integer, @NotNull GUIItem> getItems() { + public Map<@NotNull Integer, @NotNull GUIItem> getItems() { return new HashMap<>(items); } @@ -90,6 +89,7 @@ public class GUI { * 更新玩家箱子的视图 */ public void updateView() { + this.onUpdate(); if (this.inv != null) { List viewers = this.inv.getViewers(); IntStream.range(0, this.inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR))); @@ -148,7 +148,7 @@ public class GUI { throw new IllegalStateException("被取消或不存在的GUI"); } - Inventory inv = Bukkit.createInventory(null, this.type.getSize(), this.name); + Inventory inv = Bukkit.createInventory(null, this.type.getSize(), this.title); IntStream.range(0, inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR))); getItems().forEach((index, item) -> inv.setItem(index, item.getDisplay())); @@ -181,12 +181,18 @@ public class GUI { public void onClose() { } + /** + * 当GUI更新时执行的代码 + */ + public void onUpdate() { + } + public GUIType getGUIType() { return type; } public String getGUIName() { - return name; + return title; } public static void setOpenedGUI(Player player, GUI gui) { diff --git a/base/gui/src/main/java/cc/carm/lib/easyplugin/gui/paged/CommonPagedGUI.java b/base/gui/src/main/java/cc/carm/lib/easyplugin/gui/paged/CommonPagedGUI.java index 07dc8d4..47fe971 100644 --- a/base/gui/src/main/java/cc/carm/lib/easyplugin/gui/paged/CommonPagedGUI.java +++ b/base/gui/src/main/java/cc/carm/lib/easyplugin/gui/paged/CommonPagedGUI.java @@ -93,10 +93,8 @@ public class CommonPagedGUI extends PagedGUI { * 前往第一页 */ public void goFirstPage() { - if (hasPreviousPage()) - this.page = 1; - else - throw new IndexOutOfBoundsException(); + this.page = 1; + onPageChange(this.page); } @@ -104,10 +102,8 @@ public class CommonPagedGUI extends PagedGUI { * 前往最后一页 */ public void goLastPage() { - if (hasNextPage()) - this.page = getLastPageNumber(); - else - throw new IndexOutOfBoundsException(); + this.page = getLastPageNumber(); + onPageChange(this.page); } diff --git a/base/gui/src/main/java/cc/carm/lib/easyplugin/gui/paged/PagedGUI.java b/base/gui/src/main/java/cc/carm/lib/easyplugin/gui/paged/PagedGUI.java index ceff468..8279ccf 100644 --- a/base/gui/src/main/java/cc/carm/lib/easyplugin/gui/paged/PagedGUI.java +++ b/base/gui/src/main/java/cc/carm/lib/easyplugin/gui/paged/PagedGUI.java @@ -44,14 +44,20 @@ public abstract class PagedGUI extends GUI { return new ArrayList<>(container); } + /** + * 当GUI改变页码时执行的代码 + */ + public void onPageChange(int pageNum) { + } + /** * 前往上一页 */ public void goPreviousPage() { - if (hasPreviousPage()) + if (hasPreviousPage()) { page--; - else - throw new IndexOutOfBoundsException(); + this.onPageChange(this.page); + } else throw new IndexOutOfBoundsException(); } @@ -59,10 +65,10 @@ public abstract class PagedGUI extends GUI { * 前往下一页 */ public void goNextPage() { - if (hasNextPage()) + if (hasNextPage()) { page++; - else - throw new IndexOutOfBoundsException(); + this.onPageChange(this.page); + } else throw new IndexOutOfBoundsException(); } diff --git a/base/main/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java b/base/main/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java deleted file mode 100644 index 1b7867f..0000000 --- a/base/main/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java +++ /dev/null @@ -1,208 +0,0 @@ -package cc.carm.lib.easyplugin.utils; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.awt.*; -import java.util.List; -import java.util.*; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -/** - * 颜色解析器。 - *
普通颜色 格式 {@code &+颜色代码 },如 {@literal &c} 、{@literal &a} - *
RGB颜色(版本需要≥1.14) 格式 {@code &(#XXXXXX) },如 {@literal &(#aaaaaa)} - *
渐变RBG颜色(版本需要≥1.14) 格式 {@code &<#XXXXXX>FOOBAR&<#XXXXXX> } - *

注意:当使用渐变RGB颜色时,普通颜色代码与RGB颜色代码将失效。 - */ -public class ColorParser { - - public static final Pattern HEX_PATTERN = Pattern.compile("&\\(&?#([\\da-fA-F]{6})\\)"); - public static final Pattern GRADIENT_PATTERN = Pattern.compile("&<&?#([\\da-fA-F]{6})>"); - public static final Pattern COLOR_PATTERN = Pattern.compile("([&§][0-9a-fA-FrRxX])+"); // 会影响颜色的代码 - public static final Pattern FORMAT_PATTERN = Pattern.compile("([&§][0-9a-fA-Fk-oK-OrRxX])+"); // MC可用的格式化代码 - - /** - * 清除一条消息中的全部颜色代码 (包括RGB颜色代码与渐变颜色代码) - * - * @param text 源消息内容 - * @return 清理颜色后的消息 - */ - public static @NotNull String clear(@NotNull String text) { - text = HEX_PATTERN.matcher(text).replaceAll(""); - text = GRADIENT_PATTERN.matcher(text).replaceAll(""); - text = COLOR_PATTERN.matcher(text).replaceAll(""); - return text; - } - - /** - * 对一条消息进行颜色解析,包括普通颜色代码、RGB颜色代码与RBG渐变代码。 - * - * @param text 源消息内容 - * @return 解析后的消息 - */ - public static @NotNull String parse(@NotNull String text) { - return parseBaseColor(parseGradientColor(parseHexColor(text))); - } - - /** - * 对多条消息进行颜色解析,包括普通颜色代码、RGB颜色代码与RBG渐变代码。 - * - * @param texts 源消息内容 - * @return 解析后的消息 - */ - public static @NotNull String[] parse(@NotNull String... texts) { - return parse(Arrays.asList(texts)).toArray(new String[0]); - } - - /** - * 对多条消息进行颜色解析,包括普通颜色代码、RGB颜色代码与RBG渐变代码。 - * - * @param texts 源消息内容 - * @return 解析后的消息 - */ - public static @NotNull List parse(@NotNull Collection texts) { - return texts.stream().map(ColorParser::parse).collect(Collectors.toList()); - } - - /** - * 解析消息中的基本颜色代码格式 {@code &+颜色代码 },如 {@literal &c} 、{@literal &a} - * - * @param text 消息内容 - * @return RGB处理后的消息 - * @see net.md_5.bungee.api.ChatColor - */ - public static String parseBaseColor(final String text) { - return text.replaceAll("&", "§").replace("§§", "&"); - } - - /** - * 解析消息中的RGB颜色代码(版本需要≥1.14) 格式 {@code &(#XXXXXX) },如 {@literal &(#aaaaaa)} - * - * @param text 消息内容 - * @return RGB处理后的消息 - */ - public static String parseHexColor(String text) { - Matcher matcher = HEX_PATTERN.matcher(text); - while (matcher.find()) { - text = matcher.replaceFirst(buildHexColor(matcher.group(1)).toLowerCase()); - matcher.reset(text); - } - return text; - } - - /** - * 对一条消息进行RGB渐变处理(版本需要≥1.14),格式 {@code &<#XXXXXX>FOOBAR&<#XXXXXX> }。 - * - * @param text 消息内容 - * @return RGB渐变处理后的消息 - */ - public static @NotNull String parseGradientColor(@NotNull String text) { - List colors = new ArrayList<>(); - - Matcher matcher = ColorParser.GRADIENT_PATTERN.matcher(text); - while (matcher.find()) colors.add(matcher.group(1)); - - if (colors.isEmpty()) return text; // 无渐变颜色,直接跳出 - - String[] parts = ColorParser.GRADIENT_PATTERN.split(text); - StringBuilder builder = new StringBuilder(); - for (int i = 0; i < parts.length; i++) { - String startHex = i - 1 >= 0 && colors.size() > i - 1 ? colors.get(i - 1) : null; // 本条消息的起始颜色 - String endHex = colors.size() > i ? colors.get(i) : null; // 本条消息的结束颜色 - builder.append(gradientText(parts[i], startHex, endHex)); - } - - return builder.toString(); - } - - public static @NotNull String gradientText(@NotNull String text, - @Nullable Color startColor, @Nullable Color endColor) { - Objects.requireNonNull(text, "Text to be gradient should not be null!"); - if (startColor == null || endColor == null || text.isEmpty()) { - // 起始颜色有任一为空,则不进行渐变上色。 - // 若有起始颜色,则代表其跟在某个渐变之后,应当添加"&r"阻断前面的渐变。 - return (startColor != null ? "&r" : "") + text; - } - - // 用于记录消息中特殊格式的位置 - // 在渐变中,允许使用格式字符与颜色字符来改变其中某个字的颜色/格式,以支持更多形式内容。 - LinkedHashMap extraFormats = new LinkedHashMap<>(); - Matcher matcher = ColorParser.FORMAT_PATTERN.matcher(text); - while (matcher.find()) { - extraFormats.put(matcher.start(), matcher.group()); - text = matcher.replaceFirst(""); - matcher.reset(text); - } - - if (text.length() == 1) { - // 当只有一个实际字符时,无需进行渐变计算,直接返回 中间颜色+起始格式(如果有)+消息 即可。 - return colorText(text, extraFormats.get(0), buildHexColor(mediumHex(startColor, endColor))); - } - - String[] characters = text.split(""); - int step = characters.length; // 变换次数 - - // 决定每种颜色变换的方向 - int rDirection = startColor.getRed() < endColor.getRed() ? 1 : -1; - int gDirection = startColor.getGreen() < endColor.getGreen() ? 1 : -1; - int bDirection = startColor.getBlue() < endColor.getBlue() ? 1 : -1; - - // 决定每种颜色每次变换的度 - int rStep = Math.abs(startColor.getRed() - endColor.getRed()) / (step - 1); - int gStep = Math.abs(startColor.getGreen() - endColor.getGreen()) / (step - 1); - int bStep = Math.abs(startColor.getBlue() - endColor.getBlue()) / (step - 1); - - String[] hexes = IntStream.range(0, step).mapToObj(i -> colorToHex( - startColor.getRed() + rStep * i * rDirection, - startColor.getGreen() + gStep * i * gDirection, - startColor.getBlue() + bStep * i * bDirection - )).toArray(String[]::new); - - return IntStream.range(0, characters.length) - .mapToObj(i -> colorText(characters[i], extraFormats.get(i), buildHexColor(hexes[i]))) - .collect(Collectors.joining()); - } - - protected static String gradientText(@NotNull String text, @Nullable String startHex, @Nullable String endHex) { - return gradientText(text, - startHex == null ? null : Color.decode("0x" + startHex), - endHex == null ? null : Color.decode("0x" + endHex) - ); - } - - private static String mediumHex(@NotNull Color start, @NotNull Color end) { - return colorToHex( - Math.abs(start.getRed() - end.getRed()) / 2, - Math.abs(start.getGreen() - end.getGreen()) / 2, - Math.abs(start.getBlue() - end.getBlue()) / 2 - ); - } - - private static String colorText(String message, @Nullable String format, @Nullable String color) { - if (format != null && COLOR_PATTERN.matcher(format).find()) { - // format中存在影响颜色的内容,则当前消息的颜色会被覆盖。 - // 为了减少最终消息的长度,故直接返回键入的FORMAT和对应消息的内容。 - return format + message; - } - return (color == null ? "" : color) + (format == null ? "" : parseBaseColor(format)) + message; - } - - protected static String colorToHex(Color color) { - return colorToHex(color.getRed(), color.getGreen(), color.getBlue()); - } - - protected static String colorToHex(int r, int g, int b) { - // 将R、G、B转换为16进制(若非2位则补0)输出 - return String.format("%02X%02X%02X", r, g, b); - } - - protected static String buildHexColor(String hexCode) { - return Arrays.stream(hexCode.split("")).map(s -> '§' + s) - .collect(Collectors.joining("", '§' + "x", "")); - } - -} \ No newline at end of file diff --git a/pom.xml b/pom.xml index fddba35..695c036 100644 --- a/pom.xml +++ b/pom.xml @@ -88,7 +88,7 @@ true - + minebench-repo https://repo.minebench.de/