tutorial:persistent_states
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:persistent_states [2022/12/22 02:54] – Fixed bug in code, and added imports jmanc3 | tutorial:persistent_states [2023/09/07 12:39] – Remove section: Long Term Storage (Client) - It made no sense. modmuss50 | ||
---|---|---|---|
Line 114: | Line 114: | ||
ServerState:: | ServerState:: | ||
" | " | ||
- | + | | |
- | serverState.markDirty(); | + | |
- | | + | |
return serverState; | return serverState; | ||
} | } | ||
Line 122: | Line 120: | ||
</ | </ | ||
- | One thing to note is the line '' | + | In order to signal that the server should save the state of the object at the next write cycle you must call '' |
+ | 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(" | System.out.println(" | ||
+ | serverState.markDirty(); | ||
</ | </ | ||
Line 238: | Line 238: | ||
ServerState:: | ServerState:: | ||
" | " | ||
- | + | | |
- | serverState.markDirty(); | + | |
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/ | ||
- | |||
- | * Note because of the use '' | ||
- | |||
- | <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:: | ||
- | ClientState:: | ||
- | player.getUuidAsString()); | ||
- | |||
- | return clientState; | ||
- | } | ||
- | </ | ||
- | |||
===== More Involved Player State ===== | ===== More Involved Player State ===== | ||
Line 404: | Line 379: | ||
ServerState:: | ServerState:: | ||
" | " | ||
- | |||
- | serverState.markDirty(); | ||
return serverState; | return serverState; |
tutorial/persistent_states.txt · Last modified: 2024/04/25 14:06 by mayaqq