Perform some sanitation of items before applying them as Mson tooltips.

This commit is contained in:
Olof Larsson 2017-02-23 22:38:37 +01:00
parent 94a950317a
commit 145a9e5054

View File

@ -11,6 +11,8 @@ import com.massivecraft.massivecore.nms.NmsItemStackTooltip;
import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
import com.massivecraft.massivecore.xlib.gson.JsonElement; import com.massivecraft.massivecore.xlib.gson.JsonElement;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.ItemMeta;
public final class MsonEvent implements Serializable public final class MsonEvent implements Serializable
{ {
@ -164,10 +166,31 @@ public final class MsonEvent implements Serializable
public static MsonEvent item(ItemStack item) public static MsonEvent item(ItemStack item)
{ {
if (item == null) throw new NullPointerException("item"); if (item == null) throw new NullPointerException("item");
item = getItemSanitizedForTooltip(item);
String value = NmsItemStackTooltip.get().getNbtStringTooltip(item); String value = NmsItemStackTooltip.get().getNbtStringTooltip(item);
return MsonEvent.valueOf(MsonEventAction.SHOW_ITEM, value); return MsonEvent.valueOf(MsonEventAction.SHOW_ITEM, value);
} }
private static ItemStack getItemSanitizedForTooltip(ItemStack item)
{
if (item == null) throw new NullPointerException("item");
if (!item.hasItemMeta()) return item;
ItemMeta meta = item.getItemMeta();
if (meta instanceof BookMeta)
{
BookMeta book = (BookMeta)meta;
book.setPages();
item = item.clone();
item.setItemMeta(meta);
return item;
}
return item;
}
// -------------------------------------------- // // -------------------------------------------- //
// CONVENIENCE // CONVENIENCE
// -------------------------------------------- // // -------------------------------------------- //