How do i set a custom meta for item?

Discussion in 'Plugin Development' started by spurkle, Jul 29, 2014.

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

    spurkle

    Hi. How can i set custom metadata for an item?
    I tried doing it the way you would do for a player, but it does not work as expected.
    ItemStack.setItemMeta("key",new FixedMetadataValue(this,"value"));


    Help appreciated.
    Thank yaw ;3
     
  2. Code:java
    1. ItemMeta meta = item.getItemMeta();
    2.  
    3. //do all your meta.setWhatever() stuff here
    4.  
    5. item.setItemMeta(meta);
     
  3. Offline

    rfsantos1996

    You could do more (IMO) difficult stuff. But what I did was create a lore that is not sent to the player.

    When a lore contains a line with the start "DATA:" it'll delete the line (on the packet being sent to the player, so the player won't see it, but the server can know if the item has a Data - by getting the value after the Data line).

    This is what I used to prevent DATA from being sent to the player:
    Code:java
    1. pl.getProtocolManager().addPacketListener(new PacketAdapter(pl, PacketType.Play.Server.SET_SLOT, PacketType.Play.Server.WINDOW_ITEMS) {
    2.  
    3. @Override
    4. public void onPacketSending(PacketEvent event) {
    5. if(event.getPacketType() == PacketType.Play.Server.SET_SLOT) {
    6. WrapperPlayServerSetSlot setSlot = new WrapperPlayServerSetSlot(event.getPacket());
    7. setSlot.setSlotData(removeFirstDataLore(setSlot.getSlotData()));
    8. event.setPacket(setSlot.getHandle());
    9. } else {
    10. WrapperPlayServerWindowItems windowItems = new WrapperPlayServerWindowItems(event.getPacket());
    11. ItemStack[] itemStacks = new ItemStack[windowItems.getItems().length];
    12. for(int i = 0; i < windowItems.getItems().length; i++) {
    13. itemStacks[i] = removeFirstDataLore(windowItems.getItems()[i]);
    14. }
    15. windowItems.setItems(itemStacks);
    16. event.setPacket(windowItems.getHandle());
    17. }
    18. }
    19.  
    20. public ItemStack removeFirstDataLore(ItemStack itemStack) {
    21. if(itemStack != null && itemStack.getType() != Material.AIR) {
    22. ItemMeta itemMeta = itemStack.getItemMeta();
    23. if(itemMeta.hasLore()) {
    24. List<String> stringList = itemMeta.getLore();
    25. for(int i = 0; i < stringList.size(); i++) {
    26. if(stringList.get(i).startsWith("DATA:")) {
    27. stringList.remove(stringList.get(i));
    28. }
    29. }
    30. itemMeta.setLore(stringList);
    31. }
    32. itemStack.setItemMeta(itemMeta);
    33. }
    34. return itemStack;
    35. }
    36. });[I][/I][/i][/i]
     
  4. Offline

    spurkle

    No, no. You did not understand me. I need to set custom data for the item.

    Ow. Haha. I hope there are easier ways for doing this. I don't really want to do all the packets stuff.

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

    rfsantos1996

    This stuff is really simple (really), there are more difficult ways to do this (belive me) and I belive this is the easier (at least it was for me).

    You could use NBT tags but I don't know how that works so...
     
Thread Status:
Not open for further replies.

Share This Page