| 1 | package net.minecraft.world.item.enchantment;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import java.util.List;
|
| 5 | import java.util.function.UnaryOperator;
|
| 6 | import net.minecraft.core.Holder;
|
| 7 | import net.minecraft.core.Registry;
|
| 8 | import net.minecraft.core.component.DataComponentMap;
|
| 9 | import net.minecraft.core.component.DataComponentType;
|
| 10 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 11 | import net.minecraft.sounds.SoundEvent;
|
| 12 | import net.minecraft.util.Unit;
|
| 13 | import net.minecraft.util.context.ContextKeySet;
|
| 14 | import net.minecraft.world.item.CrossbowItem;
|
| 15 | import net.minecraft.world.item.enchantment.effects.DamageImmunity;
|
| 16 | import net.minecraft.world.item.enchantment.effects.EnchantmentAttributeEffect;
|
| 17 | import net.minecraft.world.item.enchantment.effects.EnchantmentEntityEffect;
|
| 18 | import net.minecraft.world.item.enchantment.effects.EnchantmentLocationBasedEffect;
|
| 19 | import net.minecraft.world.item.enchantment.effects.EnchantmentValueEffect;
|
| 20 | import net.minecraft.world.level.storage.loot.Validatable;
|
| 21 | import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
| 22 |
|
| 23 | public interface EnchantmentEffectComponents {
|
| 24 | Codec<DataComponentType<?>> COMPONENT_CODEC = Codec.lazyInitialized(() -> BuiltInRegistries.ENCHANTMENT_EFFECT_COMPONENT_TYPE.byNameCodec());
|
| 25 | Codec<DataComponentMap> CODEC = DataComponentMap.makeCodec(COMPONENT_CODEC);
|
| 26 | DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> DAMAGE_PROTECTION = register(
|
| 27 | "damage_protection", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentValueEffect.CODEC), LootContextParamSets.ENCHANTED_DAMAGE))
|
| 28 | );
|
| 29 | DataComponentType<List<ConditionalEffect<DamageImmunity>>> DAMAGE_IMMUNITY = register(
|
| 30 | "damage_immunity", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(DamageImmunity.CODEC), LootContextParamSets.ENCHANTED_DAMAGE))
|
| 31 | );
|
| 32 | DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> DAMAGE = register(
|
| 33 | "damage", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentValueEffect.CODEC), LootContextParamSets.ENCHANTED_DAMAGE))
|
| 34 | );
|
| 35 | DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> SMASH_DAMAGE_PER_FALLEN_BLOCK = register(
|
| 36 | "smash_damage_per_fallen_block",
|
| 37 | b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentValueEffect.CODEC), LootContextParamSets.ENCHANTED_DAMAGE))
|
| 38 | );
|
| 39 | DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> KNOCKBACK = register(
|
| 40 | "knockback", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentValueEffect.CODEC), LootContextParamSets.ENCHANTED_DAMAGE))
|
| 41 | );
|
| 42 | DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> ARMOR_EFFECTIVENESS = register(
|
| 43 | "armor_effectiveness", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentValueEffect.CODEC), LootContextParamSets.ENCHANTED_DAMAGE))
|
| 44 | );
|
| 45 | DataComponentType<List<TargetedConditionalEffect<EnchantmentEntityEffect>>> POST_ATTACK = register(
|
| 46 | "post_attack", b -> b.persistent(validatedListCodec(TargetedConditionalEffect.codec(EnchantmentEntityEffect.CODEC), LootContextParamSets.ENCHANTED_DAMAGE))
|
| 47 | );
|
| 48 | DataComponentType<List<ConditionalEffect<EnchantmentEntityEffect>>> POST_PIERCING_ATTACK = register(
|
| 49 | "post_piercing_attack", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentEntityEffect.CODEC), LootContextParamSets.ENCHANTED_DAMAGE))
|
| 50 | );
|
| 51 | DataComponentType<List<ConditionalEffect<EnchantmentEntityEffect>>> HIT_BLOCK = register(
|
| 52 | "hit_block", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentEntityEffect.CODEC), LootContextParamSets.HIT_BLOCK))
|
| 53 | );
|
| 54 | DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> ITEM_DAMAGE = register(
|
| 55 | "item_damage", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentValueEffect.CODEC), LootContextParamSets.ENCHANTED_ITEM))
|
| 56 | );
|
| 57 | DataComponentType<List<TargetedConditionalEffect<EnchantmentValueEffect>>> EQUIPMENT_DROPS = register(
|
| 58 | "equipment_drops",
|
| 59 | b -> b.persistent(validatedListCodec(TargetedConditionalEffect.equipmentDropsCodec(EnchantmentValueEffect.CODEC), LootContextParamSets.ENCHANTED_DAMAGE))
|
| 60 | );
|
| 61 | DataComponentType<List<ConditionalEffect<EnchantmentLocationBasedEffect>>> LOCATION_CHANGED = register(
|
| 62 | "location_changed",
|
| 63 | b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentLocationBasedEffect.CODEC), LootContextParamSets.ENCHANTED_LOCATION))
|
| 64 | );
|
| 65 | DataComponentType<List<ConditionalEffect<EnchantmentEntityEffect>>> TICK = register(
|
| 66 | "tick", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentEntityEffect.CODEC), LootContextParamSets.ENCHANTED_ENTITY))
|
| 67 | );
|
| 68 | DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> AMMO_USE = register(
|
| 69 | "ammo_use", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentValueEffect.CODEC), LootContextParamSets.ENCHANTED_ITEM))
|
| 70 | );
|
| 71 | DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> PROJECTILE_PIERCING = register(
|
| 72 | "projectile_piercing", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentValueEffect.CODEC), LootContextParamSets.ENCHANTED_ITEM))
|
| 73 | );
|
| 74 | DataComponentType<List<ConditionalEffect<EnchantmentEntityEffect>>> PROJECTILE_SPAWNED = register(
|
| 75 | "projectile_spawned", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentEntityEffect.CODEC), LootContextParamSets.ENCHANTED_ENTITY))
|
| 76 | );
|
| 77 | DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> PROJECTILE_SPREAD = register(
|
| 78 | "projectile_spread", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentValueEffect.CODEC), LootContextParamSets.ENCHANTED_ENTITY))
|
| 79 | );
|
| 80 | DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> PROJECTILE_COUNT = register(
|
| 81 | "projectile_count", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentValueEffect.CODEC), LootContextParamSets.ENCHANTED_ENTITY))
|
| 82 | );
|
| 83 | DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> TRIDENT_RETURN_ACCELERATION = register(
|
| 84 | "trident_return_acceleration",
|
| 85 | b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentValueEffect.CODEC), LootContextParamSets.ENCHANTED_ENTITY))
|
| 86 | );
|
| 87 | DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> FISHING_TIME_REDUCTION = register(
|
| 88 | "fishing_time_reduction", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentValueEffect.CODEC), LootContextParamSets.ENCHANTED_ENTITY))
|
| 89 | );
|
| 90 | DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> FISHING_LUCK_BONUS = register(
|
| 91 | "fishing_luck_bonus", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentValueEffect.CODEC), LootContextParamSets.ENCHANTED_ENTITY))
|
| 92 | );
|
| 93 | DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> BLOCK_EXPERIENCE = register(
|
| 94 | "block_experience", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentValueEffect.CODEC), LootContextParamSets.ENCHANTED_ITEM))
|
| 95 | );
|
| 96 | DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> MOB_EXPERIENCE = register(
|
| 97 | "mob_experience", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentValueEffect.CODEC), LootContextParamSets.ENCHANTED_ENTITY))
|
| 98 | );
|
| 99 | DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> REPAIR_WITH_XP = register(
|
| 100 | "repair_with_xp", b -> b.persistent(validatedListCodec(ConditionalEffect.codec(EnchantmentValueEffect.CODEC), LootContextParamSets.ENCHANTED_ITEM))
|
| 101 | );
|
| 102 | DataComponentType<List<EnchantmentAttributeEffect>> ATTRIBUTES = register(
|
| 103 | "attributes", b -> b.persistent(EnchantmentAttributeEffect.MAP_CODEC.codec().listOf())
|
| 104 | );
|
| 105 | DataComponentType<EnchantmentValueEffect> CROSSBOW_CHARGE_TIME = register("crossbow_charge_time", b -> b.persistent(EnchantmentValueEffect.CODEC));
|
| 106 | DataComponentType<List<CrossbowItem.ChargingSounds>> CROSSBOW_CHARGING_SOUNDS = register(
|
| 107 | "crossbow_charging_sounds", b -> b.persistent(CrossbowItem.ChargingSounds.CODEC.listOf())
|
| 108 | );
|
| 109 | DataComponentType<List<Holder<SoundEvent>>> TRIDENT_SOUND = register("trident_sound", b -> b.persistent(SoundEvent.CODEC.listOf()));
|
| 110 | DataComponentType<Unit> PREVENT_EQUIPMENT_DROP = register("prevent_equipment_drop", b -> b.persistent(Unit.CODEC));
|
| 111 | DataComponentType<Unit> PREVENT_ARMOR_CHANGE = register("prevent_armor_change", b -> b.persistent(Unit.CODEC));
|
| 112 | DataComponentType<EnchantmentValueEffect> TRIDENT_SPIN_ATTACK_STRENGTH = register(
|
| 113 | "trident_spin_attack_strength", b -> b.persistent(EnchantmentValueEffect.CODEC)
|
| 114 | );
|
| 115 |
|
| 116 | private static <T extends Validatable> Codec<List<T>> validatedListCodec(final Codec<T> elementCodec, final ContextKeySet paramSet) {
|
| 117 | return elementCodec.listOf().validate(Validatable.listValidatorForContext(paramSet));
|
| 118 | }
|
| 119 |
|
| 120 | static DataComponentType<?> bootstrap(final Registry<DataComponentType<?>> registry) {
|
| 121 | return DAMAGE_PROTECTION;
|
| 122 | }
|
| 123 |
|
| 124 | private static <T> DataComponentType<T> register(final String id, final UnaryOperator<DataComponentType.Builder<T>> builder) {
|
| 125 | return Registry.register(BuiltInRegistries.ENCHANTMENT_EFFECT_COMPONENT_TYPE, id, builder.apply(DataComponentType.builder()).build());
|
| 126 | }
|
| 127 | }
|
| 128 |
|