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/04/10 11:42] – Removed more markDirty() calls lunathelemon
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 404: Line 402:
                 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