| 1 | package net.minecraft.world.level.storage.loot.functions;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 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 it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Optional;
|
| 11 | import java.util.Set;
|
| 12 | import net.minecraft.core.Holder;
|
| 13 | import net.minecraft.core.component.DataComponents;
|
| 14 | import net.minecraft.util.Util;
|
| 15 | import net.minecraft.world.effect.MobEffect;
|
| 16 | import net.minecraft.world.item.ItemStack;
|
| 17 | import net.minecraft.world.item.Items;
|
| 18 | import net.minecraft.world.item.component.SuspiciousStewEffects;
|
| 19 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 20 | import net.minecraft.world.level.storage.loot.LootContextUser;
|
| 21 | import net.minecraft.world.level.storage.loot.Validatable;
|
| 22 | import net.minecraft.world.level.storage.loot.ValidationContext;
|
| 23 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 24 | import net.minecraft.world.level.storage.loot.providers.number.ints.ContextIntProvider;
|
| 25 | import net.minecraft.world.level.storage.loot.providers.number.ints.ContextIntProviders;
|
| 26 |
|
| 27 | public class SetStewEffectFunction extends LootItemConditionalFunction {
|
| 28 | private static final Codec<List<SetStewEffectFunction.EffectEntry>> EFFECTS_LIST = SetStewEffectFunction.EffectEntry.CODEC.listOf().validate(entries -> {
|
| 29 | Set<Holder<MobEffect>> seenEffects = new ObjectOpenHashSet<>();
|
| 30 |
|
| 31 | for (SetStewEffectFunction.EffectEntry entry : entries) {
|
| 32 | if (!seenEffects.add(entry.effect())) {
|
| 33 | return DataResult.error(() -> "Encountered duplicate mob effect: '" + entry.effect() + "'");
|
| 34 | }
|
| 35 | }
|
| 36 |
|
| 37 | return DataResult.success(entries);
|
| 38 | });
|
| 39 | public static final MapCodec<SetStewEffectFunction> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 40 | i -> commonFields(i).and(EFFECTS_LIST.optionalFieldOf("effects", List.of()).forGetter(f -> f.effects)).apply(i, SetStewEffectFunction::new)
|
| 41 | );
|
| 42 | private final List<SetStewEffectFunction.EffectEntry> effects;
|
| 43 |
|
| 44 | private SetStewEffectFunction(final Optional<Holder<LootItemCondition>> condition, final List<SetStewEffectFunction.EffectEntry> effects) {
|
| 45 | super(condition);
|
| 46 | this.effects = effects;
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public MapCodec<SetStewEffectFunction> codec() {
|
| 51 | return MAP_CODEC;
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public void validate(final ValidationContext context) {
|
| 56 | super.validate(context);
|
| 57 | Validatable.validate(context, "effects", this.effects);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public ItemStack run(final ItemStack itemStack, final LootContext context) {
|
| 62 | if (itemStack.is(Items.SUSPICIOUS_STEW) && !this.effects.isEmpty()) {
|
| 63 | SetStewEffectFunction.EffectEntry entry = Util.getRandom(this.effects, context.getRandom());
|
| 64 | Holder<MobEffect> effect = entry.effect();
|
| 65 | int duration = entry.duration().value().getInt(context);
|
| 66 | if (!effect.value().isInstantaneous()) {
|
| 67 | duration *= 20;
|
| 68 | }
|
| 69 |
|
| 70 | SuspiciousStewEffects.Entry newEntry = new SuspiciousStewEffects.Entry(effect, duration);
|
| 71 | itemStack.update(DataComponents.SUSPICIOUS_STEW_EFFECTS, SuspiciousStewEffects.EMPTY, newEntry, SuspiciousStewEffects::withEffectAdded);
|
| 72 | return itemStack;
|
| 73 | } else {
|
| 74 | return itemStack;
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | public static SetStewEffectFunction.Builder stewEffect() {
|
| 79 | return new SetStewEffectFunction.Builder();
|
| 80 | }
|
| 81 |
|
| 82 | public static class Builder extends LootItemConditionalFunction.Builder<SetStewEffectFunction.Builder> {
|
| 83 | private final ImmutableList.Builder<SetStewEffectFunction.EffectEntry> effects = ImmutableList.builder();
|
| 84 |
|
| 85 | protected SetStewEffectFunction.Builder getThis() {
|
| 86 | return this;
|
| 87 | }
|
| 88 |
|
| 89 | public SetStewEffectFunction.Builder withEffect(final Holder<MobEffect> effect, final Holder<ContextIntProvider> duration) {
|
| 90 | this.effects.add(new SetStewEffectFunction.EffectEntry(effect, duration));
|
| 91 | return this;
|
| 92 | }
|
| 93 |
|
| 94 | public SetStewEffectFunction.Builder withEffect(final Holder<MobEffect> effect, final int duration) {
|
| 95 | return this.withEffect(effect, ContextIntProviders.exactly(duration));
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public LootItemFunction build() {
|
| 100 | return new SetStewEffectFunction(this.getCondition(), this.effects.build());
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | private record EffectEntry(Holder<MobEffect> effect, Holder<ContextIntProvider> duration) implements LootContextUser {
|
| 105 | public static final Codec<SetStewEffectFunction.EffectEntry> CODEC = RecordCodecBuilder.create(
|
| 106 | i -> i.group(
|
| 107 | MobEffect.CODEC.fieldOf("type").forGetter(SetStewEffectFunction.EffectEntry::effect),
|
| 108 | ContextIntProviders.CODEC.fieldOf("duration").forGetter(SetStewEffectFunction.EffectEntry::duration)
|
| 109 | )
|
| 110 | .apply(i, SetStewEffectFunction.EffectEntry::new)
|
| 111 | );
|
| 112 |
|
| 113 | @Override
|
| 114 | public void validate(final ValidationContext context) {
|
| 115 | LootContextUser.super.validate(context);
|
| 116 | Validatable.validateHolder(context, "duration", this.duration);
|
| 117 | }
|
| 118 | }
|
| 119 | }
|
| 120 |
|