Conorsmine / MojangUtils

MojangUtils is a utility class for the popular NBTAPI

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MojangUtils

MojangUtils is a utility library for the popular NBTAPI.
MojangUtils mainly focuses on the implementation of NBTPaths.

Usage

You can freely download, share, alter and ofcourse use this code.
Too use or "import" the project, download the source code and add it to your project. Though refactoring of the import is probably needed then.
However, this code is not "multi-version" compatible, yet (?). So manier import will probably not be functioning anyways and some alterations might need to be made.

MojangsonUtils

The MojangsonUtils class has 3 main functionalities:

  • Formatting; Format NBT extensively with the #getInteractiveMojangson method
  • Getting; Retrieving data via the #getDataFromPath method
  • Setting; Setting data into NBT via the <T> #setDataToPath method

Too create an instance of MojangsonUtils use the MojangsonUtilsBuilder too create an instance with the formatting you desire.

NBTPath

NBTPaths are meant to allow the traversal of NBT with text! This is especially useful if you want users to be able to input and work with NBT.
NBTPaths are therefore just a collection of NBTKeys with a certain string value.
Too create a NBTPath create a NBTPathBuilder first and add all neccessery keys to it, before ceating the actual NBTPath.
This is done to ensure that NBTPaths don't change after being created.

NBTKey

There are 2 types of NBTKeys:

  • NBTKey; A string key for key-value pairs/NBTCompounds
  • NBTArrayKey; A integer key for lists or arrays

Creating either can be done with their respective #parseStringToKey function.

Code Examples

Since MojangsonUtils is the main aspect of the code library, let's start there:

final MojangsonUtils utils = new MojangsonUtilsBuilder("##")
                .setValCol(ChatColor.RED)
                .setValTypeCol(ChatColor.MAGIC)
                .setTagCol(ChatColor.AQUA)
                .create();

Here we create an instance of MojansonUtils where the Separator is "##". This means that NBTPathBuilders using this instance will split the provided String at every instance of "##".

final NBTPath baublesPath = new NBTPathBuilder(utils).parseString("ForgeCaps##baubles:container##Items").create();

Here utils is a the instance of the MojangsonUtils class from above.
The #praseString method will split the provided String at every "##", resulting in the following path:
ForgeCaps -> baubles:container -> Items

But how do NBTArrayKeys work? Very simply:

final NBTArrayKey arrKey = NBTArrayKey.parseToArrayKey(0);

This creates in secret a regular NBTKey with a key value of "[0]". The same formatting can be used when parsing a String to NBTPathBuilder:

final NBTPath arrKey = new NBTPathBuilder(utils).parseString("[0]").create();

Both do practically the same, but one is alot easier todo and recommended.

In case you want to create an empty NBTPath you can do it the following:

final NBTPath emptyPath = new NBTPathBuilder(utils).create();

About

MojangUtils is a utility class for the popular NBTAPI


Languages

Language:Java 100.0%