ViaBackwards
ViaBackwards copied to clipboard
Can't show item in chat for 1.15 & - players
'/viaversion dump' Output
https://dump.viaversion.com/53f951e68a5158294eb4ea58e5e34523b7250c77d4f36216c7bea1e3d08ea5c5
Console Error
No error
Bug Description
Steps to Reproduce
Server in 1.16.5
- Player A in 1.18: show good item
- Player B in 1.8 (or 1.12): show wrong item
Code:
ComponentBuilder itemComponent = new ComponentBuilder(ChatListener.styleItem(to, item, getStorage()));
String itemJson = convertItemStackToJson(item);
itemComponent.event(new HoverEvent(Action.SHOW_ITEM, new ComponentBuilder(itemJson).create()));
player.spigot().sendMessage(itemComponent.create());
Method that convert item to JSON:
public String convertItemStackToJson(ItemStack itemStack) {
try {
Class<?> nbtTag = PacketUtils.getNmsClass("NBTTagCompound", "nbt.");
Class<?> craftItemClass = PacketUtils.getObcClass("inventory.CraftItemStack");
Object nmsNbtTagCompoundObj = nbtTag.newInstance();
if (saveMethod == null) {
Object nmsItemStackObj = craftItemClass.getMethod("asNMSCopy", ItemStack.class).invoke(null, itemStack);
return nmsItemStackObj.getClass().getMethod("save", nbtTag)
.invoke(nmsItemStackObj, nmsNbtTagCompoundObj).toString();
} else {
Object nmsItemStackObj = craftItemClass.getMethod("asNMSCopy", ItemStack.class).invoke(null, itemStack);
return saveMethod.invoke(nmsItemStackObj, nmsNbtTagCompoundObj).toString();
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Expected Behavior
Show as the 1.18 player:
Additional Server Info
Basic spigot 1.16.5 build, no proxy
Checklist
- [X] Via plugins are only running on EITHER the backend servers (e.g. Paper) OR the proxy (e.g. BungeeCord), not on both.
- [X] I have included a ViaVersion dump.
- [X] If applicable, I have included a paste (not a screenshot) of the error.
- [X] I have tried a build from https://ci.viaversion.com/ and the issue still persists.
That's not how you create proper item hover components with Bungee chat API. Even if this might seemingly be handled fine-ish by the client, the proper and working way to do this is the following:
final ItemTag itemTag = ItemTag.ofNbt(CraftItemStack.asNMSCopy(itemStack).getOrCreateTag().toString());
final Content content = new Item(itemStack.getType().getKey().toString(), itemStack.getAmount(), itemTag);
// OR with Paper API
final Content content = Bukkit.getItemFactory().hoverContentOf(itemStack);
final ComponentBuilder builder = new ComponentBuilder("Some text").event(new HoverEvent(HoverEvent.Action.SHOW_ITEM, content));
player.spigot().sendMessage(builder.create());
Thanks for your comment and informations :)
Yes but this is only available for new version. The Item
class isn't available for all spigot versions.