How do I save data on an ItemStack in ver 1.10.2?

Discussion in 'Plugin Development' started by Benjamint22, Aug 20, 2016.

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

    Benjamint22

    Hello, I would like to know how I can save data on an ItemStack in Minecraft version 1.10.2?
    I need the data to be invisible to the player and I need it to be saved in between shutdowns and such just like NBT Tags.
    Unfortunately I've tried NBT Tags and several NBT plugins and for some reasons, I never get an error but nothing is ever saved (or at-least never retrieved). Could somebody help me please?

    I would even be fine with a complex solution such as finding some sort of identifier for the item and saving it in a database.
     
  2. Offline

    InstanceofDeath

    I searched the hole internet for a solution .. nothing found ... I just had a (medium-genius) idea

    I am sitting at my IPad so I have no check if this code is correct

    Code:
    
    ItemStack markedItemStack = new ItemStack(MATERIAL.Stone_Sword);
    markedItemStack.setDurability((byte)1);
    ItemMeta markedItemStackMeta = markedItemStack.getItemMeta();
    markedItemStackMeta.setUnbreakable(true);
    markedItemStack.setItemStack(markedItemStackMeta);
    
    //Its impossible to loose durability on a stone sword with unbreakable tag
    
    PlayerVariable.getInventory().addItem(markedItemStack);
    
    if(markedItemStack.getDurability == (byte)1) {
        //bla bla
    }
    
    
    Two problems
    1. Only works with items that are breakable (weapons for example, but not blocks)
    2. Item will get unbreakable

    I will search for a better solution ..
     
  3. Offline

    I Al Istannen

    @Benjamint22
    Nbt does work. Have a look at this plugin. Source within, just open with 7-zip/winrar. Magic in BookUtil, NBTUtil and ReflectionUtil.

    Tested for 1.8 but afaik it works on 1.10 too.
     
  4. Offline

    InstanceofDeath

    And in which nbt tag you will place the data then?? Added nbt-tags get auto-deleted from bukkit at the start of the server. I found out 4 options:
    1. Use ProtocolLib to intercept a PacketPlayIn something .. very complicated!
    2. In the onDisable Method you create a loop, that loops trough all players that were on the server, look if they has the item, on which slot it is and the players name .. and then you check it where you need it
    3. You can add into the onDestroy nbt-tag a specified block so you can check the item trough it
    4. save it trough the negative durability
     
  5. Offline

    I Al Istannen

    @InstanceofDeath
    The "auto-delete" was a bug a long time ago. It is fixed for me (1.8.8) and afaik also for higher versions.
    Else no command block contraption would work these days with bukkit.

    And this plugin I linked works through restarts. So no, they should not get auto-deleted. What version are you running?
     
  6. Offline

    InstanceofDeath

    1.8.1 mistake found :)
     
  7. Offline

    I Al Istannen

    @InstanceofDeath
    Let's hope it is really this :p

    I just read the fact it was a bug in some thread some time ago, and it works for me. So I am quite positive it was fixed ;)
     
  8. Offline

    Benjamint22

    Thanks!! I'll be looking at it soon and reply once I found something.
     
  9. Offline

    Lordloss

    You can take a look at this Util class too, its extremely simple to use.
     
    I Al Istannen likes this.
  10. Offline

    I Al Istannen

    @Lordloss
    While I agree, it is not as seemless (Granted, complaining on SUCH a high level xD). But thanks for mentioning it!
     
Thread Status:
Not open for further replies.

Share This Page