Examples
Store your own data
Save and update a versioned JSON record per player with PersistenceService and compare-and-set, without blocking the server.
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import net.sofycore.api.storage.PersistenceService;
import net.sofycore.api.storage.StoredRecord;
final class KillsStore {
private static final String NS = "myplugin-kills";
private final PersistenceService store;
KillsStore(PersistenceService store) { this.store = store; }
CompletableFuture<Boolean> increment(UUID player) {
String key = player.toString();
return store.getAsync(NS, key).thenCompose(record -> {
long revision = record.map(StoredRecord::revision).orElse(-1L);
int kills = record.map(r -> Integer.parseInt(new String(r.payload(), StandardCharsets.UTF_8))).orElse(0);
byte[] next = String.valueOf(kills + 1).getBytes(StandardCharsets.UTF_8);
return store.putAsync(NS, key, revision, next); // false = someone else wrote first
});
}
}If putAsync returns false, read again and retry a limited number of times. Do not retry blindly after an I/O error: the write may have happened.