Skip to content

Commit d890b74

Browse files
committed
Add persistPlayerData for OfflinePlayer
Introduced a new method to handle data persistence and manipulation for `OfflinePlayer`. Ensures separation of logic from the existing `Player`-based method while maintaining consistent functionality.
1 parent b07d9f5 commit d890b74

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

api/src/main/java/net/thenextlvl/perworlds/WorldGroup.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,23 @@ default boolean isLoadingData(Player player) {
366366
*
367367
* @param player the player whose data is to be persisted and modified
368368
* @param data a {@link Consumer} that manipulates the {@link PlayerData} object
369+
* @see #persistPlayerData(OfflinePlayer, Consumer)
369370
*/
370371
@Contract(mutates = "io")
371372
void persistPlayerData(Player player, Consumer<PlayerData> data);
373+
374+
/**
375+
* Persists and modifies the data of the specified offline player using the provided consumer.
376+
* The method allows manipulation through the consumer and ensures the updated data is saved to persistent storage.
377+
* <p>
378+
* Opposed to {@link #persistPlayerData(Player, Consumer)},
379+
* this method does not try to resolve the player's data even if they are online.
380+
*
381+
* @param player the offline player whose data is to be persisted and modified
382+
* @param data a {@link Consumer} that manipulates the {@link PlayerData} object
383+
* @see #persistPlayerData(Player, Consumer)
384+
* @since 1.0.3
385+
*/
386+
@Contract(mutates = "io")
387+
void persistPlayerData(OfflinePlayer player, Consumer<PlayerData> data);
372388
}

src/main/java/net/thenextlvl/perworlds/group/PaperWorldGroup.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,13 @@ public void persistPlayerData(Player player, Consumer<PlayerData> data) {
474474
writePlayerData(player, playerData);
475475
}
476476

477+
@Override
478+
public void persistPlayerData(OfflinePlayer player, Consumer<PlayerData> data) {
479+
var playerData = new PaperPlayerData(player.getUniqueId(), this);
480+
data.accept(playerData);
481+
writePlayerData(player, playerData);
482+
}
483+
477484
private Optional<PlayerData> readPlayerData(OfflinePlayer player, Path file) throws IOException {
478485
return readFile(file, file.resolveSibling(file.getFileName() + "_old"), PaperPlayerData.class)
479486
.map(paperPlayerData -> paperPlayerData.finalize(player, this));

0 commit comments

Comments
 (0)