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

Compare commits

..

6 Commits

26 changed files with 289 additions and 370 deletions
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<version>1.5.7</version>
<version>1.5.11</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
@@ -162,9 +162,14 @@ public class ColorParser {
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());
StringBuilder sb = new StringBuilder();
String extra = null;
for (int i = 0; i < characters.length; i++) {
extra = buildExtraFormat(extra, extraFormats.get(i));
String s = colorText(characters[i], extra, buildHexColor(hexes[i]));
sb.append(s);
}
return sb.toString();
}
protected static String gradientText(@NotNull String text, @Nullable String startHex, @Nullable String endHex) {
@@ -205,4 +210,13 @@ public class ColorParser {
.collect(Collectors.joining("", '§' + "x", ""));
}
protected static String buildExtraFormat(String current, String extra) {
if (extra != null) current = (current == null ? "" : current) + extra;
return isResetCode(current) ? null : current;
}
protected static boolean isResetCode(String input) {
return input != null && (input.toLowerCase().endsWith("&r") || input.toLowerCase().endsWith("§r"));
}
}
+1 -1
View File
@@ -15,7 +15,7 @@ public class ColorParseTest {
System.out.println(parseGradientColor("&<#AAAAAA>我真的&<#BBBBBB>爱死&<#111111>你&<#FFFFFF>"));
// 测试穿插
System.out.println(parse("&<#AAAAAA>&l&r真&b的&<#BBBBBB>&o爱死&<#111111>你&<#FFFFFF>了&r"));
System.out.println(parse("&<#AAAAAA>&l&m我真的&o尊的&r真的&<#BBBBBB>&o爱死&<#111111>你&<#FFFFFF>了&r"));
System.out.println(parse("&<#AAAAAA>&l我&r真&(#666666)的&<#BBBBBB>&o爱死&<#111111>你&<#FFFFFF>了&r"));
System.out.println(parse("&r正常的颜色理应&c&l不受影响&r。"));
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<version>1.5.7</version>
<version>1.5.11</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.7</version>
<version>1.5.11</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.7</version>
<version>1.5.11</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -19,7 +19,7 @@ import java.util.stream.IntStream;
public class GUI {
private static JavaPlugin plugin;
private static final HashMap<UUID, GUI> openedGUIs = new HashMap<>();
private static final Map<UUID, GUI> openedGUIs = new HashMap<>();
public static void initialize(JavaPlugin plugin) {
GUI.plugin = plugin;
@@ -29,43 +29,32 @@ public class GUI {
return plugin;
}
public static HashMap<UUID, GUI> getOpenedGUIs() {
public static Map<UUID, GUI> getOpenedGUIs() {
return openedGUIs;
}
protected GUIType type;
protected String name;
public HashMap<Integer, GUIItem> items;
public Inventory inv;
protected final @NotNull GUIType type;
protected @NotNull String title;
protected Inventory inv;
/**
* 当玩家点击目标GUI时是否取消
*/
boolean cancelOnTarget = true;
protected final SortedMap<Integer, GUIItem> items = new TreeMap<>();
protected ItemStack emptyItem = null;
/**
* 当玩家点击自己背包时是否取消
*/
boolean cancelOnSelf = true;
/**
* 当玩家点击界面外时是否取消
*/
boolean cancelOnOuter = true;
protected boolean cancelOnTarget = true; // 当玩家点击GUI时是否取消对应事件
protected boolean cancelOnSelf = true; // 当玩家点击自己背包时是否取消对应事件
protected boolean cancelOnOuter = true; // 当玩家点击界面外时是否取消对应事件
protected final Map<String, Object> flags = new LinkedHashMap<>();
protected GUIListener listener;
public GUI(GUIType type, String name) {
public GUI(@NotNull GUIType type, @NotNull String title) {
this.type = type;
this.name = ColorParser.parse(name);
this.items = new HashMap<>();
this.title = ColorParser.parse(title);
}
public HashMap<@NotNull Integer, @NotNull GUIItem> getItems() {
return new HashMap<>(items);
public SortedMap<@NotNull Integer, @NotNull GUIItem> getItems() {
return items;
}
public final void setItem(int index, @Nullable GUIItem item) {
@@ -76,24 +65,59 @@ public class GUI {
}
}
public void setItemStack(int index, @Nullable ItemStack item) {
setItem(index, item == null ? null : new GUIItem(item));
}
public void setItem(GUIItem item, int... index) {
for (int i : index) {
setItem(i, item);
}
}
public void setItemStack(ItemStack item, int... index) {
for (int i : index) {
setItemStack(i, item);
}
}
public GUIItem getItem(int index) {
return this.items.get(index);
}
public void setEmptyItem(ItemStack item) {
this.emptyItem = item;
}
protected void fillEmptySlots(@NotNull Inventory inventory) {
if (emptyItem == null) return;
IntStream.range(0, inventory.getSize())
.filter(i -> inventory.getItem(i) == null)
.forEach(index -> inventory.setItem(index, emptyItem));
}
protected void applyToInventory(Inventory inventory) {
IntStream.range(0, inventory.getSize()).forEach(index -> inventory.setItem(index, new ItemStack(Material.AIR)));
getItems().forEach((index, item) -> inventory.setItem(index, item.getDisplay()));
fillEmptySlots(inventory);
}
public void updateTitle(@NotNull String title) {
this.title = ColorParser.parse(title);
if (this.inv != null) {
this.inv = Bukkit.createInventory(null, this.type.getSize(), this.title);
applyToInventory(this.inv);
}
}
/**
* 更新玩家箱子的视图
*/
public void updateView() {
this.onUpdate();
if (this.inv != null) {
List<HumanEntity> viewers = this.inv.getViewers();
IntStream.range(0, this.inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR)));
getItems().forEach((index, item) -> inv.setItem(index, item.getDisplay()));
applyToInventory(this.inv);
viewers.forEach(p -> ((Player) p).updateInventory());
}
}
@@ -148,22 +172,22 @@ public class GUI {
throw new IllegalStateException("被取消或不存在的GUI");
}
Inventory inv = Bukkit.createInventory(null, this.type.getSize(), this.name);
IntStream.range(0, inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR)));
getItems().forEach((index, item) -> inv.setItem(index, item.getDisplay()));
Inventory ui = Bukkit.createInventory(null, this.type.getSize(), this.title);
applyToInventory(ui);
GUI previous = getOpenedGUI(player);
if (previous != null) {
if (previous != null && previous.listener != null) {
previous.listener.close(player);
}
setOpenedGUI(player, this);
this.inv = inv;
player.openInventory(inv);
this.inv = ui;
player.openInventory(ui);
if (listener == null) {
Bukkit.getPluginManager().registerEvents(listener = new GUIListener(this), getPlugin());
this.listener = new GUIListener(this);
Bukkit.getPluginManager().registerEvents(this.listener, getPlugin());
}
}
@@ -181,12 +205,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) {
@@ -205,4 +235,11 @@ public class GUI {
getOpenedGUIs().remove(player.getUniqueId());
}
public static void closeAll() {
Set<HumanEntity> viewers = new HashSet<>();
getOpenedGUIs().values().stream().flatMap(gui -> gui.inv.getViewers().stream()).forEach(viewers::add);
viewers.forEach(HumanEntity::closeInventory);
getOpenedGUIs().clear();
}
}
@@ -10,11 +10,11 @@ import java.util.Set;
public class GUIItem {
ItemStack display;
boolean actionActive = true;
protected ItemStack display;
protected boolean actionActive = true;
public Set<GUIClickAction> actions = new HashSet<>();
public Set<GUIClickAction> actionsIgnoreActive = new HashSet<>();
protected final Set<GUIClickAction> actions = new HashSet<>();
protected final Set<GUIClickAction> actionsIgnoreActive = new HashSet<>();
public GUIItem(ItemStack display) {
this.display = display;
@@ -78,6 +78,14 @@ public class GUIItem {
}
public Set<GUIClickAction> getActions() {
return actions;
}
public Set<GUIClickAction> getActionsIgnoreActive() {
return actionsIgnoreActive;
}
public abstract static class GUIClickAction {
public abstract void run(ClickType type, Player player);
}
@@ -11,7 +11,7 @@ import org.bukkit.event.player.PlayerQuitEvent;
public class GUIListener implements Listener {
GUI currentGUI;
protected final GUI currentGUI;
public GUIListener(GUI gui) {
this.currentGUI = gui;
@@ -12,6 +12,7 @@ public enum GUIType {
FOUR_BY_NINE(4, 36),
FIVE_BY_NINE(5, 45),
SIX_BY_NINE(6, 54),
CANCEL(0, 0);
private final int lines;
@@ -4,36 +4,54 @@ import cc.carm.lib.easyplugin.gui.GUIItem;
import cc.carm.lib.easyplugin.gui.GUIType;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class AutoPagedGUI extends CommonPagedGUI {
public static Function<Player, ItemStack> defaultPreviousPage = null;
public static Function<Player, ItemStack> defaultNextPage = null;
ItemStack previousPageUI;
ItemStack nextPageUI;
int previousPageSlot = -1;
int nextPageSlot = -1;
protected ItemStack previousPageUI;
protected ItemStack nextPageUI;
protected ItemStack noPreviousPageUI;
protected ItemStack noNextPageUI;
protected int previousPageSlot = -1;
protected int nextPageSlot = -1;
public AutoPagedGUI(GUIType type, String name, int[] range) {
super(type, name, range);
public AutoPagedGUI(@NotNull GUIType type, @NotNull String title, int[] range) {
super(type, title, range);
}
public AutoPagedGUI(GUIType type, String name, int a, int b) {
super(type, name, a, b);
public AutoPagedGUI(@NotNull GUIType type, @NotNull String title, int a, int b) {
super(type, title, a, b);
}
public void setPreviousPageUI(ItemStack lastPageUI) {
public void setPreviousPageUI(@Nullable ItemStack lastPageUI) {
this.previousPageUI = lastPageUI;
}
public void setNextPageUI(ItemStack nextPageUI) {
public void setNoPreviousPageUI(@Nullable ItemStack noPreviousPageUI) {
this.noPreviousPageUI = noPreviousPageUI;
}
public void setNextPageUI(@Nullable ItemStack nextPageUI) {
this.nextPageUI = nextPageUI;
}
public void setNoNextPageUI(@Nullable ItemStack noNextPageUI) {
this.noNextPageUI = noNextPageUI;
}
public void setPreviousPageSlot(int slot) {
this.previousPageSlot = slot;
}
@@ -42,12 +60,24 @@ public class AutoPagedGUI extends CommonPagedGUI {
this.nextPageSlot = slot;
}
@Override
protected void fillEmptySlots(@NotNull Inventory inventory) {
if (emptyItem == null) return;
Set<Integer> rangeSet = Arrays.stream(this.range).boxed().collect(Collectors.toSet());
if (previousPageSlot >= 0) rangeSet.add(previousPageSlot);
if (nextPageSlot >= 0) rangeSet.add(nextPageSlot);
IntStream.range(0, inventory.getSize())
.filter(i -> inventory.getItem(i) == null)
.filter(i -> !rangeSet.contains(i))
.forEach(index -> inventory.setItem(index, emptyItem));
}
@Override
public void openGUI(Player user) {
if (previousPageSlot >= 0) {
if (hasPreviousPage()) {
setItem(previousPageSlot, new GUIItem(
previousPageUI == null ? getDefaultPreviousPage(user) : previousPageUI) {
setItem(previousPageSlot, new GUIItem(Optional.ofNullable(defaultPreviousPage).map(d -> d.apply(user)).orElse(previousPageUI)) {
@Override
public void onClick(Player clicker, ClickType type) {
if (type == ClickType.RIGHT) {
@@ -59,14 +89,13 @@ public class AutoPagedGUI extends CommonPagedGUI {
}
});
} else {
setItem(previousPageSlot, null);
setItem(previousPageSlot, this.noPreviousPageUI == null ? null : new GUIItem(noPreviousPageUI));
}
}
if (nextPageSlot >= 0) {
if (hasNextPage()) {
setItem(nextPageSlot, new GUIItem(
nextPageUI == null ? getDefaultNextPage(user) : nextPageUI) {
setItem(nextPageSlot, new GUIItem(Optional.ofNullable(defaultNextPage).map(d -> d.apply(user)).orElse(nextPageUI)) {
@Override
public void onClick(Player clicker, ClickType type) {
if (type == ClickType.RIGHT) {
@@ -78,19 +107,12 @@ public class AutoPagedGUI extends CommonPagedGUI {
}
});
} else {
setItem(nextPageSlot, null);
setItem(nextPageSlot, this.noNextPageUI == null ? null : new GUIItem(noNextPageUI));
}
}
super.openGUI(user);
}
private static ItemStack getDefaultNextPage(Player player) {
return defaultNextPage != null ? defaultNextPage.apply(player) : null;
}
private static ItemStack getDefaultPreviousPage(Player player) {
return defaultPreviousPage != null ? defaultPreviousPage.apply(player) : null;
}
}
@@ -4,70 +4,38 @@ package cc.carm.lib.easyplugin.gui.paged;
import cc.carm.lib.easyplugin.gui.GUIItem;
import cc.carm.lib.easyplugin.gui.GUIType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class CommonPagedGUI extends PagedGUI {
private int[] range;
protected final int[] range;
private CommonPagedGUI(GUIType type, String name) {
super(type, name);
public CommonPagedGUI(@NotNull GUIType type, @NotNull String title, int a, int b) {
this(type, title, toRange(type, a, b));
}
public CommonPagedGUI(GUIType type, String Name, int a, int b) {
this(type, Name, toRange(type, a, b));
}
public CommonPagedGUI(GUIType type, String Name, int[] range) {
super(type, Name);
public CommonPagedGUI(@NotNull GUIType type, @NotNull String title, int[] range) {
super(type, title);
Arrays.sort(range);
this.range = range;
}
/*
int[] matrix = new int[]{
0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53
}
*/
private static int[] toRange(GUIType type, int a, int b) {
if (a > b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
int lineA = getLine(a);
int columnA = getColumn(a);
int lineB = getLine(b);
int columnB = getColumn(b);
if (lineB > type.getLines())
throw new IndexOutOfBoundsException("页面内容范围超过了GUI的大小");
int[] range = new int[(lineB - lineA + 1) * (columnB - columnA + 1)];
for (int i = 0, l = 0; i < type.getSize(); i++) {
int li = getLine(i);
int ci = getColumn(i);
if (li >= lineA && li <= lineB && ci >= columnA && ci <= columnB) {
range[l] = i;
l++;
}
}
return range;
@Override
protected void fillEmptySlots(@NotNull Inventory inventory) {
if (emptyItem == null) return;
Set<Integer> rangeSet = Arrays.stream(this.range).boxed().collect(Collectors.toSet());
IntStream.range(0, inventory.getSize())
.filter(i -> inventory.getItem(i) == null)
.filter(i -> !rangeSet.contains(i))
.forEach(index -> inventory.setItem(index, emptyItem));
}
private static int getLine(int i) {
@@ -93,10 +61,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 +70,8 @@ public class CommonPagedGUI extends PagedGUI {
* 前往最后一页
*/
public void goLastPage() {
if (hasNextPage())
this.page = getLastPageNumber();
else
throw new IndexOutOfBoundsException();
this.page = getLastPageNumber();
onPageChange(this.page);
}
@@ -116,8 +80,9 @@ public class CommonPagedGUI extends PagedGUI {
*
* @return 最后一页的页码
*/
@Override
public int getLastPageNumber() {
return (this.container.size() / range.length) + ((this.container.size() % range.length) == 0 ? 0 : 1);
return getLastPageNumber(range.length);
}
/**
@@ -159,4 +124,44 @@ public class CommonPagedGUI extends PagedGUI {
super.openGUI(player);
}
/*
int[] matrix = new int[]{
0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53
}
*/
private static int[] toRange(GUIType type, int a, int b) {
if (a > b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
int lineA = getLine(a);
int columnA = getColumn(a);
int lineB = getLine(b);
int columnB = getColumn(b);
if (lineB > type.getLines())
throw new IndexOutOfBoundsException("页面内容范围超过了GUI的大小");
int[] range = new int[(lineB - lineA + 1) * (columnB - columnA + 1)];
int l = 0;
for (int i = 0; i < type.getSize(); i++) {
int li = getLine(i);
int ci = getColumn(i);
if (li >= lineA && li <= lineB && ci >= columnA && ci <= columnB) {
range[l] = i;
l++;
}
}
return range;
}
}
@@ -4,54 +4,94 @@ package cc.carm.lib.easyplugin.gui.paged;
import cc.carm.lib.easyplugin.gui.GUI;
import cc.carm.lib.easyplugin.gui.GUIItem;
import cc.carm.lib.easyplugin.gui.GUIType;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public abstract class PagedGUI extends GUI {
List<GUIItem> container = new ArrayList<>();
public int page = 1;
protected List<GUIItem> container = new ArrayList<>();
protected int page = 1;
public PagedGUI(GUIType type, String name) {
super(type, name);
protected PagedGUI(@NotNull GUIType type, @NotNull String title) {
super(type, title);
}
public int addItem(GUIItem i) {
public int setCurrentPage(int page) {
this.page = Math.max(1, Math.min(page, getLastPageNumber()));
return this.page;
}
public int getCurrentPage() {
return page;
}
public int getLastPageNumber() {
return getLastPageNumber(getGUIType().getSize());
}
/**
* 得到最后一页的页码
*
* @return 最后一页的页码
*/
public int getLastPageNumber(int size) {
return (this.container.size() / size) + ((this.container.size() % size) == 0 ? 0 : 1);
}
public int addItem(@NotNull GUIItem i) {
container.add(i);
return container.size() - 1;
}
public int addItem(int index, @NotNull GUIItem i) {
container.add(index, i);
return container.size() - 1;
}
public int addItemStack(@NotNull ItemStack itemStack) {
return addItem(new GUIItem(itemStack));
}
/**
* 从GUI中移除一个物品
*
* @param item 物品
*/
public void removeItem(GUIItem item) {
public void removeItem(@NotNull GUIItem item) {
container.remove(item);
}
/**
* 从GUI中移除一个物品
*
* @param slot 物品格子数
* @param index 物品格子数
*/
public void removeItem(int slot) {
container.remove(slot);
public void removeItem(int index) {
container.remove(index);
}
public List<GUIItem> getItemsContainer() {
return new ArrayList<>(container);
public @NotNull List<GUIItem> getPagedContainer() {
return this.container;
}
/**
* 当GUI改变页码时执行的代码
*/
public void onPageChange(int pageNum) {
}
/**
* 前往上一页
*/
public void goPreviousPage() {
if (hasPreviousPage())
page--;
else
throw new IndexOutOfBoundsException();
if (hasPreviousPage()) {
this.page--;
this.onPageChange(this.page);
} else throw new IndexOutOfBoundsException();
}
@@ -59,10 +99,10 @@ public abstract class PagedGUI extends GUI {
* 前往下一页
*/
public void goNextPage() {
if (hasNextPage())
page++;
else
throw new IndexOutOfBoundsException();
if (hasNextPage()) {
this.page++;
this.onPageChange(this.page);
} else throw new IndexOutOfBoundsException();
}
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.7</version>
<version>1.5.11</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -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;
/**
* 颜色解析器。
* <br> 普通颜色 格式 {@code &+颜色代码 },如 {@literal &c} 、{@literal &a}
* <br> RGB颜色(版本需要≥1.14) 格式 {@code &(#XXXXXX) },如 {@literal &(#aaaaaa)}
* <br> 渐变RBG颜色(版本需要≥1.14) 格式 {@code &<#XXXXXX>FOOBAR&<#XXXXXX> }
* <p> 注意:当使用渐变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<String> parse(@NotNull Collection<String> 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<String> 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<Integer, String> 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", ""));
}
}
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<version>1.5.7</version>
<version>1.5.11</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.7</version>
<version>1.5.11</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<version>1.5.7</version>
<version>1.5.11</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.7</version>
<version>1.5.11</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.7</version>
<version>1.5.11</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.7</version>
<version>1.5.11</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.7</version>
<version>1.5.11</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.7</version>
<version>1.5.11</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.7</version>
<version>1.5.11</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.7</version>
<version>1.5.11</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+2 -2
View File
@@ -15,7 +15,7 @@
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<packaging>pom</packaging>
<version>1.5.7</version>
<version>1.5.11</version>
<modules>
<module>base/color</module>
<module>base/utils</module>
@@ -88,7 +88,7 @@
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>minebench-repo</id>
<url>https://repo.minebench.de/</url>