mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-28 05:35:26 +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));
|
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) {
|
public static NodeBuilder makePrefixNode(int priority, String prefix) {
|
||||||
return new NodeBuilder("prefix." + priority + "." + MetaUtils.escapeCharacters(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.ContextSet;
|
||||||
import me.lucko.luckperms.api.context.ImmutableContextSet;
|
import me.lucko.luckperms.api.context.ImmutableContextSet;
|
||||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
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.NodeBuilder;
|
||||||
import me.lucko.luckperms.common.core.NodeFactory;
|
import me.lucko.luckperms.common.core.NodeFactory;
|
||||||
import me.lucko.luckperms.common.core.model.Group;
|
import me.lucko.luckperms.common.core.model.Group;
|
||||||
import me.lucko.luckperms.common.core.model.PermissionHolder;
|
import me.lucko.luckperms.common.core.model.PermissionHolder;
|
||||||
import me.lucko.luckperms.common.core.model.User;
|
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.ObjectAlreadyHasException;
|
||||||
import me.lucko.luckperms.exceptions.ObjectLacksException;
|
import me.lucko.luckperms.exceptions.ObjectLacksException;
|
||||||
import me.lucko.luckperms.sponge.service.base.LPSubject;
|
import me.lucko.luckperms.sponge.service.base.LPSubject;
|
||||||
@ -431,24 +433,59 @@ public class LuckPermsSubjectData implements LPSubjectData {
|
|||||||
@Override
|
@Override
|
||||||
public boolean setOption(@NonNull ContextSet context, @NonNull String key, @NonNull String value) {
|
public boolean setOption(@NonNull ContextSet context, @NonNull String key, @NonNull String value) {
|
||||||
try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_SET_OPTION)) {
|
try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_SET_OPTION)) {
|
||||||
List<Node> toRemove = holder.getNodes().stream()
|
if (key.equalsIgnoreCase("prefix") || key.equalsIgnoreCase("suffix")) {
|
||||||
.filter(n -> n.isMeta() && n.getMeta().getKey().equals(key))
|
// special handling.
|
||||||
.collect(Collectors.toList());
|
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;
|
||||||
|
|
||||||
toRemove.forEach(n -> {
|
|
||||||
try {
|
try {
|
||||||
holder.unsetPermission(n);
|
if (enduring) {
|
||||||
} catch (ObjectLacksException ignored) {
|
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());
|
||||||
|
|
||||||
|
toRemove.forEach(n -> {
|
||||||
|
try {
|
||||||
|
holder.unsetPermission(n);
|
||||||
|
} catch (ObjectLacksException ignored) {}
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (enduring) {
|
||||||
|
holder.setPermission(NodeFactory.makeMetaNode(key, value).withExtraContext(context).build());
|
||||||
|
} else {
|
||||||
|
holder.setTransientPermission(NodeFactory.makeMetaNode(key, value).withExtraContext(context).build());
|
||||||
|
}
|
||||||
|
} catch (ObjectAlreadyHasException ignored) {}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
if (enduring) {
|
|
||||||
holder.setPermission(NodeFactory.makeMetaNode(key, value).withExtraContext(context).build());
|
|
||||||
} else {
|
|
||||||
holder.setTransientPermission(NodeFactory.makeMetaNode(key, value).withExtraContext(context).build());
|
|
||||||
}
|
|
||||||
} catch (ObjectAlreadyHasException ignored) {}
|
|
||||||
objectSave(holder);
|
objectSave(holder);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -457,15 +494,18 @@ public class LuckPermsSubjectData implements LPSubjectData {
|
|||||||
@Override
|
@Override
|
||||||
public boolean unsetOption(ContextSet contexts, String key) {
|
public boolean unsetOption(ContextSet contexts, String key) {
|
||||||
try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_SET_OPTION)) {
|
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))
|
.filter(n -> n.isMeta() && n.getMeta().getKey().equals(key))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
toRemove.forEach(n -> {
|
toRemove.forEach(n -> {
|
||||||
try {
|
try {
|
||||||
holder.unsetPermission(n);
|
if (enduring) {
|
||||||
} catch (ObjectLacksException ignored) {
|
holder.unsetPermission(n);
|
||||||
}
|
} else {
|
||||||
|
holder.unsetTransientPermission(n);
|
||||||
|
}
|
||||||
|
} catch (ObjectLacksException ignored) {}
|
||||||
});
|
});
|
||||||
|
|
||||||
objectSave(holder);
|
objectSave(holder);
|
||||||
@ -526,8 +566,7 @@ public class LuckPermsSubjectData implements LPSubjectData {
|
|||||||
} else {
|
} else {
|
||||||
holder.unsetTransientPermission(n);
|
holder.unsetTransientPermission(n);
|
||||||
}
|
}
|
||||||
} catch (ObjectLacksException ignored) {
|
} catch (ObjectLacksException ignored) {}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
objectSave(holder);
|
objectSave(holder);
|
||||||
|
Loading…
Reference in New Issue
Block a user