BlockTintCache .java

net.minecraft.client.color.block.BlockTintCache (class): members: latestChunkOnThread, invalidateForChunk, LatestCacheInfo, invalidateAll, CacheData, lock.

net.minecraft.client.color.blockclass166 linesclient4.6 KB

API Surface (6)

int getColor(final BlockPos pos)
void invalidateForChunk(final int chunkX, final int chunkZ)
void invalidateAll()
int[] getLayer(final int y)
boolean isInvalidated()
void invalidate()

Fields & Constants (5)

MAX_CACHE_ENTRIES
latestChunkOnThread
cache
lock
source

Code Graph: Dependencies (4)

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

Full Source

BlockTintCache.java166 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.color.block;
2
3
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
4
import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap;
5
import java.util.Arrays;
6
import java.util.concurrent.locks.ReentrantReadWriteLock;
7
import java.util.function.ToIntFunction;
8
import net.fabricmc.api.EnvType;
9
import net.fabricmc.api.Environment;
10
import net.minecraft.core.BlockPos;
11
import net.minecraft.core.SectionPos;
12
import net.minecraft.util.Mth;
13
import net.minecraft.world.level.ChunkPos;
14
import org.jspecify.annotations.Nullable;
15
16
@Environment(EnvType.CLIENT)
17
public class BlockTintCache {
18
	private static final int MAX_CACHE_ENTRIES = 256;
19
	private final ThreadLocal<BlockTintCache.LatestCacheInfo> latestChunkOnThread = ThreadLocal.withInitial(BlockTintCache.LatestCacheInfo::new);
20
	private final Long2ObjectLinkedOpenHashMap<BlockTintCache.CacheData> cache = new Long2ObjectLinkedOpenHashMap<>(256, 0.25F);
21
	private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
22
	private final ToIntFunction<BlockPos> source;
23
24
	public BlockTintCache(final ToIntFunction<BlockPos> source) {
25
		this.source = source;
26
	}
27
28
	public int getColor(final BlockPos pos) {
29
		int chunkX = SectionPos.blockToSectionCoord(pos.getX());
30
		int chunkZ = SectionPos.blockToSectionCoord(pos.getZ());
31
		BlockTintCache.LatestCacheInfo chunkInfo = this.latestChunkOnThread.get();
32
		if (chunkInfo.x != chunkX || chunkInfo.z != chunkZ || chunkInfo.cache == null || chunkInfo.cache.isInvalidated()) {
33
			chunkInfo.x = chunkX;
34
			chunkInfo.z = chunkZ;
35
			chunkInfo.cache = this.findOrCreateChunkCache(chunkX, chunkZ);
36
		}
37
38
		int[] layer = chunkInfo.cache.getLayer(pos.getY());
39
		int x = pos.getX() & 15;
40
		int z = pos.getZ() & 15;
41
		int index = z << 4 | x;
42
		int cached = layer[index];
43
		if (cached != -1) {
44
			return cached;
45
		}
46
47
		int calculated = this.source.applyAsInt(pos);
48
		layer[index] = calculated;
49
		return calculated;
50
	}
51
52
	public void invalidateForChunk(final int chunkX, final int chunkZ) {
53
		try {
54
			this.lock.writeLock().lock();
55
56
			for (int offsetX = -1; offsetX <= 1; offsetX++) {
57
				for (int offsetZ = -1; offsetZ <= 1; offsetZ++) {
58
					long key = ChunkPos.pack(chunkX + offsetX, chunkZ + offsetZ);
59
					BlockTintCache.CacheData removed = this.cache.remove(key);
60
					if (removed != null) {
61
						removed.invalidate();
62
					}
63
				}
64
			}
65
		} finally {
66
			this.lock.writeLock().unlock();
67
		}
68
	}
69
70
	public void invalidateAll() {
71
		try {
72
			this.lock.writeLock().lock();
73
			this.cache.values().forEach(BlockTintCache.CacheData::invalidate);
74
			this.cache.clear();
75
		} finally {
76
			this.lock.writeLock().unlock();
77
		}
78
	}
79
80
	private BlockTintCache.CacheData findOrCreateChunkCache(final int x, final int z) {
81
		long key = ChunkPos.pack(x, z);
82
		this.lock.readLock().lock();
83
84
		try {
85
			BlockTintCache.CacheData existing = this.cache.get(key);
86
			if (existing != null) {
87
				return existing;
88
			}
89
		} finally {
90
			this.lock.readLock().unlock();
91
		}
92
93
		this.lock.writeLock().lock();
94
95
		try {
96
			BlockTintCache.CacheData existingNow = this.cache.get(key);
97
			if (existingNow != null) {
98
				return existingNow;
99
			}
100
101
			BlockTintCache.CacheData newCache = new BlockTintCache.CacheData();
102
			if (this.cache.size() >= 256) {
103
				BlockTintCache.CacheData cacheData = this.cache.removeFirst();
104
				if (cacheData != null) {
105
					cacheData.invalidate();
106
				}
107
			}
108
109
			this.cache.put(key, newCache);
110
			return newCache;
111
		} finally {
112
			this.lock.writeLock().unlock();
113
		}
114
	}
115
116
	@Environment(EnvType.CLIENT)
117
	private static class CacheData {
118
		private final Int2ObjectArrayMap<int[]> cache = new Int2ObjectArrayMap<>(16);
119
		private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
120
		private static final int BLOCKS_PER_LAYER = Mth.square(16);
121
		private volatile boolean invalidated;
122
123
		public int[] getLayer(final int y) {
124
			this.lock.readLock().lock();
125
126
			try {
127
				int[] existing = this.cache.get(y);
128
				if (existing != null) {
129
					return existing;
130
				}
131
			} finally {
132
				this.lock.readLock().unlock();
133
			}
134
135
			this.lock.writeLock().lock();
136
137
			try {
138
				return this.cache.computeIfAbsent(y, n -> this.allocateLayer());
139
			} finally {
140
				this.lock.writeLock().unlock();
141
			}
142
		}
143
144
		private int[] allocateLayer() {
145
			int[] newCache = new int[BLOCKS_PER_LAYER];
146
			Arrays.fill(newCache, -1);
147
			return newCache;
148
		}
149
150
		public boolean isInvalidated() {
151
			return this.invalidated;
152
		}
153
154
		public void invalidate() {
155
			this.invalidated = true;
156
		}
157
	}
158
159
	@Environment(EnvType.CLIENT)
160
	private static class LatestCacheInfo {
161
		private int x = Integer.MIN_VALUE;
162
		private int z = Integer.MIN_VALUE;
163
		private BlockTintCache.@Nullable CacheData cache;
164
	}
165
}
166

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