package net.md_5.bungee.connection;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.RequiredArgsConstructor;
import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.ServerPing;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.netty.ChannelWrapper;
import net.md_5.bungee.netty.PacketHandler;
import net.md_5.bungee.protocol.packet.Kick;

@RequiredArgsConstructor
public class PingHandler extends PacketHandler
{

    private final ServerInfo target;
    private final Callback<ServerPing> callback;

    @Override
    public void connected(ChannelWrapper channel) throws Exception
    {
        // TODO: Update this to 1.7 style!
        channel.write( Unpooled.wrappedBuffer( new byte[]
        {
            (byte) 0xFE, (byte) 0x01
        } ) );
    }

    @Override
    public void exception(Throwable t) throws Exception
    {
        callback.done( null, t );
    }

    @Override
    public void handle(Kick kick) throws Exception
    {
        String[] split = kick.getMessage().split( "\00" );
        // ServerPing ping = new ServerPing( Byte.parseByte( split[1] ), split[2], split[3], Integer.parseInt( split[4] ), Integer.parseInt( split[5] ) );
        // callback.done( ping, null );
    }

    @Override
    public String toString()
    {
        return "[Ping Handler] -> " + target.getName();
    }
}

@@ -41,103 +41,73 @@
 ort 
-io.netty.buffer.ByteBuf;%0Aimport io.netty.buffer.Unpooled;%0Aimport lombok.RequiredArgsConstructor
+lombok.RequiredArgsConstructor;%0Aimport net.md_5.bungee.BungeeCord
 ;%0Aim
@@ -351,19 +351,175 @@
 col.
-packet.Kick
+Protocol;%0Aimport net.md_5.bungee.protocol.packet.Handshake;%0Aimport net.md_5.bungee.protocol.packet.StatusRequest;%0Aimport net.md_5.bungee.protocol.packet.StatusResponse
 ;%0A%0A@
@@ -677,16 +677,52 @@
 allback;
+%0A    private ChannelWrapper channel;
 %0A%0A    @O
@@ -814,158 +814,214 @@
     
-// TODO: Update this to 1.7 style!%0A        channel.write( Unpooled.wrappedBuffer( new byte%5B%5D%0A        %7B%0A            (byte) 0xFE, (byte) 0x01%0A        %7D 
+this.channel = channel;%0A%0A        channel.write( new Handshake( Protocol.PROTOCOL_VERSION, target.getAddress().getHostString(), target.getAddress().getPort(), 1 ) );%0A        channel.write( new StatusRequest(
 ) );
@@ -1186,17 +1186,37 @@
 dle(
-Kick kick
+StatusResponse statusResponse
 ) th
@@ -1248,251 +1248,143 @@
     
-String%5B%5D split = kick.getMessage().split( %22%5C00%22 );%0A        // ServerPing ping = new ServerPing( Byte.parseByte( split%5B1%5D ), split%5B2%5D, split%5B3%5D, Integer.parseInt( split%5B4%5D ), Integer.parseInt( split%5B5%5D ) );%0A        // callback.done( ping, null 
+callback.done( BungeeCord.getInstance().gson.fromJson( statusResponse.getResponse(), ServerPing.class ), null );%0A        channel.close(
 );%0A 

Implement 1.7 style pings