package valandur.webapi.servlets;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.spongepowered.api.Platform;
import org.spongepowered.api.Server;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.plugin.PluginContainer;
import valandur.webapi.Permission;

import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

public class InfoServlet extends WebAPIServlet {
    @Override
    @Permission(perm = "info")
    protected void handleGet(ServletData data) {
        data.setStatus(HttpServletResponse.SC_OK);

        Server server = Sponge.getServer();
        Platform platform = Sponge.getPlatform();

        JsonObject json = data.getJson();
        json.addProperty("motd", server.getMotd().toPlain());
        json.addProperty("players", server.getOnlinePlayers().size());
        json.addProperty("maxPlayers", server.getMaxPlayers());
        json.addProperty("uptimeTicks", server.getRunningTimeTicks());
        json.addProperty("hasWhitelist", server.hasWhitelist());
        json.addProperty("minecraftVersion", platform.getMinecraftVersion().getName());

        PluginContainer api = platform.getApi();
        JsonObject obj = new JsonObject();
        obj.addProperty("id", api.getId());
        obj.addProperty("name", api.getName());
        Optional<String> version = api.getVersion();
        obj.addProperty("version", version.isPresent() ? version.get() : null);
        Optional<String> descr = api.getDescription();
        obj.addProperty("description", descr.isPresent() ? descr.get() : null);
        Optional<String> url = api.getVersion();
        obj.addProperty("url", url.isPresent() ? url.get() : null);
        JsonArray arr = new JsonArray();
        for (String author : api.getAuthors()) {
            arr.add(new JsonPrimitive(author));
        }
        obj.add("authors", arr);

        json.add("api", obj);
    }
}

@@ -1178,48 +1178,365 @@
     
-PluginContainer api = platform.getApi();
+json.add(%22game%22, containerToJson(platform.getContainer(Platform.Component.GAME)));%0A        json.add(%22api%22, containerToJson(platform.getContainer(Platform.Component.API)));%0A        json.add(%22implementation%22, containerToJson(platform.getContainer(Platform.Component.IMPLEMENTATION)));%0A    %7D%0A%0A    private JsonObject containerToJson(PluginContainer container) %7B
 %0A   
@@ -1605,19 +1605,25 @@
 y(%22id%22, 
-api
+container
 .getId()
@@ -1657,19 +1657,25 @@
 %22name%22, 
-api
+container
 .getName
@@ -1714,19 +1714,25 @@
 rsion = 
-api
+container
 .getVers
@@ -1851,19 +1851,25 @@
 descr = 
-api
+container
 .getDesc
@@ -1990,19 +1990,25 @@
 %3E url = 
-api
+container
 .getVers
@@ -2152,19 +2152,25 @@
 uthor : 
-api
+container
 .getAuth
@@ -2269,25 +2269,24 @@
 , arr);%0A
-%0A
         
 json.add
@@ -2281,28 +2281,18 @@
     
-json.add(%22api%22,
+return
  obj
-)
 ;%0A  

Add information about the game, api and implementation (of sponge)