GithubHelp home page GithubHelp logo

powernukkit / nbt-manipulator Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 1.0 4.71 MB

A kotlin/java lib that allows you to read and write NBT data in a clean way

License: MIT License

Kotlin 99.40% Java 0.55% SCSS 0.05%
kotlin-library java-library nbt nbt-library nbt-api minecraft minecraft-library

nbt-manipulator's Introduction

NBT Manipulator

This is a Kotlin/Java library that allows you to read and write NBT files and data in a clean and simple way.

Can be used both for Minecraft Java and Minecraft Bedrock Edition NBT structures.

Here you can find the library documentation:

You may also want to see the changelog file to be aware of all changes in the tool that may impact you.

TIP: If you are using NbtList with Kotlin, add a import br.com.gamemods.nbtmanipulator.* for easy list management.

For example:

val list = listOf(2,3,4,5,6).toNbtList()
list += 7
check(7 in list)
list -= 7
check(7 !in list)

Adding to your project

The library is shared in the maven center, so you don't need to declare any custom repository.

Gradle

Kotlin DSL

repositories {
    mavenCentral()
}

dependencies {
    implementation("br.com.gamemods:nbt-manipulator:3.1.0")
}

Groovy DSL

repositories {
    mavenCentral()
}

dependencies {
    implementation 'br.com.gamemods:nbt-manipulator:3.1.0'
}

Maven

<dependencies>
    <dependency>
      <groupId>br.com.gamemods</groupId>
      <artifactId>nbt-manipulator</artifactId>
      <version>3.1.0</version>
    </dependency>
</dependencies>

Ivy

<dependency org="br.com.gamemods" name="nbt-manipulator" rev="3.1.0"/>

Direct JAR

Download it from maven central.

Examples

val compound = NbtCompound()
compound["A string tag"] = "The string tag value"
compound["An int tag"] = 2
val byteValue: Byte = 3
compound["An byte tag"] = byteValue

println(compound.getString("A string tag"))
println(compound.getInt("A string tag"))
println(compound.getInt("An int tag"))
println(compound.getNullableByte("An byte tag"))

println(compound.getNullableStringList("This is missing.."))

val otherConstructor = NbtCompound(
    "a" to NbtString("string"),
    "b" to NbtList(listOf(NbtDouble(0.0), NbtDouble(1.1)))
)
println(otherConstructor["a"])
println(otherConstructor.getDoubleList("b"))
internal fun convertLevelFile(from: File, to: File) {
    val input = NbtIO.readNbtFile(from)
    val inputData = input.compound.getCompound("Data")

    val outputData = NbtCompound()
    outputData.copyFrom(inputData, "GameRules")
    outputData.copyFrom(inputData, "DayTime")
    outputData.copyFrom(inputData, "GameType")
    outputData.copyFrom(inputData, "generatorName")
    outputData.copyFrom(inputData, "generatorVersion")
    outputData.copyFrom(inputData, "generatorVersion")
    outputData.copyFrom(inputData, "generatorOptions", NbtString(""))
    outputData["hardcore"] = false
    outputData["initialized"] = false
    outputData.copyFrom(inputData, "LastPlayed")
    outputData.copyFrom(inputData, "LevelName")
    outputData.copyFrom(inputData, "raining")
    outputData.copyFrom(inputData, "rainTime")
    outputData.copyFrom(inputData, "RandomSeed")
    outputData.copyFrom(inputData, "SizeOnDisk")
    outputData.copyFrom(inputData, "SpawnX")
    outputData.copyFrom(inputData, "SpawnY")
    outputData.copyFrom(inputData, "SpawnZ")
    outputData.copyFrom(inputData, "thundering")
    outputData.copyFrom(inputData, "thunderTime")
    outputData.copyFrom(inputData, "Time")
    outputData.copyFrom(inputData, "version")

    val output = NbtCompound("Data" to outputData)
    val file = NbtFile("", output)
    NbtIO.writeNbtFile(to, file)
}

nbt-manipulator's People

Contributors

joserobjr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

koterite

nbt-manipulator's Issues

NbtIO.writeNbtFile produces ClassCastException

Version: 3.1.0

Thanks for the really useful library, just noticed an issue when using this method:

NbtIO.writeNbtFile(File("test.mcstructure"), NbtFile("", NbtCompound()), compressed=false, littleEndian=true)

which produces:

Exception in thread "main" java.lang.ClassCastException: class java.io.BufferedOutputStream cannot be cast to class java.io.DataOutputStream (java.io.BufferedOutputStream and java.io.DataOutputStream are in module java.base of loader 'bootstrap')
        at br.com.gamemods.nbtmanipulator.LittleEndianDataOutputStream.<init>(LittleEndianDataOutputStream.kt:15)
        at br.com.gamemods.nbtmanipulator.NbtIO.writeNbtFile(NbtIO.kt:42)
        at br.com.gamemods.nbtmanipulator.NbtIO.writeNbtFile(NbtIO.kt:87)
        at br.com.gamemods.nbtmanipulator.NbtIO.writeNbtFile$default(NbtIO.kt:81)

a similar thing also happens with compressed=true

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.