tutorial:persistent_states
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
tutorial:persistent_states [2023/09/28 05:37] – Add missing imports jmanc3 | tutorial:persistent_states [2024/01/11 06:44] – typo jmanc3 | ||
---|---|---|---|
Line 104: | Line 104: | ||
import net.minecraft.world.PersistentStateManager; | import net.minecraft.world.PersistentStateManager; | ||
import net.minecraft.world.World; | import net.minecraft.world.World; | ||
- | import net.minecraft.datafixer.DataFixTypes; | ||
public class StateSaverAndLoader extends PersistentState { | public class StateSaverAndLoader extends PersistentState { | ||
Line 147: | Line 146: | ||
// ... (Previously written code) | // ... (Previously written code) | ||
+ | |||
+ | private static Type< | ||
+ | StateSaverAndLoader:: | ||
+ | StateSaverAndLoader:: | ||
+ | null // Supposed to be an ' | ||
+ | ); | ||
public static StateSaverAndLoader getServerState(MinecraftServer server) { | public static StateSaverAndLoader getServerState(MinecraftServer server) { | ||
Line 155: | Line 160: | ||
// stores it inside the ' | // stores it inside the ' | ||
// ' | // ' | ||
- | | + | StateSaverAndLoader state = persistentStateManager.getOrCreate(type, ExampleMod.MOD_ID); |
- | // (Note: ' | + | |
- | | + | |
- | new Type<> | + | |
- | | + | |
// If state is not marked dirty, when Minecraft closes, ' | // If state is not marked dirty, when Minecraft closes, ' | ||
Line 181: | Line 182: | ||
import net.minecraft.world.PersistentStateManager; | import net.minecraft.world.PersistentStateManager; | ||
import net.minecraft.world.World; | import net.minecraft.world.World; | ||
- | import net.minecraft.datafixer.DataFixTypes; | ||
public class StateSaverAndLoader extends PersistentState { | public class StateSaverAndLoader extends PersistentState { | ||
Line 198: | Line 198: | ||
return state; | return state; | ||
} | } | ||
+ | |||
+ | private static Type< | ||
+ | StateSaverAndLoader:: | ||
+ | StateSaverAndLoader:: | ||
+ | null // Supposed to be an ' | ||
+ | ); | ||
public static StateSaverAndLoader getServerState(MinecraftServer server) { | public static StateSaverAndLoader getServerState(MinecraftServer server) { | ||
Line 206: | Line 212: | ||
// stores it inside the ' | // stores it inside the ' | ||
// ' | // ' | ||
- | | + | StateSaverAndLoader state = persistentStateManager.getOrCreate(type, ExampleMod.MOD_ID); |
- | // (Note: ' | + | |
- | | + | |
- | new Type<> | + | |
- | | + | |
// If state is not marked dirty, when Minecraft closes, ' | // If state is not marked dirty, when Minecraft closes, ' | ||
Line 457: | Line 459: | ||
import net.minecraft.world.PersistentStateManager; | import net.minecraft.world.PersistentStateManager; | ||
import net.minecraft.world.World; | import net.minecraft.world.World; | ||
- | import net.minecraft.datafixer.DataFixTypes; | ||
import java.util.HashMap; | import java.util.HashMap; | ||
Line 501: | Line 502: | ||
return state; | return state; | ||
} | } | ||
+ | |||
+ | private static Type< | ||
+ | StateSaverAndLoader:: | ||
+ | StateSaverAndLoader:: | ||
+ | null // Supposed to be an ' | ||
+ | ); | ||
public static StateSaverAndLoader getServerState(MinecraftServer server) { | public static StateSaverAndLoader getServerState(MinecraftServer server) { | ||
Line 509: | Line 516: | ||
// stores it inside the ' | // stores it inside the ' | ||
// ' | // ' | ||
- | | + | StateSaverAndLoader state = persistentStateManager.getOrCreate(type, ExampleMod.MOD_ID); |
- | // (Note: ' | + | |
- | | + | |
- | new Type<> | + | |
- | | + | |
// If state is not marked dirty, when Minecraft closes, ' | // If state is not marked dirty, when Minecraft closes, ' | ||
Line 538: | Line 541: | ||
Running the client now, all our player-specific data is correctly saved. | Running the client now, all our player-specific data is correctly saved. | ||
- | | + | ==== Important Caveat ==== |
+ | |||
+ | | ||
Just remember if you add new fields to '' | Just remember if you add new fields to '' | ||
Line 643: | Line 648: | ||
* Note: The '' | * Note: The '' | ||
- | | + | ==== Important Caveat ==== |
+ | |||
+ | | ||
====== More Involved Player Data ====== | ====== More Involved Player Data ====== | ||
Line 674: | Line 681: | ||
import net.minecraft.world.PersistentStateManager; | import net.minecraft.world.PersistentStateManager; | ||
import net.minecraft.world.World; | import net.minecraft.world.World; | ||
- | import net.minecraft.datafixer.DataFixTypes; | ||
import java.util.HashMap; | import java.util.HashMap; | ||
Line 735: | Line 741: | ||
return state; | return state; | ||
} | } | ||
+ | |||
+ | private static Type< | ||
+ | StateSaverAndLoader:: | ||
+ | StateSaverAndLoader:: | ||
+ | null // Supposed to be an ' | ||
+ | ); | ||
public static StateSaverAndLoader getServerState(MinecraftServer server) { | public static StateSaverAndLoader getServerState(MinecraftServer server) { | ||
Line 743: | Line 755: | ||
// stores it inside the ' | // stores it inside the ' | ||
// ' | // ' | ||
- | | + | StateSaverAndLoader state = persistentStateManager.getOrCreate(type, ExampleMod.MOD_ID); |
- | // (Note: ' | + | |
- | | + | |
- | new Type<> | + | |
- | | + | |
// If state is not marked dirty, when Minecraft closes, ' | // If state is not marked dirty, when Minecraft closes, ' |
tutorial/persistent_states.txt · Last modified: 2024/04/25 14:06 by mayaqq