tutorial:blockentity_modify_data
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorial:blockentity_modify_data [2025/04/01 12:46] – [Modify BlockEntity data] solidblock | tutorial:blockentity_modify_data [2025/06/11 16:07] (current) – update solidblock | ||
|---|---|---|---|
| Line 31: | Line 31: | ||
| // returns an empty compound when the field does not exist: | // returns an empty compound when the field does not exist: | ||
| NbtCompound config = nbt.getCompound(" | NbtCompound config = nbt.getCompound(" | ||
| + | </ | ||
| Besides, since 1.21.5, the whole NBT compound and its fields can be decoded directly with [[codec]]s, such as: | Besides, since 1.21.5, the whole NBT compound and its fields can be decoded directly with [[codec]]s, such as: | ||
| Line 45: | Line 46: | ||
| Optional< | Optional< | ||
| </ | </ | ||
| + | |||
| + | Besides, since 1.21.5, lists support mixing elements of different types. In previous versions, mixing elements of different types in a list results in exceptions. | ||
| More information about NBT changes, see [[https:// | More information about NBT changes, see [[https:// | ||
| ===== Serializing Data ===== | ===== Serializing Data ===== | ||
| - | If you want to store any data in your '' | + | If you want to store any data in your '' |
| - | '' | + | '' |
| In older versions, it was very important to call '' | In older versions, it was very important to call '' | ||
| Line 57: | Line 60: | ||
| Knowing this, the example below demonstrates saving an integer from your '' | Knowing this, the example below demonstrates saving an integer from your '' | ||
| + | //For versions 1.21.5 and before:// | ||
| <code java DemoBlockEntity.java> | <code java DemoBlockEntity.java> | ||
| public class DemoBlockEntity extends BlockEntity { | public class DemoBlockEntity extends BlockEntity { | ||
| Line 71: | Line 75: | ||
| public void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registries) { | public void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registries) { | ||
| // Save the current value of the number to the nbt | // Save the current value of the number to the nbt | ||
| - | nbt.putInt(" | ||
| - | |||
| super.writeNbt(nbt, | super.writeNbt(nbt, | ||
| + | nbt.putInt(" | ||
| } | } | ||
| } | } | ||
| </ | </ | ||
| - | In order to read the data, you will also need to override '' | + | //For versions 1.21.6 and after:// |
| + | <code java> | ||
| + | @Override | ||
| + | public void writeData(WriteView view) { | ||
| + | super.writeData(view); | ||
| + | // Save the current value of the number to the nbt | ||
| + | view.putInt(" | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | In order to read the data, you will also need to override '' | ||
| + | //For versions 1.21.5 and before:// | ||
| <code java> | <code java> | ||
| // Deserialize the BlockEntity | // Deserialize the BlockEntity | ||
| Line 87: | Line 102: | ||
| | | ||
| number = nbt.getInt(" | number = nbt.getInt(" | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | //For versions 1.21.6 and after:// | ||
| + | <code java> | ||
| + | @Override | ||
| + | public void readData(ReadView view) { | ||
| + | super.readData(ReadView view); | ||
| + | | ||
| + | number = view.getInt(" | ||
| } | } | ||
| </ | </ | ||
| Line 108: | Line 133: | ||
| @Override | @Override | ||
| - | public NbtCompound toInitialChunkDataNbt(RegistryWrapper.WrapperLookup | + | public NbtCompound toInitialChunkDataNbt(RegistryWrapper.WrapperLookup |
| - | return createNbt(registryLookup); | + | return createNbt(registries); |
| } | } | ||
| </ | </ | ||
| Line 122: | Line 147: | ||
| } | } | ||
| + | // The following two methods are changed since 1.21.6, see the examples above. | ||
| @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 222: | Line 247: | ||
| } | } | ||
| + | // For versions 1.21.5 and before: | ||
| @Override | @Override | ||
| public void removeFromCopiedStackNbt(NbtCompound nbt) { | public void removeFromCopiedStackNbt(NbtCompound nbt) { | ||
| nbt.remove(" | nbt.remove(" | ||
| + | } | ||
| + | | ||
| + | // For versions 1.21.6 and after: | ||
| + | @Override | ||
| + | public void removeFromCopiedStackData(WriteView view) { | ||
| + | view.remove(" | ||
| } | } | ||
| </ | </ | ||
| - | The usage of '' | + | The usage of '' |
tutorial/blockentity_modify_data.1743511587.txt.gz · Last modified: 2025/04/01 12:46 by solidblock