package net.minecraft.world.level.block.state.properties; import com.google.common.base.MoreObjects; import com.mojang.serialization.Codec; import com.mojang.serialization.DataResult; import com.mojang.serialization.DynamicOps; import java.util.List; import java.util.Optional; import java.util.stream.Stream; import net.minecraft.world.level.block.state.StateHolder; import org.jspecify.annotations.Nullable; public abstract class Property> { private final Class clazz; private final String name; @Nullable private Integer hashCode; private final Codec codec = Codec.STRING .comapFlatMap( namex -> (DataResult)this.getValue(namex) .map(DataResult::success) .orElseGet(() -> DataResult.error(() -> "Unable to read property: " + this + " with value: " + namex)), this::getName ); private final Codec> valueCodec = this.codec.xmap(this::value, Property.Value::value); protected Property(final String name, final Class clazz) { this.clazz = clazz; this.name = name; } public Property.Value value(final T value) { return new Property.Value<>(this, value); } public Property.Value value(final StateHolder stateHolder) { return new Property.Value<>(this, stateHolder.getValue(this)); } public Stream> getAllValues() { return this.getPossibleValues().stream().map(this::value); } public Codec codec() { return this.codec; } public Codec> valueCodec() { return this.valueCodec; } public String getName() { return this.name; } public Class getValueClass() { return this.clazz; } public abstract List getPossibleValues(); public abstract String getName(final T value); public abstract Optional getValue(final String name); public abstract int getInternalIndex(final T value); public String toString() { return MoreObjects.toStringHelper(this).add("name", this.name).add("clazz", this.clazz).add("values", this.getPossibleValues()).toString(); } public boolean equals(final Object o) { if (this == o) { return true; } else { return !(o instanceof Property that) ? false : this.clazz.equals(that.clazz) && this.name.equals(that.name); } } public final int hashCode() { if (this.hashCode == null) { this.hashCode = this.generateHashCode(); } return this.hashCode; } public int generateHashCode() { return 31 * this.clazz.hashCode() + this.name.hashCode(); } public > DataResult parseValue(final DynamicOps ops, final S state, final U value) { DataResult parsed = this.codec.parse(ops, value); return parsed.map(v -> state.setValue(this, v)).setPartial(state); } public record Value>(Property property, T value) { public Value(Property property, T value) { if (!property.getPossibleValues().contains(value)) { throw new IllegalArgumentException("Value " + value + " does not belong to property " + property); } else { this.property = property; this.value = value; } } public String toString() { return this.property.getName() + "=" + this.property.getName(this.value); } public String valueName() { return this.property.getName(this.value); } } }