ItemStacks in chat

Discussion in 'Plugin Development' started by wilmervanheerde, Jan 2, 2014.

Thread Status:
Not open for further replies.
  1. Offline



    I've read that it is somehow possible in 1.7.2 to send ItemStacks to the chat. Is there a Bukkit API available for this and if there is, how would I use it?

  2. Offline


    wilmervanheerde Do you mean display the ItemStack in chat. If so just send the player a message and use [ItemStackName].toString().
  3. Offline


  4. Offline


    Yes it is possible, but no, there isn't a Bukkit API for it yet. it's not that difficult to do though. First, you need a method to send a custom chat packet like so:

    2. public static void send(Player player, IChatBaseComponent chat) {
    3. Bukkit.getLogger().info(chat.toString());
    4. PacketPlayOutChat packet = new PacketPlayOutChat(chat, true);
    5. ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
    6. }

    now you need to get an IChatBaseComponent representation of the itemstack. this takes a bit of NMS:

    2. public IChatBaseComponent bukkitStackToChatComponent(ItemStack stack) {
    3. net.minecraft.server.v1_7_R1.ItemStack nms = CraftItemStack.asNMSCopy(stack);
    4. return nms.E();
    5. }

    now put it together:
    2. send(player, bukkitStackToChat(new ItemStack(Material.IRON_SWORD));
    NathanWolf likes this.
  5. Offline


    That's awesome, thanks. I will try it out tonight

    It's working and it's magnificant, but is it possible to add any text to the message? Right now it's only sending the item.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 6, 2016
  6. Offline


    you can 'append' additional IChatBaseComponent's to another one, There is a ChatComponentText class you can use to create text nodes, and append the ItemStack to one of those.. If you intend to use it extensively you can probably find a 3rd party library somewhere on the forums that wraps IChatBaseComponent. I have one that I hacked together for my own use which you can look at, or copy if you like, but I'm not sure how it compares to what other people have done. I'm sure someone like Comphenix has something much nicer up his sleeve :)
  7. Offline


  8. Offline


    I tried a similar approach to that before the one that I currently landed on, but I was not a fan of working at the json level, its very difficult to get data from an ItemStack into JSON format, and the whole sending process requires an extra layer of serializing/deserializing. I found it more convenient to subclass the existing NMS Implementations of IChatBaseComponent and just add a bit of syntactic sugar, no extra serialize/deserialize step, and you can take advantage of things like ItemStack.E() more readily.
  9. Offline


    I've looked around a bit and found some libraries, but it turned out that your own-made library is best to understand and use. Thanks a lot for sharing it with me!

    Thanks to metalhedd for his help, Comphenix for ProtocolLib, a lot of coffee and some coding hours, I created this awesome chat plugin.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 6, 2016
  10. Offline


    Nice Work!
    wilmervanheerde likes this.
  11. Offline


    My "Text" objects have a setHover and setClick methods, is that what you mean?
  12. Offline


    You access those methods by creating instances of Text, it isn't supposed to be accessed statically. Eg.

    1. new Text("test").setClick(ClickAction.RUN_COMMAND, "/BROADCAST test");

    2. Text text = new Text("");
    3. for (String kit: kits) {
    4. Text item = new Text(kit)
    5. .setHoverText("Click here to choose the " + kit + " kit.")
    6. .setClick(ClickAction.RUN_COMMAND, "/kit " + kit);
    7. text.append(item).append(" ");
    8. }

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 6, 2016
    elementalgodz11 likes this.
  13. Offline


    no, your list of players is in a list called 'in' and you're adding all of their names to the text. I can't see where you populated that list, so I don't know what's wrong. It's not related to the chat library though, so this probably isn't an appropriate thread for it.
  14. Offline


    Multi-Lined Hover Text? I added this method to the Text class in a more recent version:

    public Text setHoverText(String[] text) {
            ItemStack stack = new ItemStack(Material.DIRT);
            ItemMeta meta = stack.getItemMeta();
            List<String> lines = Arrays.asList(text);
            String line1 = lines.remove(0);
            net.minecraft.server.v1_7_R1.ItemStack nms = CraftItemStack.asNMSCopy(stack);
            NBTTagCompound tag = new NBTTagCompound();
            setHover(HoverAction.SHOW_ITEM, new ChatComponentText(tag.toString()));
            return this;
  15. Offline


    I am grateful you have shared this, thank you.

    I have used this in a lot of my commands, with the release of 1.7.5 changing from v1_7_R1 to v1_7_R2 has broken the support for this.

    I have tried simplying renaming it to 2 and fixing the imports, but NPE's would occur in the logger.

  16. Offline


    I'll look at updating the code tonight.
  17. Offline


    metalhedd Any update on this?
  18. Offline


    No sorry, I completely forgot, but I will attempt to update the code and package it as a standalone library

    elementalgodz11 Here ya go:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 6, 2016
  19. Offline


Thread Status:
Not open for further replies.

Share This Page