Class PermissionResult

java.lang.Object
dev.polv.taleapi.permission.PermissionResult

public final class PermissionResult extends Object
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 Details

    • ALLOWED

      public static final PermissionResult ALLOWED
      Result indicating the permission was allowed with no payload.
    • DENIED

      public static final PermissionResult DENIED
      Result indicating the permission was denied with no payload.
    • UNDEFINED

      public static final PermissionResult UNDEFINED
      Result indicating the permission was not defined (undefined).
  • Method Details

    • of

      public static PermissionResult of(Tristate state)
      Creates a result with the given state and no payload.
      Parameters:
      state - the permission state
      Returns:
      a PermissionResult with the given state
    • of

      public static PermissionResult of(Tristate state, Object payload)
      Creates a result with the given state and payload.
      Parameters:
      state - the permission state
      payload - the dynamic value payload
      Returns:
      a new PermissionResult
    • allow

      public static PermissionResult allow(Object payload)
      Creates an ALLOW result with the given payload.
      Parameters:
      payload - the dynamic value
      Returns:
      a PermissionResult with ALLOW state and the payload
    • deny

      public static PermissionResult deny(Object payload)
      Creates a DENY result with the given payload.
      Parameters:
      payload - the dynamic value
      Returns:
      a PermissionResult with DENY state and the payload
    • getState

      public Tristate getState()
      Returns the permission state.
      Returns:
      the tristate (ALLOW, DENY, or UNDEFINED)
    • isAllowed

      public boolean isAllowed()
      Checks if the permission is allowed.
      Returns:
      true if state is ALLOW
    • isDenied

      public boolean isDenied()
      Checks if the permission is denied.
      Returns:
      true if state is DENY
    • isUndefined

      public boolean isUndefined()
      Checks if the permission is undefined.
      Returns:
      true if state is UNDEFINED
    • hasPayload

      public boolean hasPayload()
      Returns whether this result has a payload value.
      Returns:
      true if a payload is present
    • getPayload

      public Optional<Object> 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

      public OptionalInt 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

      public OptionalLong 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

      public OptionalDouble asOptionalDouble()
      Returns the payload as an optional double.
      Returns:
      OptionalDouble with the value, or empty
    • asString

      public String asString()
      Returns the payload as a string.
      Returns:
      the payload's string representation, or null if no payload
    • asString

      public String asString(String defaultValue)
      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

      public <T> Optional<T> as(Class<T> type)
      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

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object