|
| 1 | +import { Callout } from 'nextra-theme-docs' |
| 2 | + |
| 3 | +# Serverbound Packets |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Players using Lunar Client may send packets to the server for specific Apollo modules, such as the `PacketEnrichment Module` and/or when the player is joining the server. This example demonstrates how to handle packets sent from the client that are related to Apollo while using JSON messages. |
| 8 | + |
| 9 | +Additionally, the `Transfer Module` expects a response packet from the client after the server sends a request. For an example of how to handle roundtrip packets, visit [Packet Roundtrip Example](/apollo/developers/lightweight/json/roundtrip-packets) |
| 10 | + |
| 11 | +<Callout type="info"> |
| 12 | + Note that this method uses a different plugin channel for |
| 13 | + sending and receiving packets, which is `apollo:json`. |
| 14 | +</Callout> |
| 15 | + |
| 16 | +## Integration |
| 17 | + |
| 18 | +```java |
| 19 | +public class ApolloPacketReceiveJsonListener implements PluginMessageListener { |
| 20 | + |
| 21 | + private static final String TYPE_PREFIX = "type.googleapis.com/"; |
| 22 | + private static final JsonParser JSON_PARSER = new JsonParser(); |
| 23 | + |
| 24 | + public ApolloPacketReceiveJsonListener(ApolloExamplePlugin plugin) { |
| 25 | + Bukkit.getServer().getMessenger().registerIncomingPluginChannel(plugin, "apollo:json", this); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public void onPluginMessageReceived(@NonNull String channel, @NonNull Player player, byte[] bytes) { |
| 30 | + JsonObject payload; |
| 31 | + try { |
| 32 | + payload = JSON_PARSER.parse(new String(bytes, StandardCharsets.UTF_8)).getAsJsonObject(); |
| 33 | + } catch (Exception e) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + if (!payload.has("@type")) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + String type = payload.get("@type").getAsString(); |
| 42 | + if (type.startsWith(TYPE_PREFIX)) { |
| 43 | + type = type.substring(TYPE_PREFIX.length()); |
| 44 | + } |
| 45 | + |
| 46 | + if ("lunarclient.apollo.player.v1.PlayerHandshakeMessage".equals(type)) { |
| 47 | + this.onPlayerHandshake(payload); |
| 48 | + } |
| 49 | + |
| 50 | + // Packet Enrichment Module |
| 51 | + if ("lunarclient.apollo.packetenrichment.v1.PlayerAttackMessage".equals(type)) { |
| 52 | + this.onPlayerAttack(payload); |
| 53 | + } else if ("lunarclient.apollo.packetenrichment.v1.PlayerChatOpenMessage".equals(type)) { |
| 54 | + this.onPlayerChatOpen(payload); |
| 55 | + } else if ("lunarclient.apollo.packetenrichment.v1.PlayerChatCloseMessage".equals(type)) { |
| 56 | + this.onPlayerChatClose(payload); |
| 57 | + } else if ("lunarclient.apollo.packetenrichment.v1.PlayerUseItemMessage".equals(type)) { |
| 58 | + this.onPlayerUseItem(payload); |
| 59 | + } else if ("lunarclient.apollo.packetenrichment.v1.PlayerUseItemBucketMessage".equals(type)) { |
| 60 | + this.onPlayerUseItemBucket(payload); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private void onPlayerHandshake(JsonObject message) { |
| 65 | + String checkoutSupport = message.get("embedded_checkout_support").getAsString(); |
| 66 | + |
| 67 | + JsonObject minecraftVersion = message.getAsJsonObject("minecraft_version"); |
| 68 | + String version = minecraftVersion.get("enum").getAsString(); |
| 69 | + |
| 70 | + JsonObject lunarClientVersion = message.getAsJsonObject("lunar_client_version"); |
| 71 | + String gitBranch = lunarClientVersion.get("git_branch").getAsString(); |
| 72 | + String gitCommit = lunarClientVersion.get("git_commit").getAsString(); |
| 73 | + String semVer = lunarClientVersion.get("semver").getAsString(); |
| 74 | + |
| 75 | + for (JsonElement element : message.getAsJsonArray("installed_mods")) { |
| 76 | + JsonObject mod = element.getAsJsonObject(); |
| 77 | + |
| 78 | + String modId = mod.get("id").getAsString(); |
| 79 | + String displayName = mod.get("name").getAsString(); |
| 80 | + String modVersion = mod.get("version").getAsString(); |
| 81 | + String modType = mod.get("type").getAsString(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private void onPlayerAttack(JsonObject message) { |
| 86 | + long instantiationTimeMs = JsonUtil.toJavaTimestamp(message); |
| 87 | + |
| 88 | + JsonObject targetInfo = message.getAsJsonObject("target_info"); |
| 89 | + JsonObject attackerInfo = message.getAsJsonObject("attacker_info"); |
| 90 | + |
| 91 | + this.onPlayerInfo(targetInfo); |
| 92 | + this.onPlayerInfo(attackerInfo); |
| 93 | + } |
| 94 | + |
| 95 | + private void onPlayerChatOpen(JsonObject message) { |
| 96 | + long instantiationTimeMs = JsonUtil.toJavaTimestamp(message); |
| 97 | + this.onPlayerInfo(message.getAsJsonObject("player_info")); |
| 98 | + } |
| 99 | + |
| 100 | + private void onPlayerChatClose(JsonObject message) { |
| 101 | + long instantiationTimeMs = JsonUtil.toJavaTimestamp(message); |
| 102 | + this.onPlayerInfo(message.getAsJsonObject("player_info")); |
| 103 | + } |
| 104 | + |
| 105 | + private void onPlayerUseItem(JsonObject message) { |
| 106 | + long instantiationTimeMs = JsonUtil.toJavaTimestamp(message); |
| 107 | + this.onPlayerInfo(message.getAsJsonObject("player_info")); |
| 108 | + |
| 109 | + boolean mainHand = message.get("main_hand").getAsBoolean(); |
| 110 | + } |
| 111 | + |
| 112 | + private void onPlayerUseItemBucket(JsonObject message) { |
| 113 | + long instantiationTimeMs = JsonUtil.toJavaTimestamp(message); |
| 114 | + this.onPlayerInfo(message.getAsJsonObject("player_info")); |
| 115 | + |
| 116 | + JsonObject rayTraceResult = message.getAsJsonObject("ray_trace_result"); |
| 117 | + |
| 118 | + if (rayTraceResult.has("block")) { |
| 119 | + JsonObject blockHit = rayTraceResult.getAsJsonObject("block"); |
| 120 | + |
| 121 | + JsonObject hitLocation = blockHit.getAsJsonObject("hit_location"); |
| 122 | + JsonObject blockLocation = blockHit.getAsJsonObject("block_location"); |
| 123 | + String directionStr = blockHit.get("direction").getAsString(); |
| 124 | + } else if (rayTraceResult.has("entity")) { |
| 125 | + JsonObject entityHit = rayTraceResult.getAsJsonObject("entity"); |
| 126 | + |
| 127 | + JsonObject hitLocation = entityHit.getAsJsonObject("hit_location"); |
| 128 | + String entityId = entityHit.get("entity_id").getAsString(); |
| 129 | + } else { |
| 130 | + // MISS |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private void onPlayerInfo(JsonObject info) { |
| 135 | + UUID uuid = JsonUtil.toJavaUuid(info.getAsJsonObject("player_uuid")); |
| 136 | + Location location = JsonUtil.toBukkitPlayerLocation(info.getAsJsonObject("location")); |
| 137 | + boolean sneaking = info.get("sneaking").getAsBoolean(); |
| 138 | + boolean sprinting = info.get("sprinting").getAsBoolean(); |
| 139 | + boolean jumping = info.get("jumping").getAsBoolean(); |
| 140 | + float forwardSpeed = info.get("forward_speed").getAsFloat(); |
| 141 | + float strafeSpeed = info.get("strafe_speed").getAsFloat(); |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
0 commit comments