ViaBackwards icon indicating copy to clipboard operation
ViaBackwards copied to clipboard

Can't show item in chat for 1.15 & - players

Open Elikill58 opened this issue 2 years ago • 1 comments

'/viaversion dump' Output

https://dump.viaversion.com/53f951e68a5158294eb4ea58e5e34523b7250c77d4f36216c7bea1e3d08ea5c5

Console Error

No error

Bug Description

image

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: image

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.

Elikill58 avatar Apr 03 '22 09:04 Elikill58

Platform: 3096a--Spigot--9fb885e--af1a232%20%28MC%3A%201.16.5%29 ViaVersion (4.3.0-22w13a-SNAPSHOT): Even with dev ViaBackwards(4.3.0-22w13a-SNAPSHOT): Even with dev ViaRewind(2.0.3-SNAPSHOT): Even with master

Barvalg avatar Apr 03 '22 09:04 Barvalg

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());

kennytv avatar Sep 28 '22 18:09 kennytv

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.

Elikill58 avatar Sep 28 '22 19:09 Elikill58