Cleanup subscription updates & UserManager#loadAllUsers

This commit is contained in:
Luck 2017-09-13 20:18:37 +01:00
parent b1584aa2c3
commit 2412edaa3a
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
5 changed files with 33 additions and 29 deletions

View File

@ -25,6 +25,7 @@
package me.lucko.luckperms.bukkit.model; package me.lucko.luckperms.bukkit.model;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@ -56,20 +57,26 @@ public class SubscriptionManager {
// we compare changes to avoid unnecessary time wasted on the main thread mutating this data. // we compare changes to avoid unnecessary time wasted on the main thread mutating this data.
// the changes can be calculated here async, and then only the needed changes can be applied. // the changes can be calculated here async, and then only the needed changes can be applied.
Map.Entry<Set<String>, Set<String>> changes = compareSets(newPerms, currentSubscriptions); Map.Entry<Set<String>, Set<String>> changes = compareSets(newPerms, currentSubscriptions);
permissible.getPlugin().doSync(new SubscriptionUpdateTask(permissible, changes.getKey(), changes.getValue()));
Set<String> toAdd = changes.getKey(); this.currentSubscriptions = newPerms;
Set<String> toRemove = changes.getValue(); }
permissible.getPlugin().doSync(() -> { @AllArgsConstructor
public static final class SubscriptionUpdateTask implements Runnable {
private final LPPermissible permissible;
private final Set<String> toAdd;
private final Set<String> toRemove;
@Override
public void run() {
for (String s : toAdd) { for (String s : toAdd) {
permissible.getPlugin().getServer().getPluginManager().subscribeToPermission(s, permissible.getParent()); permissible.getPlugin().getServer().getPluginManager().subscribeToPermission(s, permissible.getParent());
} }
for (String s : toRemove) { for (String s : toRemove) {
permissible.getPlugin().getServer().getPluginManager().unsubscribeFromPermission(s, permissible.getParent()); permissible.getPlugin().getServer().getPluginManager().unsubscribeFromPermission(s, permissible.getParent());
} }
}); }
this.currentSubscriptions = newPerms;
} }
/** /**

View File

@ -34,8 +34,8 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.references.UserIdentifier; import me.lucko.luckperms.common.references.UserIdentifier;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@RequiredArgsConstructor @RequiredArgsConstructor
public class GenericUserManager extends AbstractManager<UserIdentifier, User> implements UserManager { public class GenericUserManager extends AbstractManager<UserIdentifier, User> implements UserManager {
@ -109,16 +109,14 @@ public class GenericUserManager extends AbstractManager<UserIdentifier, User> im
} }
@Override @Override
public void updateAllUsers() { public CompletableFuture<Void> updateAllUsers() {
plugin.doSync(() -> { return CompletableFuture.supplyAsync(plugin::getOnlinePlayers, plugin.getScheduler().sync())
Set<UUID> players = plugin.getOnlinePlayers(); .thenAcceptAsync(players -> {
plugin.doAsync(() -> { for (UUID uuid : players) {
for (UUID uuid : players) { UUID internal = plugin.getUuidCache().getUUID(uuid);
UUID internal = plugin.getUuidCache().getUUID(uuid); plugin.getStorage().loadUser(internal, "null").join();
plugin.getStorage().loadUser(internal, "null").join(); }
} }, plugin.getScheduler().async());
});
});
} }
public static boolean giveDefaultIfNeeded(User user, boolean save, LuckPermsPlugin plugin) { public static boolean giveDefaultIfNeeded(User user, boolean save, LuckPermsPlugin plugin) {

View File

@ -30,6 +30,7 @@ import me.lucko.luckperms.common.references.Identifiable;
import me.lucko.luckperms.common.references.UserIdentifier; import me.lucko.luckperms.common.references.UserIdentifier;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public interface UserManager extends Manager<UserIdentifier, User> { public interface UserManager extends Manager<UserIdentifier, User> {
@ -73,6 +74,6 @@ public interface UserManager extends Manager<UserIdentifier, User> {
/** /**
* Reloads the data of all online users * Reloads the data of all online users
*/ */
void updateAllUsers(); CompletableFuture<Void> updateAllUsers();
} }

View File

@ -55,7 +55,7 @@ public class UpdateTask implements Runnable {
plugin.getStorage().loadAllTracks().join(); plugin.getStorage().loadAllTracks().join();
// Refresh all online users. // Refresh all online users.
plugin.getUserManager().updateAllUsers(); plugin.getUserManager().updateAllUsers().join();
plugin.onPostUpdate(); plugin.onPostUpdate();

View File

@ -207,16 +207,14 @@ public class SpongeUserManager implements UserManager, LPSubjectCollection {
} }
@Override @Override
public void updateAllUsers() { public CompletableFuture<Void> updateAllUsers() {
plugin.doSync(() -> { return CompletableFuture.supplyAsync(plugin::getOnlinePlayers, plugin.getScheduler().sync())
Set<UUID> players = plugin.getOnlinePlayers(); .thenAcceptAsync(players -> {
plugin.doAsync(() -> { for (UUID uuid : players) {
for (UUID uuid : players) { UUID internal = plugin.getUuidCache().getUUID(uuid);
UUID internal = plugin.getUuidCache().getUUID(uuid); plugin.getStorage().loadUser(internal, "null").join();
plugin.getStorage().loadUser(internal, "null").join(); }
} }, plugin.getScheduler().async());
});
});
} }
/* ------------------------------------------ /* ------------------------------------------