While creating a Mod, I discovered that when using the ItemStack.copy() method to duplicate items, the stacking components of the items actually undergo a shallow copy, which may lead to potential issues.
Here are the steps to replicate the issue:
Launch the game in debug mode using IDEA and switch to creative mode.
Obtain a Shulker Box containing items.
Open a GUI (not the player's inventory).
Use the mouse middle button to duplicate the item.
Set a breakpoint in the program and inspect the player's open inventory.
You will notice that the components of the items in the container have the same address values.
Note: Please do not replicate this issue in the player's inventory GUI. Instead, replicate it in the GUI of a chest container.
在制作Mod时,我发现使用ItemStack.copy()方法复制物品时,物品的堆叠组件实际上进行的是浅拷贝,这可能会带来一些潜在的问题。
1.用IDEA的debug启动游戏并切换到创造模式
2.拿一个装有物品的潜影盒
3.打开一个GUI(不要打开玩家的物品栏)
4.使用鼠标中键复制物品
5.在程序中打一个断点,然后查看玩家打开的物品栏
6.可以看到容器中物品的组件有相同的地址值
注:不要在玩家的物品栏GUI里复刻该问题,应该在一个箱子容器的GUI中复刻
How does this affect vanilla gameplay?
I'm pretty sure this is intended. In vanilla all data components are immutable which means they can be shallowly copied without problems. When modding it is your responsibility to keep the components immutable.