Skip to content

Commit

Permalink
added player.inAudience documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerzinator24 committed Jan 3, 2025
1 parent aabbe1f commit 11d42a2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<out AudienceEntry>`.
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:

<CodeSnippet tag="check_player_in_audience" json={require("../../../snippets.json")} />

## 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -87,4 +90,12 @@ class AudienceDisplayWithEvents : AudienceDisplay() {
}
// highlight-end
}
//</code-block:audience_display_with_events>
//</code-block:audience_display_with_events>

fun checkPlayerInAudience(player: Player, ref: Ref<out AudienceEntry>) {
//<code-block:check_player_in_audience>
if (player.inAudience(ref)) {
// Do something with the player
}
//</code-block:check_player_in_audience>
}

0 comments on commit 11d42a2

Please sign in to comment.