Solved Removing attributes from item

Discussion in 'Plugin Development' started by mncat77, Jul 2, 2013.

Thread Status:
Not open for further replies.
  1. Hello,

    Does anyone know, how to remove attributes from an item (1.6). I thought it would be enough to clear out the AttributeModifiers NBTTagList, but apparently that doesn't work...
    Code:java
    1. public static ItemStack removeAttributes(ItemStack item){
    2. net.minecraft.server.v1_6_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
    3. NBTTagCompound tag;
    4. if (!nmsStack.hasTag()){
    5. tag = new NBTTagCompound();
    6. nmsStack.setTag(tag);
    7. }
    8. else {
    9. tag = nmsStack.getTag();
    10. }
    11. NBTTagList am = new NBTTagList();
    12. tag.set("AttributeModifiers", am);
    13. nmsStack.setTag(tag);
    14. return CraftItemStack.asBukkitCopy(nmsStack);
    15. }


    Since there is no API for this yet how would I go about doing this (or would I be better off waiting for a beta)?
     
  2. Would it not be easier to recreate the itemstack simply using the same Item ID?
     
  3. No because you'd still get the attribute...
     
  4. Offline

    MP5K

    mncat77
    may you explain what attributes are used for :3
    i have never heard about them :D
     
  5. i would suggest waiting for the API. that's what i'm gonna do. no point in wasting time right now for stuff you have to update on every update.
     
  6. Offline

    stirante

    I think these attributes are default and you can't delete them, but i'll look into that.
     
  7. I am pretty sure you can, I can't seem to do it though...
     
  8. Offline

    stirante

    To delete them you have to add them xD
    Code:
        public Multimap D() {
            Object object;
     
            if (this.hasTag() && this.tag.hasKey("AttributeModifiers")) {
                object = HashMultimap.create();
                NBTTagList nbttaglist = this.tag.getList("AttributeModifiers");
     
                for (int i = 0; i < nbttaglist.size(); ++i) {
                    NBTTagCompound nbttagcompound = (NBTTagCompound) nbttaglist.get(i);
                    AttributeModifier attributemodifier = GenericAttributes.a(nbttagcompound);
     
                    if (attributemodifier.a().getLeastSignificantBits() != 0L && attributemodifier.a().getMostSignificantBits() != 0L) {
                        ((Multimap) object).put(nbttagcompound.getString("AttributeName"), attributemodifier);
                    }
                }
            } else {
                object = this.getItem().h();//this calls method from ItemSword which adds damage modifier
            }
     
            return (Multimap) object;
        }
    Just add empty AttributeModifiers tag and it should work :D
     
  9. Don't I do that with the code posted in the first post? I create new NBTTagList and set it to the path "AttributeModifiers" and that doesn't seem to work...
     
  10. Offline

    stirante

    mncat77
    Maybe you used wrong CraftItemStack method. I think it was sth like asCraftMirror

    mncat77 I think i kinda know what's wrong. I added to sword some attributes and glow. When i opened inventory all nbt tags were cleared :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
  11. Does that mean I'd have to modify the packets sent to the player with ProtocolLib and add my tags there, because I can't do it using craftbukkit? Thats sad :/. I'll try if that works later on.
     
  12. This thread has already been solved about 10 days ago.
     
  13. Offline

    bobnixon1

    Can you give me the code? I cant seem to add the empty tag
     
  14. Offline

    EnderTroll68

    I do not see it solved, could you possibly give us what you did or tell us where to look?
     
  15. Here's a ProtcolLib dependant version to remove all attributes, taken from my AttributeHider plugin:
    Show Spoiler
    Code:java
    1. package me.mncat77.plugins.attributehider;
    2.  
    3. import com.comphenix.protocol.PacketType;
    4. import com.comphenix.protocol.ProtocolLibrary;
    5. import com.comphenix.protocol.events.PacketAdapter;
    6. import com.comphenix.protocol.events.PacketContainer;
    7. import com.comphenix.protocol.events.PacketEvent;
    8. import java.lang.reflect.Field;
    9. import java.util.HashSet;
    10. import java.util.Set;
    11. import java.util.logging.Level;
    12. import java.util.logging.Logger;
    13. import net.minecraft.server.v1_7_R1.ContainerMerchant;
    14. import net.minecraft.server.v1_7_R1.EntityPlayer;
    15. import net.minecraft.server.v1_7_R1.IMerchant;
    16. import net.minecraft.server.v1_7_R1.MerchantRecipe;
    17. import net.minecraft.server.v1_7_R1.MerchantRecipeList;
    18. import net.minecraft.server.v1_7_R1.NBTTagCompound;
    19. import net.minecraft.server.v1_7_R1.NBTTagList;
    20. import net.minecraft.server.v1_7_R1.PacketDataSerializer;
    21. import net.minecraft.util.io.netty.buffer.Unpooled;
    22. import org.bukkit.Material;
    23. import org.bukkit.craftbukkit.v1_7_R1.entity.CraftPlayer;
    24. import org.bukkit.craftbukkit.v1_7_R1.inventory.CraftItemStack;
    25. import org.bukkit.inventory.ItemStack;
    26. import org.bukkit.plugin.java.JavaPlugin;
    27.  
    28. public class AttributeHider extends JavaPlugin {
    29.  
    30. private Field cC;
    31.  
    32.  
    33. @Override
    34. public void onEnable() {
    35. try {
    36. cC = EntityPlayer.class.getDeclaredField("containerCounter");
    37. cC.setAccessible(true);
    38. }
    39. catch(Exception e) {
    40. this.setEnabled(false);
    41. }
    42.  
    43. Set<PacketType> packets = new HashSet<PacketType>();
    44. packets.add(PacketType.Play.Server.SET_SLOT);
    45. packets.add(PacketType.Play.Server.WINDOW_ITEMS);
    46. packets.add(PacketType.Play.Server.CUSTOM_PAYLOAD);
    47.  
    48. ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(this, packets) {
    49. @Override
    50. public void onPacketSending(PacketEvent event) {
    51. PacketContainer packet = event.getPacket();
    52. PacketType type = packet.getType();
    53. if(type == PacketType.Play.Server.WINDOW_ITEMS) {
    54. try {
    55. ItemStack[] read = packet.getItemArrayModifier().read(0);
    56. for(int i=0; i<read.length; i++) {
    57. read[i] = removeAttributes(read[i]);
    58. }
    59. packet.getItemArrayModifier().write(0, read);
    60. }
    61. catch(Exception e) {
    62. Logger.getLogger(AttributeHider.class.getName()).log(Level.SEVERE, null, e);
    63. }
    64. }
    65. else if(type == PacketType.Play.Server.CUSTOM_PAYLOAD) {
    66. if(!packet.getStrings().read(0).equalsIgnoreCase("MC|TrList")) {
    67. return;
    68. }
    69. try {
    70. EntityPlayer p = ((CraftPlayer)event.getPlayer()).getHandle();
    71. ContainerMerchant cM = ((ContainerMerchant) p.activeContainer);
    72. Field fieldMerchant = cM.getClass().getDeclaredField("merchant");
    73. fieldMerchant.setAccessible(true);
    74. IMerchant imerchant = (IMerchant)fieldMerchant.get(cM);
    75.  
    76. MerchantRecipeList merchantrecipelist = imerchant.getOffers(p);
    77. MerchantRecipeList nlist = new MerchantRecipeList();
    78. for(Object orecipe : merchantrecipelist) {
    79. MerchantRecipe recipe = (MerchantRecipe)orecipe;
    80. int uses = recipe.i().getInt("uses");
    81. int maxUses = recipe.i().getInt("maxUses");
    82. MerchantRecipe nrecipe = new MerchantRecipe(removeAttributes(recipe.getBuyItem1()), removeAttributes(recipe.getBuyItem2()), removeAttributes(recipe.getBuyItem3()));
    83. nrecipe.a(maxUses-7);
    84. for(int i=0; i < uses; i++) {
    85. nrecipe.f();
    86. }
    87. nlist.add(nrecipe);
    88. }
    89.  
    90. PacketDataSerializer packetdataserializer = new PacketDataSerializer(Unpooled.buffer());
    91. packetdataserializer.writeInt(cC.getInt(p));
    92. nlist.a(packetdataserializer);
    93. byte[] b = packetdataserializer.array();
    94. packet.getByteArrays().write(0, b);
    95. packet.getIntegers().write(0, b.length);
    96. }
    97. catch(Exception e){
    98. Logger.getLogger(AttributeHider.class.getName()).log(Level.SEVERE, null, e);
    99. }
    100. }
    101. else {
    102. try {
    103. packet.getItemModifier().write(0, removeAttributes(packet.getItemModifier().read(0)));
    104. }
    105. catch(Exception e) {
    106. Logger.getLogger(AttributeHider.class.getName()).log(Level.SEVERE, null, e);
    107. }
    108. }
    109. }
    110.  
    111. });
    112. }
    113.  
    114. public static ItemStack removeAttributes(ItemStack i){
    115. if(i == null) {
    116. return i;
    117. }
    118. if(i.getType() == Material.BOOK_AND_QUILL) {
    119. return i;
    120. }
    121. ItemStack item = i.clone();
    122. net.minecraft.server.v1_7_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
    123. NBTTagCompound tag;
    124. if (!nmsStack.hasTag()){
    125. tag = new NBTTagCompound();
    126. nmsStack.setTag(tag);
    127. }
    128. else {
    129. tag = nmsStack.getTag();
    130. }
    131. NBTTagList am = new NBTTagList();
    132. tag.set("AttributeModifiers", am);
    133. nmsStack.setTag(tag);
    134. return CraftItemStack.asCraftMirror(nmsStack);
    135. }
    136.  
    137. public static net.minecraft.server.v1_7_R1.ItemStack removeAttributes(net.minecraft.server.v1_7_R1.ItemStack i){
    138. if(i == null) {
    139. return i;
    140. }
    141. if(net.minecraft.server.v1_7_R1.Item.b(i.getItem()) == 386) {
    142. return i;
    143. }
    144. net.minecraft.server.v1_7_R1.ItemStack item = i.cloneItemStack();
    145. NBTTagCompound tag;
    146. if (!item.hasTag()){
    147. tag = new NBTTagCompound();
    148. item.setTag(tag);
    149. }
    150. else {
    151. tag = item.getTag();
    152. }
    153. NBTTagList am = new NBTTagList();
    154. tag.set("AttributeModifiers", am);
    155. item.setTag(tag);
    156. return item;
    157. }
    158.  
    159. }[/i][/i]
     
    uyuyuy99 likes this.
  16. Offline

    EnderTroll68

    mncat77
    Will you marry me? I love you. I have spent like a week and a half looking for a way to do this. Thank you so much
     
    mariosunny and uyuyuy99 like this.
Thread Status:
Not open for further replies.

Share This Page