zh_cn:tutorial:blockentity_modify_data
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
zh_cn:tutorial:blockentity_modify_data [2025/04/01 12:48] – [关于 NbtCompound 的注意事项] solidblock | zh_cn:tutorial:blockentity_modify_data [2025/06/11 16:02] (current) – [使用数据组件] solidblock | ||
---|---|---|---|
Line 52: | Line 52: | ||
===== 序列化数据 ===== | ===== 序列化数据 ===== | ||
- | 如果想在 '' | + | 如果想在 '' |
- | '' | + | '' |
在旧版本中,调用 '' | 在旧版本中,调用 '' | ||
Line 60: | Line 60: | ||
知道了这一点,下面的示例演示了如何将 '' | 知道了这一点,下面的示例演示了如何将 '' | ||
+ | //在 1.21.5 及之前的版本:// | ||
<code java DemoBlockEntity.java> | <code java DemoBlockEntity.java> | ||
public class DemoBlockEntity extends BlockEntity { | public class DemoBlockEntity extends BlockEntity { | ||
Line 74: | Line 75: | ||
public void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registries) { | public void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registries) { | ||
// 将数字的当前值保存到 nbt | // 将数字的当前值保存到 nbt | ||
- | nbt.putInt(" | ||
- | |||
super.writeNbt(nbt, | super.writeNbt(nbt, | ||
+ | nbt.putInt(" | ||
} | } | ||
} | } | ||
</ | </ | ||
- | 为了以后读取数据,您还需要覆盖 '' | + | //在 1.21.6 及之后的版本:// |
+ | <code java> | ||
+ | @Override | ||
+ | public void writeData(WriteView view) { | ||
+ | super.writeData(view); | ||
+ | // 将数字的当前值保存到 nbt | ||
+ | view.putInt(" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 为了以后读取数据,您还需要覆盖 '' | ||
+ | |||
+ | //在 1.21.5 及之前的版本:// | ||
<code java> | <code java> | ||
// 反序列化方块实体 | // 反序列化方块实体 | ||
Line 90: | Line 102: | ||
| | ||
number = nbt.getInt(" | number = nbt.getInt(" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | //在 1.21.6 及之后的版本:// | ||
+ | <code java> | ||
+ | @Override | ||
+ | public void readData(ReadView view) { | ||
+ | super.readData(ReadView view); | ||
+ | | ||
+ | number = view.getInt(" | ||
} | } | ||
</ | </ | ||
Line 111: | Line 133: | ||
@Override | @Override | ||
- | public NbtCompound toInitialChunkDataNbt() { | + | public NbtCompound toInitialChunkDataNbt(RegistryWrapper.WrapperLookup registries) { |
- | return createNbt(); | + | return createNbt(registries); |
} | } | ||
</ | </ | ||
Line 125: | Line 147: | ||
} | } | ||
+ | // 以下两个方法,在 1.21.6 中有所改变,参见上面的示例。 | ||
@Override | @Override | ||
public void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registries) { | public void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registries) { | ||
+ | super.writeNbt(nbt, | ||
nbt.putInt(" | nbt.putInt(" | ||
- | |||
- | super.writeNbt(nbt, | ||
} | } | ||
| | ||
Line 225: | Line 247: | ||
} | } | ||
+ | // 仅限 1.21.5 及之前的版本: | ||
@Override | @Override | ||
public void removeFromCopiedStackNbt(NbtCompound nbt) { | public void removeFromCopiedStackNbt(NbtCompound nbt) { | ||
nbt.remove(" | nbt.remove(" | ||
+ | } | ||
+ | | ||
+ | // 对于 1.21.6 及之后的版本: | ||
+ | @Override | ||
+ | public void removeFromCopiedStackData(WriteView view) { | ||
+ | view.remove(" | ||
} | } | ||
</ | </ | ||
- | '' | + | '' |
zh_cn/tutorial/blockentity_modify_data.1743511684.txt.gz · Last modified: 2025/04/01 12:48 by solidblock