Coder Social home page Coder Social logo

nbt's Introduction

NBT

Java NBT (Named Binary Tag) library.

Getting started

For Gradle projects:

repositories {
    maven { url 'https://maven.izzel.io/releases' }
}
dependencies {
    compile 'io.izzel:nbt:VERSION'
}

The library requires Java 8 or above.

Code examples

Conversion between different formats:

import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;

import io.izzel.nbt.util.*;

// string <===> gzipped binary
public void copyLevelData() throws IOException {
    Path input = Paths.get("level.dat");
    Path output = Paths.get("level.dat_old");

    String stringNbt = new CompressedNbtReader(input).toStringNbt();
    new StringNbtReader(stringNbt).toCompressedBinaryFile(output);
}

// string <===> uncompressed binary
public void backupServers() throws IOException {
    Path input = Paths.get("servers.dat");
    Path output = Paths.get("backup/servers.dat");

    String stringNbt = new NbtReader(input).toStringNbt();
    new StringNbtReader(stringNbt).toBinaryFile(output);
}

Dealing with in-memory objects:

import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;

import io.izzel.nbt.*;
import io.izzel.nbt.util.*;

public void filterOutLocalServers() throws IOException {
    Path file = Paths.get("servers.dat");

    CompoundTag root = new NbtReader(file).toCompoundTag();
    ListTag serverList = root.getListOrDefault("servers");

    ListTag.Builder builder = ListTag.builder();
    for (int i = 0; i < serverList.size(); ++i) {
        CompoundTag server = serverList.getCompoundOrDefault(i);
        String ip = server.getString("ip", /*fallback*/"127.0.0.1");
        if (!"127.0.0.1".equals(ip) && !"localhost".equals(ip)) {
            builder.add(server);
        }
    }

    ListTag newServerList = builder.build();
    CompoundTag newRoot = CompoundTag.builder().add("servers", newServerList).build();

    new TagReader(newRoot).toBinaryFile(file);
}

Open-source license

The library is open-source project, under MIT license.

Built from source

Linux or macOS:

git clone https://github.com/IzzelAliz/nbt.git IzzelAlizNBT --depth 20
cd IzzelAlizNBT/
./gradlew build --exclude-task test

Microsoft Windows:

git clone https://github.com/IzzelAliz/nbt.git IzzelAlizNBT --depth 20
cd IzzelAlizNBT/
gradlew.bat build --exclude-task test

Please make sure you have installed Git on your machine.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.