diff --git a/documentation/docs/develop/02-extensions/04-entries/manifest/audience.mdx b/documentation/docs/develop/02-extensions/04-entries/manifest/audience.mdx index 5e75fef618..2db6078b30 100644 --- a/documentation/docs/develop/02-extensions/04-entries/manifest/audience.mdx +++ b/documentation/docs/develop/02-extensions/04-entries/manifest/audience.mdx @@ -65,6 +65,16 @@ Events will trigger for _**all**_ players, not just those in the audience. **Always check if the player is in the audience before performing audience-specific actions.** ::: +### Check if a player is in the audience of an entry + +Suppose you have a `Ref`. +To check if a player is in the audience of an entry, +you can use the `player.inAudience` method that returns `true` if the player is in the audience of the entry. +This is incredibly convenient, especially because it automatically handles all parents of the entry for you as well. +Here's an example: + + + ## Best Practices 1. **State Management**: While state is allowed within the `AudienceDisplay`, ensure that all used state is contained within the display and does not leak outside. diff --git a/extensions/_DocsExtension/src/main/kotlin/com/typewritermc/example/entries/manifest/ExampleAudienceEntry.kt b/extensions/_DocsExtension/src/main/kotlin/com/typewritermc/example/entries/manifest/ExampleAudienceEntry.kt index ba309810c8..defe8b4d3e 100644 --- a/extensions/_DocsExtension/src/main/kotlin/com/typewritermc/example/entries/manifest/ExampleAudienceEntry.kt +++ b/extensions/_DocsExtension/src/main/kotlin/com/typewritermc/example/entries/manifest/ExampleAudienceEntry.kt @@ -2,10 +2,13 @@ package com.typewritermc.example.entries.manifest import com.typewritermc.example.entries.trigger.SomeBukkitEvent import com.typewritermc.core.books.pages.Colors +import com.typewritermc.core.entries.Ref +import com.typewritermc.core.entries.emptyRef import com.typewritermc.core.extension.annotations.Entry import com.typewritermc.engine.paper.entry.entries.AudienceDisplay import com.typewritermc.engine.paper.entry.entries.AudienceEntry import com.typewritermc.engine.paper.entry.entries.TickableDisplay +import com.typewritermc.engine.paper.entry.inAudience import com.typewritermc.engine.paper.utils.ThreadType import org.bukkit.entity.Player import org.bukkit.event.EventHandler @@ -87,4 +90,12 @@ class AudienceDisplayWithEvents : AudienceDisplay() { } // highlight-end } -// \ No newline at end of file +// + +fun checkPlayerInAudience(player: Player, ref: Ref) { + // + if (player.inAudience(ref)) { + // Do something with the player + } + // +}