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

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.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,24 +433,59 @@ 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)) {
List<Node> toRemove = holder.getNodes().stream()
.filter(n -> n.isMeta() && n.getMeta().getKey().equals(key))
.collect(Collectors.toList());
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;
toRemove.forEach(n -> {
try {
holder.unsetPermission(n);
} catch (ObjectLacksException ignored) {
}
});
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());
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);
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 {
holder.unsetPermission(n);
} catch (ObjectLacksException ignored) {
}
if (enduring) {
holder.unsetPermission(n);
} 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);