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 , 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.
  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. -> 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)):
    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.