Persistent block metadata

Discussion in 'Plugin Development' started by LKits, Jan 18, 2021.

  1. Offline

    LKits

    Hello!

    I am aware that you can store info in block metadata, but it gets deleted after server restart so it's not persistent.
    Any way to make it persistent?
    Maybe someone has created some additional plugin for that?

    PS: I am aware that I could store my own data in yml or database or wherever. I am not here to invent bicycle. So if someone has already made persistent block metadata plugin, then I will use that instead of creating my own.

    Thank you!
     
  2. Offline

    Ankoki

    Could use NBTAPI to do this i believe.
     
  3. Offline

    Kars

    Then you're in the wrong section.
     
  4. Offline

    Legendary_zotar



    If your using later versions then i highly recommend PersistentDataContainer

    Edit:
    (Doesnt look like its stored in the metadata though)

    Heres an example of how to use it, assuming you working with a chest:
    Code:
    //Setting (NOTE: there are other PersistentDataTypes. And you can just create 1 instance of a NamespacedKey)
    chest.getPersistentDataContainer().set(new NamespacedKey(main, "Key"), PersistentDataType.STRING, "Value");
    
    //Checking
    if(chest.getPersistentDataContainer().has(new NamespacedKey(main, "Key"), PersistentDataType.STRING))
    
    //Getting (NOTE: Returns null if not found)
    chest.getPersistentDataContainer().get(new NamespacedKey(main, "Key"), PersistentDataType.STRING);
     
    Last edited: Jan 18, 2021
  5. Offline

    ForbiddenSoul

    Currently there is no publicly known way to store information on blocks persistently (after server restart).
    There is the PersistentDataContainer as mentioned before which can be used to store info on all PersistenDataHolders.
    Some of these are blocks, like ageable (wheat, cactus, etc.), chest, and many others.
    See All known Subinterfaces of PersistentDataHolder for a complete list.
    If you just want to store information on generic blocks like dirt, pretty sure you are out of luck for anything built it.

    You can however serialize the location of the block, map that to some value, and store that in a .yml, FlatFile, or DataBase, then load that information on plugin start up.
     
    Last edited: Jan 19, 2021

Share This Page