| 1 | package net.minecraft.server.permissions;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import it.unimi.dsi.fastutil.objects.ReferenceArraySet;
|
| 5 | import it.unimi.dsi.fastutil.objects.ReferenceSet;
|
| 6 |
|
| 7 | public class PermissionSetUnion implements PermissionSet {
|
| 8 | private final ReferenceSet<PermissionSet> permissions = new ReferenceArraySet<>();
|
| 9 |
|
| 10 | PermissionSetUnion(final PermissionSet first, final PermissionSet second) {
|
| 11 | this.permissions.add(first);
|
| 12 | this.permissions.add(second);
|
| 13 | this.ensureNoUnionsWithinUnions();
|
| 14 | }
|
| 15 |
|
| 16 | private PermissionSetUnion(final ReferenceSet<PermissionSet> oldPermissions, final PermissionSet other) {
|
| 17 | this.permissions.addAll(oldPermissions);
|
| 18 | this.permissions.add(other);
|
| 19 | this.ensureNoUnionsWithinUnions();
|
| 20 | }
|
| 21 |
|
| 22 | private PermissionSetUnion(final ReferenceSet<PermissionSet> oldPermissions, final ReferenceSet<PermissionSet> other) {
|
| 23 | this.permissions.addAll(oldPermissions);
|
| 24 | this.permissions.addAll(other);
|
| 25 | this.ensureNoUnionsWithinUnions();
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | public boolean hasPermission(final Permission permission) {
|
| 30 | for (PermissionSet set : this.permissions) {
|
| 31 | if (set.hasPermission(permission)) {
|
| 32 | return true;
|
| 33 | }
|
| 34 | }
|
| 35 |
|
| 36 | return false;
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public PermissionSet union(final PermissionSet other) {
|
| 41 | return other instanceof PermissionSetUnion otherUnion
|
| 42 | ? new PermissionSetUnion(this.permissions, otherUnion.permissions)
|
| 43 | : new PermissionSetUnion(this.permissions, other);
|
| 44 | }
|
| 45 |
|
| 46 | @VisibleForTesting
|
| 47 | public ReferenceSet<PermissionSet> getPermissions() {
|
| 48 | return new ReferenceArraySet<>(this.permissions);
|
| 49 | }
|
| 50 |
|
| 51 | private void ensureNoUnionsWithinUnions() {
|
| 52 | for (PermissionSet set : this.permissions) {
|
| 53 | if (set instanceof PermissionSetUnion) {
|
| 54 | throw new IllegalArgumentException("Cannot have PermissionSetUnion within another PermissionSetUnion");
|
| 55 | }
|
| 56 | }
|
| 57 | }
|
| 58 | }
|
| 59 |
|