|
14 | 14 | import org.bukkit.Bukkit; |
15 | 15 | import org.bukkit.entity.Player; |
16 | 16 |
|
| 17 | +import java.util.concurrent.CompletableFuture; |
| 18 | + |
17 | 19 | /** |
18 | 20 | * Abstract class for handling sharing of data between worlds and game modes. |
19 | 21 | */ |
@@ -45,21 +47,22 @@ sealed abstract class ShareHandler permits GameModeShareHandler, ReadOnlyShareHa |
45 | 47 | * Finalizes the transfer from one world to another. This handles the switching |
46 | 48 | * inventories/stats for a player and persisting the changes. |
47 | 49 | */ |
48 | | - public final void handleSharing() { |
| 50 | + public CompletableFuture<Void> handleSharing() { |
49 | 51 | long startTime = System.nanoTime(); |
50 | 52 | this.prepareProfiles(); |
51 | 53 | ShareHandlingEvent event = this.createEvent(); |
52 | 54 | Bukkit.getPluginManager().callEvent(event); |
53 | 55 | if (event.isCancelled()) { |
54 | 56 | Logging.fine("Share handling has been cancelled by another plugin!"); |
55 | | - return; |
| 57 | + return CompletableFuture.completedFuture(null); |
56 | 58 | } |
57 | 59 | logAffectedProfilesCount(); |
58 | 60 | ProfileDataSnapshot snapshot = getSnapshot(); |
59 | 61 | updatePlayer(); |
60 | | - updateProfiles(snapshot); |
| 62 | + CompletableFuture<Void> future = updateProfiles(snapshot); |
61 | 63 | double timeTaken = (System.nanoTime() - startTime) / 1000000.0; |
62 | 64 | logHandlingComplete(timeTaken, event); |
| 65 | + return future; |
63 | 66 | } |
64 | 67 |
|
65 | 68 | protected abstract void prepareProfiles(); |
@@ -89,22 +92,23 @@ private void updatePlayer() { |
89 | 92 | } |
90 | 93 | } |
91 | 94 |
|
92 | | - private void updateProfiles(ProfileDataSnapshot snapshot) { |
| 95 | + private CompletableFuture<Void> updateProfiles(ProfileDataSnapshot snapshot) { |
93 | 96 | if (affectedProfiles.getWriteProfiles().isEmpty()) { |
94 | 97 | Logging.finest("No profiles to write - nothing more to do."); |
95 | | - return; |
96 | | - } |
97 | | - for (PersistingProfile writeProfile : affectedProfiles.getWriteProfiles()) { |
98 | | - updatePersistingProfile(writeProfile, snapshot); |
| 98 | + return CompletableFuture.completedFuture(null); |
99 | 99 | } |
| 100 | + return CompletableFuture.allOf(affectedProfiles.getWriteProfiles() |
| 101 | + .stream() |
| 102 | + .map(writeProfile -> updatePersistingProfile(writeProfile, snapshot)) |
| 103 | + .toArray(CompletableFuture[]::new)); |
100 | 104 | } |
101 | 105 |
|
102 | | - private void updatePersistingProfile(PersistingProfile persistingProfile, ProfileDataSnapshot snapshot) { |
| 106 | + private CompletableFuture<Void> updatePersistingProfile(PersistingProfile persistingProfile, ProfileDataSnapshot snapshot) { |
103 | 107 | if (persistingProfile.getShares().isEmpty()) { |
104 | 108 | Logging.finest("No shares to write - nothing more to do."); |
105 | | - return; |
| 109 | + return CompletableFuture.completedFuture(null); |
106 | 110 | } |
107 | | - profileDataStore.getPlayerProfile(persistingProfile.getProfileKey()) |
| 111 | + return profileDataStore.getPlayerProfile(persistingProfile.getProfileKey()) |
108 | 112 | .thenCompose(playerProfile -> { |
109 | 113 | Logging.finer("Persisted: " + persistingProfile.getShares() + " to " |
110 | 114 | + playerProfile.getContainerType() + ":" + playerProfile.getContainerName() |
|
0 commit comments