001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package net.tridentsdk.server.packets.status;
019
020import com.google.gson.JsonObject;
021import com.google.gson.JsonPrimitive;
022import io.netty.buffer.ByteBuf;
023import net.tridentsdk.PingInfo;
024import net.tridentsdk.Trident;
025import net.tridentsdk.server.netty.Codec;
026import net.tridentsdk.server.netty.packet.OutPacket;
027
028/**
029 * Status response to PacketStatusInRequest
030 *
031 * @author The TridentSDK Team
032 * @see PacketStatusInRequest
033 */
034public class PacketStatusOutResponse extends OutPacket {
035    /**
036     * The actual response, represented in JSON in the protocol
037     */
038    PingInfo info;
039
040    public PacketStatusOutResponse() {
041        this.info = Trident.server().info();
042    }
043
044    @Override
045    public int id() {
046        return 0x00;
047    }
048
049    public PingInfo info() {
050        return info;
051    }
052
053    @Override
054    public void encode(ByteBuf buf) {
055        // TODO event
056
057        JsonObject object = new JsonObject();
058        JsonObject players = new JsonObject();
059        JsonObject version = new JsonObject();
060        JsonObject motd = new JsonObject();
061
062        players.add("max", new JsonPrimitive(info.maxPlayers()));
063        players.add("online", new JsonPrimitive(info.playerCount()));
064
065        version.add("name", new JsonPrimitive(info.version()));
066        version.add("protocol", new JsonPrimitive(109));
067
068        motd.add("text", new JsonPrimitive(info.motd()));
069
070        object.add("players", players);
071        object.add("version", version);
072        object.add("description", motd);
073
074        Codec.writeString(buf, object.toString());
075    }
076}