Interface PerkService


public interface PerkService
Rank-based limits from perks.yml: named tiers tied to permissions, and temporary boosts given to single players.

A player holds every enabled tier whose permission they have, and is shown as belonging to the highest-priority one. The resolution mode decides where each perk comes from: that tier, or the best value among all the tiers they hold. A tier's value comes from the tier itself, then up its parent chain. Every perk no tier gives, and every perk of a player without a tier, follows the server-wide default: settings.yml for host limits, party size and scenario settings, the defaults of perks.yml for the rest, and an addon's own default for its perks. Active personal and server-wide boosts apply on top.

Addons can register perks of their own, which resolve exactly as the built-in ones do. A number value of PerkDefinition.UNLIMITED in perks.yml means unlimited for perks that support it; getNumber(org.bukkit.entity.Player, dev.itsharshxd.zentrix.api.perk.Perk) answers Integer.MAX_VALUE for it, so limits compare as usual.

Zentrix asks for a perk when the player acts - creating a game, inviting someone - so a rank that expires takes nothing away the player already has. When a perk stops a player, PerkLimitReachedEvent fires.

Reads answer at once, on the main thread, from what Zentrix holds for online players. Boost changes are stored with the player's data, work for offline players and complete on the main thread; they complete exceptionally while storage is unavailable.


 PerkService perks = ZentrixAPI.get().getPerkService();
 int slots = perks.getNumber(player, Perk.CUSTOM_GAMES_MAX_HOSTED);
 perks.grantBoost(player.getUniqueId(), Perk.CUSTOM_GAMES_MAX_HOSTED, 1, Duration.ofHours(24), "shop");
 
Since:
1.9.0
  • Method Details

    • getTiers

      @NotNull @NotNull List<PerkTier> getTiers()
      The enabled tiers, highest priority first. Empty when perks.yml enables none.
    • getTier

      @NotNull @NotNull Optional<PerkTier> getTier(@NotNull @NotNull String id)
      An enabled tier by its perks.yml key, ignoring case.
    • getTier

      @NotNull @NotNull Optional<PerkTier> getTier(@NotNull @NotNull org.bukkit.entity.Player player)
      The tier a player belongs to now, or empty when they hold no tier's permission.
    • getNumber

      int getNumber(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull Perk perk)
      A Perk.Type.NUMBER perk as it applies to a player now: their tier's value or the server-wide default, plus their active boosts.
      Throws:
      IllegalArgumentException - for a toggle perk
    • isAllowed

      boolean isAllowed(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull Perk perk)
      Whether a Perk.Type.TOGGLE perk is allowed for a player now: by their tier, the server-wide default, or an active boost.
      Throws:
      IllegalArgumentException - for a number perk
    • getDefaultNumber

      int getDefaultNumber(@NotNull @NotNull Perk perk)
      The server-wide default of a Perk.Type.NUMBER perk, for players without a tier.
    • isAllowedByDefault

      boolean isAllowedByDefault(@NotNull @NotNull Perk perk)
      The server-wide default of a Perk.Type.TOGGLE perk, for players without a tier.
    • getNextTier

      @NotNull @NotNull Optional<PerkTier> getNextTier(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull Perk perk)
      The tier a player would have to reach for more of a perk: the lowest-priority enabled tier above their own whose value is higher, or that allows a toggle they are refused. Under PerkResolutionMode.BEST_VALUE, the lowest-priority enabled tier they do not hold that would beat what they get now. What the limit message names, so a shop can offer it.
      Returns:
      empty when no tier above theirs would give them more
    • getBoosts

      @NotNull @NotNull List<PerkBoost> getBoosts(@NotNull @NotNull UUID playerId)
      A player's active boosts, as last read. Empty for offline players; see refreshBoosts(java.util.UUID).
    • refreshBoosts

      @NotNull @NotNull CompletableFuture<List<PerkBoost>> refreshBoosts(@NotNull @NotNull UUID playerId)
      Reads a player's boosts as stored now, online or offline, which another server may have changed.
    • grantBoost

      @NotNull @NotNull CompletableFuture<PerkBoost> grantBoost(@NotNull @NotNull UUID playerId, @NotNull @NotNull Perk perk, int amount, @NotNull @NotNull Duration duration, @NotNull @NotNull String source)
      Gives a player a temporary boost, online or offline. It ends on its own after duration.
      Parameters:
      amount - added to a number perk; must be positive. Ignored by a toggle perk, which the boost allows
      duration - how long it lasts; must be positive
      source - who or what gave it, kept with the boost
      Returns:
      completes on the main thread with the boost as stored
      Throws:
      IllegalArgumentException - when the amount or duration is not positive
    • removeBoost

      @NotNull @NotNull CompletableFuture<Boolean> removeBoost(@NotNull @NotNull UUID playerId, @NotNull @NotNull String boostId)
      Ends one of a player's boosts early.
      Returns:
      completes with whether the boost existed
    • clearBoosts

      @NotNull @NotNull CompletableFuture<Integer> clearBoosts(@NotNull @NotNull UUID playerId, @Nullable @Nullable Perk perk)
      Ends a player's boosts early: every boost, or every boost of one perk.
      Parameters:
      perk - the perk whose boosts end, or null for all of them
      Returns:
      completes with how many boosts ended
    • registerPerk

      void registerPerk(@NotNull org.bukkit.plugin.Plugin owner, @NotNull @NotNull PerkDefinition definition)
      Registers an addon perk. From then on tiers in perks.yml can set it under its key, boosts can raise it, and /perks, the tier editor and placeholders (%zentrix_perk_<key>%, dots and dashes as underscores) show it, exactly as they do built-in perks.

      Values and boosts of a perk that is not registered are kept as they are, so an addon that loads late, or is removed for a while, finds them again. The perk is unregistered when its owner disables. Main thread, such as from the addon's onEnable.

      Parameters:
      owner - the addon registering it
      definition - the perk; build one with PerkDefinition.number(java.lang.String) or PerkDefinition.toggle(java.lang.String)
      Throws:
      IllegalArgumentException - when the key is a built-in perk's, or another addon's
      IllegalStateException - off the main thread
      Since:
      1.16.0
    • unregisterPerk

      boolean unregisterPerk(@NotNull @NotNull String key)
      Unregisters an addon perk. Its values in perks.yml and its stored boosts are kept.
      Returns:
      whether it was registered
      Since:
      1.16.0
    • getPerks

      @NotNull @NotNull List<PerkDefinition> getPerks()
      Every perk, built-in perks first in their order, then registered addon perks.
      Since:
      1.16.0
    • getPerk

      @NotNull @NotNull Optional<PerkDefinition> getPerk(@NotNull @NotNull String key)
      A built-in or registered addon perk by its exact key.
      Since:
      1.16.0
    • getNumber

      int getNumber(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull String perkKey)
      A number perk, built-in or addon, as it applies to a player now. Integer.MAX_VALUE when unlimited.
      Throws:
      IllegalArgumentException - for a toggle perk or a key nothing registered
      Since:
      1.16.0
    • isAllowed

      boolean isAllowed(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull String perkKey)
      Whether a toggle perk, built-in or addon, is allowed for a player now.
      Throws:
      IllegalArgumentException - for a number perk or a key nothing registered
      Since:
      1.16.0
    • getNextTier

      @NotNull @NotNull Optional<PerkTier> getNextTier(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull String perkKey)
      The tier a player would have to reach for more of a perk, built-in or addon, as getNextTier(Player, Perk) answers it.
      Since:
      1.16.0
    • getResolutionMode

      @NotNull @NotNull PerkResolutionMode getResolutionMode()
      How a player holding several tiers gets each perk, from perks.yml.
      Since:
      1.16.0
    • getAllBoosts

      @NotNull @NotNull List<PerkBoost> getAllBoosts(@NotNull @NotNull UUID playerId)
      A player's active boosts of every perk, addon perks included, as last read. Boosts of addon perks have a null PerkBoost.perk(); getBoosts(java.util.UUID) leaves them out.
      Since:
      1.16.0
    • grantBoost

      @NotNull @NotNull CompletableFuture<PerkBoost> grantBoost(@NotNull @NotNull UUID playerId, @NotNull @NotNull String perkKey, int amount, @NotNull @NotNull Duration duration, @NotNull @NotNull String source)
      Gives a player a temporary boost of any perk, built-in or addon, by its key.
      Throws:
      IllegalArgumentException - when the amount or duration is not positive, or the key names no perk
      Since:
      1.16.0
    • getServerBoosts

      @NotNull @NotNull List<ServerBoost> getServerBoosts()
      The server-wide boosts in force, as last read. They apply to every player, on top of their own boosts, and are shared by every server on the same database.
      Since:
      1.16.0
    • startServerBoost

      @NotNull @NotNull CompletableFuture<ServerBoost> startServerBoost(@NotNull @NotNull String perkKey, int amount, @NotNull @NotNull Duration duration, @NotNull @NotNull String source)
      Starts a server-wide boost, announced to players and ending on its own after duration.
      Parameters:
      amount - added to a number perk; must be positive. Ignored by a toggle perk
      Returns:
      completes on the main thread with the boost as stored
      Throws:
      IllegalArgumentException - when the amount or duration is not positive, or the key names no perk
      Since:
      1.16.0
    • endServerBoost

      @NotNull @NotNull CompletableFuture<Boolean> endServerBoost(@NotNull @NotNull String boostId)
      Ends one server-wide boost early.
      Returns:
      completes with whether it existed
      Since:
      1.16.0