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

Signed-off-by: 502647092 <admin@yumc.pw>
merge/1/MERGE
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 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("}");
}

View File

@ -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<MessagePart> 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<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}
*/
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;
}
}