From 2e334af7a1e79807f942eb32313bb747fa187c03 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Mon, 19 Jul 2021 01:27:50 +0200 Subject: [PATCH] Improve chat API + removed old Player API --- .../java/fr/pandacube/lib/core/chat/Chat.java | 151 +++++++++++------- .../pandacube/lib/core/chat/ChatStatic.java | 4 +- .../lib/core/players/IOnlinePlayer.java | 43 ----- 3 files changed, 99 insertions(+), 99 deletions(-) diff --git a/Core/src/main/java/fr/pandacube/lib/core/chat/Chat.java b/Core/src/main/java/fr/pandacube/lib/core/chat/Chat.java index 177a1b4..68c5f01 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/chat/Chat.java +++ b/Core/src/main/java/fr/pandacube/lib/core/chat/Chat.java @@ -2,15 +2,24 @@ package fr.pandacube.lib.core.chat; import java.awt.Color; import java.util.Objects; +import java.util.function.Consumer; import java.util.function.Supplier; import java.util.function.UnaryOperator; import org.checkerframework.checker.nullness.qual.NonNull; import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.BlockNBTComponent; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.ComponentBuilder; import net.kyori.adventure.text.ComponentLike; +import net.kyori.adventure.text.EntityNBTComponent; +import net.kyori.adventure.text.KeybindComponent; +import net.kyori.adventure.text.ScoreComponent; +import net.kyori.adventure.text.SelectorComponent; +import net.kyori.adventure.text.StorageNBTComponent; import net.kyori.adventure.text.TextComponent; +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; @@ -26,33 +35,33 @@ import net.md_5.bungee.api.chat.BaseComponent; public abstract class Chat extends ChatStatic implements HoverEventSource, ComponentLike { - protected Component component; + protected ComponentBuilder builder; protected boolean console = false; - protected Chat(Component c) { - Objects.requireNonNull(c, "Provided component must not be null"); - component = c; - } - - public BaseComponent get() { - return toBungee(component); + protected Chat(ComponentBuilder b) { + Objects.requireNonNull(b, "Provided component builder must not be null"); + builder = b; } public Component getAdv() { - return component; + return builder.build(); + } + + public BaseComponent get() { + return toBungee(getAdv()); } public BaseComponent[] getAsArray() { - return toBungeeArray(component); + return toBungeeArray(getAdv()); } public String getLegacyText() { - return LegacyComponentSerializer.legacySection().serializeOr(component, ""); + return LegacyComponentSerializer.legacySection().serializeOr(getAdv(), ""); } public String getPlainText() { - return PlainComponentSerializer.plain().serializeOr(component, ""); + return PlainComponentSerializer.plain().serializeOr(getAdv(), ""); } @@ -61,15 +70,6 @@ public abstract class Chat extends ChatStatic implements HoverEventSource c) { super(c); } public FormatableChat console(boolean c) { console = c; return this; } - public FormatableChat color(TextColor c) { component.color(c); return this; } + public FormatableChat color(TextColor c) { builder.color(c); return this; } public FormatableChat color(ChatColor c) { return color(TextColor.color(c.getColor().getRGB())); } public FormatableChat color(Color c) { return color(TextColor.color(c.getRGB())); } public FormatableChat color(String c) { return color(ChatColor.of(c)); } @@ -246,15 +240,11 @@ public abstract class Chat extends ChatStatic implements HoverEventSource styleOp) { - component.style(styleOp.apply(component.style())); - return this; - } - private FormatableChat setDecoration(TextDecoration deco, Boolean state) { - return setStyle(s -> s.decoration(deco, State.byBoolean(state))); - } + private FormatableChat setStyle(Consumer styleOp) { builder.style(styleOp); return this; } - public FormatableChat font(Key f) { return setStyle(s -> s.font(f)); } + private FormatableChat setDecoration(TextDecoration deco, Boolean state) { + return setStyle(b -> b.decoration(deco, State.byBoolean(state))); + } public FormatableChat bold(Boolean b) { return setDecoration(TextDecoration.BOLD, b); } public FormatableChat bold() { return bold(true); } @@ -270,17 +260,19 @@ public abstract class Chat extends ChatStatic implements HoverEventSource s.font(f)); } + + public FormatableChat shiftClickInsertion(String i) { builder.insertion(i); return this; } + + private FormatableChat click(ClickEvent e) { builder.clickEvent(e); return this; } public FormatableChat clickCommand(String cmdWithSlash) { return click(ClickEvent.runCommand(cmdWithSlash)); } public FormatableChat clickSuggest(String cmdWithSlash) { return click(ClickEvent.suggestCommand(cmdWithSlash)); } public FormatableChat clickClipboard(String value) { return click(ClickEvent.copyToClipboard(value)); } public FormatableChat clickURL(String url) { return click(ClickEvent.openUrl(url)); } public FormatableChat clickBookPage(int page) { return click(ClickEvent.changePage(page)); } - public FormatableChat hover(HoverEventSource e) { component.hoverEvent(e); return this; } + public FormatableChat hover(HoverEventSource e) { builder.hoverEvent(e); return this; } public FormatableChat hover(Chat v) { return hover(v.getAdv()); } public FormatableChat hover(BaseComponent v) { return hover(toAdventure(v)); } public FormatableChat hover(BaseComponent[] v) { return hover(toAdventure(v)); } @@ -334,6 +326,8 @@ public abstract class Chat extends ChatStatic implements HoverEventSource componentToBuilder(Component c) { + ComponentBuilder builder; + if (c instanceof TextComponent) { + builder = Component.text() + .content(((TextComponent) c).content()); + } + else if (c instanceof TranslatableComponent) { + builder = Component.translatable() + .key(((TranslatableComponent) c).key()) + .args(((TranslatableComponent) c).args()); + } + else if (c instanceof SelectorComponent) { + builder = Component.selector() + .pattern(((SelectorComponent) c).pattern()); + } + else if (c instanceof ScoreComponent) { + builder = Component.score() + .name(((ScoreComponent) c).name()) + .objective(((ScoreComponent) c).objective()); + } + else if (c instanceof KeybindComponent) { + builder = Component.keybind() + .keybind(((KeybindComponent) c).keybind()); + } + else if (c instanceof BlockNBTComponent) { + builder = Component.blockNBT() + .interpret(((BlockNBTComponent) c).interpret()) + .nbtPath(((BlockNBTComponent) c).nbtPath()) + .pos(((BlockNBTComponent) c).pos()); + } + else if (c instanceof EntityNBTComponent) { + builder = Component.entityNBT() + .interpret(((EntityNBTComponent) c).interpret()) + .nbtPath(((EntityNBTComponent) c).nbtPath()) + .selector(((EntityNBTComponent) c).selector()); + } + else if (c instanceof StorageNBTComponent) { + builder = Component.storageNBT() + .interpret(((StorageNBTComponent) c).interpret()) + .nbtPath(((StorageNBTComponent) c).nbtPath()) + .storage(((StorageNBTComponent) c).storage()); + } + else { + throw new IllegalArgumentException("Unknows component type " + c.getClass()); + } + return builder.style(c.style()); + } + public static Chat italicFalseIfNotSet(Chat c) { - if (c instanceof FormatableChat) { - if (c.component.style().decoration(TextDecoration.ITALIC) == State.NOT_SET) + c.builder.style(b -> { + if (b.build().decoration(TextDecoration.ITALIC) == State.NOT_SET) { ((FormatableChat) c).italic(false); - } + } + }); return c; } diff --git a/Core/src/main/java/fr/pandacube/lib/core/chat/ChatStatic.java b/Core/src/main/java/fr/pandacube/lib/core/chat/ChatStatic.java index ccc526f..99734f5 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/chat/ChatStatic.java +++ b/Core/src/main/java/fr/pandacube/lib/core/chat/ChatStatic.java @@ -12,11 +12,11 @@ public abstract class ChatStatic { public static FormatableChat chatComponent(Component c) { - return new FormatableChat(c); + return new FormatableChat(Chat.componentToBuilder(c)); } public static FormatableChat chatComponent(BaseComponent c) { - return new FormatableChat(Chat.toAdventure(c)); + return new FormatableChat(Chat.componentToBuilder(Chat.toAdventure(c))); } public static FormatableChat chat() { diff --git a/Core/src/main/java/fr/pandacube/lib/core/players/IOnlinePlayer.java b/Core/src/main/java/fr/pandacube/lib/core/players/IOnlinePlayer.java index d1c88ea..85df13f 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/players/IOnlinePlayer.java +++ b/Core/src/main/java/fr/pandacube/lib/core/players/IOnlinePlayer.java @@ -9,7 +9,6 @@ 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; public interface IOnlinePlayer extends IOffPlayer { @@ -112,14 +111,6 @@ public interface IOnlinePlayer extends IOffPlayer { * Sending packet and stuff to player */ - /** - * Display the provided message in the player’s chat, if - * the chat is activated. - * @param message the message to display. - */ - @Deprecated - public abstract void sendMessage(BaseComponent message); - /** * Display the provided message in the player’s chat, if * the chat is activated. @@ -145,19 +136,6 @@ public interface IOnlinePlayer extends IOffPlayer { sendMessage(message.getAdv()); } - /** - * Display the provided message in the player’s 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 don’t 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. - */ - @Deprecated - public abstract void sendMessage(BaseComponent message, UUID sender); - /** * Display the provided message in the player’s chat, if * they allows to display CHAT messages @@ -215,16 +193,6 @@ public interface IOnlinePlayer extends IOffPlayer { sendMessage(IPlayerManager.prefixedAndColored(message)); } - /** - * Display the provided message in the player’s chat, if the chat is - * activated, prepended with the server prefix. - * @param message the message to display - */ - @Deprecated - public default void sendPrefixedMessage(BaseComponent message) { - sendMessage(IPlayerManager.prefixedAndColored(message)); - } - /** * Display the provided message in the player’s chat, if the chat is * activated, prepended with the server prefix. @@ -234,17 +202,6 @@ public interface IOnlinePlayer extends IOffPlayer { sendPrefixedMessage(message.getAdv()); } - /** - * 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 - */ - @Deprecated - 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