forked from Minestom/Minestom
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* [#34] Implement new notification system with tests * [#34] Update demo to use new notification system * [#34] Add deprecation text to old notification system * [#34] Improve javadocs and rename builder implementation * [#34] Improve javadocs and fix tests * [#34] Remove empty lines * [#34] Add package description (cherry picked from commit 29ec3fe)
- Loading branch information
1 parent
2027a55
commit 211e7ac
Showing
9 changed files
with
320 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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; |
58 changes: 58 additions & 0 deletions
58
src/test/java/net/minestom/server/notifications/NotificationIntegrationTest.java
This file contains 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.coordinate.Pos; | ||
import net.minestom.server.item.ItemStack; | ||
import net.minestom.server.item.Material; | ||
import net.minestom.server.network.packet.server.play.AdvancementsPacket; | ||
import net.minestom.testing.Collector; | ||
import net.minestom.testing.Env; | ||
import net.minestom.testing.EnvTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@EnvTest | ||
class NotificationIntegrationTest { | ||
|
||
@Test | ||
void testBuilder(Env env) { | ||
var notification = Notification.builder() | ||
.icon(Material.ITEM_FRAME) | ||
.title(Component.text("unit test")) | ||
.frameType(FrameType.TASK) | ||
.build(); | ||
assertEquals(FrameType.TASK, notification.type()); | ||
assertEquals(ItemStack.of(Material.ITEM_FRAME), notification.icon()); | ||
assertEquals(Component.text("unit test"), notification.title()); | ||
} | ||
|
||
@Test | ||
void testSend(Env env) { | ||
var instance = env.createFlatInstance(); | ||
var connection = env.createConnection(); | ||
Collector<AdvancementsPacket> advancementsPacketCollector = connection.trackIncoming(AdvancementsPacket.class); | ||
var player = connection.connect(instance, new Pos(0, 42, 0)).join(); | ||
var notification = Notification.builder() | ||
.icon(Material.ITEM_FRAME) | ||
.title(Component.text("unit test")) | ||
.frameType(FrameType.TASK) | ||
.build(); | ||
notification.send(player); | ||
advancementsPacketCollector.assertCount(2); | ||
AdvancementsPacket advancementsPacket = advancementsPacketCollector.collect().get(1); | ||
assertNotNull(advancementsPacket); | ||
Optional<AdvancementsPacket.AdvancementMapping> advancementMapping = advancementsPacket.advancementMappings().stream().findFirst(); | ||
advancementMapping.ifPresent(advancementMapping1 -> { | ||
AdvancementsPacket.Advancement advancement = advancementMapping1.value(); | ||
assertFalse(advancement.sendTelemetryData()); | ||
var displayData = advancement.displayData(); | ||
assertEquals(ItemStack.of(Material.ITEM_FRAME), displayData.icon()); | ||
assertEquals(Component.text("unit test"), displayData.title()); | ||
assertEquals(FrameType.TASK, displayData.frameType()); | ||
}); | ||
} | ||
} |