| 1 | package net.minecraft.world.item.enchantment.effects;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.List;
|
| 7 | import java.util.function.Function;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.world.entity.Entity;
|
| 11 | import net.minecraft.world.item.enchantment.EnchantedItemInUse;
|
| 12 | import net.minecraft.world.phys.Vec3;
|
| 13 |
|
| 14 | public interface AllOf {
|
| 15 | static <T, A extends T> MapCodec<A> codec(final Codec<T> topLevelCodec, final Function<List<T>, A> constructor, final Function<A, List<T>> accessor) {
|
| 16 | return RecordCodecBuilder.mapCodec(i -> i.group(topLevelCodec.listOf().fieldOf("effects").forGetter(accessor)).apply(i, constructor));
|
| 17 | }
|
| 18 |
|
| 19 | static AllOf.EntityEffects entityEffects(final EnchantmentEntityEffect... effects) {
|
| 20 | return new AllOf.EntityEffects(List.of(effects));
|
| 21 | }
|
| 22 |
|
| 23 | static AllOf.LocationBasedEffects locationBasedEffects(final EnchantmentLocationBasedEffect... effects) {
|
| 24 | return new AllOf.LocationBasedEffects(List.of(effects));
|
| 25 | }
|
| 26 |
|
| 27 | static AllOf.ValueEffects valueEffects(final EnchantmentValueEffect... effects) {
|
| 28 | return new AllOf.ValueEffects(List.of(effects));
|
| 29 | }
|
| 30 |
|
| 31 | record EntityEffects(List<EnchantmentEntityEffect> effects) implements EnchantmentEntityEffect {
|
| 32 | public static final MapCodec<AllOf.EntityEffects> CODEC = AllOf.codec(EnchantmentEntityEffect.CODEC, AllOf.EntityEffects::new, AllOf.EntityEffects::effects);
|
| 33 |
|
| 34 | @Override
|
| 35 | public void apply(final ServerLevel serverLevel, final int enchantmentLevel, final EnchantedItemInUse item, final Entity entity, final Vec3 position) {
|
| 36 | for (EnchantmentEntityEffect effect : this.effects) {
|
| 37 | effect.apply(serverLevel, enchantmentLevel, item, entity, position);
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public MapCodec<AllOf.EntityEffects> codec() {
|
| 43 | return CODEC;
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | record LocationBasedEffects(List<EnchantmentLocationBasedEffect> effects) implements EnchantmentLocationBasedEffect {
|
| 48 | public static final MapCodec<AllOf.LocationBasedEffects> CODEC = AllOf.codec(
|
| 49 | EnchantmentLocationBasedEffect.CODEC, AllOf.LocationBasedEffects::new, AllOf.LocationBasedEffects::effects
|
| 50 | );
|
| 51 |
|
| 52 | @Override
|
| 53 | public void onChangedBlock(
|
| 54 | final ServerLevel serverLevel,
|
| 55 | final int enchantmentLevel,
|
| 56 | final EnchantedItemInUse item,
|
| 57 | final Entity entity,
|
| 58 | final Vec3 position,
|
| 59 | final boolean becameActive
|
| 60 | ) {
|
| 61 | for (EnchantmentLocationBasedEffect effect : this.effects) {
|
| 62 | effect.onChangedBlock(serverLevel, enchantmentLevel, item, entity, position, becameActive);
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public void onDeactivated(final EnchantedItemInUse item, final Entity entity, final Vec3 position, final int level) {
|
| 68 | for (EnchantmentLocationBasedEffect effect : this.effects) {
|
| 69 | effect.onDeactivated(item, entity, position, level);
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public MapCodec<AllOf.LocationBasedEffects> codec() {
|
| 75 | return CODEC;
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | record ValueEffects(List<EnchantmentValueEffect> effects) implements EnchantmentValueEffect {
|
| 80 | public static final MapCodec<AllOf.ValueEffects> CODEC = AllOf.codec(EnchantmentValueEffect.CODEC, AllOf.ValueEffects::new, AllOf.ValueEffects::effects);
|
| 81 |
|
| 82 | @Override
|
| 83 | public float process(final int enchantmentLevel, final RandomSource random, float value) {
|
| 84 | for (EnchantmentValueEffect effect : this.effects) {
|
| 85 | value = effect.process(enchantmentLevel, random, value);
|
| 86 | }
|
| 87 |
|
| 88 | return value;
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public MapCodec<AllOf.ValueEffects> codec() {
|
| 93 | return CODEC;
|
| 94 | }
|
| 95 | }
|
| 96 | }
|
| 97 |
|