Skip to content

Commit 594edc7

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 9164e4b commit 594edc7

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
@@ -356,7 +356,23 @@ public interface WorldGroup {
356356
*
357357
* @param player the player whose data is to be persisted and modified
358358
* @param data a {@link Consumer} that manipulates the {@link PlayerData} object
359+
* @see #persistPlayerData(OfflinePlayer, Consumer)
359360
*/
360361
@Contract(mutates = "io")
361362
void persistPlayerData(Player player, Consumer<PlayerData> data);
363+
364+
/**
365+
* Persists and modifies the data of the specified offline player using the provided consumer.
366+
* The method allows manipulation through the consumer and ensures the updated data is saved to persistent storage.
367+
* <p>
368+
* Opposed to {@link #persistPlayerData(Player, Consumer)},
369+
* this method does not try to resolve the player's data even if they are online.
370+
*
371+
* @param player the offline player whose data is to be persisted and modified
372+
* @param data a {@link Consumer} that manipulates the {@link PlayerData} object
373+
* @see #persistPlayerData(Player, Consumer)
374+
* @since 1.0.3
375+
*/
376+
@Contract(mutates = "io")
377+
void persistPlayerData(OfflinePlayer player, Consumer<PlayerData> data);
362378
}

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

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

445+
@Override
446+
public void persistPlayerData(OfflinePlayer player, Consumer<PlayerData> data) {
447+
var playerData = new PaperPlayerData(player.getUniqueId(), this);
448+
data.accept(playerData);
449+
writePlayerData(player, playerData);
450+
}
451+
445452
private Optional<PlayerData> readPlayerData(OfflinePlayer player, Path file) throws IOException {
446453
return readFile(file, file.resolveSibling(file.getFileName() + "_old"), PaperPlayerData.class)
447454
.map(paperPlayerData -> paperPlayerData.finalize(player, this));

0 commit comments

Comments
 (0)