Search in sources :

Example 1 with LanternJsonTextSerializer

use of org.lanternpowered.server.text.gson.LanternJsonTextSerializer in project LanternServer by LanternPowered.

the class HandlerStatusRequest method handle.

@Override
public void handle(NetworkContext context, MessageStatusInRequest message) {
    final NetworkSession session = context.getSession();
    final LanternServer server = session.getServer();
    final Gson gson = new Gson();
    final Text description = server.getMotd();
    final InetSocketAddress address = session.getAddress();
    final InetSocketAddress virtualAddress = session.getVirtualHost();
    final int protocol = session.getProtocolVersion();
    final MinecraftVersion clientVersion = Lantern.getGame().getMinecraftVersionCache().getVersionOrUnknown(protocol, false);
    if (clientVersion == LanternMinecraftVersion.UNKNOWN) {
        Lantern.getLogger().debug("Client with unknown protocol version {} pinged the server.", protocol);
    }
    final LanternStatusClient client = new LanternStatusClient(address, clientVersion, virtualAddress);
    final ClientPingServerEvent.Response.Players players = LanternStatusHelper.createPlayers(server);
    final LanternStatusResponse response = new LanternStatusResponse(Lantern.getGame().getPlatform().getMinecraftVersion(), server.getFavicon(), description, players);
    final Cause cause = Cause.of(EventContext.empty(), new WrappedRemoteConnection(session));
    final ClientPingServerEvent event = SpongeEventFactory.createClientPingServerEvent(cause, client, response);
    Sponge.getEventManager().post(event);
    // Cancelled, we are done here
    if (event.isCancelled()) {
        context.getChannel().close();
        return;
    }
    final JsonObject rootObject = new JsonObject();
    final JsonObject versionObject = new JsonObject();
    checkState(response.getVersion() instanceof LanternMinecraftVersion);
    final LanternMinecraftVersion serverVersion = (LanternMinecraftVersion) response.getVersion();
    versionObject.addProperty("name", serverVersion.getName());
    versionObject.addProperty("protocol", serverVersion.getProtocol());
    if (response.getPlayers().isPresent()) {
        final JsonObject playersObject = new JsonObject();
        playersObject.addProperty("max", players.getMax());
        playersObject.addProperty("online", players.getOnline());
        List<GameProfile> profiles = players.getProfiles();
        if (!profiles.isEmpty()) {
            final JsonArray array = new JsonArray();
            for (GameProfile profile : profiles) {
                Optional<String> optName = profile.getName();
                if (!optName.isPresent()) {
                    continue;
                }
                final JsonObject profileObject = new JsonObject();
                profileObject.addProperty("name", optName.get());
                profileObject.addProperty("id", profile.getUniqueId().toString());
                array.add(profileObject);
            }
            playersObject.add("sample", array);
        }
        rootObject.add("players", playersObject);
    }
    rootObject.add("version", versionObject);
    rootObject.add("description", ((LanternJsonTextSerializer) TextSerializers.JSON).getGson().toJsonTree(response.getDescription()));
    response.getFavicon().ifPresent(icon -> rootObject.addProperty("favicon", ((LanternFavicon) icon).getEncoded()));
    final JsonObject fmlObject = new JsonObject();
    // Trick the client that the server is fml, we support fml channels anyway
    fmlObject.addProperty("type", "FML");
    // The client shouldn't know the plugins (mods) list
    fmlObject.add("modList", new JsonArray());
    // Add the fml info
    rootObject.add("modinfo", fmlObject);
    session.send(new MessageStatusOutResponse(gson.toJson(rootObject)));
}
Also used : NetworkSession(org.lanternpowered.server.network.NetworkSession) MessageStatusOutResponse(org.lanternpowered.server.network.vanilla.message.type.status.MessageStatusOutResponse) LanternMinecraftVersion(org.lanternpowered.server.game.version.LanternMinecraftVersion) InetSocketAddress(java.net.InetSocketAddress) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) Text(org.spongepowered.api.text.Text) LanternJsonTextSerializer(org.lanternpowered.server.text.gson.LanternJsonTextSerializer) LanternStatusResponse(org.lanternpowered.server.network.status.LanternStatusResponse) MessageStatusOutResponse(org.lanternpowered.server.network.vanilla.message.type.status.MessageStatusOutResponse) JsonArray(com.google.gson.JsonArray) LanternStatusClient(org.lanternpowered.server.network.status.LanternStatusClient) ClientPingServerEvent(org.spongepowered.api.event.server.ClientPingServerEvent) LanternServer(org.lanternpowered.server.LanternServer) GameProfile(org.spongepowered.api.profile.GameProfile) Cause(org.spongepowered.api.event.cause.Cause) LanternFavicon(org.lanternpowered.server.network.status.LanternFavicon) LanternStatusResponse(org.lanternpowered.server.network.status.LanternStatusResponse) LanternMinecraftVersion(org.lanternpowered.server.game.version.LanternMinecraftVersion) MinecraftVersion(org.spongepowered.api.MinecraftVersion) WrappedRemoteConnection(org.lanternpowered.server.network.WrappedRemoteConnection)

Example 2 with LanternJsonTextSerializer

use of org.lanternpowered.server.text.gson.LanternJsonTextSerializer in project LanternServer by LanternPowered.

the class TextSerializersRegistryModule method registerDefaults.

@EarlyRegistration
@Override
public void registerDefaults() {
    register(new PlainTextSerializer("minecraft", "plain"));
    register(new FormattingCodeTextSerializer("minecraft", "legacy_formatting_code", TextConstants.LEGACY_CHAR));
    register(new FormattingCodeTextSerializer("minecraft", "formatting_code", '&'));
    register(new LanternJsonTextSerializer("minecraft", "json", Lantern.getGame().getRegistry().getRegistryModule(TranslationManagerRegistryModule.class).get().getTranslationManager()));
}
Also used : PlainTextSerializer(org.lanternpowered.server.text.PlainTextSerializer) FormattingCodeTextSerializer(org.lanternpowered.server.text.FormattingCodeTextSerializer) LanternJsonTextSerializer(org.lanternpowered.server.text.gson.LanternJsonTextSerializer) EarlyRegistration(org.lanternpowered.server.game.registry.EarlyRegistration)

Aggregations

LanternJsonTextSerializer (org.lanternpowered.server.text.gson.LanternJsonTextSerializer)2 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 InetSocketAddress (java.net.InetSocketAddress)1 LanternServer (org.lanternpowered.server.LanternServer)1 EarlyRegistration (org.lanternpowered.server.game.registry.EarlyRegistration)1 LanternMinecraftVersion (org.lanternpowered.server.game.version.LanternMinecraftVersion)1 NetworkSession (org.lanternpowered.server.network.NetworkSession)1 WrappedRemoteConnection (org.lanternpowered.server.network.WrappedRemoteConnection)1 LanternFavicon (org.lanternpowered.server.network.status.LanternFavicon)1 LanternStatusClient (org.lanternpowered.server.network.status.LanternStatusClient)1 LanternStatusResponse (org.lanternpowered.server.network.status.LanternStatusResponse)1 MessageStatusOutResponse (org.lanternpowered.server.network.vanilla.message.type.status.MessageStatusOutResponse)1 FormattingCodeTextSerializer (org.lanternpowered.server.text.FormattingCodeTextSerializer)1 PlainTextSerializer (org.lanternpowered.server.text.PlainTextSerializer)1 MinecraftVersion (org.spongepowered.api.MinecraftVersion)1 Cause (org.spongepowered.api.event.cause.Cause)1 ClientPingServerEvent (org.spongepowered.api.event.server.ClientPingServerEvent)1 GameProfile (org.spongepowered.api.profile.GameProfile)1