| 1 | package net.minecraft.advancements.predicates;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.datafixers.util.Either;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import io.netty.buffer.ByteBuf;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.stream.Collectors;
|
| 11 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 12 | import net.minecraft.network.codec.StreamCodec;
|
| 13 | import net.minecraft.util.StringRepresentable;
|
| 14 | import net.minecraft.world.level.block.state.BlockState;
|
| 15 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 16 | import net.minecraft.world.level.block.state.StateHolder;
|
| 17 | import net.minecraft.world.level.block.state.properties.Property;
|
| 18 | import net.minecraft.world.level.material.FluidState;
|
| 19 |
|
| 20 | public record StatePropertiesPredicate(List<StatePropertiesPredicate.PropertyMatcher> properties) {
|
| 21 | private static final Codec<List<StatePropertiesPredicate.PropertyMatcher>> PROPERTIES_CODEC = Codec.unboundedMap(
|
| 22 | Codec.STRING, StatePropertiesPredicate.ValueMatcher.CODEC
|
| 23 | )
|
| 24 | .xmap(
|
| 25 | map -> map.entrySet().stream().map(entry -> new StatePropertiesPredicate.PropertyMatcher(entry.getKey(), entry.getValue())).toList(),
|
| 26 | properties -> properties.stream()
|
| 27 | .collect(Collectors.toMap(StatePropertiesPredicate.PropertyMatcher::name, StatePropertiesPredicate.PropertyMatcher::valueMatcher))
|
| 28 | );
|
| 29 | public static final Codec<StatePropertiesPredicate> CODEC = PROPERTIES_CODEC.xmap(StatePropertiesPredicate::new, StatePropertiesPredicate::properties);
|
| 30 | public static final StreamCodec<ByteBuf, StatePropertiesPredicate> STREAM_CODEC = StatePropertiesPredicate.PropertyMatcher.STREAM_CODEC
|
| 31 | .apply(ByteBufCodecs.list())
|
| 32 | .map(StatePropertiesPredicate::new, StatePropertiesPredicate::properties);
|
| 33 |
|
| 34 | public <S extends StateHolder<?, S>> boolean matches(final StateDefinition<?, S> definition, final S state) {
|
| 35 | for (StatePropertiesPredicate.PropertyMatcher matcher : this.properties) {
|
| 36 | if (!matcher.match(definition, state)) {
|
| 37 | return false;
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | return true;
|
| 42 | }
|
| 43 |
|
| 44 | public boolean matches(final BlockState state) {
|
| 45 | return this.matches(state.getBlock().getStateDefinition(), state);
|
| 46 | }
|
| 47 |
|
| 48 | public boolean matches(final FluidState state) {
|
| 49 | return this.matches(state.getType().getStateDefinition(), state);
|
| 50 | }
|
| 51 |
|
| 52 | public Optional<String> checkState(final StateDefinition<?, ?> states) {
|
| 53 | for (StatePropertiesPredicate.PropertyMatcher property : this.properties) {
|
| 54 | Optional<String> unknownProperty = property.checkState(states);
|
| 55 | if (unknownProperty.isPresent()) {
|
| 56 | return unknownProperty;
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | return Optional.empty();
|
| 61 | }
|
| 62 |
|
| 63 | public static class Builder {
|
| 64 | private final ImmutableList.Builder<StatePropertiesPredicate.PropertyMatcher> matchers = ImmutableList.builder();
|
| 65 |
|
| 66 | private Builder() {
|
| 67 | }
|
| 68 |
|
| 69 | public static StatePropertiesPredicate.Builder properties() {
|
| 70 | return new StatePropertiesPredicate.Builder();
|
| 71 | }
|
| 72 |
|
| 73 | public StatePropertiesPredicate.Builder hasProperty(final Property<?> property, final String value) {
|
| 74 | this.matchers.add(new StatePropertiesPredicate.PropertyMatcher(property.getName(), new StatePropertiesPredicate.ExactMatcher(value)));
|
| 75 | return this;
|
| 76 | }
|
| 77 |
|
| 78 | public StatePropertiesPredicate.Builder hasProperty(final Property<Integer> property, final int value) {
|
| 79 | return this.hasProperty(property, Integer.toString(value));
|
| 80 | }
|
| 81 |
|
| 82 | public StatePropertiesPredicate.Builder hasProperty(final Property<Boolean> property, final boolean value) {
|
| 83 | return this.hasProperty(property, Boolean.toString(value));
|
| 84 | }
|
| 85 |
|
| 86 | public <T extends Comparable<T> & StringRepresentable> StatePropertiesPredicate.Builder hasProperty(final Property<T> property, final T value) {
|
| 87 | return this.hasProperty(property, value.getSerializedName());
|
| 88 | }
|
| 89 |
|
| 90 | public Optional<StatePropertiesPredicate> build() {
|
| 91 | return Optional.of(new StatePropertiesPredicate(this.matchers.build()));
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | private record ExactMatcher(String value) implements StatePropertiesPredicate.ValueMatcher {
|
| 96 | public static final Codec<StatePropertiesPredicate.ExactMatcher> CODEC = Codec.STRING
|
| 97 | .xmap(StatePropertiesPredicate.ExactMatcher::new, StatePropertiesPredicate.ExactMatcher::value);
|
| 98 | public static final StreamCodec<ByteBuf, StatePropertiesPredicate.ExactMatcher> STREAM_CODEC = ByteBufCodecs.STRING_UTF8
|
| 99 | .map(StatePropertiesPredicate.ExactMatcher::new, StatePropertiesPredicate.ExactMatcher::value);
|
| 100 |
|
| 101 | @Override
|
| 102 | public <T extends Comparable<T>> boolean match(final StateHolder<?, ?> state, final Property<T> property) {
|
| 103 | T actualValue = state.getValue(property);
|
| 104 | Optional<T> typedExpected = property.getValue(this.value);
|
| 105 | return typedExpected.isPresent() && actualValue.compareTo(typedExpected.get()) == 0;
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | private record PropertyMatcher(String name, StatePropertiesPredicate.ValueMatcher valueMatcher) {
|
| 110 | public static final StreamCodec<ByteBuf, StatePropertiesPredicate.PropertyMatcher> STREAM_CODEC = StreamCodec.composite(
|
| 111 | ByteBufCodecs.STRING_UTF8,
|
| 112 | StatePropertiesPredicate.PropertyMatcher::name,
|
| 113 | StatePropertiesPredicate.ValueMatcher.STREAM_CODEC,
|
| 114 | StatePropertiesPredicate.PropertyMatcher::valueMatcher,
|
| 115 | StatePropertiesPredicate.PropertyMatcher::new
|
| 116 | );
|
| 117 |
|
| 118 | public <S extends StateHolder<?, S>> boolean match(final StateDefinition<?, S> definition, final S state) {
|
| 119 | Property<?> property = definition.getProperty(this.name);
|
| 120 | return property != null && this.valueMatcher.match(state, property);
|
| 121 | }
|
| 122 |
|
| 123 | public Optional<String> checkState(final StateDefinition<?, ?> states) {
|
| 124 | Property<?> property = states.getProperty(this.name);
|
| 125 | return property != null ? Optional.empty() : Optional.of(this.name);
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | private record RangedMatcher(Optional<String> minValue, Optional<String> maxValue) implements StatePropertiesPredicate.ValueMatcher {
|
| 130 | public static final Codec<StatePropertiesPredicate.RangedMatcher> CODEC = RecordCodecBuilder.create(
|
| 131 | i -> i.group(
|
| 132 | Codec.STRING.optionalFieldOf("min").forGetter(StatePropertiesPredicate.RangedMatcher::minValue),
|
| 133 | Codec.STRING.optionalFieldOf("max").forGetter(StatePropertiesPredicate.RangedMatcher::maxValue)
|
| 134 | )
|
| 135 | .apply(i, StatePropertiesPredicate.RangedMatcher::new)
|
| 136 | );
|
| 137 | public static final StreamCodec<ByteBuf, StatePropertiesPredicate.RangedMatcher> STREAM_CODEC = StreamCodec.composite(
|
| 138 | ByteBufCodecs.optional(ByteBufCodecs.STRING_UTF8),
|
| 139 | StatePropertiesPredicate.RangedMatcher::minValue,
|
| 140 | ByteBufCodecs.optional(ByteBufCodecs.STRING_UTF8),
|
| 141 | StatePropertiesPredicate.RangedMatcher::maxValue,
|
| 142 | StatePropertiesPredicate.RangedMatcher::new
|
| 143 | );
|
| 144 |
|
| 145 | @Override
|
| 146 | public <T extends Comparable<T>> boolean match(final StateHolder<?, ?> state, final Property<T> property) {
|
| 147 | T value = state.getValue(property);
|
| 148 | if (this.minValue.isPresent()) {
|
| 149 | Optional<T> typedMinValue = property.getValue(this.minValue.get());
|
| 150 | if (typedMinValue.isEmpty() || value.compareTo(typedMinValue.get()) < 0) {
|
| 151 | return false;
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | if (this.maxValue.isPresent()) {
|
| 156 | Optional<T> typedMaxValue = property.getValue(this.maxValue.get());
|
| 157 | if (typedMaxValue.isEmpty() || value.compareTo(typedMaxValue.get()) > 0) {
|
| 158 | return false;
|
| 159 | }
|
| 160 | }
|
| 161 |
|
| 162 | return true;
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | private interface ValueMatcher {
|
| 167 | Codec<StatePropertiesPredicate.ValueMatcher> CODEC = Codec.either(StatePropertiesPredicate.ExactMatcher.CODEC, StatePropertiesPredicate.RangedMatcher.CODEC)
|
| 168 | .xmap(Either::unwrap, matcher -> {
|
| 169 | if (matcher instanceof StatePropertiesPredicate.ExactMatcher exact) {
|
| 170 | return Either.left(exact);
|
| 171 | } else if (matcher instanceof StatePropertiesPredicate.RangedMatcher ranged) {
|
| 172 | return Either.right(ranged);
|
| 173 | } else {
|
| 174 | throw new UnsupportedOperationException();
|
| 175 | }
|
| 176 | });
|
| 177 | StreamCodec<ByteBuf, StatePropertiesPredicate.ValueMatcher> STREAM_CODEC = ByteBufCodecs.either(
|
| 178 | StatePropertiesPredicate.ExactMatcher.STREAM_CODEC, StatePropertiesPredicate.RangedMatcher.STREAM_CODEC
|
| 179 | )
|
| 180 | .map(Either::unwrap, matcher -> {
|
| 181 | if (matcher instanceof StatePropertiesPredicate.ExactMatcher exact) {
|
| 182 | return Either.left(exact);
|
| 183 | } else if (matcher instanceof StatePropertiesPredicate.RangedMatcher ranged) {
|
| 184 | return Either.right(ranged);
|
| 185 | } else {
|
| 186 | throw new UnsupportedOperationException();
|
| 187 | }
|
| 188 | });
|
| 189 |
|
| 190 | <T extends Comparable<T>> boolean match(StateHolder<?, ?> state, Property<T> property);
|
| 191 | }
|
| 192 | }
|
| 193 |
|