Skip to content
Back to skills

Nms Data Component

ASecurity

操作 Minecraft DataComponentType(1.20.5+) 物品組件系統,讀寫 CustomData、MaxStackSize、Enchantments 等組件(Paper NMS + Mojang-mapped)/ Read and write 1.21 DataComponentType item components including CustomData, MaxStackSize, Enchantments

  • 3 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added November 23, 2026
ai-agentsgojavaapi

Works with

  • api

Security analysis

A100/100

Pro scans all 5 files and shows the line behind each finding

Scanned December 3, 2026

npx -y skills add MrPippi/MJP-Paper-Skills --skill nms-data-component --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Nms Data Component?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Nms Data Component
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/mrpippi-nms-data-component/badge)](https://www.skillsdirectory.com/skills/mrpippi-nms-data-component)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: nms-data-component
description: "操作 Minecraft DataComponentType(1.20.5+) 物品組件系統,讀寫 CustomData、MaxStackSize、Enchantments 等組件(Paper NMS + Mojang-mapped)/ Read and write 1.21 DataComponentType item components including CustomData, MaxStackSize, Enchantments"
---

# NMS Data Component

## Skill Name

`nms-data-component`

## Purpose

Work with the `DataComponentType` item component system introduced in Minecraft 1.20.5. Read and write components such as `CustomData`, `MaxStackSize`, `Enchantments` and `AttributeModifiers` directly, replacing the legacy NBT `getTag()`/`setTag()` pattern.

**Try the Paper API first.** Paper exposes most components without NMS through `io.papermc.paper.datacomponent.DataComponentTypes` and `ItemStack#setData` / `getData` (present on 1.21.11 and 26.3; Paper 26.3 removed the experimental marker from most of it). Use this NMS skill only for:

- components Paper does not expose. On 26.3, `brewing_fuel`, `cooking_fuel`, `compostable` and `block_transformer` exist in `DataComponents` but not in `DataComponentTypes`
- raw `CustomData` NBT

### Component changes 26.2 → 26.3

Compared from the `net.minecraft.core.component.DataComponents` fields of the 26.2 and 26.3 dev bundles:

| Change | Components |
|------|------|
| Added | `attack_animation`, `interact_animation`, `mob_visibility`, `villager_food`, `brewing_fuel`, `cooking_fuel` (replaces the `#brewing_fuel` item tag), `compostable`, `block_transformer`, `provides_pottery_pattern`, `sign_text_front`, `sign_text_back`, `waxed` |
| Removed | `swing_animation` (split into `attack_animation` / `interact_animation`), `map_color` |
| Reshaped | `pot_decorations` is an object (`back` / `left` / `right` / `front`, each optional) instead of a list of four |

Code that names a removed component no longer compiles on 26.3. Guard it with `// @1.21.11:` (one line) or `// @only 1.21.11` (one block).

## NMS Version Requirements

- Paper 1.21.11 / 26.3(DataComponent was added in 1.20.5+)
- Paperweight userdev 2.0.0-beta.24+
- Mojang official names (Minecraft is no longer obfuscated since 26.1)
- **Spigot**: Paper only — built on the Paperweight dev bundle, which exists only for Paper (see `references/paper-nms-platform.md` §9)

## Triggers

- "NMS DataComponent", "DataComponents", "CustomData", "CustomData component", "component map"
- "NMS 物品組件", "cooking_fuel", "brewing_fuel", "compostable", "block_transformer" (components the Paper API does not expose)
- For ordinary item components use `paper-data-components` (Paper API, no NMS)

## Inputs

| Parameter | Example | Description |
|------|------|------|
| `package_name` | `com.example.item` | Package of the generated classes |
| `class_name` | `ItemComponentUtil` | Utility class name |

## Outputs

- `ItemComponentUtil.java` — DataComponent read/write utility
- `CustomDataHelper.java` — CustomData (custom NBT component) operations

## Build Setup

See [`references/paper-nms-platform.md`](references/paper-nms-platform.md). Key dependency:

```groovy
dependencies {
    paperweight.paperDevBundle('26.3.build.273-stable')
}
```

## Code Template

### `ItemComponentUtil.java`

```java
package com.example.item;

import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.CustomData;
import net.minecraft.util.Unit;
import net.minecraft.world.item.component.ItemLore;
import net.minecraft.world.item.component.TooltipDisplay;
import net.minecraft.world.item.enchantment.ItemEnchantments;
import org.bukkit.craftbukkit.inventory.CraftItemStack;

import java.util.Optional;

@SuppressWarnings("UnstableApiUsage")
public final class ItemComponentUtil {

    private ItemComponentUtil() {}

    /** Reads the value of the given DataComponentType. */
    public static <T> Optional<T> get(
            org.bukkit.inventory.ItemStack item, DataComponentType<T> type) {
        ItemStack nms = CraftItemStack.asNMSCopy(item);
        return Optional.ofNullable(nms.get(type));
    }

    /** Sets the given DataComponentType and returns the modified Bukkit ItemStack (immutable). */
    public static <T> org.bukkit.inventory.ItemStack set(
            org.bukkit.inventory.ItemStack item, DataComponentType<T> type, T value) {
        ItemStack nms = CraftItemStack.asNMSCopy(item);
        nms.set(type, value);
        return CraftItemStack.asBukkitCopy(nms);
    }

    /** Removes the given DataComponentType. */
    public static org.bukkit.inventory.ItemStack remove(
            org.bukkit.inventory.ItemStack item, DataComponentType<?> type) {
        ItemStack nms = CraftItemStack.asNMSCopy(item);
        nms.remove(type);
        return CraftItemStack.asBukkitCopy(nms);
    }

    /** Checks whether the item has the given component. */
    public static boolean has(
            org.bukkit.inventory.ItemStack item, DataComponentType<?> type) {
        ItemStack nms = CraftItemStack.asNMSCopy(item);
        return nms.has(type);
    }

    // ─── Common component shortcuts ───────────────────────────────────

    /** Sets the item's max stack size. */
    public static org.bukkit.inventory.ItemStack setMaxStackSize(
            org.bukkit.inventory.ItemStack item, int size) {
        return set(item, DataComponents.MAX_STACK_SIZE, size);
    }

    /**
     * Makes the item unbreakable.
     * Since 1.21.5, UNBREAKABLE is a Unit marker component; whether the tooltip is shown is controlled by hiddenComponents in TOOLTIP_DISPLAY.
     */
    public static org.bukkit.inventory.ItemStack setUnbreakable(
            org.bukkit.inventory.ItemStack item, boolean showTooltip) {
        ItemStack nms = CraftItemStack.asNMSCopy(item);
        nms.set(DataComponents.UNBREAKABLE, Unit.INSTANCE);
        TooltipDisplay display = nms.getOrDefault(DataComponents.TOOLTIP_DISPLAY, TooltipDisplay.DEFAULT);
        nms.set(DataComponents.TOOLTIP_DISPLAY, display.withHidden(DataComponents.UNBREAKABLE, !showTooltip));
        return CraftItemStack.asBukkitCopy(nms);
    }

    /** Reads the enchantment list. */
    public static Optional<ItemEnchantments> getEnchantments(
            org.bukkit.inventory.ItemStack item) {
        return get(item, DataComponents.ENCHANTMENTS);
    }

    /**
     * Reads the first float value of the custom model data.
     * Since 1.21.4, CustomModelData is no longer a single int but four lists: floats / flags / strings / colors.
     */
    public static Optional<Float> getCustomModelData(
            org.bukkit.inventory.ItemStack item) {
        return get(item, DataComponents.CUSTOM_MODEL_DATA)
            .filter(cmd -> !cmd.floats().isEmpty())
            .map(cmd -> cmd.floats().get(0));
    }
}
```

### `CustomDataHelper.java` (custom NBT component)

```java
package com.example.item;

import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.CustomData;
import org.bukkit.craftbukkit.inventory.CraftItemStack;

import java.util.Optional;

/**
 * CustomData component operations (replaces the legacy getTag().getCompound("custom_key") pattern).
 *
 * Since 1.20.5, CustomData is a standalone component stored under DataComponents.CUSTOM_DATA;
 * custom keys are no longer stored directly at the root tag level.
 */
@SuppressWarnings("UnstableApiUsage")
public final class CustomDataHelper {

    private CustomDataHelper() {}

    /** Reads the given string field from the CustomData component. */
    public static Optional<String> getString(
            org.bukkit.inventory.ItemStack item, String key) {
        ItemStack nms = CraftItemStack.asNMSCopy(item);
        CustomData customData = nms.get(DataComponents.CUSTOM_DATA);
        if (customData == null) return Optional.empty();
        // Since 1.21.5, CompoundTag getters return Optional (there is also getStringOr(key, default))
        return customData.copyTag().getString(key);
    }

    /** Writes a string into the CustomData component and returns the modified Bukkit ItemStack. */
    public static org.bukkit.inventory.ItemStack setString(
            org.bukkit.inventory.ItemStack item, String key, String value) {
        ItemStack nms = CraftItemStack.asNMSCopy(item);
        CustomData existing = nms.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY);
        CompoundTag tag = existing.copyTag();
        tag.putString(key, value);
        nms.set(DataComponents.CUSTOM_DATA, CustomData.of(tag));
        return CraftItemStack.asBukkitCopy(nms);
    }

    /** Writes an integer into the CustomData component. */
    public static org.bukkit.inventory.ItemStack setInt(
            org.bukkit.inventory.ItemStack item, String key, int value) {
        ItemStack nms = CraftItemStack.asNMSCopy(item);
        CustomData existing = nms.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY);
        CompoundTag tag = existing.copyTag();
        tag.putInt(key, value);
        nms.set(DataComponents.CUSTOM_DATA, CustomData.of(tag));
        return CraftItemStack.asBukkitCopy(nms);
    }

    /** Removes the given field from CustomData. */
    public static org.bukkit.inventory.ItemStack removeKey(
            org.bukkit.inventory.ItemStack item, String key) {
        ItemStack nms = CraftItemStack.asNMSCopy(item);
        CustomData existing = nms.get(DataComponents.CUSTOM_DATA);
        if (existing == null) return item;
        CompoundTag tag = existing.copyTag();
        tag.remove(key);
        nms.set(DataComponents.CUSTOM_DATA, CustomData.of(tag));
        return CraftItemStack.asBukkitCopy(nms);
    }

    /** Returns a copy of the full CustomData CompoundTag. */
    public static CompoundTag getTag(org.bukkit.inventory.ItemStack item) {
        ItemStack nms = CraftItemStack.asNMSCopy(item);
        CustomData customData = nms.get(DataComponents.CUSTOM_DATA);
        return customData != null ? customData.copyTag() : new CompoundTag();
    }
}
```

## Recommended Directory Structure

```
src/main/java/com/example/
├── MyNmsPlugin.java
└── item/
    ├── ItemComponentUtil.java
    └── CustomDataHelper.java
```

## Thread Safety

- ✅ `ItemComponentUtil` / `CustomDataHelper` operate on NMS copies and **do not modify world state directly**, so they can be called from any thread
- ⚠️ Updating an ItemStack in a Bukkit inventory (e.g. `player.getInventory().setItem()`) **must happen on the main thread**
- See [`references/nms-threading.md`](references/nms-threading.md)

## Fallback

| Error | Cause | Solution |
|------|------|------|
| `get()` returns empty | Component is not set | Use `getOrDefault()` to provide a default value |
| `CustomData` data is lost | Written with the legacy `getTag()` (removed in 1.20.5+) | Use `CustomDataHelper.setString()` instead |
| `CustomModelData.value()` does not exist | Since 1.21.4 it is four lists: floats / flags / strings / colors | Access the lists via `floats()` etc. (see `getCustomModelData()`) |
| Component change has no effect | You modified an NMS copy, not the original object | Return the result via `CraftItemStack.asBukkitCopy()` and update the inventory |
| `DataComponents.SWING_ANIMATION` / `MAP_COLOR` cannot be found | Removed in 26.3 | Use `ATTACK_ANIMATION` / `INTERACT_ANIMATION` on 26.3; keep the old line behind `// @1.21.11:` |
| The same thing is possible without NMS | Paper has a data component API | Prefer `ItemStack#setData(DataComponentTypes.X, ...)`; it survives Minecraft updates better than NMS |

Files in this skill

  • SKILL.md11.5 KB
  • examples.md2.5 KB
  • references/nms-obfuscation.md5.2 KB
  • references/nms-threading.md3.8 KB
  • references/paper-nms-platform.md11.2 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.