Fix permissions not being removed on Sponge

This commit is contained in:
Luck 2016-09-04 17:59:01 +01:00
parent ed85eee36c
commit 770b7dc282
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
10 changed files with 84 additions and 117 deletions

View File

@ -44,23 +44,18 @@ public class BukkitUserManager extends UserManager {
} }
@Override @Override
public void unload(User user) { public void preUnload(User user) {
if (user != null) { if (user instanceof BukkitUser) {
BukkitUser u = (BukkitUser) user;
if (user instanceof BukkitUser) { if (u.getAttachment() != null) {
BukkitUser u = (BukkitUser) user; Player player = plugin.getServer().getPlayer(plugin.getUuidCache().getExternalUUID(u.getUuid()));
if (u.getAttachment() != null) { if (player != null) {
Player player = plugin.getServer().getPlayer(plugin.getUuidCache().getExternalUUID(u.getUuid())); player.removeAttachment(u.getAttachment());
if (player != null) {
player.removeAttachment(u.getAttachment());
}
u.setAttachment(null);
} }
u.setAttachment(null);
} }
getAll().remove(user.getUuid());
} }
} }

View File

@ -34,15 +34,6 @@ public class BungeeUserManager extends UserManager {
this.plugin = plugin; this.plugin = plugin;
} }
@Override
public void unload(User user) {
if (user != null) {
// Cannot clear the ProxiedPlayer's permission map, they're leaving so that will get GCed anyway
// Calling getPermissions.clear() throws an UnsupportedOperationException
getAll().remove(user.getUuid());
}
}
@Override @Override
public void cleanup(User user) { public void cleanup(User user) {
if (plugin.getProxy().getPlayer(plugin.getUuidCache().getExternalUUID(user.getUuid())) == null) { if (plugin.getProxy().getPlayer(plugin.getUuidCache().getExternalUUID(user.getUuid())) == null) {

View File

@ -35,7 +35,7 @@ public class TrackManager extends AbstractManager<String, Track> {
* @return a set of tracks that the groups could be a member of * @return a set of tracks that the groups could be a member of
*/ */
public Set<Track> getApplicableTracks(String group) { public Set<Track> getApplicableTracks(String group) {
return objects.values().stream() return getAll().values().stream()
.filter(t -> t.containsGroup(group)) .filter(t -> t.containsGroup(group))
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }

View File

@ -45,7 +45,7 @@ public abstract class UserManager extends AbstractManager<UUID, User> {
@SuppressWarnings("OptionalGetWithoutIsPresent") @SuppressWarnings("OptionalGetWithoutIsPresent")
public User get(String name) { public User get(String name) {
try { try {
return objects.values().stream() return getAll().values().stream()
.filter(u -> u.getName().equalsIgnoreCase(name)) .filter(u -> u.getName().equalsIgnoreCase(name))
.limit(1).findAny().get(); .limit(1).findAny().get();
} catch (NoSuchElementException e) { } catch (NoSuchElementException e) {
@ -54,9 +54,8 @@ public abstract class UserManager extends AbstractManager<UUID, User> {
} }
@Override @Override
public void set(User u) { public void preSet(User u) {
giveDefaultIfNeeded(u, true); giveDefaultIfNeeded(u, true);
super.set(u);
} }
@Override @Override

View File

@ -22,8 +22,10 @@
package me.lucko.luckperms.utils; package me.lucko.luckperms.utils;
import com.google.common.collect.ImmutableMap;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* An abstract manager class * An abstract manager class
@ -31,10 +33,12 @@ import java.util.concurrent.ConcurrentHashMap;
* @param <T> the class this manager is "managing" * @param <T> the class this manager is "managing"
*/ */
public abstract class AbstractManager<I, T extends Identifiable<I>> { public abstract class AbstractManager<I, T extends Identifiable<I>> {
protected final Map<I, T> objects = new ConcurrentHashMap<>(); private final Map<I, T> objects = new HashMap<>();
public Map<I, T> getAll() { public final Map<I, T> getAll() {
return objects; synchronized (objects) {
return ImmutableMap.copyOf(objects);
}
} }
/** /**
@ -42,28 +46,39 @@ public abstract class AbstractManager<I, T extends Identifiable<I>> {
* @param id The id to search by * @param id The id to search by
* @return a {@link T} object if the object is loaded, returns null if the object is not loaded * @return a {@link T} object if the object is loaded, returns null if the object is not loaded
*/ */
public T get(I id) { public final T get(I id) {
return objects.get(id); synchronized (objects) {
return objects.get(id);
}
} }
/** /**
* Add a object to the loaded objects map * Add a object to the loaded objects map
* @param t The object to add * @param t The object to add
*/ */
public void set(T t) { public final void set(T t) {
objects.put(t.getId(), t); preSet(t);
synchronized (objects) {
objects.put(t.getId(), t);
}
}
public void preSet(T t) {
} }
/** /**
* Updates (or sets if the object wasn't already loaded) an object in the objects map * Updates (or sets if the object wasn't already loaded) an object in the objects map
* @param t The object to update or set * @param t The object to update or set
*/ */
public void updateOrSet(T t) { public final void updateOrSet(T t) {
if (!isLoaded(t.getId())) { synchronized (objects) {
// The object isn't already loaded if (!isLoaded(t.getId())) {
set(t); // The object isn't already loaded
} else { set(t);
copy(t, objects.get(t.getId())); } else {
copy(t, objects.get(t.getId()));
}
} }
} }
@ -74,25 +89,36 @@ public abstract class AbstractManager<I, T extends Identifiable<I>> {
* @param id The id of the object * @param id The id of the object
* @return true if the object is loaded * @return true if the object is loaded
*/ */
public boolean isLoaded(I id) { public final boolean isLoaded(I id) {
return objects.containsKey(id); synchronized (objects) {
return objects.containsKey(id);
}
} }
/** /**
* Removes and unloads the object from the manager * Removes and unloads the object from the manager
* @param t The object to unload * @param t The object to unload
*/ */
public void unload(T t) { public final void unload(T t) {
if (t != null) { if (t != null) {
objects.remove(t.getId()); preUnload(t);
synchronized (objects) {
objects.remove(t.getId());
}
} }
} }
public void preUnload(T t) {
}
/** /**
* Unloads all objects from the manager * Unloads all objects from the manager
*/ */
public void unloadAll() { public final void unloadAll() {
objects.clear(); synchronized (objects) {
objects.clear();
}
} }
/** /**

View File

@ -22,7 +22,6 @@
package me.lucko.luckperms; package me.lucko.luckperms;
import me.lucko.luckperms.api.sponge.LuckPermsSubject;
import me.lucko.luckperms.constants.Message; import me.lucko.luckperms.constants.Message;
import me.lucko.luckperms.users.User; import me.lucko.luckperms.users.User;
import me.lucko.luckperms.utils.AbstractListener; import me.lucko.luckperms.utils.AbstractListener;
@ -31,8 +30,6 @@ import org.spongepowered.api.event.network.ClientConnectionEvent;
import org.spongepowered.api.profile.GameProfile; import org.spongepowered.api.profile.GameProfile;
import org.spongepowered.api.text.serializer.TextSerializers; import org.spongepowered.api.text.serializer.TextSerializers;
import java.util.Iterator;
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
public class SpongeListener extends AbstractListener { public class SpongeListener extends AbstractListener {
private final LPSpongePlugin plugin; private final LPSpongePlugin plugin;
@ -78,12 +75,5 @@ public class SpongeListener extends AbstractListener {
@Listener @Listener
public void onClientLeave(ClientConnectionEvent.Disconnect e) { public void onClientLeave(ClientConnectionEvent.Disconnect e) {
onLeave(e.getTargetEntity().getUniqueId()); onLeave(e.getTargetEntity().getUniqueId());
Iterator<LuckPermsSubject> iterator = plugin.getService().getUserSubjects().getCache().iterator();
while (iterator.hasNext()) {
LuckPermsSubject subject = iterator.next();
if (subject.getIdentifier().equalsIgnoreCase(e.getTargetEntity().getUniqueId().toString())) {
iterator.remove();
}
}
} }
} }

View File

@ -52,13 +52,17 @@ import static me.lucko.luckperms.utils.ArgumentChecker.unescapeCharacters;
@EqualsAndHashCode(of = {"holder"}) @EqualsAndHashCode(of = {"holder"})
public class LuckPermsSubject implements Subject { public class LuckPermsSubject implements Subject {
public static Subject wrapHolder(PermissionHolder holder, LuckPermsService service) {
return new LuckPermsSubject(holder, service);
}
@Getter @Getter
private final PermissionHolder holder; private final PermissionHolder holder;
private final EnduringData enduringData; private final EnduringData enduringData;
private final TransientData transientData; private final TransientData transientData;
private final LuckPermsService service; private final LuckPermsService service;
public LuckPermsSubject(PermissionHolder holder, LuckPermsService service) { private LuckPermsSubject(PermissionHolder holder, LuckPermsService service) {
this.holder = holder; this.holder = holder;
this.enduringData = new EnduringData(this, service, holder); this.enduringData = new EnduringData(this, service, holder);
this.transientData = new TransientData(service, holder); this.transientData = new TransientData(service, holder);
@ -77,13 +81,13 @@ public class LuckPermsSubject implements Subject {
@Override @Override
public String getIdentifier() { public String getIdentifier() {
return enduringData.getHolder().getObjectName(); return holder.getObjectName();
} }
@Override @Override
public Optional<CommandSource> getCommandSource() { public Optional<CommandSource> getCommandSource() {
if (enduringData.getHolder() instanceof User) { if (holder instanceof User) {
final UUID uuid = ((User) enduringData.getHolder()).getUuid(); final UUID uuid = ((User) holder).getUuid();
Optional<Player> p = Sponge.getServer().getPlayer(uuid); Optional<Player> p = Sponge.getServer().getPlayer(uuid);
if (p.isPresent()) { if (p.isPresent()) {
@ -96,7 +100,7 @@ public class LuckPermsSubject implements Subject {
@Override @Override
public SubjectCollection getContainingCollection() { public SubjectCollection getContainingCollection() {
if (enduringData.getHolder() instanceof Group) { if (holder instanceof Group) {
return service.getGroupSubjects(); return service.getGroupSubjects();
} else { } else {
return service.getUserSubjects(); return service.getUserSubjects();
@ -130,18 +134,16 @@ public class LuckPermsSubject implements Subject {
context.put(c.getKey(), c.getValue()); context.put(c.getKey(), c.getValue());
} }
me.lucko.luckperms.api.Tristate t = enduringData.getHolder().inheritsPermission(new me.lucko.luckperms.utils.Node.Builder(node).withExtraContext(context).build()); switch (holder.inheritsPermission(new me.lucko.luckperms.utils.Node.Builder(node).withExtraContext(context).build())) {
if (t == me.lucko.luckperms.api.Tristate.UNDEFINED) { case UNDEFINED:
return Tristate.UNDEFINED; return Tristate.UNDEFINED;
case TRUE:
return Tristate.TRUE;
case FALSE:
return Tristate.FALSE;
default:
return null;
} }
if (t == me.lucko.luckperms.api.Tristate.TRUE) {
return Tristate.TRUE;
}
if (t == me.lucko.luckperms.api.Tristate.FALSE) {
return Tristate.FALSE;
}
return null;
} }
@Override @Override

View File

@ -27,7 +27,6 @@ import lombok.NonNull;
import me.lucko.luckperms.api.sponge.LuckPermsService; import me.lucko.luckperms.api.sponge.LuckPermsService;
import me.lucko.luckperms.api.sponge.LuckPermsSubject; import me.lucko.luckperms.api.sponge.LuckPermsSubject;
import me.lucko.luckperms.api.sponge.simple.SimpleSubject; import me.lucko.luckperms.api.sponge.simple.SimpleSubject;
import me.lucko.luckperms.core.PermissionHolder;
import me.lucko.luckperms.groups.GroupManager; import me.lucko.luckperms.groups.GroupManager;
import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.context.Context;
import org.spongepowered.api.service.permission.PermissionService; import org.spongepowered.api.service.permission.PermissionService;
@ -35,17 +34,14 @@ import org.spongepowered.api.service.permission.Subject;
import org.spongepowered.api.service.permission.SubjectCollection; import org.spongepowered.api.service.permission.SubjectCollection;
import org.spongepowered.api.service.permission.SubjectData; import org.spongepowered.api.service.permission.SubjectData;
import java.util.AbstractMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@AllArgsConstructor @AllArgsConstructor
public class GroupCollection implements SubjectCollection { public class GroupCollection implements SubjectCollection {
private final LuckPermsService service; private final LuckPermsService service;
private final GroupManager manager; private final GroupManager manager;
private final Set<LuckPermsSubject> cache = ConcurrentHashMap.newKeySet();
@Override @Override
public String getIdentifier() { public String getIdentifier() {
@ -55,16 +51,7 @@ public class GroupCollection implements SubjectCollection {
@Override @Override
public Subject get(@NonNull String id) { public Subject get(@NonNull String id) {
if (manager.isLoaded(id)) { if (manager.isLoaded(id)) {
PermissionHolder holder = manager.get(id); return LuckPermsSubject.wrapHolder(manager.get(id), service);
for (LuckPermsSubject subject : cache) {
if (subject.getHolder().getObjectName().equalsIgnoreCase(holder.getObjectName())) {
return subject;
}
}
LuckPermsSubject subject = new LuckPermsSubject(manager.get(id), service);
cache.add(subject);
return subject;
} }
return new SimpleSubject(id, service, this); return new SimpleSubject(id, service, this);
@ -78,7 +65,7 @@ public class GroupCollection implements SubjectCollection {
@Override @Override
public Iterable<Subject> getAllSubjects() { public Iterable<Subject> getAllSubjects() {
return manager.getAll().values().stream() return manager.getAll().values().stream()
.map(u -> new LuckPermsSubject(u, service)) .map(u -> LuckPermsSubject.wrapHolder(u, service))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@ -90,10 +77,9 @@ public class GroupCollection implements SubjectCollection {
@Override @Override
public Map<Subject, Boolean> getAllWithPermission(@NonNull Set<Context> contexts, @NonNull String node) { public Map<Subject, Boolean> getAllWithPermission(@NonNull Set<Context> contexts, @NonNull String node) {
return manager.getAll().values().stream() return manager.getAll().values().stream()
.map(u -> new LuckPermsSubject(u, service)) .map(u -> LuckPermsSubject.wrapHolder(u, service))
.filter(sub -> sub.hasPermission(contexts, node)) .filter(sub -> sub.hasPermission(contexts, node))
.map(sub -> new AbstractMap.SimpleEntry<Subject, Boolean>(sub, sub.getPermissionValue(contexts, node).asBoolean())) .collect(Collectors.toMap(sub -> sub, sub -> sub.getPermissionValue(contexts, node).asBoolean()));
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
} }
@Override @Override

View File

@ -23,7 +23,6 @@
package me.lucko.luckperms.api.sponge.collections; package me.lucko.luckperms.api.sponge.collections;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import me.lucko.luckperms.api.sponge.LuckPermsService; import me.lucko.luckperms.api.sponge.LuckPermsService;
import me.lucko.luckperms.api.sponge.LuckPermsSubject; import me.lucko.luckperms.api.sponge.LuckPermsSubject;
@ -37,11 +36,9 @@ import org.spongepowered.api.service.permission.Subject;
import org.spongepowered.api.service.permission.SubjectCollection; import org.spongepowered.api.service.permission.SubjectCollection;
import org.spongepowered.api.service.permission.SubjectData; import org.spongepowered.api.service.permission.SubjectData;
import java.util.AbstractMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@AllArgsConstructor @AllArgsConstructor
@ -49,9 +46,6 @@ public class UserCollection implements SubjectCollection {
private final LuckPermsService service; private final LuckPermsService service;
private final UserManager manager; private final UserManager manager;
@Getter
private final Set<LuckPermsSubject> cache = ConcurrentHashMap.newKeySet();
@Override @Override
public String getIdentifier() { public String getIdentifier() {
return PermissionService.SUBJECTS_USER; return PermissionService.SUBJECTS_USER;
@ -74,15 +68,7 @@ public class UserCollection implements SubjectCollection {
} }
if (holder != null) { if (holder != null) {
for (LuckPermsSubject subject : cache) { return LuckPermsSubject.wrapHolder(holder, service);
if (subject.getHolder().getObjectName().equalsIgnoreCase(holder.getObjectName())) {
return subject;
}
}
LuckPermsSubject subject = new LuckPermsSubject(holder, service);
cache.add(subject);
return subject;
} }
service.getPlugin().getLog().warn("Couldn't get subject for: " + id); service.getPlugin().getLog().warn("Couldn't get subject for: " + id);
@ -106,7 +92,7 @@ public class UserCollection implements SubjectCollection {
@Override @Override
public Iterable<Subject> getAllSubjects() { public Iterable<Subject> getAllSubjects() {
return manager.getAll().values().stream() return manager.getAll().values().stream()
.map(u -> new LuckPermsSubject(u, service)) .map(u -> LuckPermsSubject.wrapHolder(u, service))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@ -118,10 +104,9 @@ public class UserCollection implements SubjectCollection {
@Override @Override
public Map<Subject, Boolean> getAllWithPermission(@NonNull Set<Context> contexts, @NonNull String node) { public Map<Subject, Boolean> getAllWithPermission(@NonNull Set<Context> contexts, @NonNull String node) {
return manager.getAll().values().stream() return manager.getAll().values().stream()
.map(u -> new LuckPermsSubject(u, service)) .map(u -> LuckPermsSubject.wrapHolder(u, service))
.filter(sub -> sub.hasPermission(contexts, node)) .filter(sub -> sub.hasPermission(contexts, node))
.map(sub -> new AbstractMap.SimpleEntry<Subject, Boolean>(sub, sub.getPermissionValue(contexts, node).asBoolean())) .collect(Collectors.toMap(sub -> sub, sub -> sub.getPermissionValue(contexts, node).asBoolean()));
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
} }
@Override @Override

View File

@ -34,16 +34,9 @@ public class SpongeUserManager extends UserManager {
this.plugin = plugin; this.plugin = plugin;
} }
@Override
public void unload(User user) {
if (user != null) {
getAll().remove(user.getUuid());
}
}
@Override @Override
public void cleanup(User user) { public void cleanup(User user) {
if (plugin.getGame().getServer().getPlayer(plugin.getUuidCache().getExternalUUID(user.getUuid())).isPresent()) { if (!plugin.getGame().getServer().getPlayer(plugin.getUuidCache().getExternalUUID(user.getUuid())).isPresent()) {
unload(user); unload(user);
} }
} }