use of net.luckperms.api.context.Context in project LuckPerms by lucko.
the class NodeCommandFactory method undoCommand.
public static String undoCommand(Node node, String holder, HolderType holderType, boolean explicitGlobalContext) {
StringBuilder sb = new StringBuilder(32);
sb.append(holderType.toString()).append(' ').append(holder).append(' ');
if (node instanceof InheritanceNode) {
// command
sb.append("parent remove");
if (node.hasExpiry()) {
sb.append("temp");
}
sb.append(' ');
// value
sb.append(((InheritanceNode) node).getGroupName());
} else if (node.getValue() && node instanceof ChatMetaNode<?, ?>) {
ChatMetaNode<?, ?> chatNode = (ChatMetaNode<?, ?>) node;
// command
sb.append("meta remove");
if (node.hasExpiry()) {
sb.append("temp");
}
sb.append(chatNode.getMetaType().toString());
// values
sb.append(' ').append(chatNode.getPriority()).append(' ');
appendEscaped(sb, chatNode.getMetaValue());
} else if (node.getValue() && node instanceof MetaNode) {
MetaNode metaNode = (MetaNode) node;
// command
sb.append("meta unset");
if (node.hasExpiry()) {
sb.append("temp");
}
sb.append(' ');
// value
appendEscaped(sb, metaNode.getMetaKey());
} else {
// command
sb.append("permission unset");
if (node.hasExpiry()) {
sb.append("temp");
}
sb.append(' ');
// value
appendEscaped(sb, node.getKey());
}
if (!node.getContexts().isEmpty()) {
for (Context context : node.getContexts()) {
sb.append(' ').append(context.getKey()).append("=").append(context.getValue());
}
} else if (explicitGlobalContext) {
sb.append(" global");
}
return sb.toString();
}
use of net.luckperms.api.context.Context in project LuckPerms by lucko.
the class ImmutableContextSetImpl method toMultimap.
public ImmutableSetMultimap<String, String> toMultimap() {
if (this.cachedMap == null) {
ImmutableSetMultimap.Builder<String, String> builder = ImmutableSetMultimap.builder();
for (Context entry : this.array) {
builder.put(entry.getKey(), entry.getValue());
}
this.cachedMap = builder.build();
}
return this.cachedMap;
}
use of net.luckperms.api.context.Context in project LuckPerms by lucko.
the class ContextSetComparator method compare.
@Override
public int compare(ImmutableContextSet o1, ImmutableContextSet o2) {
if (o1.equals(o2)) {
return 0;
}
// compare presence of any context (non-empty)
int result = Boolean.compare(!o1.isEmpty(), !o2.isEmpty());
if (result != 0) {
return result;
}
// compare presence of a server context
result = Boolean.compare(o1.containsKey(DefaultContextKeys.SERVER_KEY), o2.containsKey(DefaultContextKeys.SERVER_KEY));
if (result != 0) {
return result;
}
// compare presence of a world context
result = Boolean.compare(o1.containsKey(DefaultContextKeys.WORLD_KEY), o2.containsKey(DefaultContextKeys.WORLD_KEY));
if (result != 0) {
return result;
}
// compare overall size
result = Integer.compare(o1.size(), o2.size());
if (result != 0) {
return result;
}
// At this point, we don't really care about the order between the two sets.
// However, we *have* to maintain transitivity in this comparator (despite how
// expensive/complex it may be) as it is used in the PermissionHolder nodes treemap.
// in order to have consistent ordering, we have to compare the content of the context sets.
// to do this, obtain sorted array representations of each set, then compare which is greater
Context[] o1Array = o1 instanceof ImmutableContextSetImpl ? ((ImmutableContextSetImpl) o1).toArray() : toArray(o1);
Context[] o2Array = o2 instanceof ImmutableContextSetImpl ? ((ImmutableContextSetImpl) o2).toArray() : toArray(o2);
for (int i = 0; i < o1Array.length; i++) {
Context ent1 = o1Array[i];
Context ent2 = o2Array[i];
result = ContextComparator.INSTANCE.compare(ent1, ent2);
if (result != 0) {
return result;
}
}
throw new AssertionError("sets are equal? " + o1 + " - " + o2);
}
use of net.luckperms.api.context.Context in project LuckPerms by lucko.
the class ContextSetTest method testImmutableBuilder.
@Test
public void testImmutableBuilder() {
List<Consumer<ImmutableContextSet.Builder>> tests = ImmutableList.of(builder -> {
builder.add("test", "a");
builder.add("test", "b");
builder.add("test", "c");
}, builder -> {
builder.add("test", "c");
builder.add("test", "b");
builder.add("test", "a");
}, builder -> {
builder.add("test", "b");
builder.add("test", "a");
builder.add("test", "c");
}, builder -> {
builder.add("test", "b");
builder.add("test", "c");
builder.add("test", "a");
}, builder -> {
builder.add("test", "a");
builder.add("test", "a");
builder.add("test", "b");
builder.add("test", "c");
});
for (Consumer<ImmutableContextSet.Builder> action : tests) {
ImmutableContextSet.Builder builder = new ImmutableContextSetImpl.BuilderImpl();
action.accept(builder);
ImmutableContextSet set = builder.build();
ImmutableSet<Context> expected = ImmutableSet.of(new ContextImpl("test", "a"), new ContextImpl("test", "b"), new ContextImpl("test", "c"));
assertEquals(expected, set.toSet());
assertEquals(3, set.size());
assertTrue(set.contains("test", "a"));
assertTrue(set.contains("test", "b"));
assertTrue(set.contains("test", "c"));
}
}
Aggregations