From 304faa072e6f8f9ed53e9d2fd7ce2798e5fb3c4a Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Fri, 9 Jul 2021 00:27:59 +0200 Subject: [PATCH] Build Chat API over Adventure instead of Bungeecord-chat --- Core/pom.xml | 20 ++ .../java/fr/pandacube/lib/core/chat/Chat.java | 214 +++++++++++------- .../pandacube/lib/core/chat/ChatStatic.java | 29 +-- .../fr/pandacube/lib/core/chat/ChatUtil.java | 6 +- Paper/pom.xml | 15 +- .../pandacube/lib/paper/gui/GUIInventory.java | 10 +- .../lib/paper/util/AutoUpdatedBossBar.java | 3 +- .../lib/paper/util/ItemStackBuilder.java | 36 ++- .../lib/paper/util/ScoreBoardUtil.java | 30 ++- .../fr/pandacube/lib/paper/util/Skull.java | 8 +- 10 files changed, 240 insertions(+), 131 deletions(-) diff --git a/Core/pom.xml b/Core/pom.xml index 16b72fd..6a5becc 100644 --- a/Core/pom.xml +++ b/Core/pom.xml @@ -13,6 +13,13 @@ jar PandaLib-Core + + + + sonatype-oss-snapshots + https://oss.sonatype.org/content/repositories/snapshots/ + + @@ -21,6 +28,19 @@ ${bungeecord.version} compile + + + net.kyori + adventure-api + 4.7.0 + + + net.kyori + adventure-platform-bungeecord + 4.0.0-SNAPSHOT + + + io.github.classgraph classgraph 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 8d91f41..e3c13a8 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 @@ -1,39 +1,53 @@ package fr.pandacube.lib.core.chat; import java.awt.Color; -import java.util.UUID; +import java.util.Objects; 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.Component; +import net.kyori.adventure.text.ComponentLike; +import net.kyori.adventure.text.TextComponent; +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.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.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.BaseComponent; -import net.md_5.bungee.api.chat.ClickEvent; -import net.md_5.bungee.api.chat.HoverEvent; -import net.md_5.bungee.api.chat.ItemTag; -import net.md_5.bungee.api.chat.TextComponent; -import net.md_5.bungee.api.chat.hover.content.Content; -import net.md_5.bungee.api.chat.hover.content.Entity; -import net.md_5.bungee.api.chat.hover.content.Item; -import net.md_5.bungee.api.chat.hover.content.Text; -public abstract class Chat extends ChatStatic { +public abstract class Chat extends ChatStatic implements HoverEventSource, ComponentLike { - protected BaseComponent component; + protected Component component; protected boolean console = false; - public Chat(BaseComponent c) { + public Chat(Component c) { + Objects.requireNonNull(c, "Provided component must not be null"); component = c; } public BaseComponent get() { + return toBungee(component); + } + + + public Component getAdv() { return component; } public BaseComponent[] getAsArray() { - return new BaseComponent[] { component }; + return toBungeeArray(component); } public String getLegacyText() { - return component.toLegacyText(); + return LegacyComponentSerializer.legacySection().serializeOr(component, ""); } @@ -41,30 +55,30 @@ public abstract class Chat extends ChatStatic { + public Chat then(BaseComponent subComponent) { - // here are some optimizations to avoid unnecessary component nesting - if (subComponent instanceof TextComponent) { - TextComponent txtComp = (TextComponent) subComponent; - if (!txtComp.hasFormatting() && (txtComp.getText() == null || txtComp.getText().isEmpty())) { + return then(toAdventure(subComponent)); + } + public Chat then(Chat comp) { + return then(comp.getAdv()); + } + public Chat then(BaseComponent[] components) { + return then(toAdventure(components)); + } + public Chat then(Component comp) { + if (comp instanceof TextComponent) { + TextComponent txtComp = (TextComponent) comp; + if (!txtComp.hasStyling() && (txtComp.content() == null || txtComp.content().isEmpty())) { // no need to add the provided component to the current component. // but eventual child component must be added - if (txtComp.getExtra() != null) { - for (BaseComponent child : txtComp.getExtra()) + if (txtComp.children() != null && !txtComp.children().isEmpty()) { + for (Component child : txtComp.children()) then(child); } return this; } } - component.addExtra(subComponent); - return this; - } - public Chat then(Chat comp) { return then(comp.get()); } - public Chat then(BaseComponent[] components) { - if (components != null) { - for (BaseComponent c : components) { - then(c); - } - } + component.append(comp); return this; } @@ -82,7 +96,7 @@ public abstract class Chat extends ChatStatic { public Chat thenPlayerName(String legacyText) { return then(playerNameText(legacyText)); } - public Chat thenNewLine() { return thenText("\n"); } + public Chat thenNewLine() { return then(Component.newline()); } public Chat thenLegacyText(Object legacyText) { return then(legacyText(legacyText)); } @@ -90,7 +104,7 @@ public abstract class Chat extends ChatStatic { public Chat thenKeyBind(String key) { return then(keybind(key)); } - public Chat thenScore(String name, String objective, String value) { return then(score(name, objective, value)); } + public Chat thenScore(String name, String objective) { return then(score(name, objective)); } @@ -191,13 +205,14 @@ public abstract class Chat extends ChatStatic { public static class FormatableChat extends Chat { - public FormatableChat(BaseComponent c) { + public FormatableChat(Component c) { super(c); } public FormatableChat console(boolean c) { console = c; return this; } - - public FormatableChat color(ChatColor c) { component.setColor(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(Color c) { return color(ChatColor.of(c)); } public FormatableChat color(String c) { return color(ChatColor.of(c)); } @@ -224,63 +239,52 @@ public abstract class Chat extends ChatStatic { public FormatableChat dataColor() { return color(config.dataColor); } public FormatableChat decorationColor() { return color(config.decorationColor); } - public FormatableChat font(String f) { component.setFont(f); return this; } + private FormatableChat setStyle(UnaryOperator