Skip to Content

Player Detection

Overview

This example demonstrates how to detect whether a player is using Lunar Client by listening for the PlayerRegisterChannelEvent on the lunar:apollo channel. Additionally, it showcases how to enable Apollo Modules using utility methods from JsonPacketUtil

Note that this method uses a different plugin channel for sending packets, which is apollo:json, while still using the lunar:apollo for player detection.

Integration

public class ApolloPlayerJsonListener implements Listener { private static final Set<UUID> PLAYERS_RUNNING_APOLLO = new HashSet<>(); public ApolloPlayerJsonListener(ApolloExamplePlugin plugin) { Messenger messenger = Bukkit.getServer().getMessenger(); messenger.registerIncomingPluginChannel(plugin, "lunar:apollo", (s, player, bytes) -> { }); messenger.registerIncomingPluginChannel(plugin, "apollo:json", (s, player, bytes) -> { }); messenger.registerOutgoingPluginChannel(plugin, "apollo:json"); Bukkit.getPluginManager().registerEvents(this, plugin); } public static boolean isPlayerRunningApollo(Player player) { return PLAYERS_RUNNING_APOLLO.contains(player.getUniqueId()); } @EventHandler private void onRegisterChannel(PlayerRegisterChannelEvent event) { if (!event.getChannel().equalsIgnoreCase("lunar:apollo")) { return; } Player player = event.getPlayer(); JsonPacketUtil.enableModules(player); // Sending the player's world name to the client is required for some modules JsonPacketUtil.sendPacket(player, this.createUpdatePlayerWorldMessage(player)); PLAYERS_RUNNING_APOLLO.add(player.getUniqueId()); player.sendMessage("You are using LunarClient!"); } @EventHandler private void onPlayerChangedWorld(PlayerChangedWorldEvent event) { Player player = event.getPlayer(); // Sending the player's world name to the client is required for some modules JsonPacketUtil.sendPacket(player, this.createUpdatePlayerWorldMessage(player)); } private JsonObject createUpdatePlayerWorldMessage(Player player) { JsonObject message = new JsonObject(); message.addProperty("@type", "type.googleapis.com/lunarclient.apollo.player.v1.UpdatePlayerWorldMessage"); message.addProperty("world", player.getWorld().getName()); return message; } }