StatType .java

net.minecraft.stats.StatType (class, 53 lines): members: getRegistry, displayName, registry, iterator, streamCodec, getDisplayName, map, contains, get.

net.minecraft.statsclass53 linescommon1.5 KB

Implements

API Surface (7)

StreamCodec<RegistryFriendlyByteBuf, Stat<T>> streamCodec()
boolean contains(final T key)
Stat<T> get(final T argument, final StatFormatter formatter)
Registry<T> getRegistry()
Iterator<Stat<T>> iterator()
Stat<T> get(final T argument)
Component getDisplayName()

Fields & Constants (4)

registry
map
displayName
streamCodec

Full Source

StatType.java53 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.stats;
2
3
import java.util.IdentityHashMap;
4
import java.util.Iterator;
5
import java.util.Map;
6
import net.minecraft.core.Registry;
7
import net.minecraft.network.RegistryFriendlyByteBuf;
8
import net.minecraft.network.chat.Component;
9
import net.minecraft.network.codec.ByteBufCodecs;
10
import net.minecraft.network.codec.StreamCodec;
11
12
public class StatType<T> implements Iterable<Stat<T>> {
13
	private final Registry<T> registry;
14
	private final Map<T, Stat<T>> map = new IdentityHashMap<>();
15
	private final Component displayName;
16
	private final StreamCodec<RegistryFriendlyByteBuf, Stat<T>> streamCodec;
17
18
	public StatType(final Registry<T> registry, final Component displayName) {
19
		this.registry = registry;
20
		this.displayName = displayName;
21
		this.streamCodec = ByteBufCodecs.registry(registry.key()).map(this::get, Stat::getValue);
22
	}
23
24
	public StreamCodec<RegistryFriendlyByteBuf, Stat<T>> streamCodec() {
25
		return this.streamCodec;
26
	}
27
28
	public boolean contains(final T key) {
29
		return this.map.containsKey(key);
30
	}
31
32
	public Stat<T> get(final T argument, final StatFormatter formatter) {
33
		return this.map.computeIfAbsent(argument, t -> new Stat<>(this, (T)t, formatter));
34
	}
35
36
	public Registry<T> getRegistry() {
37
		return this.registry;
38
	}
39
40
	@Override
41
	public Iterator<Stat<T>> iterator() {
42
		return this.map.values().iterator();
43
	}
44
45
	public Stat<T> get(final T argument) {
46
		return this.get(argument, StatFormatter.DEFAULT);
47
	}
48
49
	public Component getDisplayName() {
50
		return this.displayName;
51
	}
52
}
53

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