| 1 | package net.minecraft.world.level.block.state.properties;
|
| 2 |
|
| 3 | import com.google.common.base.MoreObjects;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.DataResult;
|
| 6 | import com.mojang.serialization.DynamicOps;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.stream.Stream;
|
| 10 | import net.minecraft.world.level.block.state.StateHolder;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | public abstract class Property<T extends Comparable<T>> {
|
| 14 | private final Class<T> clazz;
|
| 15 | private final String name;
|
| 16 | private @Nullable Integer hashCode;
|
| 17 | private final Codec<T> codec = Codec.STRING
|
| 18 | .comapFlatMap(
|
| 19 | namex -> this.getValue(namex).map(DataResult::success).orElseGet(() -> DataResult.error(() -> "Unable to read property: " + this + " with value: " + namex)),
|
| 20 | this::getName
|
| 21 | );
|
| 22 | private final Codec<Property.Value<T>> valueCodec = this.codec.xmap(this::value, Property.Value::value);
|
| 23 |
|
| 24 | protected Property(final String name, final Class<T> clazz) {
|
| 25 | this.clazz = clazz;
|
| 26 | this.name = name;
|
| 27 | }
|
| 28 |
|
| 29 | public Property.Value<T> value(final T value) {
|
| 30 | return new Property.Value<>(this, value);
|
| 31 | }
|
| 32 |
|
| 33 | public Property.Value<T> value(final StateHolder<?, ?> stateHolder) {
|
| 34 | return new Property.Value<>(this, stateHolder.getValue(this));
|
| 35 | }
|
| 36 |
|
| 37 | public Stream<Property.Value<T>> getAllValues() {
|
| 38 | return this.getPossibleValues().stream().map(this::value);
|
| 39 | }
|
| 40 |
|
| 41 | public Codec<T> codec() {
|
| 42 | return this.codec;
|
| 43 | }
|
| 44 |
|
| 45 | public Codec<Property.Value<T>> valueCodec() {
|
| 46 | return this.valueCodec;
|
| 47 | }
|
| 48 |
|
| 49 | public String getName() {
|
| 50 | return this.name;
|
| 51 | }
|
| 52 |
|
| 53 | public Class<T> getValueClass() {
|
| 54 | return this.clazz;
|
| 55 | }
|
| 56 |
|
| 57 | public abstract List<T> getPossibleValues();
|
| 58 |
|
| 59 | public abstract String getName(final T value);
|
| 60 |
|
| 61 | public abstract Optional<T> getValue(final String name);
|
| 62 |
|
| 63 | public abstract int getInternalIndex(final T value);
|
| 64 |
|
| 65 | @Override
|
| 66 | public String toString() {
|
| 67 | return MoreObjects.toStringHelper(this).add("name", this.name).add("clazz", this.clazz).add("values", this.getPossibleValues()).toString();
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public boolean equals(final Object o) {
|
| 72 | if (this == o) {
|
| 73 | return true;
|
| 74 | } else {
|
| 75 | return !(o instanceof Property<?> that) ? false : this.clazz.equals(that.clazz) && this.name.equals(that.name);
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public final int hashCode() {
|
| 81 | if (this.hashCode == null) {
|
| 82 | this.hashCode = this.generateHashCode();
|
| 83 | }
|
| 84 |
|
| 85 | return this.hashCode;
|
| 86 | }
|
| 87 |
|
| 88 | public int generateHashCode() {
|
| 89 | return 31 * this.clazz.hashCode() + this.name.hashCode();
|
| 90 | }
|
| 91 |
|
| 92 | public <U, S extends StateHolder<?, S>> DataResult<S> parseValue(final DynamicOps<U> ops, final S state, final U value) {
|
| 93 | DataResult<T> parsed = this.codec.parse(ops, value);
|
| 94 | return parsed.<S>map(v -> state.setValue(this, v)).setPartial(state);
|
| 95 | }
|
| 96 |
|
| 97 | public record Value<T extends Comparable<T>>(Property<T> property, T value) {
|
| 98 | public Value {
|
| 99 | if (!property.getPossibleValues().contains(value)) {
|
| 100 | throw new IllegalArgumentException("Value " + value + " does not belong to property " + property);
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public String toString() {
|
| 106 | return this.property.getName() + "=" + this.property.getName(this.value);
|
| 107 | }
|
| 108 |
|
| 109 | public String valueName() {
|
| 110 | return this.property.getName(this.value);
|
| 111 | }
|
| 112 | }
|
| 113 | }
|
| 114 |
|