Update everything around chat component + some other improvement

This commit is contained in:
Marc Baloup 2021-07-11 16:14:47 +02:00
parent 304faa072e
commit f4642e22a1
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893
7 changed files with 228 additions and 74 deletions

View File

@ -39,6 +39,11 @@
<artifactId>adventure-platform-bungeecord</artifactId> <artifactId>adventure-platform-bungeecord</artifactId>
<version>4.0.0-SNAPSHOT</version> <version>4.0.0-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-plain</artifactId>
<version>4.7.0</version>
</dependency>
<dependency> <dependency>

View File

@ -20,6 +20,7 @@ import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.format.TextDecoration.State; import net.kyori.adventure.text.format.TextDecoration.State;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer; import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
@ -28,7 +29,7 @@ public abstract class Chat extends ChatStatic implements HoverEventSource<Compon
protected Component component; protected Component component;
protected boolean console = false; protected boolean console = false;
public Chat(Component c) { protected Chat(Component c) {
Objects.requireNonNull(c, "Provided component must not be null"); Objects.requireNonNull(c, "Provided component must not be null");
component = c; component = c;
} }
@ -50,6 +51,10 @@ public abstract class Chat extends ChatStatic implements HoverEventSource<Compon
return LegacyComponentSerializer.legacySection().serializeOr(component, ""); return LegacyComponentSerializer.legacySection().serializeOr(component, "");
} }
public String getPlainText() {
return PlainComponentSerializer.plain().serializeOr(component, "");
}
@ -142,7 +147,7 @@ public abstract class Chat extends ChatStatic implements HoverEventSource<Compon
* @return this, for method chaining * @return this, for method chaining
*/ */
public Chat thenLeftTextCharLine(Chat leftText) { public Chat thenLeftTextCharLine(Chat leftText) {
return then(ChatUtil.leftText(chat().decorationColor().thenText(" ").then(leftText).thenText(" ").get(), config.decorationChar, return then(ChatUtil.leftText(chat().decorationColor().thenText(" ").then(leftText).thenText(" "), config.decorationChar,
config.decorationColor, config.nbCharMargin, console)); config.decorationColor, config.nbCharMargin, console));
} }
/** /**
@ -161,7 +166,7 @@ public abstract class Chat extends ChatStatic implements HoverEventSource<Compon
* @return this, for method chaining * @return this, for method chaining
*/ */
public Chat thenRightTextCharLine(Chat rightText) { public Chat thenRightTextCharLine(Chat rightText) {
return then(ChatUtil.rightText(chat().decorationColor().thenText(" ").then(rightText).thenText(" ").get(), config.decorationChar, return then(ChatUtil.rightText(chat().decorationColor().thenText(" ").then(rightText).thenText(" "), config.decorationChar,
config.decorationColor, config.nbCharMargin, console)); config.decorationColor, config.nbCharMargin, console));
} }
/** /**
@ -180,7 +185,7 @@ public abstract class Chat extends ChatStatic implements HoverEventSource<Compon
* @return this, for method chaining * @return this, for method chaining
*/ */
public Chat thenCenterTextCharLine(Chat centerText) { public Chat thenCenterTextCharLine(Chat centerText) {
return then(ChatUtil.centerText(chat().decorationColor().thenText(" ").then(centerText).thenText(" ").get(), config.decorationChar, return then(ChatUtil.centerText(chat().decorationColor().thenText(" ").then(centerText).thenText(" "), config.decorationChar,
config.decorationColor, console)); config.decorationColor, console));
} }
/** /**
@ -205,7 +210,7 @@ public abstract class Chat extends ChatStatic implements HoverEventSource<Compon
public static class FormatableChat extends Chat { public static class FormatableChat extends Chat {
public FormatableChat(Component c) { /* package */ FormatableChat(Component c) {
super(c); super(c);
} }
@ -213,7 +218,7 @@ public abstract class Chat extends ChatStatic implements HoverEventSource<Compon
public FormatableChat color(TextColor c) { component.color(c); return this; } public FormatableChat color(TextColor c) { component.color(c); return this; }
public FormatableChat color(ChatColor c) { return color(TextColor.color(c.getColor().getRGB())); } public FormatableChat color(ChatColor c) { return color(TextColor.color(c.getColor().getRGB())); }
public FormatableChat color(Color c) { return color(ChatColor.of(c)); } public FormatableChat color(Color c) { return color(TextColor.color(c.getRGB())); }
public FormatableChat color(String c) { return color(ChatColor.of(c)); } public FormatableChat color(String c) { return color(ChatColor.of(c)); }
public FormatableChat black() { return color(ChatColor.BLACK); } public FormatableChat black() { return color(ChatColor.BLACK); }
@ -279,13 +284,6 @@ public abstract class Chat extends ChatStatic implements HoverEventSource<Compon
public FormatableChat hover(BaseComponent[] v) { return hover(toAdventure(v)); } public FormatableChat hover(BaseComponent[] v) { return hover(toAdventure(v)); }
public FormatableChat hover(String legacyText) { return hover(legacyText(legacyText)); } public FormatableChat hover(String legacyText) { return hover(legacyText(legacyText)); }
@Deprecated
public FormatableChat hoverText(BaseComponent v) { return hover(v); }
@Deprecated
public FormatableChat hoverText(Chat v) { return hover(v); }
@Deprecated
public FormatableChat hoverText(String legacyText) { return hover(legacyText); }
} }
@ -341,17 +339,17 @@ public abstract class Chat extends ChatStatic implements HoverEventSource<Compon
} }
/* package */ static Component toAdventure(BaseComponent[] components) { public static Component toAdventure(BaseComponent[] components) {
return BungeeComponentSerializer.get().deserialize(components); return BungeeComponentSerializer.get().deserialize(components);
} }
/* package */ static Component toAdventure(BaseComponent component) { public static Component toAdventure(BaseComponent component) {
return toAdventure(new BaseComponent[] { component }); return toAdventure(new BaseComponent[] { component });
} }
/* package */ static BaseComponent[] toBungeeArray(Component component) { public static BaseComponent[] toBungeeArray(Component component) {
return BungeeComponentSerializer.get().serialize(component); return BungeeComponentSerializer.get().serialize(component);
} }
/* package */ static BaseComponent toBungee(Component component) { public static BaseComponent toBungee(Component component) {
BaseComponent[] arr = toBungeeArray(component); BaseComponent[] arr = toBungeeArray(component);
return arr.length == 1 ? arr[0] : new net.md_5.bungee.api.chat.TextComponent(arr); return arr.length == 1 ? arr[0] : new net.md_5.bungee.api.chat.TextComponent(arr);
} }

View File

@ -1,7 +1,6 @@
package fr.pandacube.lib.core.chat; package fr.pandacube.lib.core.chat;
import static fr.pandacube.lib.core.chat.ChatStatic.chat; import static fr.pandacube.lib.core.chat.ChatStatic.chat;
import static fr.pandacube.lib.core.chat.ChatStatic.chatComponent;
import static fr.pandacube.lib.core.chat.ChatStatic.legacyText; import static fr.pandacube.lib.core.chat.ChatStatic.legacyText;
import static fr.pandacube.lib.core.chat.ChatStatic.text; import static fr.pandacube.lib.core.chat.ChatStatic.text;
@ -11,6 +10,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.stream.Collectors;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
@ -47,24 +47,23 @@ public class ChatUtil {
public static BaseComponent createURLLink(String text, String url) { public static FormatableChat createURLLink(String text, String url) {
return createURLLink(legacyText(text), url, null); return createURLLink(legacyText(text), url, null);
} }
public static BaseComponent createURLLink(String text, String url, String hoverText) { public static FormatableChat createURLLink(String text, String url, String hoverText) {
return createURLLink(legacyText(text), url, hoverText != null ? legacyText(hoverText) : null); return createURLLink(legacyText(text), url, hoverText != null ? legacyText(hoverText) : null);
} }
/* package */ static BaseComponent createURLLink(Chat element, String url, Chat hover) { /* package */ static FormatableChat createURLLink(Chat element, String url, Chat hover) {
String dispURL = (url.length() > 50) ? (url.substring(0, 48) + "...") : url; String dispURL = (url.length() > 50) ? (url.substring(0, 48) + "...") : url;
return chat() return (FormatableChat) chat()
.clickURL(url) .clickURL(url)
.color(Chat.getConfig().urlColor) .color(Chat.getConfig().urlColor)
.hover( .hover(
hover != null ? hover : Chat.text(dispURL) hover != null ? hover : Chat.text(dispURL)
) )
.then(element) .then(element);
.get();
} }
@ -74,20 +73,20 @@ public class ChatUtil {
public static BaseComponent createCommandLink(String text, String commandWithSlash, String hoverText) { public static FormatableChat createCommandLink(String text, String commandWithSlash, String hoverText) {
return createCommandLink(text, commandWithSlash, hoverText == null ? null : legacyText(hoverText)); return createCommandLink(text, commandWithSlash, hoverText == null ? null : legacyText(hoverText));
} }
public static BaseComponent createCommandLink(String text, String commandWithSlash, Chat hoverText) { public static FormatableChat createCommandLink(String text, String commandWithSlash, Chat hoverText) {
return createCommandLink(legacyText(text), commandWithSlash, hoverText); return createCommandLink(legacyText(text), commandWithSlash, hoverText);
} }
/* package */ static BaseComponent createCommandLink(Chat d, String commandWithSlash, Chat hoverText) { /* package */ static FormatableChat createCommandLink(Chat d, String commandWithSlash, Chat hoverText) {
FormatableChat c = chat() FormatableChat c = chat()
.clickCommand(commandWithSlash) .clickCommand(commandWithSlash)
.color(Chat.getConfig().commandColor); .color(Chat.getConfig().commandColor);
if (hoverText != null) if (hoverText != null)
c.hover(hoverText); c.hover(hoverText);
return c.then(d).get(); return (FormatableChat) c.then(d);
} }
@ -99,20 +98,20 @@ public class ChatUtil {
public static BaseComponent createCommandSuggest(String text, String commandWithSlash, String hoverText) { public static FormatableChat createCommandSuggest(String text, String commandWithSlash, String hoverText) {
return createCommandSuggest(text, commandWithSlash, hoverText == null ? null : legacyText(hoverText)); return createCommandSuggest(text, commandWithSlash, hoverText == null ? null : legacyText(hoverText));
} }
public static BaseComponent createCommandSuggest(String text, String commandWithSlash, Chat hoverText) { public static FormatableChat createCommandSuggest(String text, String commandWithSlash, Chat hoverText) {
return createCommandSuggest(legacyText(text), commandWithSlash, hoverText); return createCommandSuggest(legacyText(text), commandWithSlash, hoverText);
} }
/* package */ static BaseComponent createCommandSuggest(Chat d, String commandWithSlash, Chat hoverText) { /* package */ static FormatableChat createCommandSuggest(Chat d, String commandWithSlash, Chat hoverText) {
FormatableChat c = chat() FormatableChat c = chat()
.clickSuggest(commandWithSlash) .clickSuggest(commandWithSlash)
.color(Chat.getConfig().commandColor); .color(Chat.getConfig().commandColor);
if (hoverText != null) if (hoverText != null)
c.hover(hoverText); c.hover(hoverText);
return c.then(d).get(); return (FormatableChat) c.then(d);
} }
@ -125,7 +124,7 @@ public class ChatUtil {
/** /**
* @param cmdFormat the command with %d inside to be replaced with the page number (must start with slash) * @param cmdFormat the command with %d inside to be replaced with the page number (must start with slash)
*/ */
public static BaseComponent createPagination(String prefix, String cmdFormat, int currentPage, int nbPages, int nbPagesToDisplay) { public static Chat createPagination(String prefix, String cmdFormat, int currentPage, int nbPages, int nbPagesToDisplay) {
Set<Integer> pagesToDisplay = new TreeSet<>(); Set<Integer> pagesToDisplay = new TreeSet<>();
for (int i = 0; i < nbPagesToDisplay && i < nbPages && nbPages - i > 0; i++) { for (int i = 0; i < nbPagesToDisplay && i < nbPages && nbPages - i > 0; i++) {
@ -160,7 +159,7 @@ public class ChatUtil {
else else
first = false; first = false;
FormatableChat pDisp = chatComponent(createCommandLink(Integer.toString(page), String.format(cmdFormat, page), "Aller à la page " + page)); FormatableChat pDisp = (FormatableChat) createCommandLink(Integer.toString(page), String.format(cmdFormat, page), "Aller à la page " + page);
if (page == currentPage) { if (page == currentPage) {
pDisp.color(Chat.getConfig().highlightedCommandColor); pDisp.color(Chat.getConfig().highlightedCommandColor);
} }
@ -170,7 +169,7 @@ public class ChatUtil {
} }
return d.get(); return d;
} }
@ -182,10 +181,10 @@ public class ChatUtil {
public static BaseComponent centerText(BaseComponent text, char repeatedChar, ChatColor decorationColor, public static Chat centerText(Chat text, char repeatedChar, ChatColor decorationColor,
boolean console) { boolean console) {
int textWidth = componentWidth(text, console); int textWidth = componentWidth(text.get(), console);
int maxWidth = (console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH; int maxWidth = (console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH;
if (textWidth > maxWidth) if (textWidth > maxWidth)
@ -206,14 +205,14 @@ public class ChatUtil {
if (repeatedChar != ' ') if (repeatedChar != ' ')
d.then(text(sideChars).color(decorationColor)); d.then(text(sideChars).color(decorationColor));
return d.get(); return d;
} }
public static BaseComponent leftText(BaseComponent text, char repeatedChar, ChatColor decorationColor, int nbLeft, public static Chat leftText(Chat text, char repeatedChar, ChatColor decorationColor, int nbLeft,
boolean console) { boolean console) {
int textWidth = componentWidth(text, console); int textWidth = componentWidth(text.get(), console);
int maxWidth = (console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH; int maxWidth = (console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH;
int repeatedCharWidth = charW(repeatedChar, console, false); int repeatedCharWidth = charW(repeatedChar, console, false);
int leftWidth = nbLeft * repeatedCharWidth; int leftWidth = nbLeft * repeatedCharWidth;
@ -229,14 +228,14 @@ public class ChatUtil {
if (repeatedChar != ' ') { if (repeatedChar != ' ') {
d.then(text(repeatedChar(repeatedChar, rightNbChar)).color(decorationColor)); d.then(text(repeatedChar(repeatedChar, rightNbChar)).color(decorationColor));
} }
return d.get(); return d;
} }
public static BaseComponent rightText(BaseComponent text, char repeatedChar, ChatColor decorationColor, int nbRight, public static Chat rightText(Chat text, char repeatedChar, ChatColor decorationColor, int nbRight,
boolean console) { boolean console) {
int textWidth = componentWidth(text, console); int textWidth = componentWidth(text.get(), console);
int maxWidth = (console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH; int maxWidth = (console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH;
int repeatedCharWidth = charW(repeatedChar, console, false); int repeatedCharWidth = charW(repeatedChar, console, false);
int rightWidth = nbRight * repeatedCharWidth; int rightWidth = nbRight * repeatedCharWidth;
@ -252,13 +251,13 @@ public class ChatUtil {
if (repeatedChar != ' ') { if (repeatedChar != ' ') {
d.then(text(repeatedChar(repeatedChar, nbRight)).color(decorationColor)); d.then(text(repeatedChar(repeatedChar, nbRight)).color(decorationColor));
} }
return d.get(); return d;
} }
public static BaseComponent emptyLine(char repeatedChar, ChatColor decorationColor, boolean console) { public static Chat emptyLine(char repeatedChar, ChatColor decorationColor, boolean console) {
int count = ((console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH) / charW(repeatedChar, console, false); int count = ((console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH) / charW(repeatedChar, console, false);
return text(repeatedChar(repeatedChar, count)).color(decorationColor).get(); return text(repeatedChar(repeatedChar, count)).color(decorationColor);
} }
private static String repeatedChar(char repeatedChar, int count) { private static String repeatedChar(char repeatedChar, int count) {
@ -329,6 +328,12 @@ public class ChatUtil {
public static List<Chat> wrapInLimitedPixelsToChat(String legacyText, int pixelWidth) {
return wrapInLimitedPixels(legacyText, pixelWidth).stream()
.map(t -> legacyText(t))
.collect(Collectors.toList());
}
public static List<String> wrapInLimitedPixels(String legacyText, int pixelWidth) { public static List<String> wrapInLimitedPixels(String legacyText, int pixelWidth) {
List<String> lines = new ArrayList<>(); List<String> lines = new ArrayList<>();
@ -341,7 +346,7 @@ public class ChatUtil {
String currentWord = ""; String currentWord = "";
int currentWordSize = 0; int currentWordSize = 0;
boolean bold = false; boolean bold = false;
boolean firstCharCurrentWorldBold = false; boolean firstCharCurrentWordBold = false;
do { do {
char c = legacyText.charAt(index); char c = legacyText.charAt(index);
@ -366,7 +371,7 @@ public class ChatUtil {
String lastStyle = ChatColorUtil.getLastColors(currentLine); String lastStyle = ChatColorUtil.getLastColors(currentLine);
if (currentWord.charAt(0) == ' ') { if (currentWord.charAt(0) == ' ') {
currentWord = currentWord.substring(1); currentWord = currentWord.substring(1);
currentWordSize -= charW(' ', false, firstCharCurrentWorldBold); currentWordSize -= charW(' ', false, firstCharCurrentWordBold);
} }
currentLine = (lastStyle.equals("§r") ? "" : lastStyle) + currentWord; currentLine = (lastStyle.equals("§r") ? "" : lastStyle) + currentWord;
currentLineSize = currentWordSize; currentLineSize = currentWordSize;
@ -377,7 +382,7 @@ public class ChatUtil {
} }
currentWord = ""+c; currentWord = ""+c;
currentWordSize = charW(c, false, bold); currentWordSize = charW(c, false, bold);
firstCharCurrentWorldBold = bold; firstCharCurrentWordBold = bold;
} }
else if (c == '\n') { else if (c == '\n') {
if (currentLineSize + currentWordSize > pixelWidth && currentLineSize > 0) { // wrap before word if (currentLineSize + currentWordSize > pixelWidth && currentLineSize > 0) { // wrap before word
@ -385,7 +390,7 @@ public class ChatUtil {
String lastStyle = ChatColorUtil.getLastColors(currentLine); String lastStyle = ChatColorUtil.getLastColors(currentLine);
if (currentWord.charAt(0) == ' ') { if (currentWord.charAt(0) == ' ') {
currentWord = currentWord.substring(1); currentWord = currentWord.substring(1);
currentWordSize -= charW(' ', false, firstCharCurrentWorldBold); currentWordSize -= charW(' ', false, firstCharCurrentWordBold);
} }
currentLine = (lastStyle.equals("§r") ? "" : lastStyle) + currentWord; currentLine = (lastStyle.equals("§r") ? "" : lastStyle) + currentWord;
currentLineSize = currentWordSize; currentLineSize = currentWordSize;
@ -402,7 +407,7 @@ public class ChatUtil {
currentLineSize = 0; currentLineSize = 0;
currentWord = ""; currentWord = "";
currentWordSize = 0; currentWordSize = 0;
firstCharCurrentWorldBold = bold; firstCharCurrentWordBold = bold;
} }
else { else {
currentWord += c; currentWord += c;

View File

@ -5,6 +5,10 @@ import java.util.UUID;
import fr.pandacube.lib.core.chat.Chat; import fr.pandacube.lib.core.chat.Chat;
import fr.pandacube.lib.core.db.DBException; import fr.pandacube.lib.core.db.DBException;
import net.kyori.adventure.identity.Identified;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
public interface IOnlinePlayer extends IOffPlayer { public interface IOnlinePlayer extends IOffPlayer {
@ -113,15 +117,32 @@ public interface IOnlinePlayer extends IOffPlayer {
* the chat is activated. * the chat is activated.
* @param message the message to display. * @param message the message to display.
*/ */
@Deprecated
public abstract void sendMessage(BaseComponent message); public abstract void sendMessage(BaseComponent message);
/**
* Display the provided message in the players chat, if
* the chat is activated.
* @param message the message to display.
*/
public abstract void sendMessage(Component message);
/**
* Display the provided message in the players chat, if
* the chat is activated.
* @param message the message to display.
*/
public default void sendMessage(ComponentLike message) {
sendMessage(message.asComponent());
}
/** /**
* Display the provided message in the players chat, if * Display the provided message in the players chat, if
* the chat is activated * the chat is activated
* @param message the message to display * @param message the message to display
*/ */
public default void sendMessage(Chat message) { public default void sendMessage(Chat message) {
sendMessage(message.get()); sendMessage(message.getAdv());
} }
/** /**
@ -134,8 +155,46 @@ public interface IOnlinePlayer extends IOffPlayer {
* the sender. This parameter is only there to be transmitted to the client, so client side filtering can * the sender. This parameter is only there to be transmitted to the client, so client side filtering can
* be processed. * be processed.
*/ */
@Deprecated
public abstract void sendMessage(BaseComponent message, UUID sender); public abstract void sendMessage(BaseComponent message, UUID sender);
/**
* Display the provided message in the players chat, if
* they allows to display CHAT messages
* @param message the message to display.
* @param sender the player causing the send of this message. Client side filtering may occur.
* May be null if we dont want client filtering, but still consider the message as CHAT message.
* @implNote implementation of this method should not filter the send of the message, based on
* the sender. This parameter is only there to be transmitted to the client, so client side filtering can
* be processed.
*/
public default void sendMessage(Component message, UUID sender) {
sendMessage(message, () -> sender == null ? Identity.nil() : Identity.identity(sender));
}
/**
* Display the provided message in the players chat, if
* they allows to display CHAT messages
* @param message the message to display.
* @param sender the player causing the send of this message. Client side filtering may occur.
* May be null if we dont want client filtering, but still consider the message as CHAT message.
* @implNote implementation of this method should not filter the send of the message, based on
* the sender. This parameter is only there to be transmitted to the client, so client side filtering can
* be processed.
*/
public abstract void sendMessage(Component message, Identified sender);
/**
* Display the provided message in the players chat, if
* they allows to display CHAT messages
* @param message the message to display
* @param sender the player causing the send of this message. Client side filtering may occur.
* May be null if we dont want client filtering, but still consider the message as CHAT message.
*/
public default void sendMessage(ComponentLike message, UUID sender) {
sendMessage(message.asComponent(), sender);
}
/** /**
* Display the provided message in the players chat, if * Display the provided message in the players chat, if
* they allows to display CHAT messages * they allows to display CHAT messages
@ -144,7 +203,7 @@ public interface IOnlinePlayer extends IOffPlayer {
* May be null if we dont want client filtering, but still consider the message as CHAT message. * May be null if we dont want client filtering, but still consider the message as CHAT message.
*/ */
public default void sendMessage(Chat message, UUID sender) { public default void sendMessage(Chat message, UUID sender) {
sendMessage(message.get(), sender); sendMessage(message.getAdv(), sender);
} }
/** /**
@ -152,6 +211,16 @@ public interface IOnlinePlayer extends IOffPlayer {
* activated, prepended with the server prefix. * activated, prepended with the server prefix.
* @param message the message to display * @param message the message to display
*/ */
public default void sendPrefixedMessage(Component message) {
sendMessage(IPlayerManager.prefixedAndColored(message));
}
/**
* Display the provided message in the players chat, if the chat is
* activated, prepended with the server prefix.
* @param message the message to display
*/
@Deprecated
public default void sendPrefixedMessage(BaseComponent message) { public default void sendPrefixedMessage(BaseComponent message) {
sendMessage(IPlayerManager.prefixedAndColored(message)); sendMessage(IPlayerManager.prefixedAndColored(message));
} }
@ -162,7 +231,7 @@ public interface IOnlinePlayer extends IOffPlayer {
* @param message the message to display * @param message the message to display
*/ */
public default void sendPrefixedMessage(Chat message) { public default void sendPrefixedMessage(Chat message) {
sendPrefixedMessage(message.get()); sendPrefixedMessage(message.getAdv());
} }
/** /**
@ -173,8 +242,19 @@ public interface IOnlinePlayer extends IOffPlayer {
* @param stay Stay time in tick * @param stay Stay time in tick
* @param fadeOut Fade out time in tick * @param fadeOut Fade out time in tick
*/ */
@Deprecated
public abstract void sendTitle(BaseComponent title, BaseComponent subtitle, int fadeIn, int stay, int fadeOut); public abstract void sendTitle(BaseComponent title, BaseComponent subtitle, int fadeIn, int stay, int fadeOut);
/**
* Display a title in the middle of the screen.
* @param title The big text
* @param subtitle The less big text
* @param fadeIn Fade in time in tick
* @param stay Stay time in tick
* @param fadeOut Fade out time in tick
*/
public abstract void sendTitle(Component title, Component subtitle, int fadeIn, int stay, int fadeOut);
/** /**
* Display a title in the middle of the screen. * Display a title in the middle of the screen.
* @param title The big text * @param title The big text
@ -184,7 +264,7 @@ public interface IOnlinePlayer extends IOffPlayer {
* @param fadeOut Fade out time in tick * @param fadeOut Fade out time in tick
*/ */
public default void sendTitle(Chat title, Chat subtitle, int fadeIn, int stay, int fadeOut) { public default void sendTitle(Chat title, Chat subtitle, int fadeIn, int stay, int fadeOut) {
sendTitle(title.get(), subtitle.get(), fadeIn, stay, fadeOut); sendTitle(title.getAdv(), subtitle.getAdv(), fadeIn, stay, fadeOut);
} }
/** /**

View File

@ -17,6 +17,7 @@ import fr.pandacube.lib.core.chat.Chat;
import fr.pandacube.lib.core.db.DB; import fr.pandacube.lib.core.db.DB;
import fr.pandacube.lib.core.db.DBInitTableException; import fr.pandacube.lib.core.db.DBInitTableException;
import fr.pandacube.lib.core.util.Log; import fr.pandacube.lib.core.util.Log;
import net.kyori.adventure.text.Component;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
public abstract class IPlayerManager<OP extends IOnlinePlayer, OF extends IOffPlayer> { public abstract class IPlayerManager<OP extends IOnlinePlayer, OF extends IOffPlayer> {
@ -98,9 +99,7 @@ public abstract class IPlayerManager<OP extends IOnlinePlayer, OF extends IOffPl
protected abstract OF newOffPlayerInstance(UUID p); protected abstract OF newOffPlayerInstance(UUID p);
protected abstract void sendMessageToConsole(BaseComponent message); protected abstract void sendMessageToConsole(Component message);
@ -114,12 +113,20 @@ public abstract class IPlayerManager<OP extends IOnlinePlayer, OF extends IOffPl
@Deprecated
public static BaseComponent prefixedAndColored(BaseComponent message) { public static BaseComponent prefixedAndColored(BaseComponent message) {
return prefixedAndColored(Chat.chatComponent(message)).get();
}
public static Component prefixedAndColored(Component message) {
return prefixedAndColored(Chat.chatComponent(message)).getAdv();
}
public static Chat prefixedAndColored(Chat message) {
return Chat.chat() return Chat.chat()
.color(Chat.getConfig().broadcastColor) .color(Chat.getConfig().broadcastColor)
.then(Chat.getConfig().prefix.get()) .then(Chat.getConfig().prefix.get())
.then(message) .then(message);
.get();
} }
@ -150,7 +157,7 @@ public abstract class IPlayerManager<OP extends IOnlinePlayer, OF extends IOffPl
* *
* @throws IllegalArgumentException if message is null. * @throws IllegalArgumentException if message is null.
*/ */
public static void broadcast(BaseComponent message, boolean prefix, boolean console, String permission, UUID sourcePlayer) { public static void broadcast(Component message, boolean prefix, boolean console, String permission, UUID sourcePlayer) {
Objects.requireNonNull(message, "message cannot be null"); Objects.requireNonNull(message, "message cannot be null");
IOffPlayer oSourcePlayer = getInstance().getOffline(sourcePlayer); IOffPlayer oSourcePlayer = getInstance().getOffline(sourcePlayer);
@ -168,7 +175,7 @@ public abstract class IPlayerManager<OP extends IOnlinePlayer, OF extends IOffPl
op.sendMessage(message, sourcePlayer); // CHAT message with UUID op.sendMessage(message, sourcePlayer); // CHAT message with UUID
} }
else { else {
op.sendMessage(message, null); // CHAT message without UUID op.sendMessage(message, new UUID(0, 0)); // CHAT message without UUID
} }
} }
else else
@ -191,7 +198,7 @@ public abstract class IPlayerManager<OP extends IOnlinePlayer, OF extends IOffPl
* @param permission if not null, the message is only sent to player with this permission. * @param permission if not null, the message is only sent to player with this permission.
* @throws IllegalArgumentException if message is null. * @throws IllegalArgumentException if message is null.
*/ */
public static void broadcast(BaseComponent message, boolean prefix, boolean console, String permission) { public static void broadcast(Component message, boolean prefix, boolean console, String permission) {
broadcast(message, prefix, console, permission, null); broadcast(message, prefix, console, permission, null);
} }
@ -210,7 +217,7 @@ public abstract class IPlayerManager<OP extends IOnlinePlayer, OF extends IOffPl
* to players ignoring the provided player. * to players ignoring the provided player.
* @throws IllegalArgumentException if message is null. * @throws IllegalArgumentException if message is null.
*/ */
public static void broadcast(BaseComponent message, boolean prefix, boolean console, UUID sourcePlayer) { public static void broadcast(Component message, boolean prefix, boolean console, UUID sourcePlayer) {
broadcast(message, prefix, console, null, sourcePlayer); broadcast(message, prefix, console, null, sourcePlayer);
} }
@ -228,7 +235,26 @@ public abstract class IPlayerManager<OP extends IOnlinePlayer, OF extends IOffPl
* @param console if the message must be displayed in the console. * @param console if the message must be displayed in the console.
* @throws IllegalArgumentException if message is null. * @throws IllegalArgumentException if message is null.
*/ */
@Deprecated
public static void broadcast(BaseComponent message, boolean prefix, boolean console) { public static void broadcast(BaseComponent message, boolean prefix, boolean console) {
broadcast(Chat.toAdventure(message), prefix, console, null, null);
}
/**
* Broadcast a message to all players, and eventually to the console.
* <p>
* This method does not restrict the reception of the message to a specific permission. If you
* want to specify a permission, use {@link #broadcast(BaseComponent, boolean, boolean, String)}.
* <p>
* This method assumes this message is not caused by a specific player. To specify the source player, use
* {@link #broadcast(BaseComponent, boolean, boolean, UUID)}.
*
* @param message the message to send.
* @param prefix if the server prefix will be prepended to the message.
* @param console if the message must be displayed in the console.
* @throws IllegalArgumentException if message is null.
*/
public static void broadcast(Component message, boolean prefix, boolean console) {
broadcast(message, prefix, console, null, null); broadcast(message, prefix, console, null, null);
} }
@ -248,7 +274,7 @@ public abstract class IPlayerManager<OP extends IOnlinePlayer, OF extends IOffPl
* If null, the message will be sent to all players and to console. * If null, the message will be sent to all players and to console.
* @throws IllegalArgumentException if message is null. * @throws IllegalArgumentException if message is null.
*/ */
public static void broadcast(BaseComponent message, boolean prefix, String permission) { public static void broadcast(Component message, boolean prefix, String permission) {
broadcast(message, prefix, (permission == null), permission, null); broadcast(message, prefix, (permission == null), permission, null);
} }
@ -269,7 +295,7 @@ public abstract class IPlayerManager<OP extends IOnlinePlayer, OF extends IOffPl
* to players ignoring the provided player. * to players ignoring the provided player.
* @throws IllegalArgumentException if message is null. * @throws IllegalArgumentException if message is null.
*/ */
public static void broadcast(BaseComponent message, boolean prefix, UUID sourcePlayer) { public static void broadcast(Component message, boolean prefix, UUID sourcePlayer) {
broadcast(message, prefix, true, null, sourcePlayer); broadcast(message, prefix, true, null, sourcePlayer);
} }
@ -289,7 +315,7 @@ public abstract class IPlayerManager<OP extends IOnlinePlayer, OF extends IOffPl
* @param prefix if the server prefix will be prepended to the message. * @param prefix if the server prefix will be prepended to the message.
* @throws IllegalArgumentException if message is null. * @throws IllegalArgumentException if message is null.
*/ */
public static void broadcast(BaseComponent message, boolean prefix) { public static void broadcast(Component message, boolean prefix) {
broadcast(message, prefix, true, null, null); broadcast(message, prefix, true, null, null);
} }
@ -311,7 +337,7 @@ public abstract class IPlayerManager<OP extends IOnlinePlayer, OF extends IOffPl
*/ */
public static void broadcast(Chat message, boolean prefix, boolean console, String permission, UUID sourcePlayer) { public static void broadcast(Chat message, boolean prefix, boolean console, String permission, UUID sourcePlayer) {
Objects.requireNonNull(message, "message cannot be null"); Objects.requireNonNull(message, "message cannot be null");
broadcast(message.get(), prefix, console, permission, sourcePlayer); broadcast(message.getAdv(), prefix, console, permission, sourcePlayer);
} }
/** /**

View File

@ -34,6 +34,10 @@
<groupId>net.kyori</groupId> <groupId>net.kyori</groupId>
<artifactId>adventure-api</artifactId> <artifactId>adventure-api</artifactId>
</exclusion> </exclusion>
<exclusion>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-plain</artifactId>
</exclusion>
</exclusions> </exclusions>
</dependency> </dependency>

View File

@ -1,5 +1,6 @@
package fr.pandacube.lib.paper.util; package fr.pandacube.lib.paper.util;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -21,12 +22,41 @@ import net.kyori.adventure.text.format.TextDecoration.State;
public class ItemStackBuilder { public class ItemStackBuilder {
/**
* Create a builder with a clone of the provided ItemStack.
*
* The returned builder will not alter the provided ItemStack.
* IF you want to modify the ItemStack with the builder, please use {@link #wrap(ItemStack)}.
*
* @param base the original ItemStack.
* @return the builder
*/
public static ItemStackBuilder of(ItemStack base) { public static ItemStackBuilder of(ItemStack base) {
return new ItemStackBuilder(base.clone()); return wrap(base.clone());
} }
/**
* Create a builder of a new ItemStack with the specified Material.
*
* @param mat the material of the new builded ItemStack
* @return the builder
*/
public static ItemStackBuilder of(Material mat) { public static ItemStackBuilder of(Material mat) {
return new ItemStackBuilder(new ItemStack(mat)); return wrap(new ItemStack(mat));
}
/**
* Create a builder that will alter the data of the provided ItemStack.
*
* The {@link #build()} method of thez returned builder will return the same instance
* of ItemStack as the parameter of this method.
*
* To create a builder that doesnt modify the provided ItemStack, use {@link #of(ItemStack)}.
* @param stack
* @return the builder
*/
public static ItemStackBuilder wrap(ItemStack stack) {
return new ItemStackBuilder(stack);
} }
@ -95,14 +125,14 @@ public class ItemStackBuilder {
return rawLore(Collections.emptyList()); return rawLore(Collections.emptyList());
} }
public ItemStackBuilder addLoreAfter(List<Chat> lore) { public ItemStackBuilder addLoreAfter(List<Chat> lores) {
if (lore != null) { if (lores != null) {
List<Component> baseLore = getOrInitMeta().lore(); List<Component> baseLore = getOrInitMeta().lore();
if (baseLore == null) baseLore = Collections.emptyList(); if (baseLore == null) baseLore = Collections.emptyList();
return rawLore( return rawLore(
Streams.concat( Streams.concat(
baseLore.stream(), baseLore.stream(),
lore.stream() lores.stream()
.map(line -> Chat.italicFalseIfNotSet(line).getAdv()) .map(line -> Chat.italicFalseIfNotSet(line).getAdv())
) )
.collect(Collectors.toList())); .collect(Collectors.toList()));
@ -111,6 +141,12 @@ public class ItemStackBuilder {
return this; return this;
} }
public ItemStackBuilder addLoreAfter(Chat... lores) {
if (lores == null || lores.length == 0)
return this;
return addLoreAfter(Arrays.asList(lores));
}
public ItemStackBuilder enchant(Enchantment ench, int level) { public ItemStackBuilder enchant(Enchantment ench, int level) {
getOrInitMeta().addEnchant(ench, level, true); getOrInitMeta().addEnchant(ench, level, true);
updateMeta(); updateMeta();