mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-24 11:38:40 +01:00
Respect prefix option set in subject - closes #149
This commit is contained in:
parent
a3a23d8870
commit
bd97a7c5d2
@ -129,6 +129,10 @@ public class NodeFactory {
|
||||
return new NodeBuilder("meta." + MetaUtils.escapeCharacters(key) + "." + MetaUtils.escapeCharacters(value));
|
||||
}
|
||||
|
||||
public static NodeBuilder makeChatMetaNode(boolean prefix, int priority, String s) {
|
||||
return prefix ? makePrefixNode(priority, s) : makeSuffixNode(priority, s);
|
||||
}
|
||||
|
||||
public static NodeBuilder makePrefixNode(int priority, String prefix) {
|
||||
return new NodeBuilder("prefix." + priority + "." + MetaUtils.escapeCharacters(prefix));
|
||||
}
|
||||
|
@ -34,11 +34,13 @@ import me.lucko.luckperms.api.Tristate;
|
||||
import me.lucko.luckperms.api.context.ContextSet;
|
||||
import me.lucko.luckperms.api.context.ImmutableContextSet;
|
||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
||||
import me.lucko.luckperms.common.caching.MetaHolder;
|
||||
import me.lucko.luckperms.common.core.NodeBuilder;
|
||||
import me.lucko.luckperms.common.core.NodeFactory;
|
||||
import me.lucko.luckperms.common.core.model.Group;
|
||||
import me.lucko.luckperms.common.core.model.PermissionHolder;
|
||||
import me.lucko.luckperms.common.core.model.User;
|
||||
import me.lucko.luckperms.common.utils.ExtractedContexts;
|
||||
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
|
||||
import me.lucko.luckperms.exceptions.ObjectLacksException;
|
||||
import me.lucko.luckperms.sponge.service.base.LPSubject;
|
||||
@ -431,6 +433,40 @@ public class LuckPermsSubjectData implements LPSubjectData {
|
||||
@Override
|
||||
public boolean setOption(@NonNull ContextSet context, @NonNull String key, @NonNull String value) {
|
||||
try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_SET_OPTION)) {
|
||||
if (key.equalsIgnoreCase("prefix") || key.equalsIgnoreCase("suffix")) {
|
||||
// special handling.
|
||||
String type = key.toLowerCase();
|
||||
|
||||
// remove all prefixes/suffixes from the user
|
||||
List<Node> toRemove = holder.getNodes().stream()
|
||||
.filter(n -> type.equals("prefix") ? n.isPrefix() : n.isSuffix())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
toRemove.forEach(n -> {
|
||||
try {
|
||||
if (enduring) {
|
||||
holder.unsetPermission(n);
|
||||
} else {
|
||||
holder.unsetTransientPermission(n);
|
||||
}
|
||||
} catch (ObjectLacksException ignored) {}
|
||||
});
|
||||
|
||||
MetaHolder metaHolder = holder.accumulateMeta(null, null, ExtractedContexts.generate(context));
|
||||
int priority = (type.equals("prefix") ? metaHolder.getPrefixes() : metaHolder.getSuffixes()).keySet().stream()
|
||||
.mapToInt(e -> e).max().orElse(0);
|
||||
priority += 10;
|
||||
|
||||
try {
|
||||
if (enduring) {
|
||||
holder.setPermission(NodeFactory.makeChatMetaNode(type.equals("prefix"), priority, value).withExtraContext(context).build());
|
||||
} else {
|
||||
holder.setTransientPermission(NodeFactory.makeChatMetaNode(type.equals("prefix"), priority, value).withExtraContext(context).build());
|
||||
}
|
||||
} catch (ObjectAlreadyHasException ignored) {}
|
||||
|
||||
} else {
|
||||
// standard remove
|
||||
List<Node> toRemove = holder.getNodes().stream()
|
||||
.filter(n -> n.isMeta() && n.getMeta().getKey().equals(key))
|
||||
.collect(Collectors.toList());
|
||||
@ -438,8 +474,7 @@ public class LuckPermsSubjectData implements LPSubjectData {
|
||||
toRemove.forEach(n -> {
|
||||
try {
|
||||
holder.unsetPermission(n);
|
||||
} catch (ObjectLacksException ignored) {
|
||||
}
|
||||
} catch (ObjectLacksException ignored) {}
|
||||
});
|
||||
|
||||
try {
|
||||
@ -449,6 +484,8 @@ public class LuckPermsSubjectData implements LPSubjectData {
|
||||
holder.setTransientPermission(NodeFactory.makeMetaNode(key, value).withExtraContext(context).build());
|
||||
}
|
||||
} catch (ObjectAlreadyHasException ignored) {}
|
||||
}
|
||||
|
||||
objectSave(holder);
|
||||
return true;
|
||||
}
|
||||
@ -457,15 +494,18 @@ public class LuckPermsSubjectData implements LPSubjectData {
|
||||
@Override
|
||||
public boolean unsetOption(ContextSet contexts, String key) {
|
||||
try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_SET_OPTION)) {
|
||||
List<Node> toRemove = holder.getNodes().stream()
|
||||
List<Node> toRemove = (enduring ? holder.getNodes() : holder.getTransientNodes()).stream()
|
||||
.filter(n -> n.isMeta() && n.getMeta().getKey().equals(key))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
toRemove.forEach(n -> {
|
||||
try {
|
||||
if (enduring) {
|
||||
holder.unsetPermission(n);
|
||||
} catch (ObjectLacksException ignored) {
|
||||
} else {
|
||||
holder.unsetTransientPermission(n);
|
||||
}
|
||||
} catch (ObjectLacksException ignored) {}
|
||||
});
|
||||
|
||||
objectSave(holder);
|
||||
@ -526,8 +566,7 @@ public class LuckPermsSubjectData implements LPSubjectData {
|
||||
} else {
|
||||
holder.unsetTransientPermission(n);
|
||||
}
|
||||
} catch (ObjectLacksException ignored) {
|
||||
}
|
||||
} catch (ObjectLacksException ignored) {}
|
||||
});
|
||||
|
||||
objectSave(holder);
|
||||
|
Loading…
Reference in New Issue
Block a user