From 68ef7999fd8a3596bb1cf52745a957b3d3f54f98 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 16 Apr 2022 15:55:23 +0200 Subject: [PATCH 1/2] :sparkles: TextComponentApi --- .../de/steamwar/message/SWTextComponent.java | 143 ++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 SpigotCore_Main/src/de/steamwar/message/SWTextComponent.java diff --git a/SpigotCore_Main/src/de/steamwar/message/SWTextComponent.java b/SpigotCore_Main/src/de/steamwar/message/SWTextComponent.java new file mode 100644 index 0000000..589c0d0 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/message/SWTextComponent.java @@ -0,0 +1,143 @@ +package de.steamwar.message; + +import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.HoverEvent; +import net.md_5.bungee.api.chat.TextComponent; +import net.md_5.bungee.api.chat.hover.content.Text; +import org.bukkit.entity.Player; + +import java.text.MessageFormat; + +public class SWTextComponent { + + public static SWTextComponent tc() { + return new SWTextComponent(); + } + + public static SWTextComponent tc(String text) { + return new SWTextComponent(text); + } + + public static SWTextComponent tc(String text, Message message) { + return new SWTextComponent(text, message); + } + + public static SWTextComponent tc(Message message) { + return new SWTextComponent(message); + } + + public static SWTextComponent prefix(Message message, Player player) { + return new SWTextComponent(message.parse("PREFIX", player), message); + } + + private TextComponent textComponent; + private TextComponent root; + private Message message; + + public SWTextComponent() { + this.textComponent = new TextComponent(); + this.root = this.textComponent; + } + public SWTextComponent(String text) { + this.textComponent = new TextComponent(TextComponent.fromLegacyText(text)); + this.root = this.textComponent; + } + public SWTextComponent(String text, Message message) { + this.textComponent = new TextComponent(TextComponent.fromLegacyText(text)); + this.root = this.textComponent; + this.message = message; + } + public SWTextComponent(Message message) { + this.textComponent = new TextComponent(); + this.root = this.textComponent; + this.message = message; + } + + public SWTextComponent a(String text) { + textComponent.setText(text); + root.addExtra(textComponent); + textComponent = textComponent.duplicate(); + return this; + } + + public SWTextComponent a(Object text) { + textComponent.setText(text.toString()); + root.addExtra(textComponent); + textComponent = textComponent.duplicate(); + return this; + } + + public SWTextComponent a(String text, Player player, Object... args) { + if(message == null) throw new IllegalStateException("Message is null"); + textComponent.setText(message.parse(text, player, args)); + root.addExtra(textComponent); + textComponent = textComponent.duplicate(); + return this; + } + + public SWTextComponent a(String text, Object... args) { + textComponent.setText(new MessageFormat(text).format(args)); + root.addExtra(textComponent); + textComponent = textComponent.duplicate(); + return this; + } + + public SWTextComponent c(ChatColor color) { + textComponent.setColor(color); + return this; + } + + public SWTextComponent h(String hoverText) { + textComponent.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(hoverText))); + return this; + } + + public SWTextComponent rh() { + textComponent.setHoverEvent(null); + return this; + } + + public SWTextComponent k(ClickEvent clickEvent) { + textComponent.setClickEvent(clickEvent); + return this; + } + + public SWTextComponent k(ClickEvent.Action action, String value) { + textComponent.setClickEvent(new ClickEvent(action, value)); + return this; + } + + public SWTextComponent rk() { + textComponent.setClickEvent(null); + return this; + } + + public SWTextComponent b(boolean bold) { + textComponent.setBold(bold); + return this; + } + + public SWTextComponent i(boolean italic) { + textComponent.setItalic(italic); + return this; + } + + public SWTextComponent u(boolean underlined) { + textComponent.setUnderlined(underlined); + return this; + } + + public SWTextComponent o(boolean obfuscated) { + textComponent.setObfuscated(obfuscated); + return this; + } + + public TextComponent build() { + return textComponent; + } + + public void send(Player player) { + player.spigot().sendMessage(root); + } +} -- 2.39.2 From 58c74a72f407a7a6eafc92c57cc4407bbd6347b2 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 16 Apr 2022 17:42:22 +0200 Subject: [PATCH 2/2] :sparkles: TextComponentApi --- .../src/de/steamwar/message/SWTextComponent.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/message/SWTextComponent.java b/SpigotCore_Main/src/de/steamwar/message/SWTextComponent.java index 589c0d0..8473fef 100644 --- a/SpigotCore_Main/src/de/steamwar/message/SWTextComponent.java +++ b/SpigotCore_Main/src/de/steamwar/message/SWTextComponent.java @@ -37,20 +37,20 @@ public class SWTextComponent { public SWTextComponent() { this.textComponent = new TextComponent(); - this.root = this.textComponent; + this.root = new TextComponent(); } public SWTextComponent(String text) { - this.textComponent = new TextComponent(TextComponent.fromLegacyText(text)); - this.root = this.textComponent; + this.root = new TextComponent(TextComponent.fromLegacyText(text)); + this.textComponent = new TextComponent(); } public SWTextComponent(String text, Message message) { - this.textComponent = new TextComponent(TextComponent.fromLegacyText(text)); - this.root = this.textComponent; + this.root = new TextComponent(TextComponent.fromLegacyText(text)); + this.textComponent = new TextComponent(); this.message = message; } public SWTextComponent(Message message) { this.textComponent = new TextComponent(); - this.root = this.textComponent; + this.root = new TextComponent(); this.message = message; } -- 2.39.2