Apollo
Developers
Lightweight
Protobuf
Player Detection

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 ProtobufPacketUtil

Integration

public class ApolloPlayerProtoListener implements Listener {
 
    private final Set<UUID> playersRunningApollo = new HashSet<>();
 
    public ApolloPlayerProtoListener(ApolloExamplePlugin plugin) {
        Bukkit.getServer().getMessenger().registerOutgoingPluginChannel(plugin, "lunar:apollo");
        Bukkit.getServer().getMessenger().registerIncomingPluginChannel(plugin, "lunar:apollo", (s, player, bytes) -> { });
        Bukkit.getPluginManager().registerEvents(this, plugin);
    }
 
    private boolean isPlayerRunningApollo(Player player) {
        return this.playersRunningApollo.contains(player.getUniqueId());
    }
 
    @EventHandler
    private void onRegisterChannel(PlayerRegisterChannelEvent event) {
        if (!event.getChannel().equalsIgnoreCase("lunar:apollo")) {
            return;
        }
 
        ProtobufPacketUtil.enableModules(player);
 
        // Sending the player's world name to the client is required for some modules
        ProtobufPacketUtil.sendPacket(player, this.createUpdatePlayerWorldMessage(player));
 
        this.playersRunningApollo.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
        ProtobufPacketUtil.sendPacket(player, this.createUpdatePlayerWorldMessage(player));
    }
 
    private UpdatePlayerWorldMessage createUpdatePlayerWorldMessage(Player player) {
        return UpdatePlayerWorldMessage.newBuilder()
            .setWorld(player.getWorld().getName())
            .build();
    }
}