1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-22 01:48:50 +00:00

fix: 修复数据生成错误 添加新方法

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-08-11 18:26:15 +08:00
parent 66324bd307
commit 1c37c61c58
2 changed files with 111 additions and 28 deletions

View File

@ -24,8 +24,13 @@ public class MessagePart {
*/ */
public String hoverActionName; public String hoverActionName;
/** /**
* 悬浮数据
*/ */
public String hoverActionData; public String hoverActionData;
/**
* 插入数据
*/
public String insertionData;
public MessagePart() { public MessagePart() {
this(""); this("");
@ -35,15 +40,19 @@ public class MessagePart {
this.text = text; this.text = text;
} }
/**
* 是否有文本
*/
public boolean hasText() { public boolean hasText() {
return text != null && !text.isEmpty(); return text != null && !text.isEmpty();
} }
// { /**
// "text":"TestClick", * 写入Json
// "clickEvent":{"action":"run_command","value":"yum list"}, *
// "hoverEvent":{"action":"show_text","value":{"text":"§a点击查看插件列表"}} * @param str
// } * 流对象
*/
public void writeJson(final StringBuffer str) { public void writeJson(final StringBuffer str) {
str.append("{"); str.append("{");
str.append("\"text\":\"" + text + "\""); str.append("\"text\":\"" + text + "\"");
@ -53,7 +62,11 @@ public class MessagePart {
} }
if (hoverActionName != null) { if (hoverActionName != null) {
str.append(","); 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("}"); str.append("}");
} }

View File

@ -1,6 +1,7 @@
package pw.yumc.YumCore.tellraw; package pw.yumc.YumCore.tellraw;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -17,12 +18,8 @@ import org.bukkit.entity.Player;
public class Tellraw { public class Tellraw {
List<MessagePart> messageParts = new ArrayList<>(); List<MessagePart> messageParts = new ArrayList<>();
public Tellraw() {
this("");
}
public Tellraw(final String text) { public Tellraw(final String text) {
then("text"); messageParts.add(new MessagePart(text));
} }
/** /**
@ -31,7 +28,7 @@ public class Tellraw {
* @return {@link Tellraw} * @return {@link Tellraw}
*/ */
public static Tellraw create() { public static Tellraw create() {
return new Tellraw(); return create("");
} }
/** /**
@ -45,6 +42,10 @@ public class Tellraw {
return new Tellraw(text); 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} * @return {@link Tellraw}
*/ */
public Tellraw command(final String command) { 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; return this;
} }
/**
* 打开URL
*
* @param url
* 地址
* @return {@link Tellraw}
*/
public Tellraw link(final String url) {
return onClick("open_url", url);
}
/** /**
* 发送Tellraw * 发送Tellraw
* *
@ -79,19 +106,18 @@ public class Tellraw {
* @return {@link Tellraw} * @return {@link Tellraw}
*/ */
public Tellraw suggest(final String command) { public Tellraw suggest(final String command) {
onClick("suggest_command", command); return onClick("suggest_command", command);
return this;
} }
/** /**
* 结束上一串消息 开始下一串数据 * 修改当前串文本
* *
* @param part * @param text
* 下一段内容 * 文本
* @return {@link Tellraw} * @return {@link Tellraw}
*/ */
public Tellraw then(final MessagePart part) { public Tellraw text(final String text) {
messageParts.add(part); latest().text = text;
return this; return this;
} }
@ -103,8 +129,22 @@ public class Tellraw {
* @return {@link Tellraw} * @return {@link Tellraw}
*/ */
public Tellraw then(final String text) { public Tellraw then(final String text) {
then(new MessagePart(text)); return then(new MessagePart(text));
return this; }
/**
* 悬浮消息
*
* @param text
* 文本
* @return {@link Tellraw}
*/
public Tellraw tip(final List<String> 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} * @return {@link Tellraw}
*/ */
public Tellraw tip(final String text) { public Tellraw tip(final String text) {
onHover("show_text", text); return onHover("show_text", text);
return this; }
/**
* 悬浮消息
*
* @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); messagePart.writeJson(msg);
} }
msg.append("]"); msg.append("]");
return null; return msg.toString();
} }
/** /**
@ -177,11 +227,13 @@ public class Tellraw {
* 点击名称 * 点击名称
* @param data * @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(); final MessagePart latest = latest();
latest.clickActionName = name; latest.clickActionName = name;
latest.clickActionData = data; latest.clickActionData = data;
return this;
} }
/** /**
@ -191,10 +243,28 @@ public class Tellraw {
* 悬浮显示 * 悬浮显示
* @param data * @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(); final MessagePart latest = latest();
latest.hoverActionName = name; latest.hoverActionName = name;
latest.hoverActionData = data; 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;
} }
} }