> For the complete documentation index, see [llms.txt](https://impoo.gitbook.io/authenticationsystem/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://impoo.gitbook.io/authenticationsystem/developers/examples.md).

# Examples

Practical code examples for common AuthSystem integrations.

## Example 1: Check if player is authenticated

```java
import me.authsystem.api.AuthSystemApi;
import me.authsystem.api.auth.AuthManager;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;

public class AuthListener implements Listener {

    private final AuthManager authManager;

    public AuthListener() {
        AuthSystemApi api = AuthSystemApi.get();
        this.authManager = api != null ? api.getAuthManager() : null;
    }

    @EventHandler
    public void onCommand(PlayerCommandPreprocessEvent event) {
        if (authManager == null) return;

        Player player = event.getPlayer();
        if (!authManager.isAuthenticated(player)) {
            event.setCancelled(true);
            player.sendMessage("§cYou must log in to use commands!");
        }
    }
}
```

## Example 2: Register a player programmatically

```java
import me.authsystem.api.AuthSystemApi;
import me.authsystem.api.database.AuthTable;
import me.authsystem.api.database.PlayerData;
import org.bukkit.entity.Player;

import java.sql.Timestamp;

public void registerPlayer(Player player, String password) {
    AuthSystemApi api = AuthSystemApi.get();
    if (api == null) return;

    AuthTable authTable = api.getAuthTable();

    PlayerData data = new PlayerData(
        player.getName(),
        player.getUniqueId(),
        "", // hashed password will be set by AuthManager
        player.getAddress().getHostString(),
        new Timestamp(System.currentTimeMillis()),
        false
    );

    authTable.registerPlayer(data)
        .thenAccept(success -> {
            if (success) {
                player.sendMessage("§aAccount registered successfully!");
            } else {
                player.sendMessage("§cRegistration failed.");
            }
        });
}
```

## Example 3: Get player data from database

```java
import me.authsystem.api.AuthSystemApi;
import me.authsystem.api.database.AuthTable;
import me.authsystem.api.database.PlayerData;
import org.bukkit.entity.Player;
import java.util.UUID;

public void showPlayerInfo(Player target) {
    AuthSystemApi api = AuthSystemApi.get();
    if (api == null) return;

    AuthTable authTable = api.getAuthTable();

    authTable.getPlayerByUuid(target.getUniqueId())
        .thenAccept(optionalData -> {
            if (optionalData.isPresent()) {
                PlayerData data = optionalData.get();
                target.sendMessage("§7Username: §f" + data.username());
                target.sendMessage("§7UUID: §f" + data.uuid());
                target.sendMessage("§7Premium: §f" + data.preamium());
                target.sendMessage("§7Last IP: §f" + data.lastIp());
            } else {
                target.sendMessage("§cPlayer not found in database.");
            }
        });
}
```

## Example 4: Check premium status

```java
import me.authsystem.api.AuthSystemApi;
import me.authsystem.api.database.AuthTable;
import org.bukkit.entity.Player;
import java.util.UUID;

public void checkPremium(Player player) {
    AuthSystemApi api = AuthSystemApi.get();
    if (api == null) return;

    AuthTable authTable = api.getAuthTable();

    authTable.isPremium(player.getUniqueId())
        .thenAccept(isPremium -> {
            if (isPremium) {
                player.sendMessage("§aYou are a premium player!");
            } else {
                player.sendMessage("§7You are not premium.");
            }
        });
}
```

## Example 5: Require authentication for a custom command

```java
import me.authsystem.api.AuthSystemApi;
import me.authsystem.api.auth.AuthManager;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class FlyCommand implements CommandExecutor {

    private final AuthManager authManager;

    public FlyCommand() {
        AuthSystemApi api = AuthSystemApi.get();
        this.authManager = api != null ? api.getAuthManager() : null;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player player)) {
            sender.sendMessage("§cThis command can only be run by players.");
            return true;
        }

        if (authManager == null || !authManager.isAuthenticated(player)) {
            player.sendMessage("§cYou must log in to use /fly!");
            return true;
        }

        player.setAllowFlight(!player.getAllowFlight());
        player.sendMessage("§aFlight " + (player.getAllowFlight() ? "enabled" : "disabled") + "!");
        return true;
    }
}
```

***

> :bulb: **Tip**: Always check that `AuthSystemApi.get()` is not null before using the API, in case AuthSystem is not installed.
