> For the complete documentation index, see [llms.txt](https://thisistails.gitbook.io/tailslib/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://thisistails.gitbook.io/tailslib/tailslib-rus/custom-items/nbt-state.md).

# NBT State

In this page we gonna learn how to use `NBTState` in TailsLib.

## What is NBTState?

`NBTState` is basicly wrapper for `PersistentDataContainer` (PDC) inspired by React.js `useState` .

Nothing fancy really.

## Usage

To use `NBTState` we need a type that `NBTState` gonna use for access `PDC` and that means we need `PersistentDataType`. You could create your own `PersistentDataType` to automaticly convert data from `PDC` to your object.

So basicly we have 2 constructors.

```java
public NBTState(String key, PersistentDataType<?, T> type)
public NBTState(String key, PersistentDataType<?, T> type, T defaultValue)
```

Just create a field with one of constructors.

```java
public final NBTState<Integer> clicksState = new NBTState<>(
    "clicks", // Key of NBT
    PersistentDataType.INTEGER, // Type 
    0 // Default value to be set
);
```

To access and write data to `PDC` with this state we use

```java
ItemStack item = new ItemStack(Material.DIAMOND);

// Will return 0 since item does not have any NBT
// This operation also will automaticly write 0 to NBT of this item
int clicks = clicksState.get(item);

clicksState.set(item, 10); // Set clicks to 10 for this item.
clicks = clicksState.get(item); // Now will return 10.
```

There's example if without default value

```java
NBTState<String> someState = new NBTState<>(
    "something", // Key of NBT
    PersistentDataType.STRING // Type 
);

ItemStack item = new ItemStack(Material.DIAMOND);
String something = someState.get(item); // Will return null
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://thisistails.gitbook.io/tailslib/tailslib-rus/custom-items/nbt-state.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
