Skip to content

Commit

Permalink
Use Adventure for Notification title & description (#87)
Browse files Browse the repository at this point in the history
* Use Adventure for Notification title & description

* Simplify optional fields
  • Loading branch information
ItsNature authored Jan 1, 2024
1 parent e622ea1 commit 428b92d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.time.Duration;
import lombok.Builder;
import lombok.Getter;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.Nullable;

/**
Expand All @@ -40,18 +41,36 @@ public final class Notification {
/**
* Returns the notification {@link String} title.
*
* @deprecated for removal since 1.0.6, use {@link Notification#titleComponent} instead.
* @return the notification title
* @since 1.0.0
*/
String title;
@Deprecated String title;

/**
* Returns the notification {@link String} description.
*
* @deprecated for removal since 1.0.6, use {@link Notification#descriptionComponent} instead.
* @return the notification description
* @since 1.0.0
*/
String description;
@Deprecated String description;

/**
* Returns the notification {@link String} title component.
*
* @return the notification title component
* @since 1.0.6
*/
Component titleComponent;

/**
* Returns the notification {@link String} description component.
*
* @return the notification description component
* @since 1.0.6
*/
Component descriptionComponent;

/**
* Returns the notification {@link String} resource location.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@
import com.lunarclient.apollo.player.ApolloPlayer;
import java.time.Duration;
import java.util.Optional;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.entity.Player;

public class NotificationExample {

private final NotificationModule notificationModule = Apollo.getModuleManager().getModule(NotificationModule.class);

private final Notification uhcAnnouncement = Notification.builder()
.title("UHC Announcement")
.description("UHC starts in 5 minutes...")
.titleComponent(Component.text("UHC Announcement", NamedTextColor.GREEN))
.descriptionComponent(Component.text("UHC starts in 5 minutes...", NamedTextColor.RED))
.resourceLocation("icons/golden_apple.png") // This field is optional
.displayTime(Duration.ofSeconds(5))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
*/
package com.lunarclient.apollo.module.notification;

import com.lunarclient.apollo.common.ApolloComponent;
import com.lunarclient.apollo.network.NetworkTypes;
import com.lunarclient.apollo.notification.v1.DisplayNotificationMessage;
import com.lunarclient.apollo.notification.v1.ResetNotificationsMessage;
import com.lunarclient.apollo.player.AbstractApolloPlayer;
import com.lunarclient.apollo.recipients.Recipients;
import lombok.NonNull;
import net.kyori.adventure.text.Component;

/**
* Provides the notifications module.
Expand All @@ -40,10 +42,28 @@ public final class NotificationModuleImpl extends NotificationModule {
@Override
public void displayNotification(@NonNull Recipients recipients, @NonNull Notification notification) {
DisplayNotificationMessage.Builder builder = DisplayNotificationMessage.newBuilder()
.setTitle(notification.getTitle())
.setDescription(notification.getDescription())
.setDisplayTime(NetworkTypes.toProtobuf(notification.getDisplayTime()));

String title = notification.getTitle();
if (title != null) {
builder.setTitle(title);
}

String description = notification.getDescription();
if (description != null) {
builder.setDescription(description);
}

Component titleComponent = notification.getTitleComponent();
if (titleComponent != null) {
builder.setTitleAdventureJsonLines(ApolloComponent.toJson(titleComponent));
}

Component descriptionComponent = notification.getDescriptionComponent();
if (descriptionComponent != null) {
builder.setDescriptionAdventureJsonLines(ApolloComponent.toJson(descriptionComponent));
}

String resourceLocation = notification.getResourceLocation();
if (resourceLocation != null) {
builder.setResourceLocation(resourceLocation);
Expand Down
32 changes: 24 additions & 8 deletions docs/developers/modules/notification.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ The notification module allows you to send Lunar Client notifications to players
### `Notification` Builder

```java
private final Notification notification = Notification.builder()
.title("UHC Announcement")
.description("UHC starts in 5 minutes...")
private final Notification uhcAnnouncement = Notification.builder()
.titleComponent(Component.text("UHC Announcement", NamedTextColor.GREEN))
.descriptionComponent(Component.text("UHC starts in 5 minutes...", NamedTextColor.RED))
.resourceLocation("icons/golden_apple.png") // This field is optional
.displayTime(Duration.ofSeconds(5))
.build();
Expand All @@ -26,19 +26,19 @@ private final Notification notification = Notification.builder()
#### `Notification` Options

<Callout type="warning" emoji="⚠️">
There are character limits for the `title` and `description` options.
There are character limits for the `titleComponent` option.
</Callout>

`.title(String)` is the title of the notification box. Char limit: `15`
`.titleComponent(Component)` is the title of the notification box. Char limit: `15`. See the [chat components](https://docs.advntr.dev/text.html) page for more.

```java
.title("UHC Announcement")
.titleComponent(Component.text("UHC Announcement", NamedTextColor.GREEN))
```

`.description(String)` is the description displayed inside the notification box. Char limit: `15`
`.descriptionComponent(Component)` is the description displayed inside the notification box. See the [chat components](https://docs.advntr.dev/text.html) page for more.

```java
.description("UHC starts in 5 minutes...")
.descriptionComponent(Component.text("UHC starts in 5 minutes...", NamedTextColor.RED))
```

`.resourceLocation(String)` is the resource location of the shown icon.
Expand All @@ -55,6 +55,22 @@ private final Notification notification = Notification.builder()

If this field is left empty (null) it'll display a generic info icon, as displayed here.

<Callout type="warning" emoji="⚠️">
The fields `title` and `description` are deprecated since 1.0.6, use 'titleComponent' and 'descriptionComponent' instead.
</Callout>

`.title(String)` is the title of the notification box. Char limit: `15`

```java
.title("UHC Announcement")
```

`.description(String)` is the description displayed inside the notification box.

```java
.description("UHC starts in 5 minutes...")
```

### Displaying a Notification for a specific player

```java
Expand Down

1 comment on commit 428b92d

@LunarClientBot
Copy link
Collaborator

@LunarClientBot LunarClientBot commented on 428b92d Jan 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📄 Documentation Deployment

Status:✅ Completed
Environment:production
URL:https://0c5e0d7d.lunarclient-dev.pages.dev

Please sign in to comment.