Class ContextSet

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

public final class ContextSet extends Object
An immutable set of context key-value pairs.

ContextSet defines the conditions under which a permission applies. For example, a permission might only apply in the "nether" world or when the player is in "creative" gamemode.

Example Usage


 ContextSet context = ContextSet.builder()
     .add(ContextKey.WORLD, "nether")
     .add(ContextKey.GAMEMODE, "creative")
     .build();

 // Check if context matches
 if (context.matches(playerContext)) {
     // Permission applies
 }
 
See Also:
  • Field Details

    • EMPTY

      public static final ContextSet EMPTY
      An empty context set that matches everything.
  • Method Details

    • builder

      public static ContextSet.Builder builder()
      Creates a new builder for constructing a ContextSet.
      Returns:
      a new Builder instance
    • of

      public static <T> ContextSet of(ContextKey<T> key, T value)
      Creates a ContextSet with a single context value.
      Type Parameters:
      T - the value type
      Parameters:
      key - the context key
      value - the context value
      Returns:
      a new ContextSet with the single context
    • of

      public static <T1, T2> ContextSet of(ContextKey<T1> key1, T1 value1, ContextKey<T2> key2, T2 value2)
      Creates a ContextSet with two context values.
      Type Parameters:
      T1 - the first value type
      T2 - the second value type
      Parameters:
      key1 - the first context key
      value1 - the first context value
      key2 - the second context key
      value2 - the second context value
      Returns:
      a new ContextSet with both contexts
    • get

      public <T> Optional<T> get(ContextKey<T> key)
      Gets a context value by its key.
      Type Parameters:
      T - the value type
      Parameters:
      key - the context key
      Returns:
      an Optional containing the value, or empty if not present
    • contains

      public boolean contains(ContextKey<?> key)
      Checks if this context set contains the specified key.
      Parameters:
      key - the context key to check
      Returns:
      true if the key is present
    • contains

      public <T> boolean contains(ContextKey<T> key, T value)
      Checks if this context set contains a specific key-value pair.
      Type Parameters:
      T - the value type
      Parameters:
      key - the context key
      value - the expected value
      Returns:
      true if the key exists with the specified value
    • matches

      public boolean matches(ContextSet other)
      Checks if this context set matches another context set.

      A context set "matches" another if all keys in this set are present in the other set with matching values. An empty context set matches everything.

      Parameters:
      other - the context set to match against (e.g., player's current context)
      Returns:
      true if all contexts in this set match
    • isEmpty

      public boolean isEmpty()
      Returns whether this context set is empty.
      Returns:
      true if no contexts are defined
    • size

      public int size()
      Returns the number of context entries.
      Returns:
      the size of this context set
    • keys

      public Set<String> keys()
      Returns all context keys in this set.
      Returns:
      an unmodifiable set of key names
    • asMap

      public Map<String,Object> asMap()
      Returns an unmodifiable view of all contexts.
      Returns:
      the context map
    • 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