| 1 | package net.minecraft.world.level.levelgen.structure.templatesystem;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.nbt.CompoundTag;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.level.LevelReader;
|
| 9 | import net.minecraft.world.level.block.state.BlockState;
|
| 10 | import net.minecraft.world.level.levelgen.structure.templatesystem.rule.blockentity.Passthrough;
|
| 11 | import net.minecraft.world.level.levelgen.structure.templatesystem.rule.blockentity.RuleBlockEntityModifier;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | public class ProcessorRule {
|
| 15 | public static final Passthrough DEFAULT_BLOCK_ENTITY_MODIFIER = Passthrough.INSTANCE;
|
| 16 | public static final Codec<ProcessorRule> CODEC = RecordCodecBuilder.create(
|
| 17 | i -> i.group(
|
| 18 | RuleTest.CODEC.fieldOf("input_predicate").forGetter(r -> r.inputPredicate),
|
| 19 | RuleTest.CODEC.fieldOf("location_predicate").forGetter(r -> r.locPredicate),
|
| 20 | PosRuleTest.CODEC.lenientOptionalFieldOf("position_predicate", PosAlwaysTrueTest.INSTANCE).forGetter(r -> r.posPredicate),
|
| 21 | BlockState.CODEC.fieldOf("output_state").forGetter(r -> r.outputState),
|
| 22 | RuleBlockEntityModifier.CODEC.lenientOptionalFieldOf("block_entity_modifier", DEFAULT_BLOCK_ENTITY_MODIFIER).forGetter(r -> r.blockEntityModifier)
|
| 23 | )
|
| 24 | .apply(i, ProcessorRule::new)
|
| 25 | );
|
| 26 | private final RuleTest inputPredicate;
|
| 27 | private final RuleTest locPredicate;
|
| 28 | private final PosRuleTest posPredicate;
|
| 29 | private final BlockState outputState;
|
| 30 | private final RuleBlockEntityModifier blockEntityModifier;
|
| 31 |
|
| 32 | public ProcessorRule(final RuleTest inputPredicate, final RuleTest locPredicate, final BlockState outputState) {
|
| 33 | this(inputPredicate, locPredicate, PosAlwaysTrueTest.INSTANCE, outputState);
|
| 34 | }
|
| 35 |
|
| 36 | public ProcessorRule(final RuleTest inputPredicate, final RuleTest locPredicate, final PosRuleTest posPredicate, final BlockState outputState) {
|
| 37 | this(inputPredicate, locPredicate, posPredicate, outputState, DEFAULT_BLOCK_ENTITY_MODIFIER);
|
| 38 | }
|
| 39 |
|
| 40 | public ProcessorRule(
|
| 41 | final RuleTest inputPredicate,
|
| 42 | final RuleTest locPredicate,
|
| 43 | final PosRuleTest posPredicate,
|
| 44 | final BlockState outputState,
|
| 45 | final RuleBlockEntityModifier blockEntityModifier
|
| 46 | ) {
|
| 47 | this.inputPredicate = inputPredicate;
|
| 48 | this.locPredicate = locPredicate;
|
| 49 | this.posPredicate = posPredicate;
|
| 50 | this.outputState = outputState;
|
| 51 | this.blockEntityModifier = blockEntityModifier;
|
| 52 | }
|
| 53 |
|
| 54 | public boolean test(
|
| 55 | final LevelReader level,
|
| 56 | final BlockState inputState,
|
| 57 | final BlockPos inTemplatePos,
|
| 58 | final BlockPos worldPos,
|
| 59 | final BlockPos reference,
|
| 60 | final RandomSource random
|
| 61 | ) {
|
| 62 | return this.inputPredicate.test(inputState, worldPos, random)
|
| 63 | && this.locPredicate.testAgainstWorldState(level, worldPos, random)
|
| 64 | && this.posPredicate.test(inTemplatePos, worldPos, reference, random);
|
| 65 | }
|
| 66 |
|
| 67 | public BlockState getOutputState() {
|
| 68 | return this.outputState;
|
| 69 | }
|
| 70 |
|
| 71 | public @Nullable CompoundTag getOutputTag(final RandomSource random, final @Nullable CompoundTag existingTag) {
|
| 72 | return this.blockEntityModifier.apply(random, existingTag);
|
| 73 | }
|
| 74 | }
|
| 75 |
|