Skip to content

cuisse/NBT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NBT

Maven Central javadoc License

NBT library in Java.

Usage

Creating a simple compound:

import io.github.cuisse.nbt.NamedTag;
import io.github.cuisse.nbt.tags.IntTag;
import io.github.cuisse.nbt.tags.StringTag;
import io.github.cuisse.nbt.tags.CompoundTag;

var profile = CompoundTag.create(
        new NamedTag("name", StringTag.create("John")),
        new NamedTag("age" , IntTag.create(42))
);

System.out.println(profile.get("name").value()); // String  -> John
System.out.println(profile.get("age").value());  // Integer -> 42

// Or
System.out.println(profile.get("name").value()); // String  -> John
System.out.println(profile.get("age", IntTag.class).primitiveValue()); // int -> 42

And since we all enjoy the sugar:

import io.github.cuisse.nbt.Tag;
import java.util.Map;

var profile = Tag.from(
        Map.of(
                "name", "John",
                "age" , 42
        )
);

System.out.println(profile.get("name").value()); // String  -> John
System.out.println(profile.get("age").value());  // Integer -> 42

If you don't afraid of Unchecked Exceptions:

import io.github.cuisse.nbt.Tag;
import io.github.cuisse.nbt.tags.CompoundTag;

var profile = CompoundTag.create(
        "name", Tag.from("John"),
        "age" , Tag.from(42)
);

System.out.println(profile.get("name").value()); // String  -> John
System.out.println(profile.get("age", IntTag.class).primitiveValue()); // int -> 42

You can also work with a List:

import io.github.cuisse.nbt.Tag;
import io.github.cuisse.nbt.tags.IntTag;
import io.github.cuisse.nbt.tags.ListTag;

var numbers = ListTag.create(
        IntTag.create(0),
        IntTag.create(1),
        IntTag.create(2),
);

System.out.println(numbers.getAt(1).value()); // Integer -> 1

Printing

Printing is very easy, you can use io.github.cuisse.nbt.Tag::toString or io.github.cuisse.nbt.Tag::prettyPrint(tag, depth). For example:

import io.github.cuisse.nbt.Tag;
import io.github.cuisse.nbt.NamedTag;
import io.github.cuisse.nbt.tags.IntTag;
import io.github.cuisse.nbt.tags.StringTag;
import io.github.cuisse.nbt.tags.CompoundTag;

var profile = CompoundTag.create(
        new NamedTag("name", StringTag.create("John")),
        new NamedTag("age" , IntTag.create(42))
);

System.out.println(Tag.prettyPrint(profile, 0));

// Output:
// TAG_Compound(): 2 parameters
// {
//  name: StringTag("John")
//  age: IntTag(42)
// }

Releases

No releases published

Packages

No packages published

Languages