1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-12-24 06:58:51 +00:00

feat: 添加url打开操作 调整结构

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-09-10 17:39:53 +08:00
parent f48ddd3304
commit bb6ad76b59
4 changed files with 106 additions and 80 deletions

View File

@ -60,9 +60,7 @@ public abstract class ItemSerialize {
@Override
public String parse(final ItemStack item) {
try {
final JsonBuilder j = new JsonBuilder();
j.append(nmsSaveNBTMethod.invoke(asNMSCopyMethod.invoke(null, item), nmsNBTTagCompound.newInstance()).toString());
return j.toString();
return new JsonBuilder((nmsSaveNBTMethod.invoke(asNMSCopyMethod.invoke(null, item), nmsNBTTagCompound.newInstance()).toString())).toString();
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
itemSerialize = new Manual();
return itemSerialize.parse(item);
@ -70,74 +68,6 @@ public abstract class ItemSerialize {
}
}
static class JsonBuilder {
public static final String[] REPLACEMENT_CHARS;
static {
REPLACEMENT_CHARS = new String[128];
for (int i = 0; i <= 0x1f; i++) {
REPLACEMENT_CHARS[i] = String.format("\\u%04x", i);
}
REPLACEMENT_CHARS['"'] = "\\\"";
REPLACEMENT_CHARS['\\'] = "\\\\";
REPLACEMENT_CHARS['\t'] = "\\t";
REPLACEMENT_CHARS['\b'] = "\\b";
REPLACEMENT_CHARS['\n'] = "\\n";
REPLACEMENT_CHARS['\r'] = "\\r";
REPLACEMENT_CHARS['\f'] = "\\f";
}
StringBuffer json;
public JsonBuilder() {
json = new StringBuffer();
}
public JsonBuilder(final String string) {
json = new StringBuffer(string);
}
public void append(final String value) {
int last = 0;
final int length = value.length();
for (int i = 0; i < length; i++) {
final char c = value.charAt(i);
String replacement;
if (c < 128) {
replacement = REPLACEMENT_CHARS[c];
if (replacement == null) {
continue;
}
} else if (c == '\u2028') {
replacement = "\\u2028";
} else if (c == '\u2029') {
replacement = "\\u2029";
} else {
continue;
}
if (last < i) {
json.append(value, last, i);
}
json.append(replacement);
last = i + 1;
}
if (last < length) {
json.append(value, last, length);
}
}
public void deleteCharAt(final int length) {
json.deleteCharAt(length - 1);
}
public int length() {
return json.length();
}
@Override
public String toString() {
return json.toString();
}
}
static class Manual extends ItemSerialize {
@Override
@ -162,9 +92,7 @@ public abstract class ItemSerialize {
display.append("Lore:[");
int i = 0;
for (final String line : im.getLore()) {
final JsonBuilder j = new JsonBuilder();
j.append(line);
display.append(String.format("%s:%s,", i, j.toString()));
display.append(String.format("%s:%s,", i, new JsonBuilder(line).toString()));
i++;
}
display.deleteCharAt(display.length() - 1);

View File

@ -0,0 +1,70 @@
package pw.yumc.YumCore.tellraw;
public class JsonBuilder {
public static final String[] REPLACEMENT_CHARS;
static {
REPLACEMENT_CHARS = new String[128];
for (int i = 0; i <= 0x1f; i++) {
REPLACEMENT_CHARS[i] = String.format("\\u%04x", i);
}
REPLACEMENT_CHARS['"'] = "\\\"";
REPLACEMENT_CHARS['\\'] = "\\\\";
REPLACEMENT_CHARS['\t'] = "\\t";
REPLACEMENT_CHARS['\b'] = "\\b";
REPLACEMENT_CHARS['\n'] = "\\n";
REPLACEMENT_CHARS['\r'] = "\\r";
REPLACEMENT_CHARS['\f'] = "\\f";
}
StringBuffer json;
public JsonBuilder() {
json = new StringBuffer();
}
public JsonBuilder(final String string) {
this();
append(string);
}
public void append(final String value) {
int last = 0;
final int length = value.length();
for (int i = 0; i < length; i++) {
final char c = value.charAt(i);
String replacement;
if (c < 128) {
replacement = REPLACEMENT_CHARS[c];
if (replacement == null) {
continue;
}
} else if (c == '\u2028') {
replacement = "\\u2028";
} else if (c == '\u2029') {
replacement = "\\u2029";
} else {
continue;
}
if (last < i) {
json.append(value, last, i);
}
json.append(replacement);
last = i + 1;
}
if (last < length) {
json.append(value, last, length);
}
}
public void deleteLastChar() {
json.deleteCharAt(json.length() - 1);
}
public int length() {
return json.length();
}
@Override
public String toString() {
return json.toString();
}
}

View File

@ -7,6 +7,10 @@ package pw.yumc.YumCore.tellraw;
* @author
*/
public class MessagePart {
private static final String TEXT_FORMAT = "\"text\":\"%s\"";
private static final String CLICK_FORMAT = "\"clickEvent\":{\"action\":\"%s\",\"value\":\"%s\"}";
private static final String HOVER_FORMAT = "\"hoverEvent\":{\"action\":\"%s\",\"value\":\"%s\"}";
private static final String INSERT_FORMAT = " \"insertion\":\"%s\"";
/**
* 消息文本
*/
@ -55,18 +59,18 @@ public class MessagePart {
*/
public void writeJson(final StringBuffer str) {
str.append("{");
str.append("\"text\":\"" + text + "\"");
str.append(String.format(TEXT_FORMAT, text));
if (clickActionName != null) {
str.append(",");
str.append(String.format("\"clickEvent\":{\"action\":\"%s\",\"value\":\"%s\"}", clickActionName, clickActionData));
str.append(String.format(CLICK_FORMAT, clickActionName, clickActionData));
}
if (hoverActionName != null) {
str.append(",");
str.append(String.format("\"hoverEvent\":{\"action\":\"%s\",\"value\":\"%s\"}", hoverActionName, hoverActionData));
str.append(String.format(HOVER_FORMAT, hoverActionName, hoverActionData));
}
if (insertionData != null) {
str.append(",");
str.append(String.format(" \"insertion\":\"%s\"", insertionData));
str.append(String.format(INSERT_FORMAT, insertionData));
}
str.append("}");
}

View File

@ -117,8 +117,8 @@ public class Tellraw {
/**
* 悬浮物品
*
* @param text
* 文本
* @param json
* 物品Json串
* @return {@link Tellraw}
*/
public Tellraw item(final String json) {
@ -136,6 +136,17 @@ public class Tellraw {
return onClick("open_url", url);
}
/**
* 打开网址
*
* @param url
* 网址
* @return {@link Tellraw}
*/
public Tellraw openurl(final String url) {
return onClick("open_url", url);
}
/**
* 发送Tellraw
*
@ -206,6 +217,19 @@ public class Tellraw {
return then(new MessagePart(text));
}
/**
* 悬浮物品
*
* @param name
* 物品名称
* @param item
* {@link ItemStack}
* @return {@link Tellraw}
*/
public Tellraw then(final String name, final ItemStack item) {
return then(name).item(ItemSerialize.$(item));
}
/**
* 悬浮消息
*