You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: A guide to the ItemStack DataComponentAPI
3
+
description: A guide to the ItemStack data component API.
4
4
---
5
5
6
6
# Data Component API
7
7
8
8
:::danger[Experimental]
9
-
The DataComponent API is currently experimental, and is additionally subject to change across versions.
9
+
10
+
The data component API is currently experimental, and is additionally subject to change across versions.
11
+
10
12
:::
11
13
12
-
The Data Component API provides a version-specific, programmatic interface for accessing and manipulating item data that is otherwise not representable by the `ItemMeta` API. Through this API, you can read and write properties of an item—called "data components"—in a stable and object-oriented manner.
14
+
The data component API provides a version-specific interface for accessing and manipulating item data that is otherwise not representable by the `ItemMeta` API.
15
+
Through this API, you can read and modify properties of an item, so called data components, in a stable and object-oriented manner.
16
+
13
17
14
18
## Introduction
15
19
16
-
### What is a Data Component?
20
+
### What is a data component?
17
21
A data component represents a piece of data associated with an item. Vanilla items can have properties such as custom model data, container loot contents, banner patterns, or potion effects.
For implementation details, [click here](#example-cool-sword).
21
26
22
-
#### The Prototype (Default Values)
27
+
#### The prototype (default values)
23
28
Items come with an initial set of components that we call the prototype.
24
29
These components are defined on the `ItemType` of the `ItemStack`. They control the base behavior
25
30
of the item, representing a brand new item without any modifications.
26
31
27
32
The prototype gives items their initial properties such as if they are food, a tool, a weapon, etc.
28
33
29
-
#### The Patch
34
+
#### The patch
30
35
The patch represents the modifications made to the item. This may include giving it a custom name,
31
36
modifying the enchantments, damaging it, or adding to the lore. The patch is applied ontop of the prototype,
32
37
allowing us to make modifications to an item.
33
38
34
-
The patch also allows for removing components that were previously in the prototype, this is shown by
39
+
The patch also allows for removing components that were previously in the prototype. This is shown by
35
40
the `minecraft:tool` example in red. We are removing this component, so this sword item will no longer
36
-
break cobweb / other sword blocks faster.
41
+
break cobweb or other sword blocks faster.
37
42
38
43
We can also add new components, as seen from the new `minecraft:enchantment_glint_override` component, which
39
-
allows us to make it appear with a glint.
44
+
allows us to make it appear as if it were enchanted.
40
45
41
46
42
-
## Differences Compared to `ItemMeta`
47
+
## Differences compared to `ItemMeta`
43
48
44
-
The `ItemMeta` API provides methods to modify `ItemStack`s in a hierarchical manner, such `CompassMeta` allowing you to modify the components of a `minecraft:compass`.
45
-
While `ItemMeta` is still very useful, it does not properly represent this new prototype/patch relationship that Minecraft items now use.
49
+
The `ItemMeta` API provides methods to modify `ItemStack`s in a hierarchical manner, such as `CompassMeta`, which allows you to modify the components of a `minecraft:compass`.
50
+
While `ItemMeta` is still very useful, it does not properly represent the prototype/patch relationship that Minecraft items use.
46
51
47
-
### Key Differences
52
+
### Key differences
48
53
49
-
#### Expanded Data Model
50
-
The Data Component API exposes a much broader and more detailed set of item properties than `ItemMeta`.
51
-
DataComponents allow the entire component to be modified in a fashion that better represents how Minecraft does item modifications.
54
+
#### Expanded data model
55
+
The data component API exposes a much broader and more detailed set of item properties than `ItemMeta`.
56
+
Data components allow the entire item to be modified in a fashion that better represents how Minecraft does item modifications.
52
57
53
-
#### Version-Specific
54
-
The Data Component API is designed to adapt to version changes. The Data Component API may experience breaking changes on version updates as Minecraft makes changes to components.
58
+
#### Version-specific
59
+
The data component API is designed to adapt to version changes. The data component API may experience breaking changes on version updates as Minecraft makes changes to components.
55
60
Backwards compatibility is not promised.
56
61
57
-
Because ItemMeta is represented in a different format, breaking changes made to components by Mojang may not result in breaking changes to `ItemMeta`.
62
+
Because `ItemMeta` is represented in a different format, breaking changes made to components by Mojang may not result in breaking changes to `ItemMeta`.
58
63
59
-
#### Builders and Immutability
64
+
#### Builders and immutability
60
65
Many complex data components require a builder approach for construction and editing. All data types that are returned by the api are also immutable, so they will not directly modify the component.
61
66
62
-
#### Patch-Only
63
-
ItemMetarepresents the patch of an ItemStack only. This means that you cannot get the original properties (prototype) of the ItemStack, such as its default
67
+
#### Patch-only
68
+
`ItemMeta` only represents the patch of an `ItemStack`. This means that you cannot get the original properties (prototype) of the `ItemStack`, such as its default
64
69
durability or default attributes.
65
70
66
-
#### No Snapshot
67
-
Currently, ItemMeta represents a *Snapshot* of an ItemStack's patched map.
71
+
#### No snapshots
72
+
Currently, `ItemMeta` represents a **snapshot** of an `ItemStack`'s patched map.
68
73
This is expensive as it requires the entire patch to be read, even values that you may not be using.
69
74
70
-
The DataComponent API integrates directly with `ItemStack`. Although conceptually similar, the Data Component API focuses on explicit, strongly typed data retrieval and updates without this additional overhead.
75
+
The data component API integrates directly with `ItemStack`. Although conceptually similar, the data component API focuses on explicit, strongly typed data retrieval and updates without this additional overhead.
71
76
72
77
### When should I use `DataComponents` or `ItemMeta`?
73
-
#### `ItemMeta`
74
-
- Simple changes to `ItemStacks`
75
-
- Keep the most version compatibility with your plugin
76
-
#### `DataComponent`
77
-
- More complicated `ItemStack` modifications
78
+
79
+
You would want to use `ItemMeta` if you:
80
+
- Are doing only simple changes to `ItemStack`s
81
+
- Want to keep cross-version compatibility with your plugin
82
+
83
+
You would want to use data components if you:
84
+
- Want more complicated `ItemStack` modifications
78
85
- Do not care about cross-version compatibility
79
86
- Want to access default (prototype) values
80
-
- Want to remove components from an ItemStack's prototype
87
+
- Want to remove components from an `ItemStack`'s prototype
88
+
81
89
82
-
## Basic Usage
83
-
The DataComponent API will fetch values according to the behavior seen in game. So, if the patch removes the `minecraft:tool` component,
90
+
## Basic usage
91
+
The data component API will fetch values according to the behavior seen in game. So, if the patch removes the `minecraft:tool` component,
84
92
trying to get that component will return null.
85
93
86
-
### Getting a Default (Prototype) value
94
+
### Retrieving a prototype value
87
95
88
96
```java
89
97
// Get the default durability of diamond sword
90
98
int defaultDurability =Material.DIAMOND_SWORD.getDefaultData(DataComponentTypes.MAX_DAMAGE)
91
99
```
92
100
93
-
### Checking for a Data Component
101
+
### Checking for a data component
94
102
95
103
```java
96
104
// Check if this item has a custom name data component
0 commit comments