ContextMap .java

net.minecraft.util.context.ContextMap (class): members: getOrThrow, params, getOrDefault, has, builder, EMPTY, Builder, get. imports: Contract, Sets.

net.minecraft.util.contextclass84 linescommon2.4 KB

API Surface (6)

ContextMap.Builder builder()
boolean has(final ContextKey<?> key)
<T> T getOrThrow(final ContextKey<T> key)
<T> ContextMap.Builder set(final ContextKey<T> param, final @Nullable T value)
ContextMap build()
ContextMap buildAndValidate(final ContextKeySet paramSet)

Fields & Constants (2)

EMPTY
params

String literals (3)

"_,!null->!null; _,_->_"
"Parameters not allowed in this parameter set: "
"Missing required parameters: "

Code Graph: Dependencies (2)

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

Full Source

ContextMap.java84 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.context;
2
3
import com.google.common.collect.Sets;
4
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
5
import java.util.Map;
6
import java.util.NoSuchElementException;
7
import java.util.Set;
8
import org.jetbrains.annotations.Contract;
9
import org.jspecify.annotations.Nullable;
10
11
public final class ContextMap {
12
	public static final ContextMap EMPTY = new ContextMap(Map.of());
13
	private final Map<ContextKey<?>, Object> params;
14
15
	private ContextMap(final Map<ContextKey<?>, Object> params) {
16
		this.params = params;
17
	}
18
19
	public static ContextMap.Builder builder() {
20
		return new ContextMap.Builder();
21
	}
22
23
	public boolean has(final ContextKey<?> key) {
24
		return this.params.containsKey(key);
25
	}
26
27
	public <T> T getOrThrow(final ContextKey<T> key) {
28
		T value = this.get(key);
29
		if (value == null) {
30
			throw new NoSuchElementException(key.name().toString());
31
		} else {
32
			return value;
33
		}
34
	}
35
36
	public <T> @Nullable T get(final ContextKey<T> key) {
37
		return (T)this.params.get(key);
38
	}
39
40
	@Contract("_,!null->!null; _,_->_")
41
	public <T> @Nullable T getOrDefault(final ContextKey<T> param, final @Nullable T _default) {
42
		return (T)this.params.getOrDefault(param, _default);
43
	}
44
45
	public static class Builder {
46
		private final Map<ContextKey<?>, Object> params = new Reference2ObjectOpenHashMap<>();
47
48
		private Builder() {
49
		}
50
51
		public <T> ContextMap.Builder set(final ContextKey<T> param, final @Nullable T value) {
52
			if (value == null) {
53
				this.params.remove(param);
54
			} else {
55
				this.params.put(param, value);
56
			}
57
58
			return this;
59
		}
60
61
		public <T> @Nullable T get(final ContextKey<T> param) {
62
			return (T)this.params.get(param);
63
		}
64
65
		public ContextMap build() {
66
			return this.params.isEmpty() ? ContextMap.EMPTY : new ContextMap(new Reference2ObjectOpenHashMap<>(this.params));
67
		}
68
69
		public ContextMap buildAndValidate(final ContextKeySet paramSet) {
70
			Set<ContextKey<?>> notAllowed = Sets.difference(this.params.keySet(), paramSet.allowed());
71
			if (!notAllowed.isEmpty()) {
72
				throw new IllegalArgumentException("Parameters not allowed in this parameter set: " + notAllowed);
73
			} else {
74
				Set<ContextKey<?>> missingRequired = Sets.difference(paramSet.required(), this.params.keySet());
75
				if (!missingRequired.isEmpty()) {
76
					throw new IllegalArgumentException("Missing required parameters: " + missingRequired);
77
				} else {
78
					return this.build();
79
				}
80
			}
81
		}
82
	}
83
}
84

Decompiled Minecraft 26.3 source of ContextMap (net/minecraft/util/context/ContextMap.java) with Mojang official mappings, stored verbatim on the site.