tutorial:persistent_states
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorial:persistent_states [2025/03/13 14:52] – world.getServer() only once, harmonized MOD_ID lakazatong | tutorial:persistent_states [2025/11/18 03:33] (current) – cassiancc | ||
|---|---|---|---|
| Line 128: | Line 128: | ||
| public Integer totalDirtBlocksBroken = 0; | public Integer totalDirtBlocksBroken = 0; | ||
| - | |||
| - | @Override | ||
| - | public NbtCompound writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registries) { | ||
| - | nbt.putInt(" | ||
| - | return nbt; | ||
| - | } | ||
| } | } | ||
| </ | </ | ||
| - | Note: '' | + | Note: |
| - | + | ||
| - | * '' | + | |
| - | Next add the following functions | + | - |
| <code java> | <code java> | ||
| Line 148: | Line 140: | ||
| // ... (Previously written code) | // ... (Previously written code) | ||
| - | | + | |
| - | StateSaverAndLoader state = new StateSaverAndLoader(); | + | |
| - | state.totalDirtBlocksBroken = tag.getInt(" | + | |
| - | return state; | + | |
| } | } | ||
| - | + | ||
| - | | + | |
| - | | + | |
| - | state.totalDirtBlocksBroken = 0; | + | |
| - | return state; | + | |
| } | } | ||
| + | |||
| + | private int getTotalDirtBlocksBroken() { | ||
| + | return totalDirtBlocksBroken; | ||
| + | } | ||
| + | |||
| + | private static final Codec< | ||
| + | StateSaverAndLoader:: | ||
| + | StateSaverAndLoader:: | ||
| + | ); | ||
| } | } | ||
| </ | </ | ||
| - | |||
| - | First function does the opposite of '' | ||
| - | |||
| - | * Note: how we pull out the int we stored earlier with '' | ||
| - | |||
| - | Second function refreshing variables '' | ||
| - | |||
| Now we just need to add one more utility function which hooks everything up together. This function will take a '' | Now we just need to add one more utility function which hooks everything up together. This function will take a '' | ||
| Line 175: | Line 164: | ||
| // ... (Previously written code) | // ... (Previously written code) | ||
| - | private static final Type< | + | private static final PersistentStateType< |
| - | StateSaverAndLoader:: | + | (String) ExampleMod.MOD_ID, |
| - | | + | StateSaverAndLoader:: |
| + | | ||
| null // Supposed to be an ' | null // Supposed to be an ' | ||
| ); | ); | ||
| Line 188: | Line 178: | ||
| // The first time the following ' | // The first time the following ' | ||
| // stores it inside the ' | // stores it inside the ' | ||
| - | // ' | + | // ' |
| - | StateSaverAndLoader state = serverWorld.getPersistentStateManager().getOrCreate(type, ExampleMod.MOD_ID); | + | StateSaverAndLoader state = serverWorld.getPersistentStateManager().getOrCreate(type); |
| - | // If state is not marked dirty, | + | // If state is not marked dirty, nothing will be saved when Minecraft closes. |
| // Technically it's ' | // Technically it's ' | ||
| // of mod writers are just going to be confused when their data isn't being saved, and so it's best just to ' | // of mod writers are just going to be confused when their data isn't being saved, and so it's best just to ' | ||
| Line 206: | Line 196: | ||
| <code java> | <code java> | ||
| - | import | + | import |
| - | import net.minecraft.registry.RegistryWrapper; | + | |
| import net.minecraft.server.MinecraftServer; | import net.minecraft.server.MinecraftServer; | ||
| import net.minecraft.server.world.ServerWorld; | import net.minecraft.server.world.ServerWorld; | ||
| import net.minecraft.world.PersistentState; | import net.minecraft.world.PersistentState; | ||
| + | import net.minecraft.world.PersistentStateType; | ||
| import net.minecraft.world.World; | import net.minecraft.world.World; | ||
| Line 217: | Line 208: | ||
| public Integer totalDirtBlocksBroken = 0; | public Integer totalDirtBlocksBroken = 0; | ||
| - | | + | |
| - | public NbtCompound writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registries) { | + | |
| - | nbt.putInt(" | + | |
| - | return nbt; | + | |
| } | } | ||
| - | | + | |
| - | | + | |
| - | state.totalDirtBlocksBroken = tag.getInt(" | + | |
| - | return state; | + | |
| } | } | ||
| - | | + | |
| - | | + | |
| - | state.totalDirtBlocksBroken | + | |
| - | return state; | + | |
| } | } | ||
| - | private static final Type< | + | private static final Codec< |
| - | StateSaverAndLoader:: | + | StateSaverAndLoader:: |
| - | | + | StateSaverAndLoader:: |
| + | ); | ||
| + | |||
| + | private static final PersistentStateType< | ||
| + | (String) ExampleMod.MOD_ID, | ||
| + | StateSaverAndLoader:: | ||
| + | | ||
| null // Supposed to be an ' | null // Supposed to be an ' | ||
| ); | ); | ||
| Line 248: | Line 238: | ||
| // The first time the following ' | // The first time the following ' | ||
| // stores it inside the ' | // stores it inside the ' | ||
| - | // ' | + | // ' |
| - | StateSaverAndLoader state = serverWorld.getPersistentStateManager().getOrCreate(type, ExampleMod.MOD_ID); | + | StateSaverAndLoader state = serverWorld.getPersistentStateManager().getOrCreate(type); |
| - | // If state is not marked dirty, | + | // If state is not marked dirty, nothing will be saved when Minecraft closes. |
| // Technically it's ' | // Technically it's ' | ||
| // of mod writers are just going to be confused when their data isn't being saved, and so it's best just to ' | // of mod writers are just going to be confused when their data isn't being saved, and so it's best just to ' | ||
| Line 339: | Line 329: | ||
| </ | </ | ||
| - | Note: We create a '' | + | Note: We create a '' |
| Let's add a utility function to '' | Let's add a utility function to '' | ||
tutorial/persistent_states.1741877533.txt.gz · Last modified: 2025/03/13 14:52 by lakazatong