User Tools

Site Tools


tutorial:persistent_states

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
tutorial:persistent_states [2023/09/28 05:37] – Add missing imports jmanc3tutorial:persistent_states [2023/09/28 05:52] – Dropped un-needed DataFixType parameter and made type singleton 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> type = new Type<>(
 +            StateSaverAndLoader::new, // If there's no 'StateSaverAndLoader' yet create one
 +            StateSaverAndLoader::createFromNbt, // If there is a 'StateSaverAndLoader' NBT, parse it with 'createFromNbt'
 +            null // Supposed to be an 'DataFixTypes' enum, but we can just pass null
 +    );
  
     public static StateSaverAndLoader getServerState(MinecraftServer server) {     public static StateSaverAndLoader getServerState(MinecraftServer server) {
Line 155: Line 160:
         // stores it inside the 'PersistentStateManager'. The subsequent calls to 'getOrCreate' pass in the saved         // stores it inside the 'PersistentStateManager'. The subsequent calls to 'getOrCreate' pass in the saved
         // 'StateSaverAndLoader' NBT on disk to our function 'StateSaverAndLoader::createFromNbt'.         // 'StateSaverAndLoader' NBT on disk to our function 'StateSaverAndLoader::createFromNbt'.
-        // +        StateSaverAndLoader state = persistentStateManager.getOrCreate(type, ExampleMod.MOD_ID);
-        // (Note: 'DataFixTypes.LEVEL' is used as it's a required parameter but the decision to use 'DataFixTypes.LEVEL' specifically is arbitrary.) +
-        StateSaverAndLoader state = persistentStateManager.getOrCreate( +
-                new Type<>(StateSaverAndLoader::new, StateSaverAndLoader::createFromNbt, DataFixTypes.LEVEL), +
-                ExampleMod.MOD_ID);+
  
         // If state is not marked dirty, when Minecraft closes, 'writeNbt' won't be called and therefore nothing will be saved.         // If state is not marked dirty, when Minecraft closes, 'writeNbt' won't be called and therefore nothing will be saved.
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> type = new Type<>(
 +            StateSaverAndLoader::new, // If there's no 'StateSaverAndLoader' yet create one
 +            StateSaverAndLoader::createFromNbt, // If there is a 'StateSaverAndLoader' NBT, parse it with 'createFromNbt'
 +            null // Supposed to be an 'DataFixTypes' enum, but we can just pass null
 +    );
  
     public static StateSaverAndLoader getServerState(MinecraftServer server) {     public static StateSaverAndLoader getServerState(MinecraftServer server) {
Line 206: Line 212:
         // stores it inside the 'PersistentStateManager'. The subsequent calls to 'getOrCreate' pass in the saved         // stores it inside the 'PersistentStateManager'. The subsequent calls to 'getOrCreate' pass in the saved
         // 'StateSaverAndLoader' NBT on disk to our function 'StateSaverAndLoader::createFromNbt'.         // 'StateSaverAndLoader' NBT on disk to our function 'StateSaverAndLoader::createFromNbt'.
-        // +        StateSaverAndLoader state = persistentStateManager.getOrCreate(type, ExampleMod.MOD_ID);
-        // (Note: 'DataFixTypes.LEVEL' is used as it's a required parameter but the decision to use 'DataFixTypes.LEVEL' specifically is arbitrary.) +
-        StateSaverAndLoader state = persistentStateManager.getOrCreate( +
-                new Type<>(StateSaverAndLoader::new, StateSaverAndLoader::createFromNbt, DataFixTypes.LEVEL), +
-                ExampleMod.MOD_ID);+
  
         // If state is not marked dirty, when Minecraft closes, 'writeNbt' won't be called and therefore nothing will be saved.         // If state is not marked dirty, when Minecraft closes, 'writeNbt' won't be called and therefore nothing will be saved.
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> type = new Type<>(
 +            StateSaverAndLoader::new, // If there's no 'StateSaverAndLoader' yet create one
 +            StateSaverAndLoader::createFromNbt, // If there is a 'StateSaverAndLoader' NBT, parse it with 'createFromNbt'
 +            null // Supposed to be an 'DataFixTypes' enum, but we can just pass null
 +    );
  
     public static StateSaverAndLoader getServerState(MinecraftServer server) {     public static StateSaverAndLoader getServerState(MinecraftServer server) {
Line 509: Line 516:
         // stores it inside the 'PersistentStateManager'. The subsequent calls to 'getOrCreate' pass in the saved         // stores it inside the 'PersistentStateManager'. The subsequent calls to 'getOrCreate' pass in the saved
         // 'StateSaverAndLoader' NBT on disk to our function 'StateSaverAndLoader::createFromNbt'.         // 'StateSaverAndLoader' NBT on disk to our function 'StateSaverAndLoader::createFromNbt'.
-        // +        StateSaverAndLoader state = persistentStateManager.getOrCreate(type, ExampleMod.MOD_ID);
-        // (Note: 'DataFixTypes.LEVEL' is used as it's a required parameter but the decision to use 'DataFixTypes.LEVEL' specifically is arbitrary.) +
-        StateSaverAndLoader state = persistentStateManager.getOrCreate( +
-                new Type<>(StateSaverAndLoader::new, StateSaverAndLoader::createFromNbt, DataFixTypes.LEVEL), +
-                ExampleMod.MOD_ID);+
  
         // If state is not marked dirty, when Minecraft closes, 'writeNbt' won't be called and therefore nothing will be saved.         // If state is not marked dirty, when Minecraft closes, 'writeNbt' won't be called and therefore nothing will be saved.
Line 674: Line 677:
 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 737:
         return state;         return state;
     }     }
 +
 +    private static Type<StateSaverAndLoader> type = new Type<>(
 +            StateSaverAndLoader::new, // If there's no 'StateSaverAndLoader' yet create one
 +            StateSaverAndLoader::createFromNbt, // If there is a 'StateSaverAndLoader' NBT, parse it with 'createFromNbt'
 +            null // Supposed to be an 'DataFixTypes' enum, but we can just pass null
 +    );
  
     public static StateSaverAndLoader getServerState(MinecraftServer server) {     public static StateSaverAndLoader getServerState(MinecraftServer server) {
Line 743: Line 751:
         // stores it inside the 'PersistentStateManager'. The subsequent calls to 'getOrCreate' pass in the saved         // stores it inside the 'PersistentStateManager'. The subsequent calls to 'getOrCreate' pass in the saved
         // 'StateSaverAndLoader' NBT on disk to our function 'StateSaverAndLoader::createFromNbt'.         // 'StateSaverAndLoader' NBT on disk to our function 'StateSaverAndLoader::createFromNbt'.
-        // +        StateSaverAndLoader state = persistentStateManager.getOrCreate(type, ExampleMod.MOD_ID);
-        // (Note: 'DataFixTypes.LEVEL' is used as it's a required parameter but the decision to use 'DataFixTypes.LEVEL' specifically is arbitrary.) +
-        StateSaverAndLoader state = persistentStateManager.getOrCreate( +
-                new Type<>(StateSaverAndLoader::new, StateSaverAndLoader::createFromNbt, DataFixTypes.LEVEL), +
-                ExampleMod.MOD_ID);+
  
         // If state is not marked dirty, when Minecraft closes, 'writeNbt' won't be called and therefore nothing will be saved.         // If state is not marked dirty, when Minecraft closes, 'writeNbt' won't be called and therefore nothing will be saved.
tutorial/persistent_states.txt · Last modified: 2024/04/25 14:06 by mayaqq