fix: deep nesting on permissions causing stack overflow errors

This commit is contained in:
Jason Penilla 2021-10-04 14:22:34 -07:00 committed by Jason
parent 4eb3df1f57
commit 8ea7d59736
3 changed files with 22 additions and 6 deletions

View file

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

View file

@ -64,7 +64,7 @@ public interface CommandPermission {
final Set<CommandPermission> permission = new HashSet<>(2); final Set<CommandPermission> permission = new HashSet<>(2);
permission.add(this); permission.add(this);
permission.add(other); permission.add(other);
return new OrPermission(permission); return OrPermission.of(permission);
} }
/** /**
@ -79,7 +79,7 @@ public interface CommandPermission {
final Set<CommandPermission> permission = new HashSet<>(other.length + 1); final Set<CommandPermission> permission = new HashSet<>(other.length + 1);
permission.add(this); permission.add(this);
permission.addAll(Arrays.asList(other)); permission.addAll(Arrays.asList(other));
return new OrPermission(permission); return OrPermission.of(permission);
} }
/** /**
@ -94,7 +94,7 @@ public interface CommandPermission {
final Set<CommandPermission> permission = new HashSet<>(2); final Set<CommandPermission> permission = new HashSet<>(2);
permission.add(this); permission.add(this);
permission.add(other); permission.add(other);
return new AndPermission(permission); return AndPermission.of(permission);
} }
/** /**
@ -109,7 +109,7 @@ public interface CommandPermission {
final Set<CommandPermission> permission = new HashSet<>(other.length + 1); final Set<CommandPermission> permission = new HashSet<>(other.length + 1);
permission.add(this); permission.add(this);
permission.addAll(Arrays.asList(other)); permission.addAll(Arrays.asList(other));
return new AndPermission(permission); return AndPermission.of(permission);
} }
} }

View file

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