Sending players between servers in BungeeCord

Jul 27, 2016
Sending players between servers in BungeeCord
  • Sending players between servers with BungeeCord

    How to send players between Spigot servers from the BungeeCord proxy



    It is recommended that you read the topic on creating basic commands if you haven't already here!


    In this tutorial we will create a simple command which will send the player to another server. For this tutorial we will use Hub as the server we will be connecting to.


    Creating the command(top)


    First of all, create a command and register it in in the main class:

    Code (Java):
    public class CommandHub extends Command{
        public CommandHub(){
            super("hub","permission.hub",new String[0]);
        }
        public void execute(CommandSender sender, String[] args){

        }
    }
     
    You don't have to have a permission here if you don't need one for the command.
    You'll need the permission permission.hub in your BungeeCord config to be able to execute this command.
    Code (Java):
    public void onEnable(){
            getProxy().getPluginManager().registerCommand(this, new CommandHub());
        }

    Testing for a Player(top)



    We need to test if the command is being sent by a player so that we don't receive an error when casting the CommandSender to a ProxiedPlayer:

    Code (Java):
    public void execute(CommandSender sender, String[] args){
            if(sender instanceof ProxiedPlayer){
                    ProxiedPlayer player = (ProxiedPlayer) sender;
            }else{
                sender.sendMessage(new ComponentBuilder("This command can only be run by a player!").color(ChatColor.RED).create());
            }
        }

    Check if the Player is already connected(top)



    Inside the 'if' statement we need to check if the player is already connected to the server otherwise the command would be run for no reason:

    Code (Java):
    if(!player.getServer().getInfo().getName().equalsIgnoreCase("hub")){
    }else{
            player.sendMessage(new ComponentBuilder("You are already connected to the Hub!").color(ChatColor.RED).create());
    }

    Sending the Player(top)



    We will need to declare the selected server as ServerInfo with a variable called target and use the variable to change the player's server (Place inside the nested if statement):

    Code (Java):
    ServerInfo target = ProxyServer.getInstance().getServerInfo("Hub");
    player.connect(target);
    Remember to make your plugin.yml using this tutorial!
  • Loading...
  • Loading...