WolfyScript / WolfyUtils-Spigot

The WolfyUtilities implementation for the Spigot platform
10 stars 4 forks source link

NBTQuery System to fetch specific NBT components #21

Closed WolfyScript closed 2 years ago

WolfyScript commented 2 years ago

The NBTQuery makes it possible to fetch specified values from NBT.

The way it works is that the NBTQuery constructs a NBTComound, of the values it was specified to fetch, and then returns it. It is written via simple JSON and provides options to filter elements by value, index in lists, and child NBT nodes.

To build the query and describe the desired output, you can use the following settings:

{
  /*
    NBTQuery Settings
  */
  //Primitive Settings
  //Returns the String value if it matches,
  "<key_string>": "<string_value>",
  //Returns the byte value if it matches,
  "<key_byte>": "0b/0B",
  //Returns the short value if it matches,
  "<key_short>": "0s/0S",
  //Returns the long value if it matches,
  "<key_long>": "0l/0L",
  //Returns the int value if it matches,
  "<key_int>": 100,
  "<key_int2>": "100i/100I",
  //Returns the double value if it matches,
  "<key_double>": 1.0,
  "<key_double2>": "1d/1D",
  //Returns the float value if it matches,
  "<key_float>": "1f/1F",
  "<key_primitive>": {
    "type": "string/byte/short/long/int/double/float",
    "value": "<string/byte/short/long/int/double/float>"
  },

  //Hybrid Settings (Applies to primitive, compound, and list nodes)
  //Always returns the value
  "<key>": true,
  //Never returns the value,
  "<key0>": false,

  //Compound Settings
  "<key_compound>": {
    "type": "compound",
    //Do not treat as end-node, proceed to child nodes!
    //The node is not included in the result!
    "<child_key>": "<setting>"
  },
  "<key_compound0>": {
    "type": "compound",
    "preservePath": true, //Optional: keeps the path of the parent and child nodes. Default: true
    "includesAll": false, //Optional: determines the default value for each `includes` child.
    //Optional: Only includes and computes the specified children.
    "includes": {
      "<child_key>": true, //true/false
      //If a key is not specified it will use the value from `includesAll`
    },
    //Proceed to child nodes. If "includes" is used, then it just proceeds to the included nodes!
    "children": {
      "<child_key>": "<setting>"
    },
    //Optional, child keys can be directly put into the root of the compound settings.
    "<child_key>": "<setting>"
  },

  //List Settings
  "<key_array3>": {
    "type": "list/<string/byte/short/long/int/double/float>",
    "values": [
      {
        //Optional: Returns the value at the specified index, if it matches the setting.,
        "index": 0,
        //Optional value (Empty is treated as "true" (Always include))
        "value": {
          "type": "<string/byte/short/long/int/double/float>",
          //Returns the values that match this setting,
          "value": "<setting>"
        }
      },
      //...
    ]
  },

  //Compound List Settings
  //Returns the list with the compounds as defined in the element settings
  "<key_objArray3>": {
    "type": "list/compound",
    "values": [
      {
        "index": 0,
        "value": {
          //See Compound Settings
        }
      }
    ],
  }
}

Once it constructed and returned the NBTCompound it can be used in various ways, like merging it into other NBTCompounds.