Skip to content

Commit

Permalink
Update data components registrar method (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChampionAsh5357 authored Sep 17, 2024
1 parent e8f8388 commit aa94a36
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/datastorage/attachments.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,5 @@ NeoForge.EVENT_BUS.register(PlayerEvent.Clone.class, event -> {
```

[saveddata]: ./saveddata.md
[datacomponents]: ../items/datacomponents.md
[datacomponents]: ../items/datacomponents.mdx
[network]: ../networking/index.md
2 changes: 1 addition & 1 deletion docs/datastorage/nbt.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ NBT is used in a lot of places in Minecraft. Some of the most common examples in

[blockentity]: ../blockentities/index.md
[datapack]: ../resources/index.md#data
[datacomponents]: ../items/datacomponents.md
[datacomponents]: ../items/datacomponents.mdx
[nbtwiki]: https://minecraft.wiki/w/NBT_format
62 changes: 62 additions & 0 deletions docs/items/datacomponents.md → docs/items/datacomponents.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
---
sidebar_position: 2
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Data Components

Data components are key-value pairs within a map used to store data on an `ItemStack`. Each piece of data, such as firework explosions or tools, are stored as actual objects on the stack, making the values visible and operable without having to dynamically transform a general encoded instance (e.g., `CompoundTag`, `JsonElement`).
Expand Down Expand Up @@ -65,6 +68,63 @@ Either `persistent` or `networkSynchronized` must be provided in the builder; ot

`DataComponentType` are registry objects and must be [registered].

<Tabs defaultValue="latest">
<TabItem value="latest" label="Latest">

```java
// Using ExampleRecord(int, boolean)
// Only one Codec and/or StreamCodec should be used below
// Multiple are provided for an example

// Basic codec
public static final Codec<ExampleRecord> BASIC_CODEC = RecordCodecBuilder.create(instance ->
instance.group(
Codec.INT.fieldOf("value1").forGetter(ExampleRecord::value1),
Codec.BOOL.fieldOf("value2").forGetter(ExampleRecord::value2)
).apply(instance, ExampleRecord::new)
);
public static final StreamCodec<ByteBuf, ExampleRecord> BASIC_STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.INT, ExampleRecord::value1,
ByteBufCodecs.BOOL, ExampleRecord::value2,
ExampleRecord::new
);

// Unit stream codec if nothing should be sent across the network
public static final StreamCodec<ByteBuf, ExampleRecord> UNIT_STREAM_CODEC = StreamCodec.unit(new ExampleRecord(0, false));


// In another class
// The specialized DeferredRegister.DataComponents simplifies data component registration and avoids some generic inference issues with the `DataComponentType.Builder` within a `Supplier`
public static final DeferredRegister.DataComponents REGISTRAR = DeferredRegister.createDataComponents(Registries.DATA_COMPONENT_TYPE, "examplemod");

public static final DeferredHolder<DataComponentType<?>, DataComponentType<ExampleRecord>> BASIC_EXAMPLE = REGISTRAR.registerComponentType(
"basic",
builder -> builder
// The codec to read/write the data to disk
.persistent(BASIC_CODEC)
// The codec to read/write the data across the network
.networkSynchronized(BASIC_STREAM_CODEC)
);

/// Component will not be saved to disk
public static final DeferredHolder<DataComponentType<?>, DataComponentType<ExampleRecord>> TRANSIENT_EXAMPLE = REGISTRAR.registerComponentType(
"transient",
builder -> builder.networkSynchronized(BASIC_STREAM_CODEC)
);

// No data will be synced across the network
public static final DeferredHolder<DataComponentType<?>, DataComponentType<ExampleRecord>> NO_NETWORK_EXAMPLE = REGISTRAR.registerComponentType(
"no_network",
builder -> builder
.persistent(BASIC_CODEC)
// Note we use a unit stream codec here
.networkSynchronized(UNIT_STREAM_CODEC)
);
```

</TabItem>
<TabItem value="21.1.48" label="[21.0.0, 21.1.48]">

```java
// Using ExampleRecord(int, boolean)
// Only one Codec and/or StreamCodec should be used below
Expand Down Expand Up @@ -115,6 +175,8 @@ public static final DeferredHolder<DataComponentType<?>, DataComponentType<Examp
.networkSynchronized(UNIT_STREAM_CODEC)
);
```
</TabItem>
</Tabs>

## The Component Map

Expand Down
2 changes: 1 addition & 1 deletion docs/items/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ It is also possible to implement `ItemLike` on your custom objects. Simply overr
[blockstates]: ../blocks/states.md
[breaking]: ../blocks/index.md#breaking-a-block
[creativetabs]: #creative-tabs
[datacomponents]: ./datacomponents.md
[datacomponents]: ./datacomponents.mdx
[datagen]: ../resources/index.md#data-generation
[food]: #food
[hunger]: https://minecraft.wiki/w/Hunger#Mechanics
Expand Down
2 changes: 1 addition & 1 deletion docs/items/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public static final Supplier<ArmorItem> COPPER_BOOTS = ITEMS.register("copper_bo
When creating your armor texture, it is a good idea to work on top of the vanilla armor texture to see which part goes where.

[block]: ../blocks/index.md
[datacomponents]: ./datacomponents.md
[datacomponents]: ./datacomponents.mdx
[item]: index.md
[itemability]: #itemabilitys
[tags]: ../resources/server/tags.md
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/server/datamaps/builtin.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,5 @@ Example:
}
```

[datacomponent]: ../../../items/datacomponents.md
[datacomponent]: ../../../items/datacomponents.mdx
[datamap]: index.md
2 changes: 1 addition & 1 deletion docs/resources/server/loottables/lootfunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ During datagen, call `SequenceFunction#of` with the other functions to construct
[component]: ../../client/i18n.md#components
[conditions]: lootconditions
[custom]: custom.md#custom-loot-functions
[datacomponent]: ../../../items/datacomponents.md
[datacomponent]: ../../../items/datacomponents.mdx
[entitytarget]: index.md#entity-targets
[entry]: index.md#loot-entry
[itemmodifiers]: https://minecraft.wiki/w/Item_modifier#JSON_format
Expand Down

0 comments on commit aa94a36

Please sign in to comment.