| 1 | package net.minecraft.world.item.enchantment;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.DataResult;
|
| 6 | import com.mojang.serialization.MapCodec;
|
| 7 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 8 | import java.util.List;
|
| 9 | import net.minecraft.core.Registry;
|
| 10 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 |
|
| 13 | public interface LevelBasedValue {
|
| 14 | Codec<LevelBasedValue> DISPATCH_CODEC = BuiltInRegistries.ENCHANTMENT_LEVEL_BASED_VALUE_TYPE.byNameCodec().dispatch(LevelBasedValue::codec, c -> c);
|
| 15 | Codec<LevelBasedValue> CODEC = Codec.either(LevelBasedValue.Constant.CODEC, DISPATCH_CODEC)
|
| 16 | .xmap(
|
| 17 | either -> either.map(l -> (LevelBasedValue)l, r -> (LevelBasedValue)r),
|
| 18 | levelBasedValue -> levelBasedValue instanceof LevelBasedValue.Constant constant ? Either.left(constant) : Either.right(levelBasedValue)
|
| 19 | );
|
| 20 |
|
| 21 | static MapCodec<? extends LevelBasedValue> bootstrap(final Registry<MapCodec<? extends LevelBasedValue>> registry) {
|
| 22 | Registry.register(registry, "clamped", LevelBasedValue.Clamped.CODEC);
|
| 23 | Registry.register(registry, "fraction", LevelBasedValue.Fraction.CODEC);
|
| 24 | Registry.register(registry, "levels_squared", LevelBasedValue.LevelsSquared.CODEC);
|
| 25 | Registry.register(registry, "linear", LevelBasedValue.Linear.CODEC);
|
| 26 | Registry.register(registry, "exponent", LevelBasedValue.Exponent.CODEC);
|
| 27 | return Registry.register(registry, "lookup", LevelBasedValue.Lookup.CODEC);
|
| 28 | }
|
| 29 |
|
| 30 | static LevelBasedValue.Constant constant(final float value) {
|
| 31 | return new LevelBasedValue.Constant(value);
|
| 32 | }
|
| 33 |
|
| 34 | static LevelBasedValue.Linear perLevel(final float base, final float perLevelAboveFirst) {
|
| 35 | return new LevelBasedValue.Linear(base, perLevelAboveFirst);
|
| 36 | }
|
| 37 |
|
| 38 | static LevelBasedValue.Linear perLevel(final float perLevel) {
|
| 39 | return perLevel(perLevel, perLevel);
|
| 40 | }
|
| 41 |
|
| 42 | static LevelBasedValue.Lookup lookup(final List<Float> values, final LevelBasedValue fallback) {
|
| 43 | return new LevelBasedValue.Lookup(values, fallback);
|
| 44 | }
|
| 45 |
|
| 46 | float calculate(int level);
|
| 47 |
|
| 48 | MapCodec<? extends LevelBasedValue> codec();
|
| 49 |
|
| 50 | record Clamped(LevelBasedValue value, float min, float max) implements LevelBasedValue {
|
| 51 | public static final MapCodec<LevelBasedValue.Clamped> CODEC = RecordCodecBuilder.<LevelBasedValue.Clamped>mapCodec(
|
| 52 | i -> i.group(
|
| 53 | LevelBasedValue.CODEC.fieldOf("value").forGetter(LevelBasedValue.Clamped::value),
|
| 54 | Codec.FLOAT.fieldOf("min").forGetter(LevelBasedValue.Clamped::min),
|
| 55 | Codec.FLOAT.fieldOf("max").forGetter(LevelBasedValue.Clamped::max)
|
| 56 | )
|
| 57 | .apply(i, LevelBasedValue.Clamped::new)
|
| 58 | )
|
| 59 | .validate(u -> u.max <= u.min ? DataResult.error(() -> "Max must be larger than min, min: " + u.min + ", max: " + u.max) : DataResult.success(u));
|
| 60 |
|
| 61 | @Override
|
| 62 | public float calculate(final int level) {
|
| 63 | return Mth.clamp(this.value.calculate(level), this.min, this.max);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public MapCodec<LevelBasedValue.Clamped> codec() {
|
| 68 | return CODEC;
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | record Constant(float value) implements LevelBasedValue {
|
| 73 | public static final Codec<LevelBasedValue.Constant> CODEC = Codec.FLOAT.xmap(LevelBasedValue.Constant::new, LevelBasedValue.Constant::value);
|
| 74 | public static final MapCodec<LevelBasedValue.Constant> TYPED_CODEC = RecordCodecBuilder.mapCodec(
|
| 75 | i -> i.group(Codec.FLOAT.fieldOf("value").forGetter(LevelBasedValue.Constant::value)).apply(i, LevelBasedValue.Constant::new)
|
| 76 | );
|
| 77 |
|
| 78 | @Override
|
| 79 | public float calculate(final int level) {
|
| 80 | return this.value;
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public MapCodec<LevelBasedValue.Constant> codec() {
|
| 85 | return TYPED_CODEC;
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | record Exponent(LevelBasedValue base, LevelBasedValue power) implements LevelBasedValue {
|
| 90 | public static final MapCodec<LevelBasedValue.Exponent> CODEC = RecordCodecBuilder.mapCodec(
|
| 91 | i -> i.group(
|
| 92 | LevelBasedValue.CODEC.fieldOf("base").forGetter(LevelBasedValue.Exponent::base),
|
| 93 | LevelBasedValue.CODEC.fieldOf("power").forGetter(LevelBasedValue.Exponent::power)
|
| 94 | )
|
| 95 | .apply(i, LevelBasedValue.Exponent::new)
|
| 96 | );
|
| 97 |
|
| 98 | @Override
|
| 99 | public float calculate(final int level) {
|
| 100 | return (float)Math.pow(this.base.calculate(level), this.power.calculate(level));
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public MapCodec<LevelBasedValue.Exponent> codec() {
|
| 105 | return CODEC;
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | record Fraction(LevelBasedValue numerator, LevelBasedValue denominator) implements LevelBasedValue {
|
| 110 | public static final MapCodec<LevelBasedValue.Fraction> CODEC = RecordCodecBuilder.mapCodec(
|
| 111 | i -> i.group(
|
| 112 | LevelBasedValue.CODEC.fieldOf("numerator").forGetter(LevelBasedValue.Fraction::numerator),
|
| 113 | LevelBasedValue.CODEC.fieldOf("denominator").forGetter(LevelBasedValue.Fraction::denominator)
|
| 114 | )
|
| 115 | .apply(i, LevelBasedValue.Fraction::new)
|
| 116 | );
|
| 117 |
|
| 118 | @Override
|
| 119 | public float calculate(final int level) {
|
| 120 | float denominator = this.denominator.calculate(level);
|
| 121 | return denominator == 0.0F ? 0.0F : this.numerator.calculate(level) / denominator;
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public MapCodec<LevelBasedValue.Fraction> codec() {
|
| 126 | return CODEC;
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | record LevelsSquared(float added) implements LevelBasedValue {
|
| 131 | public static final MapCodec<LevelBasedValue.LevelsSquared> CODEC = RecordCodecBuilder.mapCodec(
|
| 132 | i -> i.group(Codec.FLOAT.fieldOf("added").forGetter(LevelBasedValue.LevelsSquared::added)).apply(i, LevelBasedValue.LevelsSquared::new)
|
| 133 | );
|
| 134 |
|
| 135 | @Override
|
| 136 | public float calculate(final int level) {
|
| 137 | return Mth.square(level) + this.added;
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | public MapCodec<LevelBasedValue.LevelsSquared> codec() {
|
| 142 | return CODEC;
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | record Linear(float base, float perLevelAboveFirst) implements LevelBasedValue {
|
| 147 | public static final MapCodec<LevelBasedValue.Linear> CODEC = RecordCodecBuilder.mapCodec(
|
| 148 | i -> i.group(
|
| 149 | Codec.FLOAT.fieldOf("base").forGetter(LevelBasedValue.Linear::base),
|
| 150 | Codec.FLOAT.fieldOf("per_level_above_first").forGetter(LevelBasedValue.Linear::perLevelAboveFirst)
|
| 151 | )
|
| 152 | .apply(i, LevelBasedValue.Linear::new)
|
| 153 | );
|
| 154 |
|
| 155 | @Override
|
| 156 | public float calculate(final int level) {
|
| 157 | return this.base + this.perLevelAboveFirst * (level - 1);
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | public MapCodec<LevelBasedValue.Linear> codec() {
|
| 162 | return CODEC;
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | record Lookup(List<Float> values, LevelBasedValue fallback) implements LevelBasedValue {
|
| 167 | public static final MapCodec<LevelBasedValue.Lookup> CODEC = RecordCodecBuilder.mapCodec(
|
| 168 | i -> i.group(
|
| 169 | Codec.FLOAT.listOf().fieldOf("values").forGetter(LevelBasedValue.Lookup::values),
|
| 170 | LevelBasedValue.CODEC.fieldOf("fallback").forGetter(LevelBasedValue.Lookup::fallback)
|
| 171 | )
|
| 172 | .apply(i, LevelBasedValue.Lookup::new)
|
| 173 | );
|
| 174 |
|
| 175 | @Override
|
| 176 | public float calculate(final int level) {
|
| 177 | return level <= this.values.size() ? this.values.get(level - 1) : this.fallback.calculate(level);
|
| 178 | }
|
| 179 |
|
| 180 | @Override
|
| 181 | public MapCodec<LevelBasedValue.Lookup> codec() {
|
| 182 | return CODEC;
|
| 183 | }
|
| 184 | }
|
| 185 | }
|
| 186 |
|