ItemStacks in chat

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

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

    wilmervanheerde

    Hello,

    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?

    Regards,
    TheOddPuff
     
  2. Offline

    Forseth11

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

    metalhedd

    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:

    Code:java
    1.  
    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. }
    7.  


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

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

    now put it together:
    Code:java
    1.  
    2. send(player, bukkitStackToChat(new ItemStack(Material.IRON_SWORD));
    3.  
     
    NathanWolf likes this.
  4. Offline

    wilmervanheerde

    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
  5. Offline

    metalhedd

    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 :)
     
  6. Offline

    Garris0n

  7. Offline

    metalhedd

    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.
     
  8. Offline

    wilmervanheerde

    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
  9. Offline

    metalhedd

    Nice Work!
     
    wilmervanheerde likes this.
  10. Offline

    metalhedd

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

    metalhedd

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

    Code:java
    1. new Text("test").setClick(ClickAction.RUN_COMMAND, "/BROADCAST test");
    2.  


    Code:java
    1.  
    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. }
    9.  


    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.
  12. Offline

    metalhedd

    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.
     
  13. Offline

    metalhedd

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

    Code:
    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);
            meta.setDisplayName(line1);
            meta.setLore(lines);
            stack.setItemMeta(meta);
            net.minecraft.server.v1_7_R1.ItemStack nms = CraftItemStack.asNMSCopy(stack);
            NBTTagCompound tag = new NBTTagCompound();
            nms.save(tag);
            setHover(HoverAction.SHOW_ITEM, new ChatComponentText(tag.toString()));
            return this;
        }
    
     
  14. Offline

    elementalgodz11

    metalhedd
    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.

    Thanks.
     
  15. Offline

    metalhedd

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

    elementalgodz11

    metalhedd Any update on this?
     
  17. Offline

    metalhedd

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

    elementalgodz11 Here ya go: https://github.com/andrepl/ChatLib

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

    elementalgodz11

Thread Status:
Not open for further replies.

Share This Page