LogoLogo
JavaDocsVersions
  • Introduction
  • Getting started
    • Dependencies
    • Your first server
  • Feature
    • Adventure
    • Player capabilities
    • Events
      • Implementation
      • Server list ping
    • Items
    • Entities
      • AI
    • Tags
    • Schedulers
    • Commands
    • Inventories
    • Player UUID
    • Player skin
    • Permissions
    • Advancements
    • Map rendering
      • GLFWMapRendering
    • Query system
    • Open to LAN
  • Thread Architecture
    • Thread safety in the JVM
    • Acquirable API
      • The inside
  • World
    • Instances
    • Chunk management
      • Anvil Loader
    • Blocks
    • Coordinates
    • Generation
    • Batch
  • Extension System
    • Extensions
Powered by GitBook
On this page
  • Overview
  • API

Was this helpful?

Export as PDF
  1. Feature

Tags

Overview

A Tag represents a key, and a way to read/write a specific type of data. Generally exposed as a constant, you can use it to apply or read data from any TagReadable (e.g. Entity, ItemStack, and soon Block). They are implemented using NBT, meaning that applying a tag to an ItemStack will modify its NBT, same for Block, and can therefore be sent to the client.

Tag<String> myTag = Tag.String("key");
Entity entity = ...;
String data = entity.getTag(myTag);

Tags benefit are:

  • Control writability and readability independently with TagReadable/TagWritable, ideal for immutable classes.

  • Hidden conversion complexity, your code should not have to worry about how a List<ItemStack> is serialized.

  • Automatic serialization support (as backed by NBT), easing data persistence and debuggability.

API

First of all, it is recommenced to expose Tags as contant and reused. All Tag methods should be pure, and allow to specify additional information to handle the data.

All tags are available as static factory methods inside the Tag class.

Map

Tag mapping allows you to transform the retrieved value.

Tag<String> stringTag = Tag.String("my-string");
// You should also ensure that the string is a valid uuid!
Tag<UUID> mappedTag = stringTag.map(UUID::fromString, UUID::toString);
UUID uuid = instance.getTag(mappedTag);

Default value

Tag<String> stringTag = Tag.String("my-string");
instance.getTag(stringTag.defaultValue("default"))

TagSerializer

TagSerializer is similar to the #map method, except that you can interact with multiple tags.

Structure

A structure tag is a wrapper around an nbt compound (map) independent from all the other tags.

View

A view can access every tag and is therefore mostly unsafe, should only be used at last resort.

PreviousAINextSchedulers

Last updated 1 year ago

Was this helpful?