How can I make a player join a server with a spigot plugin?

Discussion in 'Spigot Plugin Development' started by BakedPotato_HD, Jul 7, 2015.

  1. How can I make a player join a server (BungeeCord) with a spigot plugin?

    I tried:
    Bukkit.dispatchCommand(player, "/server <name>");

    Thanks
     
  2. Dispatch command won't work because it runs a command from the Bukkit/Spigot server, and /server is from BungeeCord.
     
  3. @BetaNyan
    Yeah I knew that after it didn't work :p
    But do you know how to get it to work?
     
  4. You probably will have to use "PluginMessage-Channels"..

    (Or try out my API BungeeBridge;))
     
    • Agree Agree x 1
  5. @dommi2212
    What's PluginMessage-Channels?
    And would your API help me?
     
  6. Gianluca

    Gianluca Retired Resource Staff
    Retired Patron

    You can install a plugin called MicroJump, and then your dispatch method will work
     
  7. I tried this:
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.writeUTF("Connect to");
    out.writeUTF("Hub");
    p.sendPluginMessage(this, "BungeeCord", out.toByteArray());
    But it gives an NullPointerException
    even though I put
    if (!(e.getCurrentItem == null)) {
    }
     
  8. First Question: See above
    Second one: Yes, of course! BungeeBridge is an easy to use API for letting spigot and bungee communicate. ;)
     
  9. You should replace "Connect to" with "Connect", please provide the stack trace and the line that the error points too
     
  10. With Connect it also didn't work and the line was
    p.sendPluginMessage()
    I am going to try MicroJump

    Thanks for all the replys
     
  11. Are you sure that p is not null? I guess you are using PlayerInteractEvent. You could try event.getPlayer().sendPluginMessage(...); You could also try adding a line with Bukkit.broadcastMessage("Player is null: " + event.getPlayer()); if it says "true" then you got some serious errors in your code (probably a faulty plugin or some part of your code calling a fake player interact event with the player = null, or you are using a very old version of bukkit/spigot with some bugs in it).
     
  12. When I put Bukkit.dispatchCommand(p, "/server hub") it says unknown so p can't be null (right?)
    I tried using MicroJump but it doesn't work, it says unknown command
     
  13. BTW should I use
    import net.minecraft.util.com.google.common.io.ByteArrayDataOutput;
    import net.minecraft.util.com.google.common.io.ByteStreams;

    or the other import?
     
  14. Could you print the whole stack trace, with the necessary code?

    That is the correct import.
     
  15. @xADudex
    Code (Text):
    if (!(e.getCurrentItem() == null)) {
                    if (e.getCurrentItem().getItemMeta().getDisplayName().equals(ChatColor.BLUE + "OPFactions")) {
                        p.sendMessage(ChatColor.YELLOW + "You have been teleported to OPFactions!");
                        ByteArrayDataOutput out = ByteStreams.newDataOutput();
                        out.writeUTF("Connect to");
                        out.writeUTF("OPFactions");
                        p.sendPluginMessage(this, "BungeeCord", out.toByteArray());
                        return;
                    } else if (e.getCurrentItem().getItemMeta().getDisplayName().equals(ChatColor.BLUE + "Prison")) {
                        p.sendMessage(ChatColor.YELLOW + "You have been teleported to Prison!");
                        ByteArrayDataOutput out = ByteStreams.newDataOutput();
                        out.writeUTF("Connect to");
                        out.writeUTF("Prison");
                        p.sendPluginMessage(this, "BungeeCord", out.toByteArray());
                        return;
                    } else if (e.getCurrentItem().getType().equals(Material.STAINED_GLASS_PANE)) {
                        return;
                    } else if (e.getCurrentItem().getItemMeta().getDisplayName().equals(ChatColor.BLUE + "Minigames")) {
                        p.sendMessage(ChatColor.YELLOW + "You have been teleported to Minigames!");
                        ByteArrayDataOutput out = ByteStreams.newDataOutput();
                        out.writeUTF("Connect to");
                        out.writeUTF("Minigames");
                        p.sendPluginMessage(this, "BungeeCord", out.toByteArray());
                    } else if (e.getCurrentItem().getItemMeta().getDisplayName().equals(ChatColor.BLUE + "Factions")) {
                        p.sendMessage(ChatColor.YELLOW + "You have been teleported to Factions!");
                        ByteArrayDataOutput out = ByteStreams.newDataOutput();
                        out.writeUTF("Connect to");
                        out.writeUTF("Factions");
                        p.sendPluginMessage(this, "BungeeCord", out.toByteArray());
                    } else if (e.getCurrentItem().getItemMeta().getDisplayName().equals(ChatColor.RED + "Coming Soon")) {
                        p.sendMessage(ChatColor.RED + "This server isn't added yet. :)");
                    } else if (e.getCurrentItem().getItemMeta().getDisplayName().equals(ChatColor.BLUE + "Hub")) {
                        p.sendMessage(ChatColor.YELLOW + "You have been teleported to the Hub!");
                        ByteArrayDataOutput out = ByteStreams.newDataOutput();
                        out.writeUTF("Connect to");
                        out.writeUTF("Hub");
                        p.sendPluginMessage(this, "BungeeCord", out.toByteArray());
                    }
                } else {
                    p.sendMessage(ChatColor.DARK_RED + "INVALLID!");
                    return;
                }
    I also have microjump on the server.
     
  16. Use my method, is a correct way to use it.. :p
    Code (Text):

      public static void changeServer(Player p, String server) {
        ByteArrayOutputStream data = new ByteArrayOutputStream();
      DataOutputStream out = new DataOutputStream(data);
          try {
        out.writeUTF("Connect");
        out.writeUTF(server);
        } catch (Exception e) {
        }
        p.sendPluginMessage(Main.getInstance(), "BungeeCord", data.toByteArray());
        try {
        out.close();
        data.close();
        } catch (Exception e) {
        }
       }
    You need to add this to onEnable():
    Code (Text):

    getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
     
  17. @Fr33stylerTC
    What should the import be of ByteArrayOutputStream and when I import Main it gives an error of getInstace() "Undefined type for Main"

    Thanks