Interface CosmeticsService


public interface CosmeticsService
The cosmetics each player owns, per category, and the one they chose in each.

Ownership is stored with the rest of the player's data, so a database shared by several servers shares it too. Every change works for offline players, applies to the data as stored now - which another server may have changed - and completes on the main thread. Online players' cosmetics are also cached, read as they join; getOwnership(java.util.UUID, dev.itsharshxd.zentrix.api.cosmetics.CosmeticCategory) answers from that cache.

Changes complete exceptionally while storage is unavailable, including while Zentrix runs on its in-memory stand-in for a database it could not reach; check isAvailable() first. A change that altered anything fires CosmeticOwnershipChangeEvent.


 CosmeticsService cosmetics = ZentrixAPI.get().getCosmeticsService();
 cosmetics.grant(playerId, CosmeticCategory.GLIDERS, List.of("phoenix"))
     .thenAccept(change -> {
         if (change.changed()) {
             // unlocked
         }
     });
 

Every type, and your own menu (since 1.11.0)

Methods taking a CosmeticTypeKey work alike for Zentrix's own categories and for types addons register. Together they hold everything /cosmetics does, so an addon can build its own cosmetics menu, mixing in cosmetics of other plugins if it likes:


 for (CosmeticType type : cosmetics.getTypes()) {
     if (!type.available()) continue;
     for (CosmeticState state : cosmetics.getStates(player, type.key())) {
         // draw state.cosmetic(), marked by state.equipped(), state.canUse(), state.purchasable()
     }
 }
 // on a click:
 cosmetics.equip(player, type.key(), cosmeticId).thenAccept(result -> {
     if (result.status() == CosmeticResult.Status.LOCKED) {
         // offer cosmetics.purchase(player, type.key(), cosmeticId)
     }
 });
 

Methods that read or act for an online Player must be called on the main thread.

Since:
1.8.0
  • Method Details

    • isAvailable

      boolean isAvailable()
      Whether cosmetics can be read and changed now.
    • isLoaded

      boolean isLoaded(@NotNull @NotNull UUID playerId)
      Whether an online player's cosmetics have been read since they joined.
    • getOwnership

      @NotNull @NotNull CosmeticOwnership getOwnership(@NotNull @NotNull UUID playerId, @NotNull @NotNull CosmeticCategory category)
      An online player's cosmetics of one category, as last read. Empty ownership for offline players and before the first read; use refresh(UUID) to read them from storage.
    • refresh

      @NotNull @NotNull CompletableFuture<Map<CosmeticCategory,CosmeticOwnership>> refresh(@NotNull @NotNull UUID playerId)
      Reads a player's cosmetics as stored now, online or offline, refreshing the cache of an online player.
      Returns:
      completes on the main thread with every category
    • grant

      @NotNull @NotNull CompletableFuture<CosmeticChange> grant(@NotNull @NotNull UUID playerId, @NotNull @NotNull CosmeticCategory category, @NotNull @NotNull Collection<String> ids)
      Grants cosmetics permanently. Ones already owned permanently are left as they are; ones owned for a limited time become permanent.
    • grantTimed

      @NotNull default @NotNull CompletableFuture<CosmeticChange> grantTimed(@NotNull @NotNull UUID playerId, @NotNull @NotNull CosmeticCategory category, @NotNull @NotNull Collection<String> ids, @NotNull @NotNull Duration duration)
      Grants cosmetics for a limited time, such as a glider for seven days.

      A cosmetic the player does not own is granted until duration from now. One they already own for a limited time keeps its remaining time with duration added. One they own permanently is left as it is: a timed grant never shortens a permanent one.

      When the time runs out the cosmetic is taken away on its own, even while the player is offline or the server is restarting, and on every server sharing the database. A selection of it falls back to the server's default, and CosmeticOwnershipChangeEvent fires with CosmeticOwnershipChangeEvent.Cause.EXPIRED.

      Parameters:
      duration - how long to grant them for; must be positive
      Returns:
      completes on the main thread with the change
      Throws:
      IllegalArgumentException - when the duration is zero or negative
      Since:
      1.9.0
    • revoke

      @NotNull @NotNull CompletableFuture<CosmeticChange> revoke(@NotNull @NotNull UUID playerId, @NotNull @NotNull CosmeticCategory category, @NotNull @NotNull Collection<String> ids)
      Takes cosmetics away. Taking the chosen one clears the choice, so the default is used again.
    • revokeAll

      @NotNull @NotNull CompletableFuture<CosmeticChange> revokeAll(@NotNull @NotNull UUID playerId, @NotNull @NotNull CosmeticCategory category)
      Takes every cosmetic of a category away, deleted ones included, and clears the choice.
    • select

      @NotNull @NotNull CompletableFuture<CosmeticChange> select(@NotNull @NotNull UUID playerId, @NotNull @NotNull CosmeticCategory category, @Nullable @Nullable String id)
      Chooses a player's cosmetic: one they own as stored now, or null for the server's default. Choosing one they do not own changes nothing. Unlike a player's own choice in the cosmetics menu, this does not fire CosmeticSelectEvent.
    • getCosmeticIds

      @NotNull @NotNull List<String> getCosmeticIds(@NotNull @NotNull CosmeticCategory category)
      The IDs of every cosmetic that currently exists in a category, in the server's order.
    • getDefaultId

      @NotNull @NotNull Optional<String> getDefaultId(@NotNull @NotNull CosmeticCategory category)
      The cosmetic used for players who chose none, when the category has one.
    • getTypes

      @NotNull @NotNull List<CosmeticType> getTypes()
      Every cosmetic type: Zentrix's own categories in the cosmetics menu's order, then the types addons registered, in the order they did. Built-in types missing from this edition are listed with CosmeticType.available() false.
      Since:
      1.11.0
    • getType

      @NotNull @NotNull Optional<CosmeticType> getType(@NotNull @NotNull CosmeticTypeKey type)
      The type a key names, while it exists.
      Since:
      1.11.0
    • getCosmetics

      @NotNull @NotNull List<Cosmetic> getCosmetics(@NotNull @NotNull CosmeticTypeKey type)
      Every cosmetic of a type, in the server's order, with what menus show of them. Empty for a type that does not exist or is not available.
      Since:
      1.11.0
    • getCosmetic

      @NotNull @NotNull Optional<Cosmetic> getCosmetic(@NotNull @NotNull CosmeticTypeKey type, @NotNull @NotNull String id)
      One cosmetic of a type, while it exists.
      Since:
      1.11.0
    • getOwnership

      @NotNull @NotNull CosmeticOwnership getOwnership(@NotNull @NotNull UUID playerId, @NotNull @NotNull CosmeticTypeKey type)
      An online player's cosmetics of one type, as last read, as getOwnership(UUID, CosmeticCategory) answers for a built-in one.
      Since:
      1.11.0
    • getState

      @NotNull @NotNull Optional<CosmeticState> getState(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull CosmeticTypeKey type, @NotNull @NotNull String id)
      Where an online player stands with one cosmetic, as last read.
      Returns:
      empty when the type or cosmetic does not exist
      Since:
      1.11.0
    • getStates

      @NotNull @NotNull List<CosmeticState> getStates(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull CosmeticTypeKey type)
      Where an online player stands with every cosmetic of a type, in the server's order, as last read. Every cosmetic is listed, also those the player can neither use nor buy; Zentrix's own menu hides those for most types.
      Since:
      1.11.0
    • getEquipped

      @NotNull @NotNull Optional<Cosmetic> getEquipped(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull CosmeticTypeKey type)
      The cosmetic in effect for an online player: the one they chose while they can still use it, otherwise the type's default.
      Returns:
      empty when they chose none and the type has no default
      Since:
      1.11.0
    • grant

      @NotNull @NotNull CompletableFuture<CosmeticChange> grant(@NotNull @NotNull UUID playerId, @NotNull @NotNull CosmeticTypeKey type, @NotNull @NotNull Collection<String> ids)
      Unlocks cosmetics of any type permanently, as grant(UUID, CosmeticCategory, Collection) does. A CosmeticUnlockEvent listener may cancel it, leaving the change empty; the 1.8.0 overload fires that event too.
      Since:
      1.11.0
    • grantTimed

      @NotNull @NotNull CompletableFuture<CosmeticChange> grantTimed(@NotNull @NotNull UUID playerId, @NotNull @NotNull CosmeticTypeKey type, @NotNull @NotNull Collection<String> ids, @NotNull @NotNull Duration duration)
      Unlocks cosmetics of any type for a limited time, as grantTimed(UUID, CosmeticCategory, Collection, Duration) does, firing CosmeticUnlockEvent first.
      Throws:
      IllegalArgumentException - when the duration is zero or negative
      Since:
      1.11.0
    • revoke

      @NotNull @NotNull CompletableFuture<CosmeticChange> revoke(@NotNull @NotNull UUID playerId, @NotNull @NotNull CosmeticTypeKey type, @NotNull @NotNull Collection<String> ids)
      Takes cosmetics of any type away, as revoke(UUID, CosmeticCategory, Collection) does.
      Since:
      1.11.0
    • revokeAll

      @NotNull @NotNull CompletableFuture<CosmeticChange> revokeAll(@NotNull @NotNull UUID playerId, @NotNull @NotNull CosmeticTypeKey type)
      Takes every cosmetic of any type away and clears the choice, as revokeAll(UUID, CosmeticCategory) does.
      Since:
      1.11.0
    • select

      @NotNull @NotNull CompletableFuture<CosmeticChange> select(@NotNull @NotNull UUID playerId, @NotNull @NotNull CosmeticTypeKey type, @Nullable @Nullable String id)
      Stores a player's choice in any type without the checks and events of a player's own choice, as select(UUID, CosmeticCategory, String) does: one they own as stored now, or null for the default. For a class, or an addon's cosmetic that is unlocked by default, ownership is not needed.
      Since:
      1.11.0
    • equip

      @NotNull @NotNull CompletableFuture<CosmeticResult> equip(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull CosmeticTypeKey type, @NotNull @NotNull String id)
      Equips a cosmetic as the player's own choice, exactly as a left-click in /cosmetics does: the player must be able to use it, and CosmeticSelectEvent (built-in categories) and CosmeticEquipEvent may refuse it. Main thread.
      Returns:
      completes on the main thread: SUCCESS, UNCHANGED when it is already equipped, UNKNOWN, LOCKED, CANCELLED, or UNAVAILABLE and FAILED when it could not be saved
      Since:
      1.11.0
    • unequip

      @NotNull @NotNull CompletableFuture<CosmeticResult> unequip(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull CosmeticTypeKey type)
      Clears the player's choice in a type, so its default is equipped, as choosing "Server Default" in /cosmetics does, with the same events as equip(org.bukkit.entity.Player, dev.itsharshxd.zentrix.api.cosmetics.CosmeticTypeKey, java.lang.String). Main thread.
      Since:
      1.11.0
    • purchase

      @NotNull @NotNull CompletableFuture<CosmeticResult> purchase(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull CosmeticTypeKey type, @NotNull @NotNull String id)
      Buys a cosmetic for the player with Zentrix's currency and unlocks it permanently, as /cosmetics does for an addon's cosmetic with a price. Only cosmetics that are for sale and the player cannot use yet can be bought. CosmeticUnlockEvent is fired before the payment, which then goes through the currency's usual CurrencyChangeEvent. Should storing the cosmetic fail after the payment, the payment is refunded. Main thread.
      Returns:
      completes on the main thread: SUCCESS, UNKNOWN, ALREADY_UNLOCKED, NOT_FOR_SALE, INSUFFICIENT_FUNDS, CANCELLED, UNAVAILABLE or FAILED
      Since:
      1.11.0
    • startPreview

      boolean startPreview(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull CosmeticTypeKey type, @NotNull @NotNull String id)
      Previews a cosmetic to the player alone, as a right-click in /cosmetics does: a victory effect plays, a glider shows on their back, a cage design around them, and an addon's type shows what its handler shows. Ends any preview already showing. CosmeticPreviewEvent may refuse it. A preview does not need the player to own the cosmetic. Classes have no previews. Main thread.

      When the server previews in the showcase, the preview shows on the player's still model instead, addons' types included. Their handler receives a CosmeticPreview with the model's location.

      Returns:
      whether the preview shows
      Since:
      1.11.0
    • stopPreview

      void stopPreview(@NotNull @NotNull org.bukkit.entity.Player player)
      Ends the player's cosmetic preview, of whatever type, if one shows. Main thread.
      Since:
      1.11.0
    • registerType

      @NotNull @NotNull Registration registerType(@NotNull @NotNull org.bukkit.plugin.Plugin owner, @NotNull @NotNull CosmeticTypeDefinition definition, @NotNull @NotNull CosmeticHandler handler)
      Adds a cosmetic type of the addon's own, under CosmeticTypeKey.of(owner, definition.getKey()). It works as Zentrix's own categories do:
      • /cosmetics shows a tile for it, opening the cosmetics the player can use or buy; left-click equips or buys, right-click previews;
      • ownership and choices are stored with the player's data, in every storage backend, and kept while the type is not registered;
      • a cosmetic's permission unlocks it, and one with a price can be bought;
      • the handler hears each player's equipped cosmetic at the moments the definition lists, and shows previews.

      The registration ends when the owner is disabled; ending it stops previews of the type, calls CosmeticHandler.onUnregister() and takes the tile out of /cosmetics. Not part of the Free edition, which refuses it. Main thread.

      Throws:
      IllegalArgumentException - when the owner is not enabled or the key is already registered
      UnsupportedOperationException - in the Free edition
      Since:
      1.11.0