Skip to content

Commit

Permalink
Implement Notification icons with nanosvg support (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNature authored Apr 22, 2024
1 parent c9a1ad5 commit 41bb888
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.lunarclient.apollo.module.notification;

import com.lunarclient.apollo.common.icon.Icon;
import java.time.Duration;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -78,10 +79,11 @@ public final class Notification {
* <p>Represents an icon that will appear for the player
* if empty (null) it'll display a generic info message</p>
*
* @deprecated for removal since 1.1.3, use {@link Notification#icon} instead.
* @return the notification resource location
* @since 1.0.0
*/
@Nullable String resourceLocation;
@Deprecated @Nullable String resourceLocation;

/**
* Returns the notification {@link Duration} display time.
Expand All @@ -90,4 +92,16 @@ public final class Notification {
* @since 1.0.0
*/
Duration displayTime;

/**
* Returns the notification {@link Icon}.
*
* <p>Can be any of the icons found in {@link com.lunarclient.apollo.common.icon} package,
* for the most common use case, use {@link com.lunarclient.apollo.common.icon.ItemStackIcon}.</p>
*
* @return the notification icon
* @since 1.1.3
*/
Icon icon;

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,20 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
Player player = (Player) sender;

if (args.length != 1) {
player.sendMessage("Usage: /notification <display|reset>");
player.sendMessage("Usage: /notification <displayItem|displayResource|reset>");
return true;
}

switch (args[0].toLowerCase()) {
case "display": {
this.notificationExample.displayNotificationExample(player);
player.sendMessage("Displaying notification....");
case "displayitem": {
this.notificationExample.displayNotificationItemExample(player);
player.sendMessage("Displaying notification item....");
break;
}

case "displayresource": {
this.notificationExample.displayNotificationResourceExample(player);
player.sendMessage("Displaying notification resource....");
break;
}

Expand All @@ -63,7 +69,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}

default: {
player.sendMessage("Usage: /notification <display|reset>");
player.sendMessage("Usage: /notification <displayItem|displayResource|reset>");
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
package com.lunarclient.apollo.example.modules;

import com.lunarclient.apollo.Apollo;
import com.lunarclient.apollo.common.icon.ItemStackIcon;
import com.lunarclient.apollo.common.icon.SimpleResourceLocationIcon;
import com.lunarclient.apollo.module.notification.Notification;
import com.lunarclient.apollo.module.notification.NotificationModule;
import com.lunarclient.apollo.player.ApolloPlayer;
Expand All @@ -37,21 +39,49 @@ public class NotificationExample {

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

private final Notification uhcAnnouncement = Notification.builder()
.titleComponent(Component.text("UHC Announcement", NamedTextColor.GREEN))
.descriptionComponent(Component.text("UHC starts in 5 minutes...", NamedTextColor.RED)
.appendNewline()
.append(Component.text("Get ready!", NamedTextColor.WHITE))
.appendNewline()
.append(Component.text("Good luck!", NamedTextColor.GOLD))
)
.resourceLocation("icons/golden_apple.png") // This field is optional
.displayTime(Duration.ofSeconds(5))
.build();

public void displayNotificationExample(Player viewer) {
public void displayNotificationItemExample(Player viewer) {
Optional<ApolloPlayer> apolloPlayerOpt = Apollo.getPlayerManager().getPlayer(viewer.getUniqueId());
apolloPlayerOpt.ifPresent(apolloPlayer -> this.notificationModule.displayNotification(apolloPlayer, this.uhcAnnouncement));

apolloPlayerOpt.ifPresent(apolloPlayer -> {
this.notificationModule.displayNotification(apolloPlayer, Notification.builder()
.titleComponent(Component.text("UHC Announcement", NamedTextColor.GREEN))
.descriptionComponent(Component.text("UHC starts in 5 minutes...", NamedTextColor.RED)
.appendNewline()
.append(Component.text("Get ready!", NamedTextColor.WHITE))
.appendNewline()
.append(Component.text("Good luck!", NamedTextColor.GOLD))
)
.displayTime(Duration.ofSeconds(5))
.icon(ItemStackIcon.builder()
.itemName("GOLDEN_APPLE")
.build()
)
.build()
);
});
}

public void displayNotificationResourceExample(Player viewer) {
Optional<ApolloPlayer> apolloPlayerOpt = Apollo.getPlayerManager().getPlayer(viewer.getUniqueId());

apolloPlayerOpt.ifPresent(apolloPlayer -> {
this.notificationModule.displayNotification(apolloPlayer, Notification.builder()
.titleComponent(Component.text("UHC Announcement", NamedTextColor.GREEN))
.descriptionComponent(Component.text("UHC starts in 5 minutes...", NamedTextColor.RED)
.appendNewline()
.append(Component.text("Get ready!", NamedTextColor.WHITE))
.appendNewline()
.append(Component.text("Good luck!", NamedTextColor.GOLD))
)
.displayTime(Duration.ofSeconds(5))
.icon(SimpleResourceLocationIcon.builder()
.resourceLocation("lunar:logo/logo-200x182.svg")
.size(12)
.build()
)
.build()
);
});
}

public void resetNotificationsExample(Player viewer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.lunarclient.apollo.module.notification;

import com.lunarclient.apollo.common.ApolloComponent;
import com.lunarclient.apollo.common.icon.Icon;
import com.lunarclient.apollo.network.NetworkTypes;
import com.lunarclient.apollo.notification.v1.DisplayNotificationMessage;
import com.lunarclient.apollo.notification.v1.ResetNotificationsMessage;
Expand Down Expand Up @@ -69,6 +70,11 @@ public void displayNotification(@NonNull Recipients recipients, @NonNull Notific
builder.setResourceLocation(resourceLocation);
}

Icon icon = notification.getIcon();
if (icon != null) {
builder.setIcon(NetworkTypes.toProtobuf(icon));
}

DisplayNotificationMessage message = builder.build();
recipients.forEach(player -> ((AbstractApolloPlayer) player).sendPacket(message));
}
Expand Down
80 changes: 59 additions & 21 deletions docs/developers/modules/notification.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,53 @@ The notification module allows you to send Lunar Client notifications to players

## Integration

### `Notification` Builder
### Sample Code

```java
private final Notification uhcAnnouncement = Notification.builder()
.titleComponent(Component.text("UHC Announcement", NamedTextColor.GREEN))
.descriptionComponent(Component.text("UHC starts in 5 minutes...", NamedTextColor.RED)
.appendNewline()
.append(Component.text("Get ready!", NamedTextColor.WHITE))
.appendNewline()
.append(Component.text("Good luck!", NamedTextColor.GOLD))
)
.resourceLocation("icons/golden_apple.png") // This field is optional
.displayTime(Duration.ofSeconds(5))
.build();
public void displayNotificationItemExample(Player viewer) {
Optional<ApolloPlayer> apolloPlayerOpt = Apollo.getPlayerManager().getPlayer(viewer.getUniqueId());

apolloPlayerOpt.ifPresent(apolloPlayer -> {
this.notificationModule.displayNotification(apolloPlayer, Notification.builder()
.titleComponent(Component.text("UHC Announcement", NamedTextColor.GREEN))
.descriptionComponent(Component.text("UHC starts in 5 minutes...", NamedTextColor.RED)
.appendNewline()
.append(Component.text("Get ready!", NamedTextColor.WHITE))
.appendNewline()
.append(Component.text("Good luck!", NamedTextColor.GOLD))
)
.displayTime(Duration.ofSeconds(5))
.icon(ItemStackIcon.builder()
.itemName("GOLDEN_APPLE")
.build()
)
.build()
);
});
}

public void displayNotificationResourceExample(Player viewer) {
Optional<ApolloPlayer> apolloPlayerOpt = Apollo.getPlayerManager().getPlayer(viewer.getUniqueId());

apolloPlayerOpt.ifPresent(apolloPlayer -> {
this.notificationModule.displayNotification(apolloPlayer, Notification.builder()
.titleComponent(Component.text("UHC Announcement", NamedTextColor.GREEN))
.descriptionComponent(Component.text("UHC starts in 5 minutes...", NamedTextColor.RED)
.appendNewline()
.append(Component.text("Get ready!", NamedTextColor.WHITE))
.appendNewline()
.append(Component.text("Good luck!", NamedTextColor.GOLD))
)
.displayTime(Duration.ofSeconds(5))
.icon(SimpleResourceLocationIcon.builder()
.resourceLocation("lunar:logo/logo-200x182.svg")
.size(12)
.build()
)
.build()
);
});
}
```

#### `Notification` Options
Expand All @@ -46,16 +79,20 @@ private final Notification uhcAnnouncement = Notification.builder()
.descriptionComponent(Component.text("UHC starts in 5 minutes...", NamedTextColor.RED))
```

`.resourceLocation(String)` is the resource location of the shown icon.
`.displayTime(java.time.Duration)` is the duration you want to keep the notification on screen. See the [java.time.Duration Javadocs](https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html) for more.

```java
.resourceLocation("icons/golden_apple.png")
.displayTime(Duration.ofSeconds(5))
```

`.displayTime(java.time.Duration)` is the duration you want to keep the notification on screen. See the [java.time.Duration Javadocs](https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html) for more.
`.icon(itemStackIcon)` is how you display a custom item icon. Read the [icons utilities page](/apollo/developers/utilities/icons) to learn more about icons.
```java
.icon(ItemStackIcon.builder().itemId("GOLDEN_APPLE").build())
```

`.icon(SimpleResourceLocationIcon)` is how you display a custom texture icon. Read the [icons utilities page](/apollo/developers/utilities/icons) to learn more about icons.
```java
.displayTime(Duration.ofSeconds(5))
.icon(SimpleResourceLocationIcon.builder().resourceLocation("lunar:logo/logo-200x182.svg").size(12).build())
```

If this field is left empty (null) it'll display a generic info icon, as displayed here.
Expand All @@ -76,13 +113,14 @@ If this field is left empty (null) it'll display a generic info icon, as display
.description("UHC starts in 5 minutes...")
```

### Displaying a Notification for a specific player
<Callout type="warning" emoji="⚠️">
The field `resourceLocation` is deprecated since 1.1.3, use 'icon' instead.
</Callout>

`.resourceLocation(String)` is the resource location of the shown icon.

```java
public void displayNotificationExample(Player viewer) {
Optional<ApolloPlayer> apolloPlayerOpt = Apollo.getPlayerManager().getPlayer(viewer.getUniqueId());
apolloPlayerOpt.ifPresent(apolloPlayer -> this.notificationModule.displayNotification(apolloPlayer, this.uhcAnnouncement));
}
.resourceLocation("icons/golden_apple.png")
```

### Resetting all notifications for a player
Expand Down

1 comment on commit 41bb888

@LunarClientBot
Copy link
Collaborator

@LunarClientBot LunarClientBot commented on 41bb888 Apr 22, 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:preview
URL:https://315c7b73.lunarclient-dev.pages.dev

Please sign in to comment.