Package dev.polv.taleapi.permission
Class PermissionResult
java.lang.Object
dev.polv.taleapi.permission.PermissionResult
The result of a permission query - not just a boolean!
PermissionResult contains the Tristate and an optional payload value.
This solves the "magic number" problem in traditional permission systems where
limits are encoded in permission strings (e.g., "plots.limit.5").
Dynamic Values
// Old way (bad): "plots.limit.5" - requires string parsing
// New way (good): Permission key is "plots.limit", payload is 5
PermissionResult result = permService.query(user, "plots.limit");
int maxPlots = result.asInt(1); // Returns 1 if not set
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final PermissionResultResult indicating the permission was allowed with no payload.static final PermissionResultResult indicating the permission was denied with no payload.static final PermissionResultResult indicating the permission was not defined (undefined). -
Method Summary
Modifier and TypeMethodDescriptionstatic PermissionResultCreates an ALLOW result with the given payload.<T> Optional<T>Returns the payload cast to the specified type.booleanasBoolean(boolean defaultValue) Returns the payload as a boolean.doubleasDouble(double defaultValue) Returns the payload as a double.intasInt(int defaultValue) Returns the payload as an integer.longasLong(long defaultValue) Returns the payload as a long.Returns the payload as an optional double.Returns the payload as an optional integer.Returns the payload as an optional long.asString()Returns the payload as a string.Returns the payload as a string with a default.static PermissionResultCreates a DENY result with the given payload.booleanReturns the raw payload value.getState()Returns the permission state.inthashCode()booleanReturns whether this result has a payload value.booleanChecks if the permission is allowed.booleanisDenied()Checks if the permission is denied.booleanChecks if the permission is undefined.static PermissionResultCreates a result with the given state and no payload.static PermissionResultCreates a result with the given state and payload.toString()
-
Field Details
-
ALLOWED
Result indicating the permission was allowed with no payload. -
DENIED
Result indicating the permission was denied with no payload. -
UNDEFINED
Result indicating the permission was not defined (undefined).
-
-
Method Details
-
of
Creates a result with the given state and no payload.- Parameters:
state- the permission state- Returns:
- a PermissionResult with the given state
-
of
Creates a result with the given state and payload.- Parameters:
state- the permission statepayload- the dynamic value payload- Returns:
- a new PermissionResult
-
allow
Creates an ALLOW result with the given payload.- Parameters:
payload- the dynamic value- Returns:
- a PermissionResult with ALLOW state and the payload
-
deny
Creates a DENY result with the given payload.- Parameters:
payload- the dynamic value- Returns:
- a PermissionResult with DENY state and the payload
-
getState
Returns the permission state.- Returns:
- the tristate (ALLOW, DENY, or UNDEFINED)
-
isAllowed
public boolean isAllowed()Checks if the permission is allowed.- Returns:
trueif state is ALLOW
-
isDenied
public boolean isDenied()Checks if the permission is denied.- Returns:
trueif state is DENY
-
isUndefined
public boolean isUndefined()Checks if the permission is undefined.- Returns:
trueif state is UNDEFINED
-
hasPayload
public boolean hasPayload()Returns whether this result has a payload value.- Returns:
trueif a payload is present
-
getPayload
Returns the raw payload value.- Returns:
- an Optional containing the payload, or empty
-
asInt
public int asInt(int defaultValue) Returns the payload as an integer.- Parameters:
defaultValue- the value to return if no payload or wrong type- Returns:
- the payload as int, or defaultValue
-
asOptionalInt
Returns the payload as an optional integer.- Returns:
- OptionalInt with the value, or empty
-
asLong
public long asLong(long defaultValue) Returns the payload as a long.- Parameters:
defaultValue- the value to return if no payload or wrong type- Returns:
- the payload as long, or defaultValue
-
asOptionalLong
Returns the payload as an optional long.- Returns:
- OptionalLong with the value, or empty
-
asDouble
public double asDouble(double defaultValue) Returns the payload as a double.- Parameters:
defaultValue- the value to return if no payload or wrong type- Returns:
- the payload as double, or defaultValue
-
asOptionalDouble
Returns the payload as an optional double.- Returns:
- OptionalDouble with the value, or empty
-
asString
Returns the payload as a string.- Returns:
- the payload's string representation, or null if no payload
-
asString
Returns the payload as a string with a default.- Parameters:
defaultValue- the value to return if no payload- Returns:
- the payload string, or defaultValue
-
asBoolean
public boolean asBoolean(boolean defaultValue) Returns the payload as a boolean.- Parameters:
defaultValue- the value to return if no payload or wrong type- Returns:
- the payload as boolean, or defaultValue
-
as
Returns the payload cast to the specified type.- Type Parameters:
T- the type parameter- Parameters:
type- the expected class type- Returns:
- an Optional containing the typed payload, or empty if wrong type
-
equals
-
hashCode
public int hashCode() -
toString
-