| 1 | package net.minecraft.advancements.predicates;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.Optional;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.core.HolderSet;
|
| 9 | import net.minecraft.core.registries.Registries;
|
| 10 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 11 | import net.minecraft.resources.ResourceKey;
|
| 12 | import net.minecraft.server.level.ServerLevel;
|
| 13 | import net.minecraft.world.level.Level;
|
| 14 | import net.minecraft.world.level.biome.Biome;
|
| 15 | import net.minecraft.world.level.block.CampfireBlock;
|
| 16 | import net.minecraft.world.level.levelgen.structure.Structure;
|
| 17 |
|
| 18 | public record LocationPredicate(
|
| 19 | Optional<LocationPredicate.PositionPredicate> position,
|
| 20 | Optional<HolderSet<Biome>> biomes,
|
| 21 | Optional<HolderSet<Structure>> structures,
|
| 22 | Optional<ResourceKey<Level>> dimension,
|
| 23 | Optional<Boolean> smokey,
|
| 24 | Optional<LightPredicate> light,
|
| 25 | Optional<BlockPredicate> block,
|
| 26 | Optional<FluidPredicate> fluid,
|
| 27 | Optional<Boolean> canSeeSky
|
| 28 | ) {
|
| 29 | public static final Codec<LocationPredicate> CODEC = RecordCodecBuilder.create(
|
| 30 | i -> i.group(
|
| 31 | LocationPredicate.PositionPredicate.CODEC.optionalFieldOf("position").forGetter(LocationPredicate::position),
|
| 32 | RegistryCodecs.holderSet(Registries.BIOME).optionalFieldOf("biomes").forGetter(LocationPredicate::biomes),
|
| 33 | RegistryCodecs.holderSet(Registries.STRUCTURE).optionalFieldOf("structures").forGetter(LocationPredicate::structures),
|
| 34 | ResourceKey.codec(Registries.DIMENSION).optionalFieldOf("dimension").forGetter(LocationPredicate::dimension),
|
| 35 | Codec.BOOL.optionalFieldOf("smokey").forGetter(LocationPredicate::smokey),
|
| 36 | LightPredicate.CODEC.optionalFieldOf("light").forGetter(LocationPredicate::light),
|
| 37 | BlockPredicate.CODEC.optionalFieldOf("block").forGetter(LocationPredicate::block),
|
| 38 | FluidPredicate.CODEC.optionalFieldOf("fluid").forGetter(LocationPredicate::fluid),
|
| 39 | Codec.BOOL.optionalFieldOf("can_see_sky").forGetter(LocationPredicate::canSeeSky)
|
| 40 | )
|
| 41 | .apply(i, LocationPredicate::new)
|
| 42 | );
|
| 43 |
|
| 44 | public boolean matches(final ServerLevel level, final double x, final double y, final double z) {
|
| 45 | if (this.position.isPresent() && !this.position.get().matches(x, y, z)) {
|
| 46 | return false;
|
| 47 | }
|
| 48 |
|
| 49 | if (this.dimension.isPresent() && this.dimension.get() != level.dimension()) {
|
| 50 | return false;
|
| 51 | }
|
| 52 |
|
| 53 | BlockPos pos = BlockPos.containing(x, y, z);
|
| 54 | boolean loaded = level.isLoaded(pos);
|
| 55 | if (!this.biomes.isPresent() || loaded && this.biomes.get().contains(level.getBiome(pos))) {
|
| 56 | if (!this.structures.isPresent() || loaded && level.structureManager().getStructureWithPieceAt(pos, this.structures.get()).isValid()) {
|
| 57 | if (!this.smokey.isPresent() || loaded && this.smokey.get() == CampfireBlock.isSmokeyPos(level, pos)) {
|
| 58 | if (this.light.isPresent() && !this.light.get().matches(level, pos)) {
|
| 59 | return false;
|
| 60 | } else if (this.block.isPresent() && !this.block.get().matches(level, pos)) {
|
| 61 | return false;
|
| 62 | } else {
|
| 63 | return this.fluid.isPresent() && !this.fluid.get().matches(level, pos)
|
| 64 | ? false
|
| 65 | : !this.canSeeSky.isPresent() || this.canSeeSky.get() == level.canSeeSky(pos);
|
| 66 | }
|
| 67 | } else {
|
| 68 | return false;
|
| 69 | }
|
| 70 | } else {
|
| 71 | return false;
|
| 72 | }
|
| 73 | } else {
|
| 74 | return false;
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | public static class Builder {
|
| 79 | private MinMaxBounds.Doubles x = MinMaxBounds.Doubles.ANY;
|
| 80 | private MinMaxBounds.Doubles y = MinMaxBounds.Doubles.ANY;
|
| 81 | private MinMaxBounds.Doubles z = MinMaxBounds.Doubles.ANY;
|
| 82 | private Optional<HolderSet<Biome>> biomes = Optional.empty();
|
| 83 | private Optional<HolderSet<Structure>> structures = Optional.empty();
|
| 84 | private Optional<ResourceKey<Level>> dimension = Optional.empty();
|
| 85 | private Optional<Boolean> smokey = Optional.empty();
|
| 86 | private Optional<LightPredicate> light = Optional.empty();
|
| 87 | private Optional<BlockPredicate> block = Optional.empty();
|
| 88 | private Optional<FluidPredicate> fluid = Optional.empty();
|
| 89 | private Optional<Boolean> canSeeSky = Optional.empty();
|
| 90 |
|
| 91 | public static LocationPredicate.Builder location() {
|
| 92 | return new LocationPredicate.Builder();
|
| 93 | }
|
| 94 |
|
| 95 | public static LocationPredicate.Builder inBiome(final Holder<Biome> biome) {
|
| 96 | return location().setBiomes(HolderSet.direct(biome));
|
| 97 | }
|
| 98 |
|
| 99 | public static LocationPredicate.Builder inDimension(final ResourceKey<Level> dimension) {
|
| 100 | return location().setDimension(dimension);
|
| 101 | }
|
| 102 |
|
| 103 | public static LocationPredicate.Builder inStructure(final Holder<Structure> structure) {
|
| 104 | return location().setStructures(HolderSet.direct(structure));
|
| 105 | }
|
| 106 |
|
| 107 | public static LocationPredicate.Builder atYLocation(final MinMaxBounds.Doubles yLocation) {
|
| 108 | return location().setY(yLocation);
|
| 109 | }
|
| 110 |
|
| 111 | public LocationPredicate.Builder setX(final MinMaxBounds.Doubles x) {
|
| 112 | this.x = x;
|
| 113 | return this;
|
| 114 | }
|
| 115 |
|
| 116 | public LocationPredicate.Builder setY(final MinMaxBounds.Doubles y) {
|
| 117 | this.y = y;
|
| 118 | return this;
|
| 119 | }
|
| 120 |
|
| 121 | public LocationPredicate.Builder setZ(final MinMaxBounds.Doubles z) {
|
| 122 | this.z = z;
|
| 123 | return this;
|
| 124 | }
|
| 125 |
|
| 126 | public LocationPredicate.Builder setBiomes(final HolderSet<Biome> biomes) {
|
| 127 | this.biomes = Optional.of(biomes);
|
| 128 | return this;
|
| 129 | }
|
| 130 |
|
| 131 | public LocationPredicate.Builder setStructures(final HolderSet<Structure> structures) {
|
| 132 | this.structures = Optional.of(structures);
|
| 133 | return this;
|
| 134 | }
|
| 135 |
|
| 136 | public LocationPredicate.Builder setDimension(final ResourceKey<Level> dimension) {
|
| 137 | this.dimension = Optional.of(dimension);
|
| 138 | return this;
|
| 139 | }
|
| 140 |
|
| 141 | public LocationPredicate.Builder setLight(final LightPredicate.Builder light) {
|
| 142 | this.light = Optional.of(light.build());
|
| 143 | return this;
|
| 144 | }
|
| 145 |
|
| 146 | public LocationPredicate.Builder setBlock(final BlockPredicate.Builder block) {
|
| 147 | this.block = Optional.of(block.build());
|
| 148 | return this;
|
| 149 | }
|
| 150 |
|
| 151 | public LocationPredicate.Builder setFluid(final FluidPredicate.Builder fluid) {
|
| 152 | this.fluid = Optional.of(fluid.build());
|
| 153 | return this;
|
| 154 | }
|
| 155 |
|
| 156 | public LocationPredicate.Builder setSmokey(final boolean smokey) {
|
| 157 | this.smokey = Optional.of(smokey);
|
| 158 | return this;
|
| 159 | }
|
| 160 |
|
| 161 | public LocationPredicate.Builder setCanSeeSky(final boolean canSeeSky) {
|
| 162 | this.canSeeSky = Optional.of(canSeeSky);
|
| 163 | return this;
|
| 164 | }
|
| 165 |
|
| 166 | public LocationPredicate build() {
|
| 167 | Optional<LocationPredicate.PositionPredicate> position = LocationPredicate.PositionPredicate.of(this.x, this.y, this.z);
|
| 168 | return new LocationPredicate(position, this.biomes, this.structures, this.dimension, this.smokey, this.light, this.block, this.fluid, this.canSeeSky);
|
| 169 | }
|
| 170 | }
|
| 171 |
|
| 172 | private record PositionPredicate(MinMaxBounds.Doubles x, MinMaxBounds.Doubles y, MinMaxBounds.Doubles z) {
|
| 173 | public static final Codec<LocationPredicate.PositionPredicate> CODEC = RecordCodecBuilder.create(
|
| 174 | i -> i.group(
|
| 175 | MinMaxBounds.Doubles.CODEC.optionalFieldOf("x", MinMaxBounds.Doubles.ANY).forGetter(LocationPredicate.PositionPredicate::x),
|
| 176 | MinMaxBounds.Doubles.CODEC.optionalFieldOf("y", MinMaxBounds.Doubles.ANY).forGetter(LocationPredicate.PositionPredicate::y),
|
| 177 | MinMaxBounds.Doubles.CODEC.optionalFieldOf("z", MinMaxBounds.Doubles.ANY).forGetter(LocationPredicate.PositionPredicate::z)
|
| 178 | )
|
| 179 | .apply(i, LocationPredicate.PositionPredicate::new)
|
| 180 | );
|
| 181 |
|
| 182 | private static Optional<LocationPredicate.PositionPredicate> of(final MinMaxBounds.Doubles x, final MinMaxBounds.Doubles y, final MinMaxBounds.Doubles z) {
|
| 183 | return x.isAny() && y.isAny() && z.isAny() ? Optional.empty() : Optional.of(new LocationPredicate.PositionPredicate(x, y, z));
|
| 184 | }
|
| 185 |
|
| 186 | public boolean matches(final double x, final double y, final double z) {
|
| 187 | return this.x.matches(x) && this.y.matches(y) && this.z.matches(z);
|
| 188 | }
|
| 189 | }
|
| 190 | }
|
| 191 |
|