Skip to content

Commit 4d1096e

Browse files
authored
Merge pull request #589 from Multiverse/fix/disable-with-players
Ensure playerdata saving is all completed within onDisable method
2 parents ae84339 + c293dd0 commit 4d1096e

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/main/java/org/mvplugins/multiverse/inventories/MultiverseInventories.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
import org.jvnet.hk2.annotations.Service;
3737
import org.mvplugins.multiverse.external.vavr.control.Try;
3838

39+
import java.util.ArrayList;
40+
import java.util.List;
41+
import java.util.concurrent.CompletableFuture;
42+
3943
/**
4044
* Multiverse-Inventories plugin main class.
4145
*/
@@ -137,15 +141,19 @@ public final void onEnable() {
137141
public void onDisable() {
138142
super.onDisable();
139143

144+
List<CompletableFuture<?>> futures = new ArrayList<>();
140145
for (final Player player : getServer().getOnlinePlayers()) {
141146
SingleShareWriter.of(this, player, Sharables.LAST_LOCATION).write(player.getLocation().clone());
142-
new WriteOnlyShareHandler(this, player).handleSharing();
147+
futures.add(new WriteOnlyShareHandler(this, player).handleSharing());
143148
if (inventoriesConfig.get().getApplyPlayerdataOnJoin()) {
144-
profileDataSource.get().modifyGlobalProfile(
145-
GlobalProfileKey.of(player), profile -> profile.setLoadOnLogin(true));
149+
futures.add(profileDataSource.get()
150+
.modifyGlobalProfile(
151+
GlobalProfileKey.of(player),
152+
profile -> profile.setLoadOnLogin(true)
153+
));
146154
}
147155
}
148-
156+
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
149157
MultiverseInventoriesApi.shutdown();
150158
this.dupingPatch.disable();
151159
this.shutdownDependencyInjection();

src/main/java/org/mvplugins/multiverse/inventories/handleshare/ShareHandler.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import org.bukkit.Bukkit;
1515
import org.bukkit.entity.Player;
1616

17+
import java.util.concurrent.CompletableFuture;
18+
1719
/**
1820
* Abstract class for handling sharing of data between worlds and game modes.
1921
*/
@@ -45,21 +47,22 @@ sealed abstract class ShareHandler permits GameModeShareHandler, ReadOnlyShareHa
4547
* Finalizes the transfer from one world to another. This handles the switching
4648
* inventories/stats for a player and persisting the changes.
4749
*/
48-
public final void handleSharing() {
50+
public CompletableFuture<Void> handleSharing() {
4951
long startTime = System.nanoTime();
5052
this.prepareProfiles();
5153
ShareHandlingEvent event = this.createEvent();
5254
Bukkit.getPluginManager().callEvent(event);
5355
if (event.isCancelled()) {
5456
Logging.fine("Share handling has been cancelled by another plugin!");
55-
return;
57+
return CompletableFuture.completedFuture(null);
5658
}
5759
logAffectedProfilesCount();
5860
ProfileDataSnapshot snapshot = getSnapshot();
5961
updatePlayer();
60-
updateProfiles(snapshot);
62+
CompletableFuture<Void> future = updateProfiles(snapshot);
6163
double timeTaken = (System.nanoTime() - startTime) / 1000000.0;
6264
logHandlingComplete(timeTaken, event);
65+
return future;
6366
}
6467

6568
protected abstract void prepareProfiles();
@@ -89,22 +92,23 @@ private void updatePlayer() {
8992
}
9093
}
9194

92-
private void updateProfiles(ProfileDataSnapshot snapshot) {
95+
private CompletableFuture<Void> updateProfiles(ProfileDataSnapshot snapshot) {
9396
if (affectedProfiles.getWriteProfiles().isEmpty()) {
9497
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);
9999
}
100+
return CompletableFuture.allOf(affectedProfiles.getWriteProfiles()
101+
.stream()
102+
.map(writeProfile -> updatePersistingProfile(writeProfile, snapshot))
103+
.toArray(CompletableFuture[]::new));
100104
}
101105

102-
private void updatePersistingProfile(PersistingProfile persistingProfile, ProfileDataSnapshot snapshot) {
106+
private CompletableFuture<Void> updatePersistingProfile(PersistingProfile persistingProfile, ProfileDataSnapshot snapshot) {
103107
if (persistingProfile.getShares().isEmpty()) {
104108
Logging.finest("No shares to write - nothing more to do.");
105-
return;
109+
return CompletableFuture.completedFuture(null);
106110
}
107-
profileDataStore.getPlayerProfile(persistingProfile.getProfileKey())
111+
return profileDataStore.getPlayerProfile(persistingProfile.getProfileKey())
108112
.thenCompose(playerProfile -> {
109113
Logging.finer("Persisted: " + persistingProfile.getShares() + " to "
110114
+ playerProfile.getContainerType() + ":" + playerProfile.getContainerName()

0 commit comments

Comments
 (0)