package me.jesensky.dan.playertracker.net.packets;

import me.jesensky.dan.playertracker.exceptions.InvalidPacketException;
import me.jesensky.dan.playertracker.net.Connection;
import me.jesensky.dan.playertracker.net.NetUtils;

import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Packet {
    private PacketType type;
    private byte[] data;

    public Packet(PacketType type, byte... data) throws InvalidPacketException {
        super();
        this.type = type;
        this.data = data;
    }

    public PacketType getType() {
        return this.type;
    }

    public void sendData(Socket sock) throws IOException {
        sock.getOutputStream().write(this.getAmmendedData());
    }

    public void sendData(Connection conn) throws IOException {
        conn.getOutputStream().write(this.getAmmendedData());
    }

    public byte[] getAmmendedData() {
        byte[] sigma = new byte[this.data.length + this.type.getHeader().length];
        System.arraycopy(this.type.getHeader(), 0, sigma, 0, this.type.getHeader().length);
        System.arraycopy(this.data, 0, sigma, this.type.getHeader().length, this.data.length);
        return sigma;
    }

    @Override
    public String toString() {
        String s = "Packet@" + super.hashCode() + "{";
        for (byte b : this.getAmmendedData())
            s += b + ", ";
        return s.substring(0, s.length() - 2) + "}";
    }

    public byte[] getDataSection(int section){
        List<Byte> r = new ArrayList<Byte>();
        int iteration = 0, index = 0;
        boolean t = false;

        while(!t){
            if(this.data[index] == 0)
                iteration++;
            if(iteration == section){
                if(section != 0)
                    index++;
                while(!t && index < this.data.length){
                    if(this.data[index] == (byte)0x0){
                        t = true;
                    }else{
                        r.add(this.data[index++]);
                    }
                    if(this.data.length == index)
                        t = true;
                }
            }
            index++;
        }
        return NetUtils.byteListToArray(r);
    }
}
@@ -356,16 +356,777 @@
 .List;%0A%0A
+/**%0A * Represents data, cached in memory, to be sent%0A * somewhere over the Internet. This class is to be the super or base%0A * class for all packets.%3Cbr /%3E%0A * %3Cbr /%3E%0A * Implementations will find it useful to call%0A * %7B@link #getDataSection(int)%7D, as it will return%0A * the data within a specified %3Ci%3Esection%3C/i%3E of the packet.%0A * A %3Ci%3Esection%3C/i%3E is defined as a series of %7B@code byte%7Ds%0A * between two %7B@code 0x0%7D bytes, or the beginning, and/or%0A * end, if either endpoint does not exist.%3Cbr /%3E%0A * %3Cbr /%3E%0A * Data may be flushed from this buffer to a%0A * %7B@link java.net.Socket%7D or%0A * %7B@link me.jesensky.dan.playertracker.net.Connection%7D%0A * by invoking%0A * %7B@link #sendData(java.net.Socket)%7D or%0A * %7B@link #sendData(me.jesensky.dan.playertracker.net.Connection)%7D.%0A */%0A
 public c
@@ -2239,32 +2239,766 @@
 ) + %22%7D%22;%0A    %7D%0A%0A
+    /**%0A     * Gets a section of the data cached within%0A     * this %7B@code Packet%7D, based on the specified%0A     * %7B@code section%7D. A %7B@code section%7D is defined%0A     * explicitly as the data between two separator%0A     * bytes (%7B@code 0x0%7D), or the beginning and/or the%0A     * end of the data stored if there is no such endpoint.%3Cbr /%3E%0A     * %3Cbr /%3E%0A     * Sections begin counting at 0, just as arrays do.%0A     *%0A     * @param section The section of data to get, bound by%0A     *                %7B@code 0x0%7D, the beginning, end, or any%0A     *                combination thereof, depending on what data%0A     *                is stored.%0A     *%0A     * @return The bytes between the endpoints of the specified%0A     * %7B@code section%7D.%0A     */%0A
     public byte%5B
@@ -3136,12 +3136,11 @@
 t = 
-fals
+tru
 e;%0A%0A
@@ -3153,17 +3153,16 @@
   while(
-!
 t)%7B%0A    
@@ -3346,17 +3346,16 @@
   while(
-!
 t && ind
@@ -3453,35 +3453,36 @@
             t = 
-tru
+fals
 e;%0A             
@@ -3646,19 +3646,20 @@
     t = 
-tru
+fals
 e;%0A     

Add some documentation for Packet