Bungee Help? Get Server by Name and Get SwitchServer

Discussion in 'BungeeCord Plugin Development' started by gh0ztbuster, May 5, 2015.

  1. I'm really stumped :/

    I need players to be sent to a the gateway server, and then a couple of seconds later, be sent to the appropriately defined server. Basically sending players to a server in between the SwitchServerEvent.

    The three problems I face are:
    1. Canceling the initial event. "e.setCancelled(true);" doesn't apply to SwitchServerEvent.
    2. Connecting a player to a specific server by name. "player.connect("gateway");"
    3. Calling the server the player was originally connecting to. "SwitchServerEvent player.connect(e.getServer());"
    Here is my code:
    Code (Text):
    package me.gh0ztbuster.gatewayserver;

    import net.md_5.bungee.api.ProxyServer;
    import net.md_5.bungee.api.connection.ProxiedPlayer;
    import net.md_5.bungee.api.connection.Server;
    import net.md_5.bungee.api.event.ServerSwitchEvent;
    import net.md_5.bungee.api.plugin.Listener;
    import net.md_5.bungee.api.plugin.Plugin;
    import net.md_5.bungee.event.EventHandler;

    public class GatewayServer extends Plugin implements Listener {
        @Override
        public void onEnable(){
            ProxyServer.getInstance().getPluginManager().registerListener(this, this);
            getLogger().info("Plugin enabled =)");
        }

        @Override
        public void onDisable(){
            getLogger().info("Plugin disable ='(");
        }

        @EventHandler
        public void onServerSwitch(ServerSwitchEvent e) {
            ProxiedPlayer player = e.getPlayer();
            e.setCancelled(true);
            player.connect("gateway");
            wait(5000);
            player.connect(e.getServer());
        }
    }
     
    Is this possible? and if not, what kind of alternative could I pursue?

    The reason I'm doing this is because symlink playerdata doesn't work on BungeeCord servers. The PlayerLoginEvent (and AsyncPlayerPreLoginEvent), on the connecting server, activates before the PlayerQuitEvent, on the disconnecting server.


    Thanks for looking over my code.
     
    #1 gh0ztbuster, May 5, 2015
    Last edited: May 5, 2015
  2. The ServerSwitchEvent is called once a player has been connected to the server — e.getServer() returns the server the player currently is connected to.

    You should try to use the ServerConnectEvent, as stated on the API docs: "Called when deciding to connect to a server. At the time when this event is called, no connection has actually been made. Cancelling the event will ensure that the connection does not proceed and can be useful to prevent certain players from accessing certain servers."
    Using that event, you can use event.setTarget(ServerInfo target) and connect the player to the desired server.
     
  3. I'm not exactly sure why this doesn't work.

    Code (Text):
    @EventHandler
        public void onServerSwitch(ServerConnectEvent e) {
            ProxiedPlayer player = e.getPlayer();
            ServerInfo server = e.getTarget();
            ServerInfo gateway = getProxy().getServers().put("gateway", server);
            e.setTarget(server);
            player.connect(gateway);
            try{
                wait(1500);
            }catch(InterruptedException e2){
                System.out.println("got interrupted!");
            }
            player.connect(server);
        }
     
    #3 gh0ztbuster, May 5, 2015
    Last edited: May 5, 2015
  4. • Use
    Code (Text):
    getProxy().getServer("gateway")
    for your gateway variable.

    • Replace these lines:
    Code (Text):
    e.setTarget(server);
    player.connect(gateway);
    with
    Code (Text):
    e.setTarget(gateway)
    • Instead of using
    Code (Text):
    wait(1500)
    use the scheduler provided by BungeeCord.
     
  5. The method getServer(String) is undefined for the type ProxyServer
     
  6. I meant getServerInfo(String)
     
  7. I didn't get rid of the "wait(1500);" yet because I want to make sure that everythings working.

    I get this error:
    Code (Text):
    WARNING: Error dispatching event ServerConnectEvent(player=Bloodreapor, target=B
    ungeeServerInfo(name=gateway, address=/172.16.1.64:25568, restricted=false), can
    celled=false) to listener [email protected]
    89
    java.lang.IllegalMonitorStateException
        at java.lang.Object.wait(Native Method)
        at io.github.gh0ztbuster.middleserver.MiddleServer.onServerSwitch(MiddleServ
    er.java:31)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at net.md_5.bungee.event.EventHandlerMethod.invoke(EventHandlerMethod.java:1
    9)
        at net.md_5.bungee.event.EventBus.post(EventBus.java:47)
        at net.md_5.bungee.api.plugin.PluginManager.callEvent(PluginManager.java:367
    )
        at net.md_5.bungee.UserConnection.connect(UserConnection.java:224)
        at net.md_5.bungee.connection.InitialHandler$6$1.run(InitialHandler.java:505
    )
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThre
    adEventExecutor.java:380)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEven
    tExecutor.java:116)
        at java.lang.Thread.run(Unknown Source)


    10:50:31 [INFO] [ReaperofDeath] <-> ServerConnector [gateway] has connected
     
  8. Show your current code.
     
  9. Code (Text):
    @EventHandler
        public void onServerSwitch(ServerConnectEvent e) {
            ProxiedPlayer player = e.getPlayer();
            ServerInfo server = e.getTarget();
            ServerInfo gateway = getProxy().getServerInfo("gateway");
            e.setTarget(gateway);
            try {
                wait(1500);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
            player.connect(server);
        }
     
  10. As I stated before, use the scheduler provided by Spigot instead of the wait method.
     
  11. How would I do that in BungeeCord?
     
  12. getProxy().getScheduler().schedule(......)
     
  13. I'm having two issues:
    1. BungeeCord thinks I'm already connected to all servers.
    2. "player.connect(server);" executes every second.

    Here is my code:
    Code (Text):
    package me.gh0ztbuster.middleserver;

    import net.md_5.bungee.api.ProxyServer;
    import net.md_5.bungee.api.config.ServerInfo;
    import net.md_5.bungee.api.connection.ProxiedPlayer;
    import net.md_5.bungee.api.event.ServerConnectEvent;
    import net.md_5.bungee.api.plugin.Listener;
    import net.md_5.bungee.api.plugin.Plugin;
    import net.md_5.bungee.event.EventHandler;
    import java.util.concurrent.TimeUnit;

    public class MiddleServer extends Plugin implements Listener {
     
        @Override
        public void onEnable(){
            ProxyServer.getInstance().getPluginManager().registerListener(this, this);
            getLogger().info("Plugin enabled =)");
        }
     
        @Override
        public void onDisable(){
            getLogger().info("Plugin disable ='(");
        }
     
        @EventHandler
        public void onServerSwitch(ServerConnectEvent e) {
            final ProxiedPlayer player = e.getPlayer();
            final ServerInfo server = e.getTarget();
            ServerInfo gateway = getProxy().getServerInfo("gateway");
            e.setTarget(gateway);
            getProxy().getScheduler().schedule(this, new Runnable() {
                @Override
                public void run() {
                    player.connect(server);
                }
                }, 1000L, TimeUnit.MILLISECONDS);
        }
    }
     
  14. This just creates one massive loop, there's got to be another way of doing this...

    Can I cancel or disable the listener on "player.getServer().equals(e.getTarget())"?