1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-22 01:48:50 +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 @Override
public String parse(final ItemStack item) { public String parse(final ItemStack item) {
try { try {
final JsonBuilder j = new JsonBuilder(); return new JsonBuilder((nmsSaveNBTMethod.invoke(asNMSCopyMethod.invoke(null, item), nmsNBTTagCompound.newInstance()).toString())).toString();
j.append(nmsSaveNBTMethod.invoke(asNMSCopyMethod.invoke(null, item), nmsNBTTagCompound.newInstance()).toString());
return j.toString();
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) { } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
itemSerialize = new Manual(); itemSerialize = new Manual();
return itemSerialize.parse(item); 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 { static class Manual extends ItemSerialize {
@Override @Override
@ -162,9 +92,7 @@ public abstract class ItemSerialize {
display.append("Lore:["); display.append("Lore:[");
int i = 0; int i = 0;
for (final String line : im.getLore()) { for (final String line : im.getLore()) {
final JsonBuilder j = new JsonBuilder(); display.append(String.format("%s:%s,", i, new JsonBuilder(line).toString()));
j.append(line);
display.append(String.format("%s:%s,", i, j.toString()));
i++; i++;
} }
display.deleteCharAt(display.length() - 1); 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 * @author
*/ */
public class MessagePart { 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) { public void writeJson(final StringBuffer str) {
str.append("{"); str.append("{");
str.append("\"text\":\"" + text + "\""); str.append(String.format(TEXT_FORMAT, text));
if (clickActionName != null) { if (clickActionName != null) {
str.append(","); str.append(",");
str.append(String.format("\"clickEvent\":{\"action\":\"%s\",\"value\":\"%s\"}", clickActionName, clickActionData)); str.append(String.format(CLICK_FORMAT, clickActionName, clickActionData));
} }
if (hoverActionName != null) { if (hoverActionName != null) {
str.append(","); str.append(",");
str.append(String.format("\"hoverEvent\":{\"action\":\"%s\",\"value\":\"%s\"}", hoverActionName, hoverActionData)); str.append(String.format(HOVER_FORMAT, hoverActionName, hoverActionData));
} }
if (insertionData != null) { if (insertionData != null) {
str.append(","); str.append(",");
str.append(String.format(" \"insertion\":\"%s\"", insertionData)); str.append(String.format(INSERT_FORMAT, insertionData));
} }
str.append("}"); str.append("}");
} }

View File

@ -117,8 +117,8 @@ public class Tellraw {
/** /**
* 悬浮物品 * 悬浮物品
* *
* @param text * @param json
* 文本 * 物品Json串
* @return {@link Tellraw} * @return {@link Tellraw}
*/ */
public Tellraw item(final String json) { public Tellraw item(final String json) {
@ -136,6 +136,17 @@ public class Tellraw {
return onClick("open_url", url); return onClick("open_url", url);
} }
/**
* 打开网址
*
* @param url
* 网址
* @return {@link Tellraw}
*/
public Tellraw openurl(final String url) {
return onClick("open_url", url);
}
/** /**
* 发送Tellraw * 发送Tellraw
* *
@ -206,6 +217,19 @@ public class Tellraw {
return then(new MessagePart(text)); 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));
}
/** /**
* 悬浮消息 * 悬浮消息
* *