An open API service indexing awesome lists of open source software.

https://github.com/defective4/jchatlib

A Minecraft NBT and text component parser library
https://github.com/defective4/jchatlib

Last synced: 4 months ago
JSON representation

A Minecraft NBT and text component parser library

Awesome Lists containing this project

README

          

# jChatLib

jChatLib is a minimal Minecraft chat component **and** NBT parser library.
It allows you to parse and encode NBT with both named and unnamed root tags, used in Minecraft's files and sent over network.
It also lets you parse text components, both from JSON and NBT!
The library is still under development, new features and improvement, as well as proper documentation are planned.

## Features
- **Complete** - supports all standard NBT data types
- **Flexible** - Allows parsing text components both from JSON and NBT, making it compatible with **all** Minecraft versions!
- **Simple to use**

## Installation
Add
```xml

io.github.defective4.minecraft
jchatlib
1.1.1

```
to your `pom.xml`'s dependencies section

## Usage

Parsing chat components from JSON:
```java
String json = "{\"text\": \"Hello \", \"extra\": [\"World\", {\"text\": \"!\"}]}";
JsonElement parsed = JsonParser.parseString(json);
ChatComponent component = ChatComponent.fromJson(parsed);
System.out.println(component.toPlainString()); // Outputs "Hello World!"
```

Parsing NBT from file:
```java
Tag nbt;
try(InputStream is = new FileInputStream("Hello_world.nbt")) {
// v----- Read root tag name
nbt = NBT.parse(is, true, false);
// ^----- (don't) use GZIP compression
} catch(Exception e) {
e.printStackTrace();
return;
}
System.out.println(nbt);
```

Parsing text components from NBT:
```java
InputStream in = ...
Tag tag = NBT.parse(in, false, false);
ChatComponent component = ChatComponent.fromNBT(tag);
System.out.println(component.toPlainString());
```

Creating a NBT file:
```java
try (OutputStream os = new FileOutputStream("test.nbt") {
CompoundTag root = new CompoundTag("root");

// v----- Tag name
StringTag text = new StringTag("text", "Hello world!");
// ^----- Tag value

root.add(text); // v--------- (don't) use network format
byte[] data = NBT.encode(root, false);
os.write(data);
}