Examples

The code provides a BungeeCord command that allows players to request, accept, or deny teleportation to friends, with favorite friends being able to teleport without needing to accept the request.

import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;

public class TPRequestCommand extends Command {

    public TPRequestCommand() {
        super("tprequest");
    }

    @Override
    public void execute(CommandSender commandSender, String[] strings) {

        if (!(commandSender instanceof ProxiedPlayer)) {
            commandSender.sendMessage("This command can only be run by a player.");
            return;
        }

        ProxiedPlayer sender = (ProxiedPlayer) commandSender;

        // Get the friend manager and friend player objects for further operations
        IFriendManager friendManager = FriendSystem.getAPI().getFriendManagerAPI();
        IFriendPlayer senderFriendPlayer = friendManager.getFriendPlayer(sender.getUniqueId().toString());

        // Check the argument length to avoid insufficient arguments error
        if (strings.length < 2) {
            sender.sendMessage("Usage: /tprequest <request/accept/deny> <player>");
            return;
        }

        // Get the target player object using the second argument of the command
        ProxiedPlayer targetPlayer = ProxyServer.getInstance().getPlayer(strings[1]);

        if (targetPlayer == null) {
            sender.sendMessage("The specified player is not online.");
            return;
        }

        // Get the target friend player player to work with the friend methods
        IFriendPlayer targetFriendPlayer = friendManager.getFriendPlayer(targetPlayer.getUniqueId().toString());

        // Determine the type of request (request/accept/deny) and call the corresponding method
        String commandType = strings[0].toLowerCase();
        if (commandType.equals("request")) {
            handleRequest(sender, targetPlayer, senderFriendPlayer, targetFriendPlayer);
        } else if (commandType.equals("accept")) {
            handleAccept(sender, targetPlayer);
        } else if (commandType.equals("deny")) {
            handleDeny(sender, targetPlayer);
        } else {
            sender.sendMessage("Usage: /tprequest <request/accept/deny> <player>");
        }
    }

    // Method to handle teleport request
    private void handleRequest(ProxiedPlayer sender, ProxiedPlayer targetPlayer, IFriendPlayer senderFriendPlayer, IFriendPlayer targetFriendPlayer) {

        // Check if the sender and target are friends
        if (senderFriendPlayer.isFriendsWith(targetPlayer.getUniqueId().toString())) {

            // If the target player has marked the sender as a favorite, teleport immediately without asking for acceptance
            if (targetFriendPlayer.getFriends().get(senderFriendPlayer.getUuid())) {
                sender.sendMessage("Teleporting to " + targetPlayer.getName() + "...");
                // TODO: Add your teleportation logic here
            } else {
                // If not favorites, send a teleport request to the target player
                targetPlayer.sendMessage(sender.getName() + " has requested to teleport to you. Use /tprequest accept " + sender.getName() + " to accept.");
                sender.sendMessage("Teleport request sent to " + targetPlayer.getName());
            }
        } else {
            // If they are not friends, send an error message
            sender.sendMessage("You are not friends with " + targetPlayer.getName());
        }
    }

    // Method to handle teleport acceptance
    private void handleAccept(ProxiedPlayer sender, ProxiedPlayer targetPlayer) {
        // TODO: Check if there's a pending TP request from the targetPlayer and handle it
        sender.sendMessage("Teleport request accepted. Teleporting " + targetPlayer.getName() + " to you...");
        // TODO: Add your teleportation logic here
    }

    // Method to handle teleport denial
    private void handleDeny(ProxiedPlayer sender, ProxiedPlayer targetPlayer) {
        // TODO: Check if there's a pending TP request from the targetPlayer and deny it
        sender.sendMessage("Teleport request from " + targetPlayer.getName() + " denied.");
    }
}

Last updated