KeyValueCondition .java

net.minecraft.client.renderer.block.dispatch.multipart.KeyValueCondition (record, 162 lines): members: tests, Terms, Term, instantiate, key, LOGGER, CODEC.

net.minecraft.client.renderer.block.dispatch.multipartrecord162 linesclient6.2 KB

Implements

API Surface (8)

<O, S extends StateHolder<O, S>> Predicate<S> instantiate(final StateDefinition<O, S> definition)
record Term(String value, boolean negated)
KeyValueCondition.Term parse(final String value)
String toString()
record Terms(List<KeyValueCondition.Term> entries)
DataResult<KeyValueCondition.Terms> parse(final String value)
String toString()
<O, S extends StateHolder<O, S>, T extends Comparable<T>> Predicate<S> instantiate(final O owner, final Property<T> property)

Fields & Constants (3)

LOGGER
CODEC
key

String literals (4)

"Empty term"
"This codec can't be used for encoding"
"Empty value for property"
"Empty term in value '"

Code Graph: Dependencies (6)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

KeyValueCondition.java162 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.renderer.block.dispatch.multipart;
2
3
import com.google.common.base.Joiner;
4
import com.google.common.base.Splitter;
5
import com.google.common.collect.Lists;
6
import com.mojang.logging.LogUtils;
7
import com.mojang.serialization.Codec;
8
import com.mojang.serialization.DataResult;
9
import java.util.ArrayList;
10
import java.util.List;
11
import java.util.Locale;
12
import java.util.Map;
13
import java.util.Optional;
14
import java.util.function.Predicate;
15
import net.fabricmc.api.EnvType;
16
import net.fabricmc.api.Environment;
17
import net.minecraft.util.ExtraCodecs;
18
import net.minecraft.util.Util;
19
import net.minecraft.world.level.block.state.StateDefinition;
20
import net.minecraft.world.level.block.state.StateHolder;
21
import net.minecraft.world.level.block.state.properties.Property;
22
import org.slf4j.Logger;
23
24
@Environment(EnvType.CLIENT)
25
public record KeyValueCondition(Map<String, KeyValueCondition.Terms> tests) implements Condition {
26
	private static final Logger LOGGER = LogUtils.getLogger();
27
	public static final Codec<KeyValueCondition> CODEC = ExtraCodecs.nonEmptyMap(Codec.unboundedMap(Codec.STRING, KeyValueCondition.Terms.CODEC))
28
		.xmap(KeyValueCondition::new, KeyValueCondition::tests);
29
30
	@Override
31
	public <O, S extends StateHolder<O, S>> Predicate<S> instantiate(final StateDefinition<O, S> definition) {
32
		List<Predicate<S>> predicates = new ArrayList<>(this.tests.size());
33
		this.tests.forEach((key, valueTest) -> predicates.add(instantiate(definition, key, valueTest)));
34
		return Util.allOf(predicates);
35
	}
36
37
	private static <O, S extends StateHolder<O, S>> Predicate<S> instantiate(
38
		final StateDefinition<O, S> definition, final String key, final KeyValueCondition.Terms valueTest
39
	) {
40
		Property<?> property = definition.getProperty(key);
41
		if (property == null) {
42
			throw new IllegalArgumentException(String.format(Locale.ROOT, "Unknown property '%s' on '%s'", key, definition.getOwner()));
43
		} else {
44
			return valueTest.instantiate(definition.getOwner(), property);
45
		}
46
	}
47
48
	@Environment(EnvType.CLIENT)
49
	public record Term(String value, boolean negated) {
50
		private static final String NEGATE = "!";
51
52
		public Term {
53
			if (value.isEmpty()) {
54
				throw new IllegalArgumentException("Empty term");
55
			}
56
		}
57
58
		public static KeyValueCondition.Term parse(final String value) {
59
			return value.startsWith("!") ? new KeyValueCondition.Term(value.substring(1), true) : new KeyValueCondition.Term(value, false);
60
		}
61
62
		@Override
63
		public String toString() {
64
			return this.negated ? "!" + this.value : this.value;
65
		}
66
	}
67
68
	@Environment(EnvType.CLIENT)
69
	public record Terms(List<KeyValueCondition.Term> entries) {
70
		private static final char SEPARATOR = '|';
71
		private static final Joiner JOINER = Joiner.on('|');
72
		private static final Splitter SPLITTER = Splitter.on('|');
73
		private static final Codec<String> LEGACY_REPRESENTATION_CODEC = Codec.either(Codec.INT, Codec.BOOL)
74
			.flatComapMap(either -> either.map(String::valueOf, String::valueOf), o -> DataResult.error(() -> "This codec can't be used for encoding"));
75
		public static final Codec<KeyValueCondition.Terms> CODEC = Codec.withAlternative(Codec.STRING, LEGACY_REPRESENTATION_CODEC)
76
			.comapFlatMap(KeyValueCondition.Terms::parse, KeyValueCondition.Terms::toString);
77
78
		public Terms {
79
			if (entries.isEmpty()) {
80
				throw new IllegalArgumentException("Empty value for property");
81
			}
82
		}
83
84
		public static DataResult<KeyValueCondition.Terms> parse(final String value) {
85
			List<KeyValueCondition.Term> terms = SPLITTER.splitToStream(value).map(KeyValueCondition.Term::parse).toList();
86
			if (terms.isEmpty()) {
87
				return DataResult.error(() -> "Empty value for property");
88
			}
89
90
			for (KeyValueCondition.Term entry : terms) {
91
				if (entry.value.isEmpty()) {
92
					return DataResult.error(() -> "Empty term in value '" + value + "'");
93
				}
94
			}
95
96
			return DataResult.success(new KeyValueCondition.Terms(terms));
97
		}
98
99
		@Override
100
		public String toString() {
101
			return JOINER.join(this.entries);
102
		}
103
104
		public <O, S extends StateHolder<O, S>, T extends Comparable<T>> Predicate<S> instantiate(final O owner, final Property<T> property) {
105
			Predicate<T> allowedValueTest = Util.anyOf(Lists.transform(this.entries, t -> this.instantiate(owner, property, t)));
106
			List<T> allowedValues = new ArrayList<>(property.getPossibleValues());
107
			int allValuesCount = allowedValues.size();
108
			allowedValues.removeIf(allowedValueTest.negate());
109
			int allowedValuesCount = allowedValues.size();
110
			if (allowedValuesCount == 0) {
111
				KeyValueCondition.LOGGER.warn("Condition {} for property {} on {} is always false", this, property.getName(), owner);
112
				return blockState -> false;
113
			}
114
115
			int rejectedValuesCount = allValuesCount - allowedValuesCount;
116
			if (rejectedValuesCount == 0) {
117
				KeyValueCondition.LOGGER.warn("Condition {} for property {} on {} is always true", this, property.getName(), owner);
118
				return blockState -> true;
119
			}
120
121
			boolean negate;
122
			List<T> valuesToMatch;
123
			if (allowedValuesCount <= rejectedValuesCount) {
124
				negate = false;
125
				valuesToMatch = allowedValues;
126
			} else {
127
				negate = true;
128
				List<T> rejectedValues = new ArrayList<>(property.getPossibleValues());
129
				rejectedValues.removeIf(allowedValueTest);
130
				valuesToMatch = rejectedValues;
131
			}
132
133
			if (valuesToMatch.size() == 1) {
134
				T expectedValue = (T)valuesToMatch.getFirst();
135
				return state -> {
136
					T value = state.getValue(property);
137
					return expectedValue.equals(value) ^ negate;
138
				};
139
			} else {
140
				return state -> {
141
					T value = state.getValue(property);
142
					return valuesToMatch.contains(value) ^ negate;
143
				};
144
			}
145
		}
146
147
		private <T extends Comparable<T>> T getValueOrThrow(final Object owner, final Property<T> property, final String input) {
148
			Optional<T> value = property.getValue(input);
149
			if (value.isEmpty()) {
150
				throw new RuntimeException(String.format(Locale.ROOT, "Unknown value '%s' for property '%s' on '%s' in '%s'", input, property, owner, this));
151
			} else {
152
				return value.get();
153
			}
154
		}
155
156
		private <T extends Comparable<T>> Predicate<T> instantiate(final Object owner, final Property<T> property, final KeyValueCondition.Term term) {
157
			T parsedValue = this.getValueOrThrow(owner, property, term.value);
158
			return term.negated ? value -> !value.equals(parsedValue) : value -> value.equals(parsedValue);
159
		}
160
	}
161
}
162

Decompiled Minecraft 26.3 source of KeyValueCondition (net/minecraft/client/renderer/block/dispatch/multipart/KeyValueCondition.java) with Mojang official mappings, stored verbatim on the site.