MemorySlot .java

net.minecraft.world.entity.ai.memory.MemorySlot (class): members: NEVER_EXPIRE, canExpire, timeToLive, hasValue, hasExpired, visit, set, value, clear.

net.minecraft.world.entity.ai.memoryclass81 linescommon1.7 KB

API Surface (11)

void tick()
<T> MemorySlot<T> create()
void set(final T value, final long timeToLive)
void set(final T value)
void clear()
boolean hasValue()
boolean canExpire()
boolean hasExpired()
long timeToLive()
String toString()
void visit(final MemoryModuleType<T> type, final Brain.Visitor visitor)

Fields & Constants (3)

NEVER_EXPIRE
value
timeToLive

String literals (2)

"<empty>"
" (ttl: "

Code Graph: Dependencies (2)

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

Full Source

MemorySlot.java81 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.memory;
2
3
import net.minecraft.world.entity.ai.Brain;
4
import org.jspecify.annotations.Nullable;
5
6
public class MemorySlot<T> {
7
	private static final long NEVER_EXPIRE = Long.MAX_VALUE;
8
	private @Nullable T value;
9
	private long timeToLive;
10
11
	private MemorySlot(final @Nullable T value, final long timeToLive) {
12
		this.value = value;
13
		this.timeToLive = timeToLive;
14
	}
15
16
	public void tick() {
17
		if (this.hasValue() && this.canExpire()) {
18
			if (this.hasExpired()) {
19
				this.clear();
20
			} else {
21
				this.timeToLive--;
22
			}
23
		}
24
	}
25
26
	public static <T> MemorySlot<T> create() {
27
		return new MemorySlot<>(null, Long.MAX_VALUE);
28
	}
29
30
	public void set(final T value, final long timeToLive) {
31
		this.value = value;
32
		this.timeToLive = timeToLive;
33
	}
34
35
	public void set(final T value) {
36
		this.set(value, Long.MAX_VALUE);
37
	}
38
39
	public void clear() {
40
		this.value = null;
41
		this.timeToLive = Long.MAX_VALUE;
42
	}
43
44
	public boolean hasValue() {
45
		return this.value != null;
46
	}
47
48
	public @Nullable T value() {
49
		return this.value;
50
	}
51
52
	public boolean canExpire() {
53
		return this.timeToLive != Long.MAX_VALUE;
54
	}
55
56
	public boolean hasExpired() {
57
		return this.timeToLive <= 0L;
58
	}
59
60
	public long timeToLive() {
61
		return this.timeToLive;
62
	}
63
64
	@Override
65
	public String toString() {
66
		return this.value == null ? "<empty>" : this.value + (this.canExpire() ? " (ttl: " + this.timeToLive + ")" : "");
67
	}
68
69
	public void visit(final MemoryModuleType<T> type, final Brain.Visitor visitor) {
70
		if (this.value != null) {
71
			if (this.canExpire()) {
72
				visitor.accept(type, this.value, this.timeToLive);
73
			} else {
74
				visitor.accept(type, this.value);
75
			}
76
		} else {
77
			visitor.acceptEmpty(type);
78
		}
79
	}
80
}
81

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