Return subjects in priority order - related CodeCrafter47/BungeeTabListPlus#175

This commit is contained in:
Luck 2017-01-19 21:09:44 +00:00
parent 2e16844141
commit 8a8d6f16ae
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 47 additions and 2 deletions

View File

@ -33,6 +33,7 @@ import lombok.ToString;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.MapMaker;
@ -44,6 +45,7 @@ import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.caching.UserCache;
import me.lucko.luckperms.common.core.model.Group;
import me.lucko.luckperms.common.core.model.User;
import me.lucko.luckperms.common.utils.ImmutableCollectors;
import me.lucko.luckperms.sponge.LPSpongePlugin;
@ -73,9 +75,11 @@ import org.spongepowered.api.text.Text;
import co.aikar.timings.Timing;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
@ -209,6 +213,45 @@ public class LuckPermsService implements PermissionService {
plugin.getContextManager().registerCalculator(new SpongeCalculatorLink(contextCalculator));
}
public List<Subject> sortSubjects(List<Subject> s) {
List<Subject> ret = new ArrayList<>(s);
ret.sort((o1, o2) -> {
if (o1.equals(o2)) {
return 0;
}
boolean o1isGroup = o1.getContainingCollection().getIdentifier().equals(PermissionService.SUBJECTS_GROUP);
boolean o2isGroup = o2.getContainingCollection().getIdentifier().equals(PermissionService.SUBJECTS_GROUP);
if (o1isGroup != o2isGroup) {
return o1isGroup ? 1 : -1;
}
// Neither are groups
if (!o1isGroup) {
return 1;
}
Group g1 = plugin.getGroupManager().getIfLoaded(o1.getIdentifier());
Group g2 = plugin.getGroupManager().getIfLoaded(o2.getIdentifier());
boolean g1Null = g1 == null;
boolean g2Null = g2 == null;
if (g1Null != g2Null) {
return g1Null ? -1 : 1;
}
// Both are null
if (g1Null) {
return 1;
}
return Integer.compare(g1.getWeight().orElse(0), g2.getWeight().orElse(0)) == 1 ? 1 : -1;
});
return ImmutableList.copyOf(ret);
}
public Contexts calculateContexts(ContextSet contextSet) {
return new Contexts(
contextSet,

View File

@ -125,13 +125,15 @@ public interface LPSubject extends Subject {
@Deprecated
@Override
default List<Subject> getParents() {
return getParents(getActiveContextSet()).stream().map(s -> s.resolve(getService())).collect(ImmutableCollectors.toImmutableList());
List<Subject> ret = getParents(getActiveContextSet()).stream().map(s -> s.resolve(getService())).collect(ImmutableCollectors.toImmutableList());
return getService().sortSubjects(ret);
}
@Deprecated
@Override
default List<Subject> getParents(@NonNull Set<Context> contexts) {
return getParents(convertContexts(contexts)).stream().map(s -> s.resolve(getService())).collect(ImmutableCollectors.toImmutableList());
List<Subject> ret = getParents(convertContexts(contexts)).stream().map(s -> s.resolve(getService())).collect(ImmutableCollectors.toImmutableList());
return getService().sortSubjects(ret);
}
@Deprecated