Search in sources :

Example 1 with StatusResponseMessage

use of net.glowstone.net.message.status.StatusResponseMessage in project Glowstone by GlowstoneMC.

the class StatusRequestHandler method handle.

@Override
@SuppressWarnings("unchecked")
public void handle(GlowSession session, StatusRequestMessage message) {
    // create and call the event
    GlowServer server = session.getServer();
    int online = server.getOnlinePlayers().size();
    InetAddress address = session.getAddress().getAddress();
    StatusEvent event = new StatusEvent(address, server.getMotd(), online, server.getMaxPlayers());
    event.players = new ArrayList<>(server.getOnlinePlayers());
    event.icon = server.getServerIcon();
    event.serverType = server.getServerType();
    event.clientModsAllowed = server.getAllowClientMods();
    EventFactory.callEvent(event);
    // build the json
    JSONObject json = new JSONObject();
    JSONObject version = new JSONObject();
    version.put("name", GlowServer.GAME_VERSION);
    int protocolVersion = GlowServer.PROTOCOL_VERSION;
    version.put("protocol", protocolVersion);
    json.put("version", version);
    JSONObject players = new JSONObject();
    players.put("max", event.getMaxPlayers());
    players.put("online", online);
    if (!event.players.isEmpty()) {
        event.players = event.players.subList(0, Math.min(event.players.size(), server.getPlayerSampleCount()));
        Collections.shuffle(event.players);
        JSONArray playersSample = new JSONArray();
        for (Player player : event.players) {
            JSONObject p = new JSONObject();
            p.put("name", player.getName());
            p.put("id", player.getUniqueId().toString());
            playersSample.add(p);
        }
        players.put("sample", playersSample);
    }
    json.put("players", players);
    JSONObject description = new JSONObject();
    description.put("text", event.getMotd());
    json.put("description", description);
    if (event.icon.getData() != null) {
        json.put("favicon", event.icon.getData());
    }
    // Mod list must be included but can be empty
    // TODO: support adding GS-ported Forge server-side mods?
    JSONArray modList = new JSONArray();
    JSONObject modinfo = new JSONObject();
    modinfo.put("type", event.serverType);
    modinfo.put("modList", modList);
    if (!event.clientModsAllowed) {
        modinfo.put("clientModsAllowed", false);
    }
    json.put("modinfo", modinfo);
    // send it off
    session.send(new StatusResponseMessage(json));
}
Also used : Player(org.bukkit.entity.Player) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) StatusResponseMessage(net.glowstone.net.message.status.StatusResponseMessage) GlowServer(net.glowstone.GlowServer) InetAddress(java.net.InetAddress)

Aggregations

InetAddress (java.net.InetAddress)1 GlowServer (net.glowstone.GlowServer)1 StatusResponseMessage (net.glowstone.net.message.status.StatusResponseMessage)1 Player (org.bukkit.entity.Player)1 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1