package org.kne.cloud.network.minecraft;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.net.ssl.HandshakeCompletedEvent;

import org.kne.io.VarDataOutputStream;

public class MinecraftOutputStream extends VarDataOutputStream {
		ByteArrayOutputStream bao=new ByteArrayOutputStream();
		private MinecraftProtocolContext context;
		
	public MinecraftOutputStream(OutputStream out,MinecraftProtocolContext context) {
		super(out);
			this.context=context;
	}
	public MinecraftProtocolContext getContext() {
		return context;
	}
	public void writePacket(MinecraftPacket mp) throws IOException {
		if (mp instanceof MinecraftUnsolvedPacket) {
			mp.writeToStream(this);
		}else {
		VarDataOutputStream vdo=new VarDataOutputStream(bao);
		vdo.writeVarInt(mp.getType());
		mp.writeToStream(vdo);
		byte[]baob=bao.toByteArray();
		bao.reset();
		writeVarBytes(baob);
		if(mp instanceof MinecraftHandShakePacket) {
			MinecraftHandShakePacket hsmp=(MinecraftHandShakePacket) mp;
			context.setState(hsmp.getNextstate());
		}
		}
	}
}
