9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-24 17:39:30 +00:00

refactor(bukkit): 优化物品操作反射机制

- 使用 Reflections 类中已有的 method$NonNullList$set 方法替代动态获取的 set 方法
- 移除冗余的异常捕获 NoSuchMethodException- 新增 NonNullList 相关的类和方法引用
This commit is contained in:
jhqwqmc
2025-02-12 05:42:28 +08:00
parent 7c455fea53
commit eb0c525dd7
2 changed files with 17 additions and 3 deletions

View File

@@ -25,10 +25,9 @@ public class ItemUtils {
.get(nmsInventory$getInventory);
Object nmsItemStack = Reflections.method$CraftItemStack$asNMSCopy
.invoke(null, itemStack);
nmsInventory$items.getClass()
.getMethod("set", int.class, Object.class)
Reflections.method$NonNullList$set
.invoke(nmsInventory$items, slot, nmsItemStack);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (InvocationTargetException | IllegalAccessException e) {
CraftEngine.instance().logger().warn("Failed to set item", e);
}
}

View File

@@ -3916,4 +3916,19 @@ public class Reflections {
INVENTORY$ITEMS
)
);
public static final Class<?> clazz$NonNullList = requireNonNull(
ReflectionUtils.getClazz(
BukkitReflectionUtils.assembleMCClass("core.NonNullList")
)
);
public static final Method method$NonNullList$set = requireNonNull(
ReflectionUtils.getMethod(
clazz$NonNullList,
Object.class,
int.class,
Object.class
)
);
}