BetterNBT - A Gson-like API for intuitively working with Minecraft NBTs

Related tags

Kotlin BetterNBT
Overview

BetterNBT

A lightweight (under 250 lines of code) Kotlin library for Fabric 1.18.x that allows intuitively working with Minecraft NBT by reading & writing to a custom data structure.

Install

Step 1: put this into your gradle.properties:

better_nbt_version=v1.0.0

Step 2: put this into your build.gradle:

repositories {
    maven {
        url = "https://redgrapefruit09.github.io/maven"
        content {
            includeGroup "com.redgrapefruit.betternbt"
        }
    }
}

Step 3: put this in your dependencies block in the build.gradle after the last line:

modImplementation "com.redgrapefruit.betternbt:betternbt:${project.better_nbt_version}"
include "com.redgrapefruit.betternbt:betternbt:${project.better_nbt_version}"

Step 4: refresh your Gradle project.

Usage guide

In this tutorial we'll be creating a simple counter item that:

  • Stores an integer counter in its NBT
  • Increments that counter every tick
  • Displays the counter in its tooltip

First, we need to create the class, from which a schema can be generated. A schema is a description of all nodes (regular fields) and sub-schemas (nested compounds) in the structure of an NBT.
The schema will be created from a class's set of mutable properties, and if a property has a node serializer, it's a node, else it will be interpreted as a sub-schema.

Our class only holds the integer property, so it'll look like this:

class CounterItemData {
    var counter: Int = 0
    
    companion object {
        // Store the schema for this class, use the T::class.schema() method to get
        // a schema for your structure class
        val SCHEMA = CounterItemData::class.schema()
    }
}

Now we'll have to create our Item class, from which we'll make useNbt calls to work with our data and increment the counter & display it in the tooltip:

class CounterItem : Item(Settings().group(ItemGroup.MISC)) {
    override fun inventoryTick(stack: ItemStack, world: World, entity: Entity, slot: Int, selected: Boolean) {
        useNbt<CounterItemData /* the serialized structure */>(
            stack.orCreateNbt /* the NBT tag */,
            CounterItemData.SCHEMA /* the schema */) { data /* the instance of your structure, which has the data read
            in and, if you mutate it, will be saved back to the given NBT tag */ ->

            data.counter++ // increment the counter
        }
    }

    override fun appendTooltip(
        stack: ItemStack,
        world: World?,
        tooltip: MutableList<Text>,
        context: TooltipContext
    ) {
        // repeat the call from above
        useNbt<CounterItemData>(stack.orCreateNbt, CounterItemData.SCHEMA) { data ->
            tooltip += LiteralText(data.counter.toString()) // add a literal text with the counter to the item's tooltip
        }
    }
}

Now register your item into the game (see this tutorial if you don't know how to do it) and you'll see a saved counter item working just fine!

You might also like...
Gestor is an innovative open-source application for universal Minecraft mod management.

Gestor Gestor is an innovative open-source application for universal Minecraft mod management. About It combines three types of tools into a single ap

VirtualTag is a name tag edit plugin for minecraft server

VirtualTag VirtualTag is a NameTag Edit plugin for modern minecraft server Support Version 1.17.x Download https://github.com/jiangdashao/VirtualTag/r

Simple random ore mod for Minecraft/Fabric. Heavily inspired by Randomite.

Rand'Ore Download Simple random ores for Fabric! This mod is open source and under a permissive license. As such, it can be included in any modp

MIT mappings for Minecraft with no exceptions

leather MIT mappings for Minecraft with no exceptions Tannery Tannery is our custom gradle plugin to create the perfect mapping workspace Tasks build

An open-source plugin that accommodates Starships on Minecraft servers.

Minecraft Starship Plugin An open-source plugin that accommodates starships on minecraft servers. 'A shameless rip-off of Star Legacy's plugins.' Mine

A powerful Minecraft Server Software coming from the future

Mirai A powerful Minecraft Server Software coming from the future Mirai is ❗ under heavy development ❗ and contributions are welcome! Features 30% fas

Create minecraft worlds without lag!

WorldGen 랙 없는 월드 생성을 위한 라이브러리 시작하기에 앞서, 이 라이브러리는 UHC_System 의 월드 생성 코드를 작성하면서 WorldCreator의 단점인 랙을 없애기 위해 만들어진 코드의 일부를 라이브러리로 공개하는 것임을 알려드립니다 WorldCre

An easy to use package manager for Fabric Minecraft mods.

pacmc pacmc is a package manager for Fabric Minecraft mods. The aim of this project is to massively reduce the effort you have to put in to installing

Simple random ore mod for Minecraft/Rift. Heavily inspired by Randomite.

Rand'Ore Download Simple random ores for Rift! This mod is open source and under a permissive license. As such, it can be included in any modpac

Owner
RedGrapefruit
Made in Russia.
RedGrapefruit
Godot's AdMob Plugin for Android (3.2.2+) using GitHub Actions for CI/CD. Working on Standard and Mono Godot versions.

Godot AdMob Android A Godot's plugin for Android of AdMob. About • Installation • Docs • Downloads About This repository is for a Godot Engine Plugin

Poing Studios 148 Jan 8, 2023
Mobile Application Dvelopment Practical-12: Working with JSON APIs

Mobile Application Dvelopment Practical-12: Working with JSON APIs Developed by,

Achal Hingrajiya 0 Jan 11, 2022
A library for working with URIs in Kotlin Multiplatform

Uri KMP Most of this work is derived from AOSP's Uri: Uri.java UriCodec.java UriTest.java UriCodecTest.java Gradle Groovy repositories { mavenCentra

Eliezer Graber 10 Jan 4, 2023
The AppMetrica Push SDK is a set of libraries for working with push notifications.

Flutter AppMetrica Push AppMetrica Push SDK — это набор библиотек для работы с push-уведомлениями. Подключив AppMetrica Push SDK, вы можете создать и

Mad Brains 6 Oct 12, 2022
A injection minecraft cheat using jvm attach api

Luminous A injection minecraft cheat using jvm attach api Website: https://lumi.getfdp.today Build We used a thing called Wrapper to make development

null 24 Dec 21, 2022
Minecraft NBT support for kotlinx.serialization

knbt An implementation of Minecraft's NBT format for kotlinx.serialization. Technical information about NBT can be found here. Using the same version

Ben Woodworth 41 Dec 21, 2022
Gradle plugin adding a task to run a Paper Minecraft server

Run Paper Run Paper is a Gradle plugin which adds a task to automatically download and run a Paper Minecraft server along with your plugin built by Gr

Jason 64 Dec 29, 2022
An under development minecraft plugin (1.8.8) to learning Kotlin language

CorePlus CorePlus is a minecraft plugin coded with Kotlin language. Still under development CorePlus will be an essential for each minecraft servers !

Gonz 3 Jun 16, 2021
Run Minecraft on the command line

HeadlessForge While headless Minecraft Clients aren't anything new, they come with a drawback. The Minecraft API is missing and you need to add all fu

null 28 Oct 17, 2022
Kotlin utility mod for Minecraft

Lambda is a free, open-source, Minecraft 1.12.2 utility mod providing a visionary system for plugins that allow customizing the clients features thank

Lambda 405 Dec 28, 2022