Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Enchantments page #165

Merged
merged 43 commits into from
Oct 13, 2024
Merged
Changes from 3 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
b4d0d5b
Add a page for Enchantments
hjake123 Sep 17, 2024
350326c
Contribution standards cleanup 1
hjake123 Sep 17, 2024
98b98b7
typo
hjake123 Sep 17, 2024
ac75724
Address review until ValueEffect
hjake123 Sep 18, 2024
82d856e
Reworked Value Effect Components section
hjake123 Sep 18, 2024
f778f72
Rework Entity Effect Component and Location Based Effect Component se…
hjake123 Sep 18, 2024
dac1fbe
Add Data Generation section
hjake123 Sep 23, 2024
a68b955
Vanilla Enchantment Value Component Types section
hjake123 Sep 26, 2024
188aeed
Vanilla Enchantment Entity Effect Component Types section
hjake123 Sep 26, 2024
9acd8e1
Other Vanilla Enchantment Component Types section
hjake123 Sep 26, 2024
2b01b68
Vanilla Enchantment Entity Effects section
hjake123 Sep 26, 2024
8b99906
Reorganize the vanilla enchantment component types sections
hjake123 Sep 26, 2024
57472fa
Merge branch 'neoforged:main' into enchantments
hjake123 Sep 26, 2024
b261913
Typo fix from conversation
hjake123 Sep 30, 2024
76b3644
Remove snakecase from conversation
hjake123 Sep 30, 2024
12c3485
Various improvements
hjake123 Oct 1, 2024
974ab22
Split into two pages and further revise
hjake123 Oct 1, 2024
b851f7a
Rearrange the Location Based Effect Component section and subsequent …
hjake123 Oct 1, 2024
56f9d0c
rewording
hjake123 Oct 1, 2024
713de34
Fix some incorrect headings
hjake123 Oct 1, 2024
fa4043e
Improve usage example
hjake123 Oct 1, 2024
cc63d51
Typo
hjake123 Oct 1, 2024
fd57b55
Simple fixes
hjake123 Oct 3, 2024
2d123e4
Formatting fixes
hjake123 Oct 3, 2024
55b2f2d
Links and more!
hjake123 Oct 3, 2024
50032ef
Fix more capitalized nouns
hjake123 Oct 3, 2024
1394c83
New Enchantment Costs section
hjake123 Oct 4, 2024
c0bf5e2
Adjust wording
hjake123 Oct 4, 2024
8146b47
Add a blank line after every subheading
hjake123 Oct 4, 2024
d4b0692
Add a blank line before and after codeblocks
hjake123 Oct 4, 2024
03f7640
Format JSON blocks to 2 space indents
hjake123 Oct 4, 2024
a13c73a
Remove an unclear segment.
hjake123 Oct 6, 2024
f93862b
Apply edit suggestions
hjake123 Oct 10, 2024
a6a9e4a
builtin review 4.1-4.3 (lacking datagen)
hjake123 Oct 10, 2024
2e9c0ae
attribute component confusion
hjake123 Oct 10, 2024
bbf6a80
Clarify the purpose of EnchantmentEntityEffect#apply
hjake123 Oct 10, 2024
d032dab
Datagen for examples
hjake123 Oct 10, 2024
d307e9b
Lag file mention
hjake123 Oct 10, 2024
6cdb9cf
No more static and more comments
hjake123 Oct 10, 2024
31da446
Untruth purge and custom component section rework
hjake123 Oct 10, 2024
946607a
Terminology fix for enchantment costs / levels
hjake123 Oct 10, 2024
51a9b76
camelCase
hjake123 Oct 12, 2024
706cbc8
Clarify the usage of LootContextParamSets
hjake123 Oct 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions docs/resources/server/enchantments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Enchantments

Enchantments are enchancements that can be applied to tools and other items. As of 1.21, enchantments are stored on items as [Data Components], are defined in JSON, and are comprised of Enchantment Components.
hjake123 marked this conversation as resolved.
Show resolved Hide resolved
hjake123 marked this conversation as resolved.
Show resolved Hide resolved

A new enchantment can be added by creating a JSON file in your namespace's `enchantment` datapack subfolder. For example, to create an enchantment called `examplemod:example_enchant`, one could create the file `data/examplemod/enchantment/example_enchantment.json`. Enchantments added this way are automatically registered into the game as part of the data pack loading process.
hjake123 marked this conversation as resolved.
Show resolved Hide resolved

The JSON format for Enchantments is summarized on the [Enchantment definition Minecraft wiki page].
hjake123 marked this conversation as resolved.
Show resolved Hide resolved

## Enchantment Components
Enchantment Components are specially-registered [Data Components] that determine what an enchantment does. Enchantment Component Types must be [registered] to `BuiltInRegistries.ENCHANTMENT_EFFECT_COMPONENT_TYPE`.
:::note
There are many already existing Enchantment Component Types in vanilla that can be used to create enchantment logic.
:::
hjake123 marked this conversation as resolved.
Show resolved Hide resolved

For simple enchantments that act as a marker for custom logic, `DataComponentType<Unit>` may be sufficient. Such a component type could be registered, for example, using a DeferredRegister:
hjake123 marked this conversation as resolved.
Show resolved Hide resolved
```java
// In some registration class
public static final DeferredRegister<DataComponentType<?>> ENCHANTMENT_COMPONENT_TYPES = DeferredRegister.create(BuiltInRegistries.ENCHANTMENT_EFFECT_COMPONENT_TYPE, "examplemod");

public static final DeferredHolder<DataComponentType<?>, DataComponentType<Unit>> EXAMPLE =
ENCHANTMENT_COMPONENT_TYPES.register("example",
() -> DataComponentType.<Unit>builder()
.persistent(Unit.CODEC)
.build());
```

### Testing For Enchantment Components
To check if an enchantment component is present on a given item stack, one must first get the item's `minecraft:enchantment` component. The value of this component is an `ItemEnchantments` instance containing a set of all the Enchantments on the item, which can be acquired using `ItemEnchantments#keySet()`. The enchantment components of a given enchantment can then be acquired using the `Enchantment::effects()` method:
hjake123 marked this conversation as resolved.
Show resolved Hide resolved
```java

for(Enchantment enchant : stack.get(DataComponents.ENCHANTMENTS).keySet()){
DataComponentMap enchantment_components = enchant.value().effects();
if(enchantment_components.has(MY_ENCHANTMENT_COMPONENT_TYPE_HOLDER)){
// Do something!
}
}
```

## Specialized Enchantment Component Types
Vanilla uses a few specific classes when building its enchantment component types to achieve the customizability present in the vanilla enchantment components.

### `ConditionalEffect`
Wrapping the type in `List<ConditionalEffect<?>>` permits the use of [predicates] when defining enchantments. This allows some effects to activate situationally. Most vanilla enchantments use this.
hjake123 marked this conversation as resolved.
Show resolved Hide resolved
Registering such an effect can be done as follows:
```java
public static final DeferredHolder<DataComponentType<?>, DataComponentType<List<ConditionalEffect<Unit>>>> EXAMPLE_CONDITIONAL_EFFECT =
ENCHANTMENT_COMPONENT_TYPES.register("example_conditional",
() -> DataComponentType.<List<ConditionalEffect<Unit>>>builder()
hjake123 marked this conversation as resolved.
Show resolved Hide resolved
.persistent(ConditionalEffect.codec(Unit.CODEC, LootContextParamSets.EMPTY).listOf())
.build());
```
The parameters to `ConditionalEffect.codec` are the codec for the template type of the `ConditionalEffect<T>`, followed by some `LootContextParamSets` item. There are many of these to choose from, so choose carefully.

### Value Effects
[Value Effects] are used for enchantments that change some numerical value to a different degree depending on the enchantment's level, and are implemented by the class `EnchantmentValueEffect`. Enchantments like Knockback, Looting, an Sharpness use this kind of component.

A vanilla-conforming way to adjust values based on custom Value Effect components is to invoke one of the overloads of `EnchantmentHelper#runIterationOnItem` and pass in a lambda that alters a `MutableFloat` or similar mutable object based on the results:
```java
MutableFloat mutable_value = new MutableFloat(unmodified_value);
hjake123 marked this conversation as resolved.
Show resolved Hide resolved
EnchantmentHelper.runIterationOnItem(item_stack, (enchantment, enchant_level) -> Enchantment.applyEffects(
enchantment.value().getEffects(MY_ENCHANTMENT_COMPONENT_TYPE_HOLDER.value()),
// Provide a loot context,
(value_effect) -> mutable_value.setValue(value_effect.process(enchant_level, server.random, mutable_value.getValue()))
));
```

Custom numerical operations for use in Value Enchantment blocks can be added by registering a subclass of `EnchantmentValueEffect` through `BuiltInRegistries.ENCHANTMENT_VALUE_EFFECT_TYPE`.

### Entity Effects
[Entity Effects] are used for enchantments that cause special effects to either the wielder or (if applicable) the target of an attack when the enchanted item is used to attack another entity. Enchantments like Flame, Fire Aspect, and Channeling use this kind of component.

Custom `EnchantmentEntityEffect` extensions can be registered through `BuiltInRegistries.ENCHANTMENT_ENTITY_EFFECT_TYPE`.
hjake123 marked this conversation as resolved.
Show resolved Hide resolved

### Location Based Effects
[Location Based Effects] are used for effects that need to reference specific places in the world relative to the player. Frost Walker is a prime example of a Location Based Effect, and Entity Effects are a subclass of Location Based Effects.
hjake123 marked this conversation as resolved.
Show resolved Hide resolved

Custom `EnchantmentLocationBasedEffect` extensions can be registered through `BuiltInRegistries.ENCHANTMENT_LOCATION_BASED_EFFECT_TYPE`. Overriding `EnchantmentEntityEffect#onChangedBlock` allows for the subclass to do something whenever the wielder's BlockPos changes.

[Data Components]: /docs/items/datacomponents
[Enchantment definition Minecraft wiki page]: https://minecraft.wiki/w/Enchantment_definition
[registered]: /docs/concepts/registries
[predicates]: https://minecraft.wiki/w/Predicate
[Value Effects]: https://minecraft.wiki/w/Enchantment_definition#Value_effects
[Entity Effects]: https://minecraft.wiki/w/Enchantment_definition#Entity_effects
[Location Based Effects]: https://minecraft.wiki/w/Enchantment_definition#Location-based_effects