Skip to content

Instantly share code, notes, and snippets.

@killjoy1221
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killjoy1221/0daf4e791f3eb9146947 to your computer and use it in GitHub Desktop.
Save killjoy1221/0daf4e791f3eb9146947 to your computer and use it in GitHub Desktop.
ServerInfo

Server Info for Bungee Cord

Server info is meant to help client mods which use per-server settings to differentiate between different servers on a bungee cord proxy. The mod needs to know about and listen to this plugin in order to benefit from it.

Server Admins

Install the plugin by dropping the jar into the plugins folder of your bungee environment. This is not made to work using bukkit. If you wish to allow the clients to see connected players, enable it in the config.

Mod Developers

The plugin channel's name you want to listen to is bungee-serverinfo. See this for help with packets.

The recieved data will be in the form of a json string. This is the format:

{
    "name": "Server's Name",
    "motd": "Server's MOTD",
    "playerList": [
        "player1",
        "player2"
    ]
}
  • name is the name the server was given in bungee's configuration file.
  • motd is the message of the day, gotten directly from the server.
  • playerList is the array of players currently on the server. This value may not exist depending on the server's configuration.
package mnm.plugins.serverinfo;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.ServerConnectedEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;
import net.md_5.bungee.event.EventHandler;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
public class ServerInfoPlugin extends Plugin implements Listener {
private static final String cfgSEND_PLAYER_LIST = "sendPlayerList";
private static final String msgCHANNEL = "serverinfo";
private static final String msgNAME = "name";
private static final String msgMOTD = "motd";
private static final String msgPLAYERS = "playerList";
private Configuration config;
@Override
public void onEnable() {
if (!getDataFolder().exists())
getDataFolder().mkdir();
File file = new File(getDataFolder(), "config.yml");
InputStream stream = getResourceAsStream("config.yml");
try {
if (!file.exists()) {
Files.copy(stream, file.toPath());
}
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file);
} catch (IOException e) {
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(stream);
} finally {
try {
stream.close();
} catch (IOException e) {
}
}
this.getProxy().getPluginManager().registerListener(this, this);
}
@EventHandler
public void onServerSwitch(ServerConnectedEvent event) {
final ProxiedPlayer player = event.getPlayer();
JsonObject msg = getServerInfo(event.getServer().getInfo());
getLogger().info("Sending server info for " + msg.get(msgNAME) + " to " + player.getName());
player.sendData(msgCHANNEL, new Gson().toJson(msg).getBytes());
}
private JsonObject getServerInfo(ServerInfo server) {
JsonObject obj = new JsonObject();
obj.addProperty(msgNAME, server.getName());
obj.addProperty(msgMOTD, server.getMotd());
if (config.getBoolean(cfgSEND_PLAYER_LIST, false)) {
JsonArray players = new JsonArray();
for (ProxiedPlayer player : server.getPlayers()) {
players.add(new JsonPrimitive(player.getName()));
}
obj.add(msgPLAYERS, players);
}
return obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment