Even more Chat API update

This commit is contained in:
Marc Baloup 2021-07-24 19:37:04 +02:00
parent d1b58c4771
commit 489b81e55f
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893
4 changed files with 108 additions and 93 deletions

View File

@ -23,12 +23,14 @@ import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.event.HoverEventSource;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.format.TextDecoration.State;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
@ -63,9 +65,8 @@ public abstract class Chat extends ChatStatic implements HoverEventSource<Compon
return LEGACY_SERIALIZER_BUNGEE_FIENDLY.serialize(getAdv());
}
@SuppressWarnings("deprecation")
public String getPlainText() {
return net.kyori.adventure.text.serializer.plain.PlainComponentSerializer.plain().serializeOr(getAdv(), "");
return PlainTextComponentSerializer.plainText().serializeOr(getAdv(), "");
}
@ -437,17 +438,17 @@ public abstract class Chat extends ChatStatic implements HoverEventSource<Compon
}
public static class Config {
public ChatColor decorationColor = ChatColor.YELLOW;
public TextColor decorationColor = NamedTextColor.YELLOW;
public char decorationChar = '-';
public int nbCharMargin = 1;
public ChatColor successColor = ChatColor.GREEN;
public ChatColor failureColor = ChatColor.RED;
public ChatColor infoColor = ChatColor.GOLD;
public ChatColor dataColor = ChatColor.GRAY;
public ChatColor urlColor = ChatColor.GREEN;
public ChatColor commandColor = ChatColor.GRAY;
public ChatColor highlightedCommandColor = ChatColor.WHITE;
public ChatColor broadcastColor = ChatColor.YELLOW;
public TextColor successColor = NamedTextColor.GREEN;
public TextColor failureColor = NamedTextColor.RED;
public TextColor infoColor = NamedTextColor.GOLD;
public TextColor dataColor = NamedTextColor.GRAY;
public TextColor urlColor = NamedTextColor.GREEN;
public TextColor commandColor = NamedTextColor.GRAY;
public TextColor highlightedCommandColor = NamedTextColor.WHITE;
public TextColor broadcastColor = NamedTextColor.YELLOW;
public Supplier<Chat> prefix;
}

View File

@ -1,10 +1,11 @@
package fr.pandacube.lib.core.chat;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.md_5.bungee.api.ChatColor;
public class ChatColorUtil {
@ -235,17 +236,29 @@ public class ChatColorUtil {
public static TextColor toAdventure(ChatColor bungee) {
if (bungee == null)
return null;
if (bungee.getColor() == null)
throw new IllegalArgumentException("The provided Bungee ChatColor must be an actual color (not format nor reset).");
return TextColor.color(bungee.getColor().getRGB());
}
public static ChatColor toBungee(TextColor col) {
if (col == null)
return null;
if (col instanceof NamedTextColor) {
return ChatColor.of(((NamedTextColor) col).toString());
}
return ChatColor.of(col.asHexString());
}
public static ChatColor interpolateColor(float v0, float v1, float v, ChatColor cc0, ChatColor cc1) {
Color c0 = cc0.getColor(), c1 = cc1.getColor();
int r0 = c0.getRed(), g0 = c0.getGreen(), b0 = c0.getBlue(),
r1 = c1.getRed(), g1 = c1.getGreen(), b1 = c1.getBlue();
public static TextColor interpolateColor(float v0, float v1, float v, TextColor cc0, TextColor cc1) {
float normV = (v - v0) / (v1 - v0);
return ChatColor.of(new Color(
(int) (r0 + (r1 - r0) * normV),
(int) (g0 + (g1 - g0) * normV),
(int) (b0 + (b1 - b0) * normV)));
return TextColor.lerp(normV, cc0, cc1);
}
@ -258,22 +271,22 @@ public class ChatColorUtil {
//private record GradientValueColor(float value, ChatColor color) { } // Java 16
private static class GradientValueColor {
private final float value;
private final ChatColor color;
public GradientValueColor(float value, ChatColor color) {
private final TextColor color;
public GradientValueColor(float value, TextColor color) {
this.value = value; this.color = color;
}
public float value() { return value; }
public ChatColor color() { return color; }
public TextColor color() { return color; }
}
List<GradientValueColor> colors = new ArrayList<>();
public synchronized ChatValueGradient add(float v, ChatColor col) {
public synchronized ChatValueGradient add(float v, TextColor col) {
colors.add(new GradientValueColor(v, col));
return this;
}
public synchronized ChatColor pickColorAt(float v) {
public synchronized TextColor pickColorAt(float v) {
if (colors.isEmpty())
throw new IllegalStateException("Must define at least one color in this ChatValueGradient instance.");
if (colors.size() == 1)
@ -293,8 +306,7 @@ public class ChatColorUtil {
}
int p0 = p1 - 1;
float v0 = colors.get(p0).value(), v1 = colors.get(p1).value();
ChatColor cc0 = colors.get(p0).color(), cc1 = colors.get(p1).color();
TextColor cc0 = colors.get(p0).color(), cc1 = colors.get(p1).color();
return interpolateColor(v0, v1, v, cc0, cc1);
}
}

View File

@ -18,6 +18,8 @@ import fr.pandacube.lib.core.chat.Chat.FormatableChat;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.format.TextDecoration.State;
import net.md_5.bungee.api.ChatColor;
@ -183,7 +185,7 @@ public class ChatUtil {
public static Chat centerText(Chat text, char repeatedChar, ChatColor decorationColor,
public static Chat centerText(Chat text, char repeatedChar, TextColor decorationColor,
boolean console) {
int textWidth = componentWidth(text.getAdv(), console);
@ -211,7 +213,7 @@ public class ChatUtil {
}
public static Chat leftText(Chat text, char repeatedChar, ChatColor decorationColor, int nbLeft,
public static Chat leftText(Chat text, char repeatedChar, TextColor decorationColor, int nbLeft,
boolean console) {
int textWidth = componentWidth(text.getAdv(), console);
@ -234,7 +236,7 @@ public class ChatUtil {
}
public static Chat rightText(Chat text, char repeatedChar, ChatColor decorationColor, int nbRight,
public static Chat rightText(Chat text, char repeatedChar, TextColor decorationColor, int nbRight,
boolean console) {
int textWidth = componentWidth(text.getAdv(), console);
@ -257,7 +259,7 @@ public class ChatUtil {
}
public static Chat emptyLine(char repeatedChar, ChatColor decorationColor, boolean console) {
public static Chat emptyLine(char repeatedChar, TextColor decorationColor, boolean console) {
int count = ((console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH) / charW(repeatedChar, console, false);
return text(repeatedChar(repeatedChar, count)).color(decorationColor);
}
@ -433,6 +435,68 @@ public class ChatUtil {
private static final String PROGRESS_BAR_START = "[";
private static final String PROGRESS_BAR_END = "]";
private static final TextColor PROGRESS_BAR_EMPTY_COLOR = NamedTextColor.DARK_GRAY;
private static final char PROGRESS_BAR_EMPTY_CHAR = '.';
private static final char PROGRESS_BAR_FULL_CHAR = '|';
public static Chat progressBar(double[] values, TextColor[] colors, double total, int pixelWidth, boolean console) {
// 1. Compute char size for each values
int progressPixelWidth = pixelWidth - strWidth(PROGRESS_BAR_START + PROGRESS_BAR_END, console, false);
int charPixelWidth = charW(PROGRESS_BAR_EMPTY_CHAR, console, false);
assert charPixelWidth == charW(PROGRESS_BAR_FULL_CHAR, console, false) : "PROGRESS_BAR_EMPTY_CHAR and PROGRESS_BAR_FULL_CHAR should have the same pixel width according to #charW(...)";
int progressCharWidth = progressPixelWidth / charPixelWidth;
int[] sizes = new int[values.length];
double sumValuesBefore = 0;
int sumSizesBefore = 0;
for (int i = 0; i < values.length; i++) {
sumValuesBefore += values[i];
int charPosition = Math.min((int) Math.round(progressCharWidth * sumValuesBefore / total), progressCharWidth);
sizes[i] = charPosition - sumSizesBefore;
sumSizesBefore += sizes[i];
}
// 2. Generate rendered text
Chat c = text(PROGRESS_BAR_START);
int sumSizes = 0;
for (int i = 0; i < sizes.length; i++) {
sumSizes += sizes[i];
FormatableChat subC = text(repeatedChar(PROGRESS_BAR_FULL_CHAR, sizes[i]));
if (colors != null && i < colors.length && colors[i] != null)
subC.color(colors[i]);
c.then(subC);
}
return c
.then(text(repeatedChar(PROGRESS_BAR_EMPTY_CHAR, progressCharWidth - sumSizes))
.color(PROGRESS_BAR_EMPTY_COLOR))
.thenText(PROGRESS_BAR_END);
}
public static Chat progressBar(double value, TextColor color, double total, int pixelWidth, boolean console) {
return progressBar(new double[] { value }, new TextColor[] { color }, total, pixelWidth, console);
}

View File

@ -1,62 +0,0 @@
package fr.pandacube.lib.core.chat;
import net.md_5.bungee.api.ChatColor;
public class TextProgressBar {
private static String pattern_start = "[";
private static String pattern_end = "]";
private static ChatColor color_empty = ChatColor.DARK_GRAY;
private static ChatColor color_default = ChatColor.RESET;
private static String pattern_empty = ".";
private static String pattern_full = "|";
public static String progressBar(double[] values, ChatColor[] colors, double total, int nbCar) {
long[] sizes = new long[values.length];
int max_size = nbCar - pattern_start.length() - pattern_end.length();
for (int i = 0; i < values.length; i++) {
double sum_values_before = 0;
for (int j = i; j >= 0; j--)
sum_values_before += values[j];
long car_position = Math.round(max_size * sum_values_before / total);
// évite les barre de progressions plus grandes que la taille
// demandée
if (car_position > max_size) car_position = max_size;
long sum_sizes_before = 0;
for (int j = i - 1; j >= 0; j--)
sum_sizes_before += sizes[j];
sizes[i] = car_position - sum_sizes_before;
}
int sum_sizes = 0;
String bar = pattern_start;
for (int i = 0; i < sizes.length; i++) {
sum_sizes += sizes[i];
ChatColor color = color_default;
if (colors != null && i < colors.length && colors[i] != null) color = colors[i];
bar = bar + color;
for (int j = 0; j < sizes[i]; j++)
bar = bar + pattern_full;
}
bar = bar + color_empty;
for (int j = 0; j < (max_size - sum_sizes); j++)
bar = bar + pattern_empty;
bar = bar + ChatColor.RESET + pattern_end;
return bar;
}
public static String progressBar(double value, ChatColor color, double max, int nbCar) {
return progressBar(new double[] { value }, new ChatColor[] { color }, max, nbCar);
}
}