98 lines
2.6 KiB
Markdown
98 lines
2.6 KiB
Markdown
# NBT Lib
|
||
|
||

|
||

|
||
|
||
Библиотека для работы с NBT (Named Binary Tag).
|
||
|
||
Начиная с версии `1.1-RC` разделена на две составляющие: теги и кодеки. Последние занимаются (де)сериализацией тегов.
|
||
|
||
## Примеры работы с NBT
|
||
|
||
**Создание тега типа String:**
|
||
|
||
```java
|
||
TagString tagString = new TagString();
|
||
tagString.setValue("hello!");
|
||
```
|
||
|
||
или в одну строчку
|
||
|
||
```java
|
||
TagString tagString = new TagString("hello!");
|
||
```
|
||
|
||
**Создание тега типа Compound:**
|
||
|
||
```java
|
||
TagCompound tagCompound = new TagCompound();
|
||
tagCompound.getValue().add(new TagLong("Seed", 1234567890));
|
||
```
|
||
|
||
## Сериализация NBT
|
||
|
||
Сериализация в бинарный формат Minecraft Vanilla выполняется библиотекой `nbt-vanilla-codec`.
|
||
|
||
Пример сериализации тега типа String в файл
|
||
|
||
```java
|
||
NbtOutputStream nbtOutputStream = new NbtOutputStream(new FileOutputStream("somefile.bin"));
|
||
nbtOutputStream.writeTag(new TagString("hello!"));
|
||
nbtOutputStream.close();
|
||
```
|
||
|
||
По такому же принципу происходит и десериализация
|
||
|
||
```java
|
||
NbtInputStream nbtInputStream = new NbtInputStream(new FileInputStream("somefile.bin"));
|
||
Tag tag = nbtInputStream.readTag();
|
||
nbtInputStream.close();
|
||
|
||
if (tag.getType() == TypeTag.STRING) {
|
||
TagString tagString = tag.asTagString();
|
||
}
|
||
```
|
||
|
||
## Подключение к проекту
|
||
|
||
### Maven
|
||
|
||
```xml
|
||
<repositories>
|
||
<repository>
|
||
<id>mc-project-repository</id>
|
||
<url>https://dmx-mc-project.gitlab.io/maven-repository/</url>
|
||
</repository>
|
||
</repositories>
|
||
|
||
<dependencies>
|
||
<dependency>
|
||
<groupId>mc-project.nbt</groupId>
|
||
<artifactId>nbt</artifactId>
|
||
<version>1.1-RC</version>
|
||
</dependency>
|
||
|
||
<dependency>
|
||
<groupId>mc-project.nbt.codec</groupId>
|
||
<artifactId>nbt-vanilla-codec</artifactId>
|
||
<version>1.1-RC</version>
|
||
</dependency>
|
||
</dependencies>
|
||
```
|
||
|
||
### Gradle
|
||
|
||
```groovy
|
||
repositories {
|
||
maven {
|
||
name 'mc-project'
|
||
url 'https://dmx-mc-project.gitlab.io/maven-repository/'
|
||
}
|
||
}
|
||
|
||
dependencies {
|
||
implementation (group: 'mc-project.nbt', name: 'nbt', version: '1.1-RC')
|
||
implementation (group: 'mc-project.nbt.codec', name: 'nbt-vanilla-codec', version: '1.1-RC')
|
||
}
|
||
```
|