core: fix And/OrPermission losing conditional tree (#296)

This commit is contained in:
Jake Potrebic 2021-09-14 17:43:45 -07:00 committed by Jason
parent 5648128217
commit 9f0ff4e504
4 changed files with 30 additions and 10 deletions

View file

@ -50,11 +50,7 @@ public final class AndPermission implements CommandPermission {
* @return Constructed permission
*/
public static @NonNull CommandPermission of(final @NonNull Collection<CommandPermission> permissions) {
final Set<CommandPermission> permissionSet = new HashSet<>();
for (final CommandPermission permission : permissions) {
permissionSet.addAll(permission.getPermissions());
}
return new AndPermission(permissionSet);
return new AndPermission(new HashSet<>(permissions));
}
@Override

View file

@ -50,11 +50,7 @@ public final class OrPermission implements CommandPermission {
* @return Constructed permission
*/
public static @NonNull CommandPermission of(final @NonNull Collection<CommandPermission> permissions) {
final Set<CommandPermission> permissionSet = new HashSet<>();
for (final CommandPermission permission : permissions) {
permissionSet.addAll(permission.getPermissions());
}
return new OrPermission(permissionSet);
return new OrPermission(new HashSet<>(permissions));
}
@Override

View file

@ -28,13 +28,16 @@ import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.keys.SimpleCloudKey;
import cloud.commandframework.meta.CommandMeta;
import cloud.commandframework.meta.SimpleCommandMeta;
import cloud.commandframework.permission.AndPermission;
import cloud.commandframework.permission.CommandPermission;
import cloud.commandframework.permission.OrPermission;
import cloud.commandframework.permission.Permission;
import cloud.commandframework.permission.PredicatePermission;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicBoolean;
@ -89,6 +92,30 @@ class CommandPermissionTest {
assertTrue(manager.hasPermission(new TestCommandSender("one", "two"), test));
}
@Test
void testComplexOrPermissions() {
final CommandPermission andOne = AndPermission.of(Arrays.asList(Permission.of("perm.one"),
Permission.of("perm.two")));
final CommandPermission andTwo = AndPermission.of(Arrays.asList(Permission.of("perm.three"),
(PredicatePermission<?>) (s) -> false));
final CommandPermission orPermission = OrPermission.of(Arrays.asList(andOne, andTwo));
assertFalse(manager.hasPermission(new TestCommandSender("does.have", "also.does.have"), orPermission));
assertFalse(manager.hasPermission(new TestCommandSender("perm.one", "perm.three"), orPermission));
assertTrue(manager.hasPermission(new TestCommandSender("perm.one", "perm.two"), orPermission));
}
@Test
void testComplexAndPermissions() {
final CommandPermission orOne = OrPermission.of(Arrays.asList(Permission.of("perm.one"),
(PredicatePermission<?>) (s) -> false));
final CommandPermission orTwo = OrPermission.of(Arrays.asList(Permission.of("perm.two"),
Permission.of("perm.three")));
final CommandPermission andPermission = AndPermission.of(Arrays.asList(orOne, orTwo));
assertFalse(manager.hasPermission(new TestCommandSender("perm.one"), andPermission));
assertTrue(manager.hasPermission(new TestCommandSender("perm.one", "perm.two"), andPermission));
assertTrue(manager.hasPermission(new TestCommandSender("perm.one", "perm.three"), andPermission));
}
@Test
void testPredicatePermissions() {
final AtomicBoolean condition = new AtomicBoolean(true);