Interface CosmeticsService
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:
- what there is:
getTypes(),getCosmetics(CosmeticTypeKey); - where a player stands:
getStates(Player, CosmeticTypeKey),getEquipped(Player, CosmeticTypeKey); - what a player does:
equip(org.bukkit.entity.Player, dev.itsharshxd.zentrix.api.cosmetics.CosmeticTypeKey, java.lang.String),unequip(org.bukkit.entity.Player, dev.itsharshxd.zentrix.api.cosmetics.CosmeticTypeKey),purchase(org.bukkit.entity.Player, dev.itsharshxd.zentrix.api.cosmetics.CosmeticTypeKey, java.lang.String),startPreview(org.bukkit.entity.Player, dev.itsharshxd.zentrix.api.cosmetics.CosmeticTypeKey, java.lang.String),stopPreview(org.bukkit.entity.Player), each as the menu does it, with the same checks and events; - what an addon does for a player:
grant,grantTimed,revoke,select.
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 Summary
Modifier and TypeMethodDescription@NotNull CompletableFuture<CosmeticResult> equip(@NotNull org.bukkit.entity.Player player, @NotNull CosmeticTypeKey type, @NotNull String id) Equips a cosmetic as the player's own choice, exactly as a left-click in/cosmeticsdoes: the player must be able to use it, andCosmeticSelectEvent(built-in categories) andCosmeticEquipEventmay refuse it.getCosmetic(@NotNull CosmeticTypeKey type, @NotNull String id) One cosmetic of a type, while it exists.getCosmeticIds(@NotNull CosmeticCategory category) The IDs of every cosmetic that currently exists in a category, in the server's order.getCosmetics(@NotNull CosmeticTypeKey type) Every cosmetic of a type, in the server's order, with what menus show of them.getDefaultId(@NotNull CosmeticCategory category) The cosmetic used for players who chose none, when the category has one.getEquipped(@NotNull org.bukkit.entity.Player player, @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.@NotNull CosmeticOwnershipgetOwnership(@NotNull UUID playerId, @NotNull CosmeticCategory category) An online player's cosmetics of one category, as last read.@NotNull CosmeticOwnershipgetOwnership(@NotNull UUID playerId, @NotNull CosmeticTypeKey type) An online player's cosmetics of one type, as last read, asgetOwnership(UUID, CosmeticCategory)answers for a built-in one.@NotNull Optional<CosmeticState> getState(@NotNull org.bukkit.entity.Player player, @NotNull CosmeticTypeKey type, @NotNull String id) Where an online player stands with one cosmetic, as last read.@NotNull List<CosmeticState> getStates(@NotNull org.bukkit.entity.Player player, @NotNull CosmeticTypeKey type) Where an online player stands with every cosmetic of a type, in the server's order, as last read.@NotNull Optional<CosmeticType> getType(@NotNull CosmeticTypeKey type) The type a key names, while it exists.@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.@NotNull CompletableFuture<CosmeticChange> grant(@NotNull UUID playerId, @NotNull CosmeticCategory category, @NotNull Collection<String> ids) Grants cosmetics permanently.@NotNull CompletableFuture<CosmeticChange> grant(@NotNull UUID playerId, @NotNull CosmeticTypeKey type, @NotNull Collection<String> ids) Unlocks cosmetics of any type permanently, asgrant(UUID, CosmeticCategory, Collection)does.default @NotNull CompletableFuture<CosmeticChange> grantTimed(@NotNull UUID playerId, @NotNull CosmeticCategory category, @NotNull Collection<String> ids, @NotNull Duration duration) Grants cosmetics for a limited time, such as a glider for seven days.@NotNull CompletableFuture<CosmeticChange> grantTimed(@NotNull UUID playerId, @NotNull CosmeticTypeKey type, @NotNull Collection<String> ids, @NotNull Duration duration) Unlocks cosmetics of any type for a limited time, asgrantTimed(UUID, CosmeticCategory, Collection, Duration)does, firingCosmeticUnlockEventfirst.booleanWhether cosmetics can be read and changed now.booleanWhether an online player's cosmetics have been read since they joined.@NotNull CompletableFuture<CosmeticResult> purchase(@NotNull org.bukkit.entity.Player player, @NotNull CosmeticTypeKey type, @NotNull String id) Buys a cosmetic for the player with Zentrix's currency and unlocks it permanently, as/cosmeticsdoes for an addon's cosmetic with a price.@NotNull CompletableFuture<Map<CosmeticCategory, CosmeticOwnership>> Reads a player's cosmetics as stored now, online or offline, refreshing the cache of an online player.@NotNull RegistrationregisterType(@NotNull org.bukkit.plugin.Plugin owner, @NotNull CosmeticTypeDefinition definition, @NotNull CosmeticHandler handler) Adds a cosmetic type of the addon's own, underCosmeticTypeKey.of(owner, definition.getKey()).@NotNull CompletableFuture<CosmeticChange> revoke(@NotNull UUID playerId, @NotNull CosmeticCategory category, @NotNull Collection<String> ids) Takes cosmetics away.@NotNull CompletableFuture<CosmeticChange> revoke(@NotNull UUID playerId, @NotNull CosmeticTypeKey type, @NotNull Collection<String> ids) Takes cosmetics of any type away, asrevoke(UUID, CosmeticCategory, Collection)does.@NotNull CompletableFuture<CosmeticChange> revokeAll(@NotNull UUID playerId, @NotNull CosmeticCategory category) Takes every cosmetic of a category away, deleted ones included, and clears the choice.@NotNull CompletableFuture<CosmeticChange> revokeAll(@NotNull UUID playerId, @NotNull CosmeticTypeKey type) Takes every cosmetic of any type away and clears the choice, asrevokeAll(UUID, CosmeticCategory)does.@NotNull CompletableFuture<CosmeticChange> select(@NotNull UUID playerId, @NotNull CosmeticCategory category, @Nullable String id) Chooses a player's cosmetic: one they own as stored now, or null for the server's default.@NotNull CompletableFuture<CosmeticChange> select(@NotNull UUID playerId, @NotNull CosmeticTypeKey type, @Nullable String id) Stores a player's choice in any type without the checks and events of a player's own choice, asselect(UUID, CosmeticCategory, String)does: one they own as stored now, or null for the default.booleanstartPreview(@NotNull org.bukkit.entity.Player player, @NotNull CosmeticTypeKey type, @NotNull String id) Previews a cosmetic to the player alone, as a right-click in/cosmeticsdoes: 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.voidstopPreview(@NotNull org.bukkit.entity.Player player) Ends the player's cosmetic preview, of whatever type, if one shows.@NotNull CompletableFuture<CosmeticResult> unequip(@NotNull org.bukkit.entity.Player player, @NotNull CosmeticTypeKey type) Clears the player's choice in a type, so its default is equipped, as choosing "Server Default" in/cosmeticsdoes, with the same events asequip(org.bukkit.entity.Player, dev.itsharshxd.zentrix.api.cosmetics.CosmeticTypeKey, java.lang.String).
-
Method Details
-
isAvailable
boolean isAvailable()Whether cosmetics can be read and changed now. -
isLoaded
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; userefresh(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
durationfrom now. One they already own for a limited time keeps its remaining time withdurationadded. 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
CosmeticOwnershipChangeEventfires withCosmeticOwnershipChangeEvent.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 fireCosmeticSelectEvent. -
getCosmeticIds
The IDs of every cosmetic that currently exists in a category, in the server's order. -
getDefaultId
The cosmetic used for players who chose none, when the category has one. -
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 withCosmeticType.available()false.- Since:
- 1.11.0
-
getType
The type a key names, while it exists.- Since:
- 1.11.0
-
getCosmetics
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, asgetOwnership(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, asgrant(UUID, CosmeticCategory, Collection)does. ACosmeticUnlockEventlistener 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, asgrantTimed(UUID, CosmeticCategory, Collection, Duration)does, firingCosmeticUnlockEventfirst.- 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, asrevoke(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, asrevokeAll(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, asselect(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/cosmeticsdoes: the player must be able to use it, andCosmeticSelectEvent(built-in categories) andCosmeticEquipEventmay refuse it. Main thread.- Returns:
- completes on the main thread:
SUCCESS,UNCHANGEDwhen it is already equipped,UNKNOWN,LOCKED,CANCELLED, orUNAVAILABLEandFAILEDwhen 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/cosmeticsdoes, with the same events asequip(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/cosmeticsdoes for an addon's cosmetic with a price. Only cosmetics that are for sale and the player cannot use yet can be bought.CosmeticUnlockEventis fired before the payment, which then goes through the currency's usualCurrencyChangeEvent. 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,UNAVAILABLEorFAILED - 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/cosmeticsdoes: 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.CosmeticPreviewEventmay 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
CosmeticPreviewwith 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, underCosmeticTypeKey.of(owner, definition.getKey()). It works as Zentrix's own categories do:/cosmeticsshows 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 registeredUnsupportedOperationException- in the Free edition- Since:
- 1.11.0
-