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 revision
Previous revision
Next revisionBoth sides next revision
tutorial:persistent_states [2022/12/22 02:54] – Fixed bug in code, and added imports jmanc3tutorial:persistent_states [2023/09/07 12:39] – Remove section: Long Term Storage (Client) - It made no sense. modmuss50
Line 114: Line 114:
                 ServerState::new,                 ServerState::new,
                 "YOUR_UNIQUE_MOD_ID (PLEASE CHANGE ME!!!)"));                  "YOUR_UNIQUE_MOD_ID (PLEASE CHANGE ME!!!)")); 
- +                
-        serverState.markDirty(); // YOU MUST DO THIS!!!! Or data wont be saved correctly. +
-    +
         return serverState;         return serverState;
     }     }
Line 122: Line 120:
 </code> </code>
  
-One thing to note is the line ''serverState.markDirty();''Had we not done that, Minecraft wouldn't ever save any changes we make to the state. Here's how to use it:+In order to signal that the server should save the state of the object at the next write cycle you must call ''markDirty()'' after you have finished modifying any variables you wish to change. 
 +Failing to do this will mean the server may not save the nbt data to disk so it cannot be guaranteed that when the nbt data is retrieved again after the server has restarted it will be the same.
  
 <code java> <code java>
 ServerState serverState = ServerState.getServerState(server); ServerState serverState = ServerState.getServerState(server);
 System.out.println("The server has seen this many furnaces crafted: " + serverState.totalFurnacesCrafted); System.out.println("The server has seen this many furnaces crafted: " + serverState.totalFurnacesCrafted);
 +serverState.markDirty();
 </code> </code>
  
Line 238: Line 238:
                 ServerState::new,                 ServerState::new,
                 "YOUR_UNIQUE_MOD_ID (PLEASE CHANGE ME!!!)");                 "YOUR_UNIQUE_MOD_ID (PLEASE CHANGE ME!!!)");
- +                
-        serverState.markDirty(); // YOU MUST DO THIS!!!! Or data wont be saved correctly. +
         return serverState;         return serverState;
     }     }
Line 278: Line 276:
  
 ----- -----
- 
-===== Long Term Storage (Client) ==== 
- 
-If you're sure the data should live on the client, but still need it to persist across sessions (load/unload world) then we can use a utility function very similar to the one we created for our server. The only difference between being that we created a new type ''ClientState'' to return. Look at the ''PlayerState'' we wrote earlier to see how to write ''ClientState'' class. 
- 
-  * Note because of the use ''MinecraftClient.getInstance()'', this function should only ever be called by the client. Never the server. 
- 
-<code java> 
-public static ClientState getPlayerState() { 
-    ClientPlayerEntity player = MinecraftClient.getInstance().player; 
-     
-    PersistentStateManager persistentStateManager = player.world.getServer() 
-        .getWorld(World.OVERWORLD).getPersistentStateManager(); 
- 
-    ClientState clientState = persistentStateManager.getOrCreate( 
-        ClientState::createFromNbt, 
-        ClientState::new, 
-        player.getUuidAsString()); // We use the UUID of the player as the key so they can retreive their data later 
- 
-    return clientState; 
-} 
-</code> 
- 
  
 ===== More Involved Player State ===== ===== More Involved Player State =====
Line 404: Line 379:
                 ServerState::new,                 ServerState::new,
                 "YOUR_UNIQUE_MOD_ID (PLEASE CHANGE ME!!!)");                 "YOUR_UNIQUE_MOD_ID (PLEASE CHANGE ME!!!)");
- 
-        serverState.markDirty(); 
  
         return serverState;         return serverState;
tutorial/persistent_states.txt · Last modified: 2024/04/25 14:06 by mayaqq