Saving Banner Patterns

Discussion in 'Spigot Plugin Development' started by Antoine Rieuneau, Apr 30, 2017.

  1. Hello,
    I am trying to save the meta of a banner, so I can use it again later. I have been using this https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/inventory/meta/BannerMeta.html , to get a List<Pattern> with the different patterns. Then to save it, I would make it into strings and send it to my database. The problem is, afterwards, I am unable to get the strings back to a List<Pattern>.
    Therefore, does anyone have an idea of how I could save the pattern of a banner so I can use it again.
    Thanks,
    AnTerZz
     
  2. It's got a getColor() and getPattern() method. Save those two's names to a string with a space in between, and to load you split on the space and use Enum#valueOf(String) to get the correct values.
     
  3. I would construct a vanilla like JSON representation of the TileEntity.
    http://minecraft.gamepedia.com/Chunk_format#Block_entity_format -> Banner
    Code (json (Unknown Language)):
    {
      Base: <BaseColor as int>,
      Patterns: [
        { Color: <PatternColor as int>, Pattern: "<PatternIdentType (2 characters) as String>" },
        { <more patterns...> }
      ]
    }
    So a short example:
    Code (json (Unknown Language)):
    {Base:0,Patterns:[{Color:1,Pattern:"bs"},{Color:1,Pattern:"ts"}]}
    I recomment this way, because it's vanilla-like. When you do things like vanilla mc does it, you'll be on the safe side.

    But how to get this JSON thingy...? In general you got 2 options:
    1. Because it's vanilla-like you could hook into NMS and serialize the TileEntityBanner to get exatly this NBTTagCompound and use NMS to convert it into JSON.
    2. Because the Bukkit API already supports banners & patterns you could make this thing by yourself.
    I'm not sure which option is better. Using NMS might be more safe because of future updates (new patterns,...), but you need to hook into NMS (e.g. by reflections) very carefully so that your code won't break within future updates.

    For the second option you can use e.g. org.json.simple (included by the bukkit server) and add each element into a single JSON object like above, then save it wherever you want. org.json.simple is pretty easy to handle.