Sending player to another server using redis.

Discussion in 'BungeeCord Plugin Development' started by Xrambad, Jan 9, 2020.

  1. I am trying to use Redis to connect the player to selected server in proxy using (PUB/SUB), than using plugin message
    so I did write the code and it works,

    here is the way I am doing it:

    first player type command /minigame

    then Spigot plugin Publish Message like this "PlayerName,ServerName" without the " of course

    then Bungeecord receive the message and processing data and sends the player to the selected server

    but it this correct way of doing it?

    code in spigot side

    Code (Text):
    public static void connectPlayer(Player player, String server_name){
            jedis.publish("ServerSwitcher", player.getName()+","+ server_name);
         
        }
    code in bungeecord side

    Code (Text):
    JedisPubSub s = new JedisPubSub() {
                @Override
                public void onMessage(String channel, String message) {
                    System.out.println(message);
                    String[] connect = message.split(",");
                    plugin.getProxy().getPlayer(connect[0]).connect(plugin.getProxy().getServerInfo(connect[1]));  
                }
            };
            j.subscribe(s, "ServerSwitcher");
           
    Edit1: bungeecord code uses redis Subscribe which is blocking method so it runs on Async thread
     
    #1 Xrambad, Jan 9, 2020
    Last edited: Jan 9, 2020
  2. Do you have sources for that statement? I'm genuinely curious.
     
  3. You should use JSON or any serializer to send data between BungeeCord --> Spigot and Spigot --> BungeeCord.

    You can use these two classes.

    RedisMessage: https://pastebin.com/kg614Sy3
    MessageAction: https://pastebin.com/eN57BnDM

    And the usage:

    In spigot:
    Code (Java):
           
    String json = new RedisMessage(MessageAction.TELEPORT_PLAYER_TO_HUB)
                    .setParam("player", player.getDisplayName())
                    .toJSON();
    jedis.publish("ChannelHere", json);
     
    In bungeecord:
    Code (Java):
       
    JedisPubSub s = new JedisPubSub()
    {
                @Override
                public void onMessage(String channel, String message)
                {
                    RedisMessage redisMessage = new Gson().fromJson(message, RedisMessage.class);
                    switch (redisMessage.getAction())
                    {
                        case TELEPORT_PLAYER_TO_HUB:
                            String playerName = redisMessage.getParam("player");

                            // Do actions here
                            break;
                     }
       

                }
    };
    j.subscribe(s, "Channel");
     
    I hope that can help you to optimize your communication.

    I've forgot to say that the class RedisMessage and MessageAction should be the same on Bungee plugin and on Spigot plugin. You can do that by creating an other java project and import this one in bungee project and spigot project.
     
  4. Okay thanks.
    idk but it happens randomly that bungeecord doesnt get the message not sure why
     
  5. Thanks for classes, i will try use them later