/*
 * This file was automatically generated by sel-utils and
 * released under the MIT License.
 * 
 * License: https://github.com/sel-project/sel-utils/blob/master/LICENSE
 * Repository: https://github.com/sel-project/sel-utils
 * Generated from https://github.com/sel-project/sel-utils/blob/master/xml/protocol/bedrock160.xml
 */
package sul.protocol.bedrock160.play;

import java.util.Arrays;

import sul.utils.*;

public class PlayerList extends Packet {

	public static final int ID = (int)63;

	public static final boolean CLIENTBOUND = true;
	public static final boolean SERVERBOUND = false;

	@Override
	public int getId() {
		return ID;
	}

	public byte action;

	public PlayerList() {}

	public PlayerList(byte action) {
		this.action = action;
	}

	@Override
	public int length() {
		return 2;
	}

	@Override
	public byte[] encode() {
		return this.encodeImpl();
	}

	private byte[] encodeImpl() {
		this._buffer = new byte[this.length()];
		this.writeVaruint(ID);
		this.writeLittleEndianByte(action);
		return this.getBuffer();
	}

	@Override
	public void decode(byte[] buffer) {
		this._buffer = buffer;
		this.readVaruint();
		action=readLittleEndianByte();
	}

	private byte[] remainingBuffer() {
		return java.util.Arrays.copyOfRange(this._buffer, this._index, this._buffer.length);
	}

	public static PlayerList fromBuffer(byte[] buffer) {
		PlayerList ret = new PlayerList();
		ret.decode(buffer);
		return ret;
	}

	@Override
	public String toString() {
		return "PlayerList(action: " + this.action + ")";
	}

	public class Add extends Packet {

		public static final byte ACTION = (byte)0;

		@Override
		public int getId() {
			return ID;
		}

		public sul.protocol.bedrock160.types.PlayerList[] players = new sul.protocol.bedrock160.types.PlayerList[0];

		public Add() {}

		public Add(sul.protocol.bedrock160.types.PlayerList[] players) {
			this.players = players;
		}

		@Override
		public int length() {
			int length=Buffer.varuintLength(players.length); for(sul.protocol.bedrock160.types.PlayerList cxevc:players){ length+=cxevc.length(); } return length;
		}

		@Override
		public byte[] encode() {
			byte[] _encode = encodeImpl();
			this._buffer = new byte[_encode.length + this.length()];
			this.writeBytes(_encode);
			this.writeVaruint((int)players.length); for(sul.protocol.bedrock160.types.PlayerList cxevc:players){ this.writeBytes(cxevc.encode()); }
			return this.getBuffer();
		}

		@Override
		public void decode(byte[] buffer) {
			this._buffer = buffer;
			int bbylcm=this.readVaruint(); players=new sul.protocol.bedrock160.types.PlayerList[bbylcm]; for(int cxevc=0;cxevc<players.length;cxevc++){ players[cxevc]=new sul.protocol.bedrock160.types.PlayerList(); players[cxevc]._index=this._index; players[cxevc].decode(this._buffer); this._index=players[cxevc]._index; }
		}

		public void decode() {
			this.decode(remainingBuffer());
		}

		@Override
		public String toString() {
			return "PlayerList.Add(players: " + Arrays.deepToString(this.players) + ")";
		}

	}

	public class Remove extends Packet {

		public static final byte ACTION = (byte)1;

		@Override
		public int getId() {
			return ID;
		}

		public sul.protocol.bedrock160.types.McpeUuid[] players = new sul.protocol.bedrock160.types.McpeUuid[0];

		public Remove() {}

		public Remove(sul.protocol.bedrock160.types.McpeUuid[] players) {
			this.players = players;
		}

		@Override
		public int length() {
			int length=Buffer.varuintLength(players.length); for(sul.protocol.bedrock160.types.McpeUuid cxevc:players){ length+=cxevc.length(); } return length;
		}

		@Override
		public byte[] encode() {
			byte[] _encode = encodeImpl();
			this._buffer = new byte[_encode.length + this.length()];
			this.writeBytes(_encode);
			this.writeVaruint((int)players.length); for(sul.protocol.bedrock160.types.McpeUuid cxevc:players){ this.writeBytes(cxevc.encode()); }
			return this.getBuffer();
		}

		@Override
		public void decode(byte[] buffer) {
			this._buffer = buffer;
			int bbylcm=this.readVaruint(); players=new sul.protocol.bedrock160.types.McpeUuid[bbylcm]; for(int cxevc=0;cxevc<players.length;cxevc++){ players[cxevc]=new sul.protocol.bedrock160.types.McpeUuid(); players[cxevc]._index=this._index; players[cxevc].decode(this._buffer); this._index=players[cxevc]._index; }
		}

		public void decode() {
			this.decode(remainingBuffer());
		}

		@Override
		public String toString() {
			return "PlayerList.Remove(players: " + Arrays.deepToString(this.players) + ")";
		}

	}

}