|
| 1 | +--- |
| 2 | +title: "Sending notifications" |
| 3 | +description: "Learn how to send notifications (similar to item pickup) with your Hytale Plugin" |
| 4 | +authors: |
| 5 | + - name: "BeerBag" |
| 6 | +--- |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +In this guide, you will learn how to send notifications to players, similar to |
| 11 | +the notification received when picking up an item from the ground. |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +## Structure of a Notification |
| 16 | + |
| 17 | +A notification consists of three main components: |
| 18 | + |
| 19 | +1. **Primary Message**: The main `Message` displayed in the notification. |
| 20 | +2. **Secondary Message**: Additional `Message`, displayed below the primary message. |
| 21 | +3. **Icon**: An item icon that visually represents the notification, shown on |
| 22 | +the left side. |
| 23 | + |
| 24 | +## Sending Notifications |
| 25 | + |
| 26 | +You can use the `NotificationUtil` class to send notifications to players. |
| 27 | +You will need access to the `PacketHandler` of the player, which can be obtained |
| 28 | +from the `PlayerRef` using the `getPacketHandler()` method (in this example, |
| 29 | +we get the `PlayerRef` from the `Universe` using the player's UUID). |
| 30 | + |
| 31 | +```java |
| 32 | +public static void onPlayerReady(PlayerReadyEvent event) { |
| 33 | + var player = event.getPlayer(); |
| 34 | + |
| 35 | + var playerRef = Universe.get().getPlayer(player.getUuid()); |
| 36 | + var packetHandler = playerRef.getPacketHandler(); |
| 37 | + |
| 38 | + var primaryMessage = Message.raw("THIS WORKS!!!").color("#00FF00"); |
| 39 | + var secondaryMessage = Message.raw("This is the secondary message").color("#228B22"); |
| 40 | + var icon = new ItemStack("Weapon_Sword_Mithril", 1).toPacket(); |
| 41 | + |
| 42 | + NotificationUtil.sendNotification( |
| 43 | + packetHandler, |
| 44 | + primaryMessage, |
| 45 | + secondaryMessage, |
| 46 | + (ItemWithAllMetadata) icon); |
| 47 | +} |
| 48 | +``` |
0 commit comments