Solved Different default server for some players?

Discussion in 'BungeeCord Plugin Development' started by MrAndeos, Feb 18, 2019.

  1. Hello.
    I need to change the default server dynamically for some players when they join the proxy, currently I'am doing this at ServerConnectEvent, like that:
    Code (Java):
    if (something is true and player recently joined the proxy) {
        ServerInfo alternativeLobby = Main.plugin.getProxy().getServerInfo(LobbyUtils.getBestLobby());
        event.setTarget(alternativeLobby);
    }
    But this sometimes doesn't work... Especially for players with poor internet connection and bigger ping, they just get connected to the default server from bungee config, is there any other way to do that?
     
    #1 MrAndeos, Feb 18, 2019
    Last edited: Feb 18, 2019
  2. simpleauthority

    Supporter

    PostLoginEvent

    Then, get the ProxiedPlayer and call connect(ServerInfo, ServerConnectEvent.Reason.JOIN_PROXY).
     
  3. Ok, but now It also doesn't always work, here are some logs from bungee console:
    Code (Text):
    14:14:33 [INFO] [Andeos] <-> ServerConnector [viplobby1] has connected
    14:14:33 [INFO] [Andeos] <-> ServerConnector [lobby1] has connected
    14:14:33 [INFO] [Andeos] disconnected with: Connection has been interrupted.
    14:14:33 [INFO] [Andeos] <-> DownstreamBridge <-> [lobby1] has disconnected
    14:14:34 [INFO] [Andeos] <-> DownstreamBridge <-> [viplobby1] has disconnected
    14:14:34 [INFO] [Andeos] -> UpstreamBridge has disconnected
    Or sometimes when it actually properly connects me to viplobby1:
    Code (Text):
    14:22:24 [INFO] [Andeos] <-> ServerConnector [viplobby1] has connected
    14:22:24 [INFO] [Andeos] <-> ServerConnector [lobby1] has connected
    14:22:25 [INFO] [Andeos] <-> DownstreamBridge <-> [lobby1] has disconnected
     
  4. simpleauthority

    Supporter

    My bad. Please try to do the following.

    First, listen to ServerConnectEvent. Within it, check that the reason is JOIN_PROXY (you may ALSO want the reason to be LOBBY_FALLBACK (when they try to connect to another, but can't), SERVER_DOWN_REDIRECT (when the server goes down, where do they go), and KICK_REDIRECT (when they are kicked off a server, where do they go)). Inside that conditional, then set the target of the event to the server you wish for that user.

    Do not cancel the event.

    Tell me if that works.

    Pseudocode:
    Code (Text):
    on ServerConnectEvent e:
      if e.reason is any(JOIN_PROXY, LOBBY_FALLBACK, SERVER_DOWN_REDIRECT, KICK_REDIRECT) then:
        e.target = ProxyServer.getServerInfo(name)
    Sidenote: I don't know what your original conditional was, so this may be what your initial solution was. If it is, can you share what your original conditional was so we can try something else?
     
  5. Looks like that worked, my initial solution was to check if current milliseconds time is smaller than the one saved on PreLoginEvent + 2000 ms, and if it was, then change the lobby server. Sometimes it probably was more than that 2000 ms before this if-statement got executed, that's why it wasn't working. Thanks for help! ;)