Respect prefix option set in subject - closes #149

This commit is contained in:
Luck 2017-02-14 17:11:17 +00:00
parent a3a23d8870
commit bd97a7c5d2
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 64 additions and 21 deletions

View File

@ -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));
} }

View File

@ -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,6 +433,40 @@ 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)) {
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() List<Node> toRemove = holder.getNodes().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());
@ -438,8 +474,7 @@ public class LuckPermsSubjectData implements LPSubjectData {
toRemove.forEach(n -> { toRemove.forEach(n -> {
try { try {
holder.unsetPermission(n); holder.unsetPermission(n);
} catch (ObjectLacksException ignored) { } catch (ObjectLacksException ignored) {}
}
}); });
try { try {
@ -449,6 +484,8 @@ public class LuckPermsSubjectData implements LPSubjectData {
holder.setTransientPermission(NodeFactory.makeMetaNode(key, value).withExtraContext(context).build()); holder.setTransientPermission(NodeFactory.makeMetaNode(key, value).withExtraContext(context).build());
} }
} catch (ObjectAlreadyHasException ignored) {} } 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 {
if (enduring) {
holder.unsetPermission(n); holder.unsetPermission(n);
} catch (ObjectLacksException ignored) { } 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);