2025-11-24 22:52:51 +03:00

50 lines
2.2 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* com.mojang.datafixers.kinds.App
* com.mojang.datafixers.kinds.Applicative
* com.mojang.serialization.Codec
* com.mojang.serialization.DataResult
* com.mojang.serialization.codecs.RecordCodecBuilder
*/
package net.minecraft.world.item.enchantment;
import com.mojang.datafixers.kinds.App;
import com.mojang.datafixers.kinds.Applicative;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import java.util.Optional;
import net.minecraft.util.ProblemReporter;
import net.minecraft.util.context.ContextKeySet;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.ValidationContext;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
public record ConditionalEffect<T>(T effect, Optional<LootItemCondition> requirements) {
public static Codec<LootItemCondition> conditionCodec(ContextKeySet paramsSet) {
return LootItemCondition.DIRECT_CODEC.validate(condition -> {
ProblemReporter.Collector problemCollector = new ProblemReporter.Collector();
ValidationContext validationContext = new ValidationContext(problemCollector, paramsSet);
condition.validate(validationContext);
if (!problemCollector.isEmpty()) {
return DataResult.error(() -> "Validation error in enchantment effect condition: " + problemCollector.getReport());
}
return DataResult.success((Object)condition);
});
}
public static <T> Codec<ConditionalEffect<T>> codec(Codec<T> effectCodec, ContextKeySet paramsSet) {
return RecordCodecBuilder.create(i -> i.group((App)effectCodec.fieldOf("effect").forGetter(ConditionalEffect::effect), (App)ConditionalEffect.conditionCodec(paramsSet).optionalFieldOf("requirements").forGetter(ConditionalEffect::requirements)).apply((Applicative)i, ConditionalEffect::new));
}
public boolean matches(LootContext context) {
if (this.requirements.isEmpty()) {
return true;
}
return this.requirements.get().test(context);
}
}