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 [2023/04/10 11:41] – Removed markDirty() from the final code snippet lunathelemontutorial:persistent_states [2023/09/07 12:39] – Remove section: Long Term Storage (Client) - It made no sense. modmuss50
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 =====
tutorial/persistent_states.txt · Last modified: 2024/04/25 14:06 by mayaqq