javasocketspluginsbukkitbungeecord

How to get the playercount from another Paper Spigot server running through Bungeecord


I'm currently working on a plugin for Bukkit (Paper Spigot) that acts as a queue. This plugin in running in a queue server that then will take players over to the proper server when the proper server has less that a set number of players. I need to obtain the playercount from the proper server and use it in code from my queue plugin. I found examples on how to do this using Bukkit's messaging channel but that requires players (I think) and I don't completely understand it even though I've read many other articles and stackoverflow posts as most of these people have had slightly different problems. I've also heard other people talk about sockets but I'm not sure how they work. I am fine with running a second plugin in either the proper server or the Bungeecord server. I just need to know what code to write and where to write it! or at least a helpful example of a working system.

Here's my code so far:

package com.Package;

import org.bukkit.Bukkit;
//import org.bukkit.command.Command;
//import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.messaging.PluginMessageListener;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import java.util.ArrayList;
import java.util.logging.Logger;

public class AnarchyQueue extends JavaPlugin implements PluginMessageListener {
    ArrayList<Player> players = new ArrayList<Player>();
    Boolean isListening = false;
    int playerNumber = 0;
    @Override
    public void onEnable() {
        getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
        getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
        Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
            public void run() {
                getCount();
                log(Integer.toString(playerNumber));
            }
        }, 200L, 200L);
    }
    @Override
    public void onDisable() {

    }
    private void log(String str)
    {
        Logger.getLogger("Minecraft").info(str);
    }
    public void onPlayerJoin(PlayerJoinEvent event)
    {
        players.add(event.getPlayer());
    }
    /*public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (command.getName().equalsIgnoreCase("players")) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("You must be in game!");
                return true;
            }
            else {
                getCount();
            }
        }
        return true;   
    }*/
    private void getCount() {   
        ByteArrayDataOutput out = ByteStreams.newDataOutput();
        out.writeUTF("PlayerCount");
        out.writeUTF("anarchy");
        Player player = Bukkit.getPlayerExact("Ed_Silver");
        player.sendPluginMessage(this, "BungeeCord", out.toByteArray());
        isListening=true;
    }
    @Override
    public void onPluginMessageReceived(String channel, Player player, byte[] message) {
        if (channel.equals("BungeeCord")) {
            ByteArrayDataInput in = ByteStreams.newDataInput(message);
            String subchannel = in.readUTF();
            if (subchannel.equals("PlayerCount") && isListening) {
                int playerCount = in.readInt();
                playerNumber = playerCount;
                isListening=false;
            }
        }
    }
}

I know this code is broken in many ways, I am not really looking to fix it - I just want a way to do what I'm doing and I'll work the rest out later. (giving code as people tend to get shirty if they think you haven't tried :) !)

Feel free to ask for more detail etc...

Thanks in advance, Edward


--EDIT--

I have found Jedis and have been experimenting. If someone could explain Jedis to me that would be great. This is what I've found so far:

public static void main(String[] args) {// priOnEnable() {
        //Connecting to Redis server on localhost 
        Jedis jedis = new Jedis("localhost"); 
        System.out.println("Connection to server sucessfully"); 
        //check whether server is running or not 
        System.out.println("Server is running: "+jedis.ping());
    }

This gets an error however. I believe that I need to be running something else to manage Redis or something.

Thanks Again,

Edward


Solution

  • What I've done: Installed Redis (https://github.com/MicrosoftArchive/redis/releases) Used guides such as https://www.tutorialspoint.com/redis/redis_java.htm to work out code. Good luck to all those out there attempting this! Have fun and I hope you work it out!