fix: 修复物品显示的无效的BUG

Signed-off-by: 502647092 <admin@yumc.pw>
merge/1/MERGE
502647092 2016-09-10 03:16:30 +08:00
parent 4c8cd290b5
commit f48ddd3304
3 changed files with 39 additions and 17 deletions

View File

@ -11,18 +11,21 @@ import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import pw.yumc.YumCore.bukkit.Log;
/** /**
* *
* @since 201699 3:47:17 * @since 201699 3:47:17
* @author * @author
*/ */
public abstract class ItemSerialize { public abstract class ItemSerialize {
static ItemSerialize itemSerialize; static ItemSerialize itemSerialize = new Manual();
static { static {
try { try {
itemSerialize = new Automatic(); itemSerialize = new Automatic();
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException e) { } catch (ClassNotFoundException | NoSuchMethodException | SecurityException e) {
itemSerialize = new Manual(); itemSerialize = new Manual();
Log.debug("初始化自动物品序列化失败!", e);
} }
} }
@ -47,7 +50,7 @@ public abstract class ItemSerialize {
} }
public Class<?> getNMSClass(final String cname) throws ClassNotFoundException { public Class<?> getNMSClass(final String cname) throws ClassNotFoundException {
return Class.forName("net.minecraft.server" + ver + "." + cname); return Class.forName("net.minecraft.server." + ver + "." + cname);
} }
public Class<?> getOBCClass(final String cname) throws ClassNotFoundException { public Class<?> getOBCClass(final String cname) throws ClassNotFoundException {
@ -57,7 +60,9 @@ public abstract class ItemSerialize {
@Override @Override
public String parse(final ItemStack item) { public String parse(final ItemStack item) {
try { try {
return nmsSaveNBTMethod.invoke(asNMSCopyMethod.invoke(null, item), nmsNBTTagCompound.newInstance()).toString(); final JsonBuilder j = new JsonBuilder();
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);
@ -86,6 +91,10 @@ public abstract class ItemSerialize {
json = new StringBuffer(); json = new StringBuffer();
} }
public JsonBuilder(final String string) {
json = new StringBuffer(string);
}
public void append(final String value) { public void append(final String value) {
int last = 0; int last = 0;
final int length = value.length(); final int length = value.length();
@ -105,23 +114,28 @@ public abstract class ItemSerialize {
continue; continue;
} }
if (last < i) { if (last < i) {
json.append(value, last, i - last); json.append(value, last, i);
} }
json.append(replacement); json.append(replacement);
last = i + 1; last = i + 1;
} }
if (last < length) { if (last < length) {
json.append(value, last, length - last); json.append(value, last, length);
} }
} }
public void deleteCharAt(final int length) { public void deleteCharAt(final int length) {
json.deleteCharAt(length); json.deleteCharAt(length - 1);
} }
public int length() { public int length() {
return json.length(); return json.length();
} }
@Override
public String toString() {
return json.toString();
}
} }
static class Manual extends ItemSerialize { static class Manual extends ItemSerialize {
@ -139,20 +153,24 @@ public abstract class ItemSerialize {
* @return * @return
*/ */
private String getDisplay(final ItemMeta im) { private String getDisplay(final ItemMeta im) {
final JsonBuilder display = new JsonBuilder(); final StringBuffer display = new StringBuffer();
display.append("{"); display.append("{");
if (im.hasDisplayName()) { if (im.hasDisplayName()) {
display.append(String.format("Name:\"%s\",", im.getDisplayName())); display.append(String.format("Name:\"%s\",", im.getDisplayName()));
} }
if (im.hasLore()) { if (im.hasLore()) {
display.append("Lore:["); display.append("Lore:[");
int i = 0;
for (final String line : im.getLore()) { for (final String line : im.getLore()) {
display.append(String.format("\"%s\",", line)); final JsonBuilder j = new JsonBuilder();
j.append(line);
display.append(String.format("%s:%s,", i, j.toString()));
i++;
} }
display.deleteCharAt(display.length()); display.deleteCharAt(display.length() - 1);
display.append("],"); display.append("],");
} }
display.deleteCharAt(display.length()); display.deleteCharAt(display.length() - 1);
display.append("}"); display.append("}");
return display.toString(); return display.toString();
} }
@ -169,7 +187,7 @@ public abstract class ItemSerialize {
for (final Map.Entry<Enchantment, Integer> ench : set) { for (final Map.Entry<Enchantment, Integer> ench : set) {
enchs.append(String.format("{id:%s,lvl:%s},", ench.getKey().getId(), ench.getValue())); enchs.append(String.format("{id:%s,lvl:%s},", ench.getKey().getId(), ench.getValue()));
} }
enchs.deleteCharAt(enchs.length()); enchs.deleteCharAt(enchs.length() - 1);
return enchs.toString(); return enchs.toString();
} }
@ -181,7 +199,7 @@ public abstract class ItemSerialize {
* @return * @return
*/ */
private String getTag(final ItemMeta im) { private String getTag(final ItemMeta im) {
final StringBuffer meta = new StringBuffer(); final StringBuffer meta = new StringBuffer("{");
if (im.hasEnchants()) { if (im.hasEnchants()) {
meta.append(String.format("ench:[%s],", getEnch(im.getEnchants().entrySet()))); meta.append(String.format("ench:[%s],", getEnch(im.getEnchants().entrySet())));
} }
@ -189,7 +207,8 @@ public abstract class ItemSerialize {
if (im.hasDisplayName() || im.hasLore()) { if (im.hasDisplayName() || im.hasLore()) {
meta.append(String.format("display:%s,", getDisplay(im))); meta.append(String.format("display:%s,", getDisplay(im)));
} }
meta.deleteCharAt(meta.length()); meta.deleteCharAt(meta.length() - 1);
meta.append("}");
return meta.toString(); return meta.toString();
} }
@ -201,10 +220,10 @@ public abstract class ItemSerialize {
* @return * @return
*/ */
private String serialize(final ItemStack item) { private String serialize(final ItemStack item) {
final StringBuffer json = new StringBuffer("{"); final JsonBuilder json = new JsonBuilder("{");
json.append(String.format("id:\"%s\",Damage:\"%s\"", item.getTypeId(), item.getDurability())); json.append(String.format("id:\"%s\",Damage:\"%s\"", item.getTypeId(), item.getDurability()));
if (item.getAmount() > 1) { if (item.getAmount() > 1) {
json.append(String.format(",Count:\"%s\"", item.getAmount())); json.append(String.format(",Count:%s", item.getAmount()));
} }
if (item.hasItemMeta()) { if (item.hasItemMeta()) {
json.append(String.format(",tag:%s", getTag(item.getItemMeta()))); json.append(String.format(",tag:%s", getTag(item.getItemMeta())));

View File

@ -62,7 +62,7 @@ public class MessagePart {
} }
if (hoverActionName != null) { if (hoverActionName != null) {
str.append(","); str.append(",");
str.append(String.format("\"hoverEvent\":{\"action\":\"%s\",\"value\":{\"text\":\"%s\"}}", hoverActionName, hoverActionData)); str.append(String.format("\"hoverEvent\":{\"action\":\"%s\",\"value\":\"%s\"}", hoverActionName, hoverActionData));
} }
if (insertionData != null) { if (insertionData != null) {
str.append(","); str.append(",");

View File

@ -11,6 +11,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.bukkit.compatible.C; import pw.yumc.YumCore.bukkit.compatible.C;
@ -21,6 +22,7 @@ import pw.yumc.YumCore.bukkit.compatible.C;
* @author * @author
*/ */
public class Tellraw { public class Tellraw {
static boolean isPaper = Bukkit.getVersion().contains("Paper");
List<MessagePart> messageParts = new ArrayList<>(); List<MessagePart> messageParts = new ArrayList<>();
public Tellraw(final String text) { public Tellraw(final String text) {
@ -142,7 +144,7 @@ public class Tellraw {
*/ */
public void send(final CommandSender sender) { public void send(final CommandSender sender) {
if (sender instanceof Player) { if (sender instanceof Player) {
if (!Bukkit.isPrimaryThread()) { if (isPaper && !Bukkit.isPrimaryThread()) {
Bukkit.getScheduler().runTask(P.instance, new Runnable() { Bukkit.getScheduler().runTask(P.instance, new Runnable() {
@Override @Override
public void run() { public void run() {
@ -257,6 +259,7 @@ public class Tellraw {
messagePart.writeJson(msg); messagePart.writeJson(msg);
} }
msg.append("]"); msg.append("]");
Log.debug(msg.toString());
return msg.toString(); return msg.toString();
} }