Block ID Index
基本信息
com.mitchej123.supernova.api.BlockIdIndex —— 包私有(final class,
无 public 修饰)的方块 id 快速索引,被
Light Color Registry 与
Translucency Registry 各自持有一份。
文件头是
final class BlockIdIndex,没有public。 因此外部 mod 无法使用,api/目录不等于「全部是公开 API」。
双索引结构
| 字段 | 类型 | 用途 |
|---|---|---|
master |
IdentityHashMap<Block, int[]> |
权威,按方块身份 |
byId |
volatile int[][] |
只读快照,按数值 id 直接下标 |
byId 是 volatile 的数组引用:写入方整体替换数组
(put 里按需 Arrays.copyOf 扩容),而不是就地改元素 ——
读方因此永远看到自洽的一代快照,无需加锁。
编码约定:value + 1
lookup 的返回值是索引而非颜色值本身:
| 情况 | 返回 |
|---|---|
entry == null |
-1 |
entry.length == 1(全 meta 统一) |
entry[0] - 1 |
| 该 meta 命中非 0 | entry[meta] - 1 |
| 该 meta 为 0(未定义) | -1 |
即数组里存的是「值 + 1」,0 保留给「无条目」。 这是为了把「无条目」与「颜色值 0(不发光)」区分开。
isMetaSensitive(blockId) 即 entry.length > 1;
hasEntry(blockId) 即 entryFor(blockId) != null。
初始长度 16
putMeta 新建数组时用 Math.max(meta + 1, 16),
即最少分配 16 个 meta 槽(覆盖原版 16 种方块 meta)。
从 length == 1 升级到 per-meta 时会 Arrays.fill 填上原统一值。
嵌套的 Cache
BlockIdIndex.Cache 是配套的两级只读缓存:
uniform(int[],每 id 一个)先查,未命中再查 perMeta(int[][])。
lookup(blockId, meta, miss) 的 miss 参数同时充当哨兵值。
相关条目
- Light Color Registry
- Translucency Registry
- Light Registries — 触发
rebuild()
源码
src/main/java/com/mitchej123/supernova/api/BlockIdIndex.java