forked from Minestom/Minestom
-
-
Notifications
You must be signed in to change notification settings - Fork 4
[#34] Better Notification System #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e3ee41e
[#34] Implement new notification system with tests
TheMeinerLP 58cfe2b
[#34] Update demo to use new notification system
TheMeinerLP 29eb52f
[#34] Add deprecation text to old notification system
TheMeinerLP 14845b8
[#34] Improve javadocs and rename builder implementation
TheMeinerLP 6bec4a1
Merge branch 'refs/heads/master' into feature/new-notification-system
TheMeinerLP 1f88133
[#34] Improve javadocs and fix tests
TheMeinerLP fd9c6ae
[#34] Remove empty lines
TheMeinerLP 36dcff2
[#34] Add package description
TheMeinerLP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/main/java/net/minestom/server/notifications/Notification.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package net.minestom.server.notifications; | ||
|
|
||
| import net.kyori.adventure.text.Component; | ||
| import net.minestom.server.advancements.FrameType; | ||
| import net.minestom.server.entity.Player; | ||
| import net.minestom.server.item.ItemStack; | ||
| import net.minestom.server.item.Material; | ||
| import net.minestom.server.network.packet.server.play.AdvancementsPacket; | ||
| import org.jetbrains.annotations.Contract; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Is used to send temporary advancements to the client, which are called notifications. | ||
| * <br> | ||
| * Here is an example of its use: | ||
| * <pre><code> | ||
| * Notification notification = Notification.builder() | ||
| * .frameType(FrameType.TASK) | ||
| * .title(Component.text("Welcome!")) | ||
| * .icon(Material.IRON_SWORD).build(); | ||
| * notification.send(player); | ||
| * </code></pre> | ||
| * | ||
| * The constant {@link #IDENTIFIER} is used for the advancement packet | ||
| * The constant {@link #REMOVE_PACKET} is used to remove previous notifications | ||
| * @since 1.4.1 | ||
| */ | ||
| public sealed interface Notification permits NotificationImpl { | ||
|
|
||
| String IDENTIFIER = "minestom:notification"; | ||
| AdvancementsPacket REMOVE_PACKET = new AdvancementsPacket(false, List.of(), List.of(IDENTIFIER), List.of()); | ||
|
|
||
| /** | ||
| * Creates a new builder instance | ||
| * @return an instance of the builder | ||
| */ | ||
| @Contract(pure = true) | ||
| static @NotNull Builder builder() { | ||
| return new NotificationBuilder(); | ||
| } | ||
|
|
||
| /** | ||
| * Send the notification to the client | ||
| * @param player to get be sent | ||
| */ | ||
| void send(@NotNull Player player); | ||
|
|
||
| /** | ||
| * Send the notification to a collection of clients | ||
| * @param players to get be sent | ||
| */ | ||
| void send(@NotNull Collection<@NotNull Player> players); | ||
|
|
||
| /** | ||
| * Gets the title of the notification as a {@link Component} | ||
| * @return the title {@link Component} | ||
| */ | ||
| @NotNull Component title(); | ||
|
|
||
| /** | ||
| * Get the {@link FrameType} of the notification | ||
| * @return the type | ||
| */ | ||
| @NotNull FrameType type(); | ||
|
|
||
| /** | ||
| * Get the displayed icon of the notification as {@link ItemStack} | ||
| * @return the {@link ItemStack} | ||
| */ | ||
| @NotNull ItemStack icon(); | ||
|
|
||
| /** | ||
| * @since 1.4.1 | ||
| */ | ||
| sealed interface Builder permits NotificationBuilder { | ||
|
|
||
| /** | ||
| * Set the title for a notification as component. | ||
| * | ||
| * If you're using a resource pack you can use {@link Component#translatable(String)} | ||
| * | ||
| * @param component to get send to the client | ||
| * @return the builder | ||
| */ | ||
| Builder title(@NotNull Component component); | ||
|
|
||
| /** | ||
| * Set the frame typ of the notification | ||
| * @param frameType to showed for the client | ||
| * @return the builder | ||
| */ | ||
| Builder frameType(@NotNull FrameType frameType); | ||
|
|
||
| /** | ||
| * Set the {@link Material} for the icon | ||
| * @param material to be shown to the client | ||
| * @return the builder | ||
| */ | ||
| Builder icon(@NotNull Material material); | ||
|
|
||
| /** | ||
| * Set the {@link ItemStack} for the icon | ||
| * @param itemStack to be shown to the client | ||
| * @return the builder | ||
| */ | ||
| Builder icon(@NotNull ItemStack itemStack); | ||
|
|
||
| /** | ||
| * Returns an instance of the creation notification | ||
| * @return the instance | ||
| */ | ||
| Notification build(); | ||
| } | ||
| } | ||
60 changes: 60 additions & 0 deletions
60
src/main/java/net/minestom/server/notifications/NotificationBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package net.minestom.server.notifications; | ||
|
|
||
| import net.kyori.adventure.text.Component; | ||
| import net.minestom.server.advancements.FrameType; | ||
| import net.minestom.server.item.ItemStack; | ||
| import net.minestom.server.item.Material; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| final class NotificationBuilder implements Notification.Builder { | ||
| private Component title; | ||
| private FrameType type; | ||
| private ItemStack icon; | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public Notification.Builder title(@NotNull Component component) { | ||
| this.title = component; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public Notification.Builder frameType(@NotNull FrameType frameType) { | ||
| this.type = frameType; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public Notification.Builder icon(@NotNull Material material) { | ||
| this.icon = ItemStack.of(material); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public Notification.Builder icon(@NotNull ItemStack itemStack) { | ||
| this.icon = itemStack; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public Notification build() { | ||
| return new NotificationImpl(title, type, icon); | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
src/main/java/net/minestom/server/notifications/NotificationImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package net.minestom.server.notifications; | ||
|
|
||
| import net.kyori.adventure.text.Component; | ||
| import net.minestom.server.advancements.FrameType; | ||
| import net.minestom.server.entity.Player; | ||
| import net.minestom.server.item.ItemStack; | ||
| import net.minestom.server.network.packet.server.play.AdvancementsPacket; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| record NotificationImpl(@NotNull Component title, @NotNull FrameType type, | ||
| @NotNull ItemStack icon) implements Notification { | ||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public void send(@NotNull Player player) { | ||
| player.sendPacket(createPacket()); | ||
| player.sendPacket(REMOVE_PACKET); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public void send(@NotNull Collection<@NotNull Player> players) { | ||
| players.forEach(this::send); | ||
| } | ||
|
|
||
| /** | ||
| * Create the advancement packet that simulates the notification. | ||
| * It's not private because integration tests | ||
| * @return the packet | ||
| */ | ||
| @NotNull AdvancementsPacket createPacket() { | ||
| final var displayData = new AdvancementsPacket.DisplayData( | ||
| title(), Component.empty(), | ||
| icon(), type(), | ||
| 0x6, null, 0f, 0f); | ||
|
|
||
| final var criteria = new AdvancementsPacket.Criteria("minestom:some_criteria", | ||
| new AdvancementsPacket.CriterionProgress(System.currentTimeMillis())); | ||
|
|
||
| final var advancement = new AdvancementsPacket.Advancement(null, displayData, | ||
| List.of(new AdvancementsPacket.Requirement(List.of(criteria.criterionIdentifier()))), | ||
| false); | ||
|
|
||
| final var mapping = new AdvancementsPacket.AdvancementMapping(IDENTIFIER, advancement); | ||
| final var progressMapping = new AdvancementsPacket.ProgressMapping(IDENTIFIER, | ||
| new AdvancementsPacket.AdvancementProgress(List.of(criteria))); | ||
| return new AdvancementsPacket(false, List.of(mapping), List.of(), List.of(progressMapping)); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/net/minestom/server/notifications/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /** | ||
| * This module contains logic about notification system for the minecraft client. | ||
| * <p> | ||
| * It allows developers to show a toast in the right upper corner with 3 different frame types: | ||
| * {@link net.minestom.server.advancements.FrameType#GOAL}, {@link net.minestom.server.advancements.FrameType#TASK} or {@link net.minestom.server.advancements.FrameType#CHALLENGE} | ||
| * </p> | ||
| * | ||
| * @since 1.4.1 | ||
| * @author TheMeinerLP | ||
| * @version 1.0 | ||
| */ | ||
| package net.minestom.server.notifications; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.