| 1 | package net.minecraft.world.level.block.state;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import java.util.Collection;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Objects;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.function.Function;
|
| 10 | import java.util.stream.Collectors;
|
| 11 | import java.util.stream.IntStream;
|
| 12 | import java.util.stream.Stream;
|
| 13 | import net.minecraft.world.level.block.state.properties.Property;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | public abstract class StateHolder<O, S> {
|
| 17 | private static final int VALUE_NOT_FOUND = -1;
|
| 18 | public static final String ID_TAG = "id";
|
| 19 | public static final String PROPERTIES_TAG = "properties";
|
| 20 | protected final O owner;
|
| 21 | private final Property<?>[] propertyKeys;
|
| 22 | private final Comparable<?>[] propertyValues;
|
| 23 | private S[][] neighbors;
|
| 24 |
|
| 25 | protected StateHolder(final O owner, final Property<?>[] propertyKeys, final Comparable<?>[] propertyValues) {
|
| 26 | assert propertyKeys.length == propertyValues.length;
|
| 27 | this.owner = owner;
|
| 28 | this.propertyKeys = propertyKeys;
|
| 29 | this.propertyValues = propertyValues;
|
| 30 | }
|
| 31 |
|
| 32 | public <T extends Comparable<T>> S cycle(final Property<T> property) {
|
| 33 | return this.setValue(property, findNextInCollection(property.getPossibleValues(), this.getValue(property)));
|
| 34 | }
|
| 35 |
|
| 36 | protected static <T> T findNextInCollection(final List<T> values, final T current) {
|
| 37 | int nextIndex = values.indexOf(current) + 1;
|
| 38 | return nextIndex == values.size() ? values.getFirst() : values.get(nextIndex);
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public String toString() {
|
| 43 | StringBuilder builder = new StringBuilder();
|
| 44 | builder.append(this.owner);
|
| 45 | if (!this.isSingletonState()) {
|
| 46 | builder.append('[');
|
| 47 | builder.append(this.getValues().map(Property.Value::toString).collect(Collectors.joining(",")));
|
| 48 | builder.append(']');
|
| 49 | }
|
| 50 |
|
| 51 | return builder.toString();
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public final boolean equals(final Object obj) {
|
| 56 | return super.equals(obj);
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public int hashCode() {
|
| 61 | return super.hashCode();
|
| 62 | }
|
| 63 |
|
| 64 | public Collection<Property<?>> getProperties() {
|
| 65 | return List.of(this.propertyKeys);
|
| 66 | }
|
| 67 |
|
| 68 | private int valueIndex(final Property<?> property) {
|
| 69 | for (int i = 0; i < this.propertyKeys.length; i++) {
|
| 70 | if (this.propertyKeys[i] == property) {
|
| 71 | return i;
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | return -1;
|
| 76 | }
|
| 77 |
|
| 78 | public boolean hasProperty(final Property<?> property) {
|
| 79 | return this.valueIndex(property) != -1;
|
| 80 | }
|
| 81 |
|
| 82 | private <T extends Comparable<T>> @Nullable T getNullableValue(final Property<T> property) {
|
| 83 | int index = this.valueIndex(property);
|
| 84 | return index == -1 ? null : property.getValueClass().cast(this.propertyValues[index]);
|
| 85 | }
|
| 86 |
|
| 87 | public <T extends Comparable<T>> T getValue(final Property<T> property) {
|
| 88 | T value = this.getNullableValue(property);
|
| 89 | if (value == null) {
|
| 90 | throw new IllegalArgumentException("Cannot get property " + property + " as it does not exist in " + this.owner);
|
| 91 | } else {
|
| 92 | return value;
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | public <T extends Comparable<T>> Optional<T> getOptionalValue(final Property<T> property) {
|
| 97 | return Optional.ofNullable(this.getNullableValue(property));
|
| 98 | }
|
| 99 |
|
| 100 | public <T extends Comparable<T>> T getValueOrElse(final Property<T> property, final T defaultValue) {
|
| 101 | return Objects.requireNonNullElse(this.getNullableValue(property), defaultValue);
|
| 102 | }
|
| 103 |
|
| 104 | public <T extends Comparable<T>, V extends T> S setValue(final Property<T> property, final V value) {
|
| 105 | int index = this.valueIndex(property);
|
| 106 | if (index == -1) {
|
| 107 | throw new IllegalArgumentException("Cannot set property " + property + " as it does not exist in " + this.owner);
|
| 108 | } else {
|
| 109 | return this.setValueInternal(property, index, value);
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | public <T extends Comparable<T>, V extends T> S trySetValue(final Property<T> property, final V value) {
|
| 114 | int index = this.valueIndex(property);
|
| 115 | return (S)(index == -1 ? this : this.setValueInternal(property, index, value));
|
| 116 | }
|
| 117 |
|
| 118 | private <T extends Comparable<T>, V extends T> S setValueInternal(final Property<T> property, final int propertyIndex, final V value) {
|
| 119 | int valueIndex = property.getInternalIndex((T)value);
|
| 120 | if (valueIndex < 0) {
|
| 121 | throw new IllegalArgumentException("Cannot set property " + property + " to " + value + " on " + this.owner + ", it is not an allowed value");
|
| 122 | } else {
|
| 123 | return this.neighbors[propertyIndex][valueIndex];
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | void initializeNeighbors(final S[][] neighbors) {
|
| 128 | if (this.neighbors != null) {
|
| 129 | throw new IllegalStateException();
|
| 130 | }
|
| 131 |
|
| 132 | this.neighbors = neighbors;
|
| 133 | }
|
| 134 |
|
| 135 | public boolean isSingletonState() {
|
| 136 | return this.propertyKeys.length == 0;
|
| 137 | }
|
| 138 |
|
| 139 | public Stream<Property.Value<?>> getValues() {
|
| 140 | int length = this.propertyKeys.length;
|
| 141 | return length == 0 ? Stream.empty() : IntStream.range(0, length).mapToObj(i -> createValue(this.propertyKeys[i], this.propertyValues[i]));
|
| 142 | }
|
| 143 |
|
| 144 | private static <T extends Comparable<T>> Property.Value<T> createValue(final Property<T> propertyKey, final Comparable<?> propertyValue) {
|
| 145 | return new Property.Value<>(propertyKey, (T)propertyValue);
|
| 146 | }
|
| 147 |
|
| 148 | protected static <O, S extends StateHolder<O, S>> Codec<S> codec(
|
| 149 | final Codec<O> ownerCodec, final Function<O, S> defaultState, final Function<O, StateDefinition<O, S>> stateDefinition
|
| 150 | ) {
|
| 151 | return ownerCodec.dispatch(
|
| 152 | "id",
|
| 153 | s -> s.owner,
|
| 154 | o -> {
|
| 155 | StateDefinition<O, S> definition = stateDefinition.apply((O)o);
|
| 156 | S defaultValue = defaultState.apply((O)o);
|
| 157 | return definition.isSingletonState()
|
| 158 | ? MapCodec.unit(defaultValue)
|
| 159 | : definition.propertiesCodec().codec().lenientOptionalFieldOf("properties").xmap(oo -> oo.orElse(defaultValue), Optional::of);
|
| 160 | }
|
| 161 | );
|
| 162 | }
|
| 163 | }
|
| 164 |
|