实体辅助
基本信息
| 属性 | 值 |
|---|---|
| 类 | com.github.lunatrius.core.entity.EntityHelper |
| 成员形式 | 全部 public static,无构造函数、无实例状态 |
| 依赖 | net.minecraft.entity.Entity、net.minecraft.inventory.IInventory、net.minecraft.item.Item、net.minecraft.item.ItemStack、Vector3f、Vector3i |
[!NOTE] 尽管包名为
entity,本类不注册也不生成任何实体。它只是一组读取工具:统计容器里的物品数量、把实体坐标转成向量。
常量
| 常量 | 类型 | 值 |
|---|---|---|
WILDMARK |
public final static int |
-1 |
WILDMARK 作为「忽略 metadata」的哨兵值,与原版 ItemStack 的 damage 语义对齐(取值域 0~32767,故 -1 不会与真实值冲突)。
容器物品统计
| 签名 | 行为 |
|---|---|
public static int getItemCountInInventory(IInventory inventory, Item item) |
委托到三参重载,metadata 传 WILDMARK |
public static int getItemCountInInventory(IInventory inventory, Item item, int itemDamage) |
遍历 slot 从 0 到 inventory.getSizeInventory() - 1,当 itemStack != null && itemStack.getItem() == item && (itemDamage == WILDMARK || itemDamage == itemStack.getItemDamage()) 时累加 itemStack.stackSize,返回总和 |
匹配条件三段:
| 条件 | 说明 |
|---|---|
itemStack != null |
跳过空格 |
itemStack.getItem() == item |
按对象引用比较,不是按 ItemStack.isItemEqual,因此不支持任何 NBT / 附魔差异判定 |
itemDamage == WILDMARK || itemDamage == itemStack.getItemDamage() |
传 WILDMARK 则忽略 metadata,否则要求精确相等 |
[!NOTE] 整个方法只读不写,不做任何扣除;也不检查
IInventory是否实现了IItemHandler。返回值是int,超出 21 亿会在累加时溢出。
实体坐标转换
| 签名 | 返回 | 计算方式 |
|---|---|---|
public static Vector3f getVector3fFromEntity(Entity entity) |
new Vector3f(...) |
(float) entity.posX / posY / posZ 直接强转 |
public static Vector3f getVector3fFromEntity(Entity entity, Vector3f vec) |
vec.set(...) |
同上,但写入传入向量(零分配) |
public static Vector3i getVector3iFromEntity(Entity entity) |
new Vector3i(...) |
(int) Math.floor(entity.posX / posY / posZ) |
public static Vector3i getVector3iFromEntity(Entity entity, Vector3i vec) |
vec.set(...) |
同上,写入传入向量 |
[!IMPORTANT] 整数版本用
Math.floor而非强制转换,因此负坐标向下取整(posX = -0.5→-1),与原版MathHelper.floor_double行为一致。浮点版本则是普通的(float)转换,保留小数。
[!NOTE] 四个方法都不考虑旋转、包围盒或渲染插值(
lastTickPosX等),读取的是实体当前逻辑坐标。