CacheSlot .java

net.minecraft.client.multiplayer.CacheSlot (class, 41 lines): members: Cleaner, compute, operation, value, context, clear. imports: Nullable.

net.minecraft.client.multiplayerclass41 linesclientextends CacheSlot.Cleaner<C>981 B

API Surface (2)

D compute(final C context)
void clear()

Fields & Constants (3)

operation
context
value

Full Source

CacheSlot.java41 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.multiplayer;
2
3
import java.util.function.Function;
4
import net.fabricmc.api.EnvType;
5
import net.fabricmc.api.Environment;
6
import org.jspecify.annotations.Nullable;
7
8
@Environment(EnvType.CLIENT)
9
public class CacheSlot<C extends CacheSlot.Cleaner<C>, D> {
10
	private final Function<C, D> operation;
11
	private @Nullable C context;
12
	private @Nullable D value;
13
14
	public CacheSlot(final Function<C, D> operation) {
15
		this.operation = operation;
16
	}
17
18
	public D compute(final C context) {
19
		if (context == this.context && this.value != null) {
20
			return this.value;
21
		}
22
23
		D newValue = this.operation.apply(context);
24
		this.value = newValue;
25
		this.context = context;
26
		context.registerForCleaning(this);
27
		return newValue;
28
	}
29
30
	public void clear() {
31
		this.value = null;
32
		this.context = null;
33
	}
34
35
	@FunctionalInterface
36
	@Environment(EnvType.CLIENT)
37
	public interface Cleaner<C extends CacheSlot.Cleaner<C>> {
38
		void registerForCleaning(CacheSlot<C, ?> slot);
39
	}
40
}
41

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