Stopwatches .java

net.minecraft.world.Stopwatches (class, 87 lines): members: currentTime, stopwatches, ids, isDirty, update, remove, add, TYPE, get, CODEC. imports: Util.

net.minecraft.worldclass87 linescommonextends SavedData2.5 KB

API Surface (6)

boolean add(final Identifier id, final Stopwatch stopwatch)
boolean update(final Identifier id, final UnaryOperator<Stopwatch> update)
boolean remove(final Identifier id)
boolean isDirty()
List<Identifier> ids()
long currentTime()

Fields & Constants (3)

CODEC
TYPE
stopwatches

Code Graph: Dependencies (6)

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

Full Source

Stopwatches.java87 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world;
2
3
import com.mojang.serialization.Codec;
4
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.TreeMap;
8
import java.util.function.UnaryOperator;
9
import net.minecraft.resources.Identifier;
10
import net.minecraft.util.Util;
11
import net.minecraft.util.datafix.DataFixTypes;
12
import net.minecraft.world.level.saveddata.SavedData;
13
import net.minecraft.world.level.saveddata.SavedDataType;
14
import org.jspecify.annotations.Nullable;
15
16
public class Stopwatches extends SavedData {
17
	private static final Codec<Stopwatches> CODEC = Codec.unboundedMap(Identifier.CODEC, Codec.LONG)
18
		.fieldOf("stopwatches")
19
		.codec()
20
		.xmap(Stopwatches::unpack, Stopwatches::pack);
21
	public static final SavedDataType<Stopwatches> TYPE = new SavedDataType<>(
22
		Identifier.withDefaultNamespace("stopwatches"), Stopwatches::new, CODEC, DataFixTypes.SAVED_DATA_STOPWATCHES
23
	);
24
	private final Map<Identifier, Stopwatch> stopwatches = new Object2ObjectOpenHashMap<>();
25
26
	private Stopwatches() {
27
	}
28
29
	private static Stopwatches unpack(final Map<Identifier, Long> stopwatches) {
30
		Stopwatches result = new Stopwatches();
31
		long currentTime = currentTime();
32
		stopwatches.forEach((id, accumulatedElapsedTime) -> result.stopwatches.put(id, new Stopwatch(currentTime, accumulatedElapsedTime)));
33
		return result;
34
	}
35
36
	private Map<Identifier, Long> pack() {
37
		long currentTime = currentTime();
38
		Map<Identifier, Long> result = new TreeMap<>();
39
		this.stopwatches.forEach((id, stopwatch) -> result.put(id, stopwatch.elapsedMilliseconds(currentTime)));
40
		return result;
41
	}
42
43
	public @Nullable Stopwatch get(final Identifier id) {
44
		return this.stopwatches.get(id);
45
	}
46
47
	public boolean add(final Identifier id, final Stopwatch stopwatch) {
48
		if (this.stopwatches.putIfAbsent(id, stopwatch) == null) {
49
			this.setDirty();
50
			return true;
51
		} else {
52
			return false;
53
		}
54
	}
55
56
	public boolean update(final Identifier id, final UnaryOperator<Stopwatch> update) {
57
		if (this.stopwatches.computeIfPresent(id, (key, value) -> update.apply(value)) != null) {
58
			this.setDirty();
59
			return true;
60
		} else {
61
			return false;
62
		}
63
	}
64
65
	public boolean remove(final Identifier id) {
66
		boolean removed = this.stopwatches.remove(id) != null;
67
		if (removed) {
68
			this.setDirty();
69
		}
70
71
		return removed;
72
	}
73
74
	@Override
75
	public boolean isDirty() {
76
		return super.isDirty() || !this.stopwatches.isEmpty();
77
	}
78
79
	public List<Identifier> ids() {
80
		return List.copyOf(this.stopwatches.keySet());
81
	}
82
83
	public static long currentTime() {
84
		return Util.getMillis();
85
	}
86
}
87

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