From 1c37c61c58ff6f41e22ca86eade0ad1b24b3e7d7 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Thu, 11 Aug 2016 18:26:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=94=9F=E6=88=90=E9=94=99=E8=AF=AF=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=96=B0=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../pw/yumc/YumCore/tellraw/MessagePart.java | 25 +++- .../java/pw/yumc/YumCore/tellraw/Tellraw.java | 114 ++++++++++++++---- 2 files changed, 111 insertions(+), 28 deletions(-) diff --git a/src/main/java/pw/yumc/YumCore/tellraw/MessagePart.java b/src/main/java/pw/yumc/YumCore/tellraw/MessagePart.java index dab0c0a..402368d 100644 --- a/src/main/java/pw/yumc/YumCore/tellraw/MessagePart.java +++ b/src/main/java/pw/yumc/YumCore/tellraw/MessagePart.java @@ -24,8 +24,13 @@ public class MessagePart { */ public String hoverActionName; /** + * 悬浮数据 */ public String hoverActionData; + /** + * 插入数据 + */ + public String insertionData; public MessagePart() { this(""); @@ -35,15 +40,19 @@ public class MessagePart { this.text = text; } + /** + * 是否有文本 + */ public boolean hasText() { return text != null && !text.isEmpty(); } - // { - // "text":"TestClick", - // "clickEvent":{"action":"run_command","value":"yum list"}, - // "hoverEvent":{"action":"show_text","value":{"text":"§a点击查看插件列表"}} - // } + /** + * 写入Json + * + * @param str + * 流对象 + */ public void writeJson(final StringBuffer str) { str.append("{"); str.append("\"text\":\"" + text + "\""); @@ -53,7 +62,11 @@ public class MessagePart { } if (hoverActionName != null) { str.append(","); - str.append(String.format("\"hoverEvent\":{\"action\":\"%s\",\"value\":{\"text\":\"%s\"}", clickActionName, clickActionData)); + str.append(String.format("\"hoverEvent\":{\"action\":\"%s\",\"value\":{\"text\":\"%s\"}}", hoverActionName, hoverActionData)); + } + if (insertionData != null) { + str.append(","); + str.append(String.format(" \"insertion\":\"%s\"", insertionData)); } str.append("}"); } diff --git a/src/main/java/pw/yumc/YumCore/tellraw/Tellraw.java b/src/main/java/pw/yumc/YumCore/tellraw/Tellraw.java index a155bbf..5143e05 100644 --- a/src/main/java/pw/yumc/YumCore/tellraw/Tellraw.java +++ b/src/main/java/pw/yumc/YumCore/tellraw/Tellraw.java @@ -1,6 +1,7 @@ package pw.yumc.YumCore.tellraw; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.bukkit.Bukkit; @@ -17,12 +18,8 @@ import org.bukkit.entity.Player; public class Tellraw { List messageParts = new ArrayList<>(); - public Tellraw() { - this(""); - } - public Tellraw(final String text) { - then("text"); + messageParts.add(new MessagePart(text)); } /** @@ -31,7 +28,7 @@ public class Tellraw { * @return {@link Tellraw} */ public static Tellraw create() { - return new Tellraw(); + return create(""); } /** @@ -45,6 +42,10 @@ public class Tellraw { return new Tellraw(text); } + public static void main(final String[] args) { + System.out.println(Tellraw.create("命令").command("yum list").tip("点击查看插件列表").toJsonString()); + } + /** * 执行命令 * @@ -53,10 +54,36 @@ public class Tellraw { * @return {@link Tellraw} */ public Tellraw command(final String command) { - onClick("run_command", command); + return onClick("run_command", command); + } + + /** + * 打开文件 + * + * @param path + * 文件路径 + * @return {@link Tellraw} + */ + public Tellraw file(final String path) { + return onClick("open_file", path); + } + + public Tellraw insertion(final String data) { + latest().insertionData = data; return this; } + /** + * 打开URL + * + * @param url + * 地址 + * @return {@link Tellraw} + */ + public Tellraw link(final String url) { + return onClick("open_url", url); + } + /** * 发送Tellraw * @@ -79,19 +106,18 @@ public class Tellraw { * @return {@link Tellraw} */ public Tellraw suggest(final String command) { - onClick("suggest_command", command); - return this; + return onClick("suggest_command", command); } /** - * 结束上一串消息 开始下一串数据 - * - * @param part - * 下一段内容 + * 修改当前串文本 + * + * @param text + * 文本 * @return {@link Tellraw} */ - public Tellraw then(final MessagePart part) { - messageParts.add(part); + public Tellraw text(final String text) { + latest().text = text; return this; } @@ -103,8 +129,22 @@ public class Tellraw { * @return {@link Tellraw} */ public Tellraw then(final String text) { - then(new MessagePart(text)); - return this; + return then(new MessagePart(text)); + } + + /** + * 悬浮消息 + * + * @param text + * 文本 + * @return {@link Tellraw} + */ + public Tellraw tip(final List texts) { + final StringBuffer text = new StringBuffer(); + for (final String t : texts) { + text.append(t).append("\n"); + } + return tip(text.toString().substring(0, text.length() - 2)); } /** @@ -115,8 +155,18 @@ public class Tellraw { * @return {@link Tellraw} */ public Tellraw tip(final String text) { - onHover("show_text", text); - return this; + return onHover("show_text", text); + } + + /** + * 悬浮消息 + * + * @param text + * 文本 + * @return {@link Tellraw} + */ + public Tellraw tip(final String... texts) { + return tip(Arrays.asList(texts)); } /** @@ -132,7 +182,7 @@ public class Tellraw { messagePart.writeJson(msg); } msg.append("]"); - return null; + return msg.toString(); } /** @@ -177,11 +227,13 @@ public class Tellraw { * 点击名称 * @param data * 点击操作 + * @return {@link Tellraw} */ - private void onClick(final String name, final String data) { + private Tellraw onClick(final String name, final String data) { final MessagePart latest = latest(); latest.clickActionName = name; latest.clickActionData = data; + return this; } /** @@ -191,10 +243,28 @@ public class Tellraw { * 悬浮显示 * @param data * 显示内容 + * @return {@link Tellraw} */ - private void onHover(final String name, final String data) { + private Tellraw onHover(final String name, final String data) { final MessagePart latest = latest(); latest.hoverActionName = name; latest.hoverActionData = data; + return this; + } + + /** + * 结束上一串消息 开始下一串数据 + * + * @param part + * 下一段内容 + * @return {@link Tellraw} + */ + private Tellraw then(final MessagePart part) { + final MessagePart last = latest(); + if (!last.hasText()) { + last.text = part.text; + } + messageParts.add(part); + return this; } }