Search in sources :

Example 1 with GlowStatusClient

use of net.glowstone.net.GlowStatusClient 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();
    StatusEvent event = new StatusEvent(new GlowStatusClient(session), server.getMotd(), server.getOnlinePlayers().size(), server.getMaxPlayers(), GlowServer.GAME_VERSION, GlowServer.PROTOCOL_VERSION, server.getServerIcon());
    event.serverType = server.getServerType();
    event.clientModsAllowed = server.getAllowClientMods();
    choosePlayerSample(server, event);
    EventFactory.getInstance().callEvent(event);
    // Disconnect immediately if event is cancelled
    if (event.isCancelled()) {
        session.getChannel().close();
        return;
    }
    // build the json
    JSONObject json = new JSONObject();
    JSONObject version = new JSONObject();
    version.put("name", event.getVersion());
    version.put("protocol", event.getProtocolVersion());
    json.put("version", version);
    if (!event.shouldHidePlayers()) {
        JSONObject players = new JSONObject();
        players.put("max", event.getMaxPlayers());
        players.put("online", event.getNumPlayers());
        JSONArray playersSample = new JSONArray();
        for (PlayerProfile profile : event.getPlayerSample()) {
            JSONObject p = new JSONObject();
            p.put("name", Strings.nullToEmpty(profile.getName()));
            p.put("id", UuidUtils.toString(MoreObjects.firstNonNull(profile.getId(), BLANK_UUID)));
            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.getServerIcon() != null) {
        json.put("favicon", event.getServerIcon().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 : JSONObject(org.json.simple.JSONObject) PlayerProfile(com.destroystokyo.paper.profile.PlayerProfile) JSONArray(org.json.simple.JSONArray) StatusResponseMessage(net.glowstone.net.message.status.StatusResponseMessage) GlowServer(net.glowstone.GlowServer) GlowStatusClient(net.glowstone.net.GlowStatusClient)

Aggregations

PlayerProfile (com.destroystokyo.paper.profile.PlayerProfile)1 GlowServer (net.glowstone.GlowServer)1 GlowStatusClient (net.glowstone.net.GlowStatusClient)1 StatusResponseMessage (net.glowstone.net.message.status.StatusResponseMessage)1 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1