第88批:物品堆叠改用数据组件写入(26.x max_stack_size 合并逻辑读组件,meta 兼容层无效)
Auto Release / build (push) Successful in 2m37s
Auto Release / build (push) Successful in 2m37s
This commit is contained in:
@@ -489,21 +489,46 @@ public class StackConfigManager implements Listener {
|
||||
* 对 ItemStack 应用自定义堆叠上限。
|
||||
* 仅在配置了堆叠数量时才修改,避免副作用。
|
||||
*
|
||||
* ★ 第88批根因(2026-11-05):26.x 中 max_stack_size 已是数据组件
|
||||
* (vanilla DataComponents.MAX_STACK_SIZE,位于 ItemStack.components)。
|
||||
* Paper 官方文档: "you can use ItemStack#setData along with the
|
||||
* MAX_STACK_SIZE data component. This faithfully represents how the
|
||||
* max stack size is actually handled internally."
|
||||
* 旧路径 meta.setMaxStackSize 只写进 Paper.CustomData 私有标签,
|
||||
* vanilla 合并逻辑 (shift-click/拖拽) 读的是组件 -> 读到类型默认 64。
|
||||
* 表现为日志(CraftItemStack 读 Paper.CustomData)max=99 但游戏内合并仍 64。
|
||||
*
|
||||
* @return true = 已修改;false = 未配置或无需修改
|
||||
*/
|
||||
public boolean applyStackSize(ItemStack item) {
|
||||
if (item == null || item.getType() == Material.AIR) return false;
|
||||
int max = getMaxStackSize(item.getType());
|
||||
if (max > 0 && item.getMaxStackSize() != max) {
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (meta != null) {
|
||||
// ★ 第70批:防御性 clamp,防止任何路径绕过 resolveAndAdd 直接写入
|
||||
meta.setMaxStackSize(Math.max(1, Math.min(99, max)));
|
||||
item.setItemMeta(meta);
|
||||
}
|
||||
return true;
|
||||
if (max <= 0) return false;
|
||||
int target = Math.max(1, Math.min(99, max));
|
||||
// 第70批:防御性 clamp 已并入 target
|
||||
Integer current = null;
|
||||
try {
|
||||
current = item.getData(io.papermc.paper.datacomponent.DataComponentTypes.MAX_STACK_SIZE);
|
||||
} catch (Exception ignored) {
|
||||
// 对象无 setData 能力时走下面的 meta 兜底
|
||||
}
|
||||
return false;
|
||||
if (current != null && current.intValue() == target) return false; // 组件已是目标值
|
||||
// 第一优先:直接写数据组件(vanilla 合并逻辑真正读取的位置)
|
||||
try {
|
||||
item.setData(io.papermc.paper.datacomponent.DataComponentTypes.MAX_STACK_SIZE, target);
|
||||
} catch (Exception e) {
|
||||
// 兜底:走 meta 路径(兼容未实现 setData 的 Paper 版本)
|
||||
try {
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (meta != null) {
|
||||
meta.setMaxStackSize(target);
|
||||
item.setItemMeta(meta);
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
// 两条路都失败则视为写入失败
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// ===================== 事件监听 =====================
|
||||
|
||||
Reference in New Issue
Block a user