forked from AzureAaron/hm-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.java
More file actions
45 lines (38 loc) · 1.79 KB
/
Example.java
File metadata and controls
45 lines (38 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package net.azureaaron.hmapi;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.azureaaron.hmapi.events.HypixelPacketEvents;
import net.azureaaron.hmapi.network.HypixelNetworking;
import net.azureaaron.hmapi.network.packet.s2c.ErrorS2CPacket;
import net.azureaaron.hmapi.network.packet.s2c.HypixelS2CPacket;
import net.azureaaron.hmapi.network.packet.v1.s2c.LocationUpdateS2CPacket;
import net.minecraft.util.Util;
public class Example {
public static void init() {
HypixelPacketEvents.LOCATION_UPDATE.register(Example::handlePacket);
//HM API will automatically send the event registration packet upon logging into Hypixel.
HypixelNetworking.registerToEvents(Util.make(new Object2IntOpenHashMap<>(), map -> {
map.put(LocationUpdateS2CPacket.ID, 1);
}));
}
/**
* This example demonstrates the ideal way to handle received packets which is by using switch pattern matching and record patterns.
*
* You can also handle more than one packet type by adding new switch cases as demonstrated.
*/
private static void handlePacket(HypixelS2CPacket packet) {
switch (packet) {
case LocationUpdateS2CPacket(var serverName, var serverType, var lobbyName, var mode, var map) -> {
//Do something with the updated location data
}
//If you merge multiple handlers together, its recommended to add a guard case for the packet id, so that you can differentiate
//between the sources of errors
case ErrorS2CPacket(var id, var errorReason) -> {
//Handle errors if you want
}
default -> {
//Its highly recommended to do nothing here, future versions of the location packet for instance may also invoke this same event
//which could cause exceptions to be thrown if thats what you do here. The same could hold true for any packet/packet event.
}
}
}
}