Cleanup some Sponge subject classes

This commit is contained in:
Luck 2016-10-09 13:14:58 +01:00
parent b0085bc1e1
commit 93c7c06b2d
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
4 changed files with 54 additions and 33 deletions

View File

@ -100,7 +100,7 @@ public class LPSpongePlugin implements LuckPermsPlugin {
private ConsecutiveExecutor consecutiveExecutor; private ConsecutiveExecutor consecutiveExecutor;
private LuckPermsService service; private LuckPermsService service;
private LocaleManager localeManager; private LocaleManager localeManager;
private ContextManager<Player> contextManager; private ContextManager<Player> contextManager; // TODO convert this to use Subject instead of Player
@Listener @Listener
public void onEnable(GamePreInitializationEvent event) { public void onEnable(GamePreInitializationEvent event) {

View File

@ -48,7 +48,8 @@ public class SpongeListener extends AbstractListener {
} }
final GameProfile p = e.getProfile(); final GameProfile p = e.getProfile();
onAsyncLogin(p.getUniqueId(), p.getName().get()); onAsyncLogin(p.getUniqueId(), p.getName().get()); // Load the user into LuckPerms
plugin.getService().getUserSubjects().load(p.getUniqueId()); // Load the user into the PermissionService
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@ -57,12 +58,14 @@ public class SpongeListener extends AbstractListener {
final GameProfile player = e.getProfile(); final GameProfile player = e.getProfile();
final User user = plugin.getUserManager().get(plugin.getUuidCache().getUUID(player.getUniqueId())); final User user = plugin.getUserManager().get(plugin.getUuidCache().getUUID(player.getUniqueId()));
// Check if the user was loaded successfully.
if (user == null) { if (user == null) {
e.setCancelled(true); e.setCancelled(true);
e.setMessage(TextSerializers.LEGACY_FORMATTING_CODE.deserialize(Message.LOADING_ERROR.toString())); e.setMessage(TextSerializers.LEGACY_FORMATTING_CODE.deserialize(Message.LOADING_ERROR.toString()));
return; return;
} }
// Refresh permissions again
user.refreshPermissions(); user.refreshPermissions();
} }

View File

@ -27,6 +27,7 @@ import lombok.NonNull;
import me.lucko.luckperms.api.sponge.LuckPermsService; import me.lucko.luckperms.api.sponge.LuckPermsService;
import me.lucko.luckperms.api.sponge.LuckPermsUserSubject; import me.lucko.luckperms.api.sponge.LuckPermsUserSubject;
import me.lucko.luckperms.api.sponge.simple.SimpleCollection; import me.lucko.luckperms.api.sponge.simple.SimpleCollection;
import me.lucko.luckperms.commands.Util;
import me.lucko.luckperms.users.User; import me.lucko.luckperms.users.User;
import me.lucko.luckperms.users.UserIdentifier; import me.lucko.luckperms.users.UserIdentifier;
import me.lucko.luckperms.users.UserManager; import me.lucko.luckperms.users.UserManager;
@ -42,6 +43,10 @@ import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/**
* Manages low level Subject instances for the PermissionService.
* Most calls are cached.
*/
public class UserCollection implements SubjectCollection { public class UserCollection implements SubjectCollection {
private final LuckPermsService service; private final LuckPermsService service;
private final UserManager manager; private final UserManager manager;
@ -61,34 +66,45 @@ public class UserCollection implements SubjectCollection {
return PermissionService.SUBJECTS_USER; return PermissionService.SUBJECTS_USER;
} }
private void load(UUID uuid) { /**
* Load a user into this manager
* @param uuid the external uuid of the user
*/
public synchronized void load(UUID uuid) {
UUID internal = service.getPlugin().getUuidCache().getUUID(uuid); UUID internal = service.getPlugin().getUuidCache().getUUID(uuid);
if (!manager.isLoaded(UserIdentifier.of(uuid, null))) { if (!manager.isLoaded(UserIdentifier.of(internal, null))) {
return; return; // Not loaded at a higher level
}
if (users.containsKey(internal)) {
return; // Already loaded
} }
User user = manager.get(internal); User user = manager.get(internal);
users.put(internal, LuckPermsUserSubject.wrapUser(user, service)); LuckPermsUserSubject subject = LuckPermsUserSubject.wrapUser(user, service);
subject.calculateActivePermissions(true); // Pre-process some of their permissions
users.put(internal, subject);
} }
/**
* Unload a user from this manager
* @param uuid the internal uuid of the user
*/
public void unload(UUID uuid) { public void unload(UUID uuid) {
users.remove(service.getPlugin().getUuidCache().getUUID(uuid)); users.remove(uuid);
} }
@Override @Override
public synchronized Subject get(@NonNull String id) { public Subject get(@NonNull String id) {
try { final UUID uuid = Util.parseUuid(id);
UUID u = service.getPlugin().getUuidCache().getUUID(UUID.fromString(id)); if (uuid != null) {
if (users.containsKey(u)) { UUID internal = service.getPlugin().getUuidCache().getUUID(uuid);
return users.get(u); if (users.containsKey(internal)) {
return users.get(internal);
} }
if (manager.isLoaded(UserIdentifier.of(u, null))) { } else {
load(u);
return users.get(u);
}
} catch (IllegalArgumentException e) {
for (LuckPermsUserSubject subject : users.values()) { for (LuckPermsUserSubject subject : users.values()) {
if (subject.getUser().getName().equals(id)) { if (subject.getUser().getName().equals(id)) {
return subject; return subject;
@ -100,20 +116,28 @@ public class UserCollection implements SubjectCollection {
service.getPlugin().getLog().warn("Couldn't get user subject for: " + id); service.getPlugin().getLog().warn("Couldn't get user subject for: " + id);
} }
// What am I meant to do here? What if no user is loaded? Load it? Create it? // Fallback to the other collection. This Subject instance will never be persisted.
// If I do load/create it, this method should always be called async??.... errr.
return fallback.get(id); return fallback.get(id);
} }
@Override @Override
public boolean hasRegistered(@NonNull String id) { public boolean hasRegistered(@NonNull String id) {
try { final UUID uuid = Util.parseUuid(id);
UUID u = UUID.fromString(id); if (uuid != null) {
return manager.isLoaded(UserIdentifier.of(service.getPlugin().getUuidCache().getUUID(u), null)); UUID internal = service.getPlugin().getUuidCache().getUUID(uuid);
} catch (IllegalArgumentException e) { if (users.containsKey(internal)) {
User user = manager.get(id); return true;
return user != null;
} }
} else {
for (LuckPermsUserSubject subject : users.values()) {
if (subject.getUser().getName().equals(id)) {
return true;
}
}
}
return false;
} }
@Override @Override

View File

@ -27,7 +27,6 @@ import me.lucko.luckperms.api.context.ContextListener;
import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.entity.living.player.Player;
import java.util.Map; import java.util.Map;
import java.util.UUID;
public class SpongeUserManager extends UserManager implements ContextListener<Player> { public class SpongeUserManager extends UserManager implements ContextListener<Player> {
private final LPSpongePlugin plugin; private final LPSpongePlugin plugin;
@ -67,11 +66,6 @@ public class SpongeUserManager extends UserManager implements ContextListener<Pl
@Override @Override
public void onContextChange(Player subject, Map.Entry<String, String> before, Map.Entry<String, String> current) throws Exception { public void onContextChange(Player subject, Map.Entry<String, String> before, Map.Entry<String, String> current) throws Exception {
UUID internal = plugin.getUuidCache().getUUID(subject.getUniqueId()); // Not needed on Sponge. The context is accumulated on each permission check.
User user = get(internal);
if (user != null) {
plugin.doAsync(user::refreshPermissions);
}
} }
} }