IDExtenderCompat
基本信息
| 属性 | 值 |
|---|---|
| 完整类名 | com.gtnewhorizons.postea.api.IDExtenderCompat |
| 包路径 | api |
| 类型 | 工具类(普通 class, 提供静态方法) |
| 调用时机 | ItemStack NBT 反序列化前后的任意阶段(典型:TileEntity 转换器内部) |
| 关联机制 | EndlessIDs / NEID 扩展 ID 存储 |
功能
读写 ItemStack NBT 中的"数值 ID"字段,自动处理 vanilla 与 ID 扩展 mod(EndlessIDs、NEID)之间的字段差异。
- vanilla 用
short id(0~32767)。 - EndlessIDs / NEID 在 id ≥ 32000 时额外写入
int idExt。
本类统一两套字段:
- 读:优先返回
idExt(int),否则返回id(int/short)。 - 写:当 id < 32000 时写入
short id;否则short id = 0+int idExt。
方法
| 方法 | 返回 | 说明 |
|---|---|---|
getItemStackID(NBTTagCompound tag) |
int |
读取 ItemStack 的数值 ID,自动处理 idExt 与 id |
setItemStackID(NBTTagCompound tag, int id) |
void |
写入 ItemStack 的数值 ID,自动决定用 short 或 int |
读取优先级:
idExt(TAG_INT) → 返回 int。id(TAG_INT) → 返回 int。id(TAG_SHORT) → 返回 short 转 int。- 都不存在 → 返回 0。
写入阈值:
| id 值 | 行为 |
|---|---|
< 32000 |
setShort("id", id) |
>= 32000 |
setShort("id", 0) + setInteger("idExt", id) |
使用模式
在 TileEntity 转换器中改写物品栏 ID
TileEntityReplacementManager.tileEntityTransformer("Chest", (tag, world, chunk) -> {
NBTTagList slots = tag.getTagList("Items", 10);
for (int i = 0; i < slots.tagCount(); i++) {
NBTTagCompound slot = slots.getCompoundTagAt(i);
if (IDExtenderCompat.getItemStackID(slot) == MyMod.targetRemovedItemId) {
IDExtenderCompat.setItemStackID(slot, Item.getIdFromItem(Items.diamond));
}
}
return null;
});
与 ItemStackReplacementManager.registerIDResolver 配合
ItemStackReplacementManager.registerIDResolver("minecraft:reeds", id -> MyMod.reedsId = id);
// ...后续 handler 中:
if (IDExtenderCompat.getItemStackID(slot) != MyMod.reedsId) continue;
第三方 mod 集成
- EndlessIDs:自动兼容——写
idExt后 EndlessIDs 的 NBT reader 会读到。 - NotEnoughIDs (NEID):自动兼容——同上。
- 无 gtnhlib 依赖。