mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-28 05:35:26 +01:00
Refactor weight lookups
This commit is contained in:
parent
07c38de44b
commit
c02fcb6508
@ -183,6 +183,17 @@ public abstract class PermissionHolder {
|
|||||||
}
|
}
|
||||||
private final TransientNodesCache transientNodesCopy = new TransientNodesCache();
|
private final TransientNodesCache transientNodesCopy = new TransientNodesCache();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Caches the holders weight lookup
|
||||||
|
*/
|
||||||
|
private final class WeightCache extends Cache<OptionalInt> {
|
||||||
|
@Override
|
||||||
|
protected OptionalInt supply() {
|
||||||
|
return calculateWeight();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private final WeightCache weightCache = new WeightCache();
|
||||||
|
|
||||||
// used to ensure thread safe access to the backing transientNodes map
|
// used to ensure thread safe access to the backing transientNodes map
|
||||||
private final ReentrantLock transientNodesLock = new ReentrantLock();
|
private final ReentrantLock transientNodesLock = new ReentrantLock();
|
||||||
|
|
||||||
@ -201,6 +212,7 @@ public abstract class PermissionHolder {
|
|||||||
private void invalidateCache() {
|
private void invalidateCache() {
|
||||||
nodesCopy.invalidate();
|
nodesCopy.invalidate();
|
||||||
transientNodesCopy.invalidate();
|
transientNodesCopy.invalidate();
|
||||||
|
weightCache.invalidate();
|
||||||
|
|
||||||
// Invalidate listeners
|
// Invalidate listeners
|
||||||
for (Runnable r : stateListeners) {
|
for (Runnable r : stateListeners) {
|
||||||
@ -1482,16 +1494,34 @@ public abstract class PermissionHolder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public OptionalInt getWeight() {
|
public OptionalInt getWeight() {
|
||||||
|
return weightCache.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
private OptionalInt calculateWeight() {
|
||||||
if (this instanceof User) return OptionalInt.empty();
|
if (this instanceof User) return OptionalInt.empty();
|
||||||
|
|
||||||
OptionalInt weight = OptionalInt.empty();
|
boolean seen = false;
|
||||||
|
int best = 0;
|
||||||
|
for (Node n : getOwnNodes()) {
|
||||||
|
if (!n.getPermission().startsWith("weight.")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String substring = n.getPermission().substring("weight.".length());
|
||||||
|
|
||||||
|
int i;
|
||||||
try {
|
try {
|
||||||
weight = getOwnNodes().stream()
|
i = Integer.parseInt(substring);
|
||||||
.filter(n -> n.getPermission().startsWith("weight."))
|
} catch (NumberFormatException e) {
|
||||||
.map(n -> n.getPermission().substring("weight.".length()))
|
continue;
|
||||||
.mapToInt(Integer::parseInt)
|
}
|
||||||
.max();
|
|
||||||
} catch (Exception ignored) {}
|
if (!seen || i > best) {
|
||||||
|
seen = true;
|
||||||
|
best = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
OptionalInt weight = seen ? OptionalInt.of(best) : OptionalInt.empty();
|
||||||
|
|
||||||
if (!weight.isPresent()) {
|
if (!weight.isPresent()) {
|
||||||
Integer w = plugin.getConfiguration().get(ConfigKeys.GROUP_WEIGHTS).get(getObjectName().toLowerCase());
|
Integer w = plugin.getConfiguration().get(ConfigKeys.GROUP_WEIGHTS).get(getObjectName().toLowerCase());
|
||||||
|
Loading…
Reference in New Issue
Block a user