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.jetbrains.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( string -> (DataResult)this.getValue(string) .map(DataResult::success) .orElseGet(() -> DataResult.error(() -> "Unable to read property: " + this + " with value: " + string)), this::getName ); private final Codec> valueCodec = this.codec.xmap(this::value, Property.Value::value); protected Property(String name, Class clazz) { this.clazz = clazz; this.name = name; } public Property.Value value(T value) { return new Property.Value<>(this, value); } public Property.Value value(StateHolder holder) { return new Property.Value<>(this, holder.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; } /** * @return the class of the values of this property */ public Class getValueClass() { return this.clazz; } public abstract List getPossibleValues(); /** * @return the name for the given value. */ public abstract String getName(T value); public abstract Optional getValue(String value); public abstract int getInternalIndex(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(Object object) { if (this == object) { return true; } else { return !(object instanceof Property property) ? false : this.clazz.equals(property.clazz) && this.name.equals(property.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(DynamicOps ops, S stateHolder, U unparsedValue) { DataResult dataResult = this.codec.parse(ops, unparsedValue); return dataResult.map(comparable -> stateHolder.setValue(this, comparable)).setPartial(stateHolder); } 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); } } }