mirror of
https://github.com/PaperMC/Velocity.git
synced 2026-02-17 22:47:42 +01:00
Merge branch 'use/adventure-permission' of https://github.com/RealBauHD/Velocity into RealBauHD-use/adventure-permission
This commit is contained in:
@@ -9,9 +9,9 @@ package com.velocitypowered.api.event.permission;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.velocitypowered.api.event.annotation.AwaitingEvent;
|
||||
import com.velocitypowered.api.permission.PermissionFunction;
|
||||
import com.velocitypowered.api.permission.PermissionProvider;
|
||||
import com.velocitypowered.api.permission.PermissionSubject;
|
||||
import net.kyori.adventure.permission.PermissionChecker;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
/**
|
||||
@@ -44,13 +44,13 @@ public final class PermissionsSetupEvent {
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the provider function to obtain a {@link PermissionFunction} for the subject.
|
||||
* Uses the provider function to obtain a {@link PermissionChecker} for the subject.
|
||||
*
|
||||
* @param subject the subject
|
||||
* @return the obtained permission function
|
||||
*/
|
||||
public PermissionFunction createFunction(PermissionSubject subject) {
|
||||
return this.provider.createFunction(subject);
|
||||
public PermissionChecker createChecker(PermissionSubject subject) {
|
||||
return this.provider.createChecker(subject);
|
||||
}
|
||||
|
||||
public PermissionProvider getProvider() {
|
||||
@@ -58,7 +58,7 @@ public final class PermissionsSetupEvent {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link PermissionFunction} that should be used for the subject.
|
||||
* Sets the {@link PermissionChecker} that should be used for the subject.
|
||||
*
|
||||
* <p>Specifying <code>null</code> will reset the provider to the default
|
||||
* instance given when the event was posted.</p>
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018-2021 Velocity Contributors
|
||||
*
|
||||
* The Velocity API is licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in the api top-level directory.
|
||||
*/
|
||||
|
||||
package com.velocitypowered.api.permission;
|
||||
|
||||
/**
|
||||
* Function that calculates the permission settings for a given {@link PermissionSubject}.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface PermissionFunction {
|
||||
|
||||
/**
|
||||
* A permission function that always returns {@link Tristate#TRUE}.
|
||||
*/
|
||||
PermissionFunction ALWAYS_TRUE = p -> Tristate.TRUE;
|
||||
|
||||
/**
|
||||
* A permission function that always returns {@link Tristate#FALSE}.
|
||||
*/
|
||||
PermissionFunction ALWAYS_FALSE = p -> Tristate.FALSE;
|
||||
|
||||
/**
|
||||
* A permission function that always returns {@link Tristate#UNDEFINED}.
|
||||
*/
|
||||
PermissionFunction ALWAYS_UNDEFINED = p -> Tristate.UNDEFINED;
|
||||
|
||||
/**
|
||||
* Gets the subjects setting for a particular permission.
|
||||
*
|
||||
* @param permission the permission
|
||||
* @return the value the permission is set to
|
||||
*/
|
||||
Tristate getPermissionValue(String permission);
|
||||
}
|
||||
@@ -7,17 +7,19 @@
|
||||
|
||||
package com.velocitypowered.api.permission;
|
||||
|
||||
import net.kyori.adventure.permission.PermissionChecker;
|
||||
|
||||
/**
|
||||
* Provides {@link PermissionFunction}s for {@link PermissionSubject}s.
|
||||
* Provides {@link PermissionChecker}s for {@link PermissionSubject}s.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface PermissionProvider {
|
||||
|
||||
/**
|
||||
* Creates a {@link PermissionFunction} for the subject.
|
||||
* Creates a {@link PermissionChecker} for the subject.
|
||||
*
|
||||
* @param subject the subject
|
||||
* @return the function
|
||||
*/
|
||||
PermissionFunction createFunction(PermissionSubject subject);
|
||||
PermissionChecker createChecker(PermissionSubject subject);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
package com.velocitypowered.api.permission;
|
||||
|
||||
import net.kyori.adventure.permission.PermissionChecker;
|
||||
import net.kyori.adventure.util.TriState;
|
||||
|
||||
/**
|
||||
* Represents a object that has a set of queryable permissions.
|
||||
@@ -21,7 +22,7 @@ public interface PermissionSubject {
|
||||
* @return whether or not the subject has the permission
|
||||
*/
|
||||
default boolean hasPermission(String permission) {
|
||||
return getPermissionValue(permission).asBoolean();
|
||||
return this.getPermissionChecker().test(permission);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -30,14 +31,14 @@ public interface PermissionSubject {
|
||||
* @param permission the permission
|
||||
* @return the value the permission is set to
|
||||
*/
|
||||
Tristate getPermissionValue(String permission);
|
||||
default TriState getPermissionValue(String permission) {
|
||||
return this.getPermissionChecker().value(permission);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the permission checker for the subject.
|
||||
*
|
||||
* @return subject's permission checker
|
||||
*/
|
||||
default PermissionChecker getPermissionChecker() {
|
||||
return permission -> getPermissionValue(permission).toAdventureTriState();
|
||||
}
|
||||
PermissionChecker getPermissionChecker();
|
||||
}
|
||||
|
||||
@@ -1,118 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018-2023 Velocity Contributors
|
||||
*
|
||||
* The Velocity API is licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in the api top-level directory.
|
||||
*/
|
||||
|
||||
package com.velocitypowered.api.permission;
|
||||
|
||||
import java.util.Optional;
|
||||
import net.kyori.adventure.util.TriState;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
/**
|
||||
* Represents three different states of a setting.
|
||||
*
|
||||
* <p>Possible values:</p>
|
||||
* <p></p>
|
||||
* <ul>
|
||||
* <li>{@link #TRUE} - a positive setting</li>
|
||||
* <li>{@link #FALSE} - a negative (negated) setting</li>
|
||||
* <li>{@link #UNDEFINED} - a non-existent setting</li>
|
||||
* </ul>
|
||||
*/
|
||||
public enum Tristate {
|
||||
|
||||
/**
|
||||
* A value indicating a positive setting.
|
||||
*/
|
||||
TRUE(true),
|
||||
|
||||
/**
|
||||
* A value indicating a negative (negated) setting.
|
||||
*/
|
||||
FALSE(false),
|
||||
|
||||
/**
|
||||
* A value indicating a non-existent setting.
|
||||
*/
|
||||
UNDEFINED(false);
|
||||
|
||||
/**
|
||||
* Returns a {@link Tristate} from a boolean.
|
||||
*
|
||||
* @param val the boolean value
|
||||
* @return {@link #TRUE} or {@link #FALSE}, if the value is <code>true</code> or
|
||||
* <code>false</code>, respectively.
|
||||
*/
|
||||
public static Tristate fromBoolean(boolean val) {
|
||||
return val ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link Tristate} from a nullable boolean.
|
||||
*
|
||||
* <p>Unlike {@link #fromBoolean(boolean)}, this method returns {@link #UNDEFINED}
|
||||
* if the value is null.</p>
|
||||
*
|
||||
* @param val the boolean value
|
||||
* @return {@link #UNDEFINED}, {@link #TRUE} or {@link #FALSE}, if the value is <code>null</code>,
|
||||
* <code>true</code> or <code>false</code>, respectively.
|
||||
*/
|
||||
public static Tristate fromNullableBoolean(@Nullable Boolean val) {
|
||||
if (val == null) {
|
||||
return UNDEFINED;
|
||||
}
|
||||
return val ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link Tristate} from an {@link Optional}.
|
||||
*
|
||||
* <p>Unlike {@link #fromBoolean(boolean)}, this method returns {@link #UNDEFINED}
|
||||
* if the value is empty.</p>
|
||||
*
|
||||
* @param val the optional boolean value
|
||||
* @return {@link #UNDEFINED}, {@link #TRUE} or {@link #FALSE}, if the value is empty,
|
||||
* <code>true</code> or <code>false</code>, respectively.
|
||||
*/
|
||||
public static Tristate fromOptionalBoolean(Optional<Boolean> val) {
|
||||
return val.map(Tristate::fromBoolean).orElse(UNDEFINED);
|
||||
}
|
||||
|
||||
|
||||
private final boolean booleanValue;
|
||||
|
||||
Tristate(boolean booleanValue) {
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of the Tristate as a boolean.
|
||||
*
|
||||
* <p>A value of {@link #UNDEFINED} converts to false.</p>
|
||||
*
|
||||
* @return a boolean representation of the Tristate.
|
||||
*/
|
||||
public boolean asBoolean() {
|
||||
return this.booleanValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the equivalent Adventure {@link TriState}.
|
||||
*
|
||||
* @return equivalent Adventure TriState
|
||||
*/
|
||||
public TriState toAdventureTriState() {
|
||||
if (this == Tristate.TRUE) {
|
||||
return TriState.TRUE;
|
||||
} else if (this == Tristate.UNDEFINED) {
|
||||
return TriState.NOT_SET;
|
||||
} else if (this == Tristate.FALSE) {
|
||||
return TriState.FALSE;
|
||||
} else {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user