package com.ezardlabs.dethsquare.multiplayer;

import com.ezardlabs.dethsquare.Component;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Random;

public abstract class NetworkBehaviour extends Component {
	static HashMap<Integer, NetworkBehaviour> myNetworkBehaviours = new HashMap<>();
	static HashMap<Integer, NetworkBehaviour> otherNetworkBehaviours = new HashMap<>();
	static int totalSize = 0;
	private int networkId;
	private int playerId = -1;
	protected final ByteBuffer data = ByteBuffer.allocate(getSize());

	public NetworkBehaviour() {
		networkId = Network.getNewNetworkId();
		playerId = Network.getPlayerId();
		if (playerId == -1) throw new Error("Player ID has not been set yet");
	}

	@Override
	public void start() {
		if (playerId == Network.getPlayerId()) {
			myNetworkBehaviours.put(networkId, this);
			totalSize += getSize();
		} else {
			otherNetworkBehaviours.put(networkId, this);
		}
	}

	@Override
	protected final void destroy() {
		if (playerId == Network.getPlayerId()) {
			myNetworkBehaviours.remove(networkId);
			totalSize -= getSize();
		} else {
			otherNetworkBehaviours.remove(networkId);
		}
	}

	void setNetworkId(int networkId) {
		this.networkId = networkId;
	}

	public int getNetworkId() {
		return networkId;
	}

	void setPlayerId(int playerId) {
		this.playerId = playerId;
	}

	public int getPlayerId() {
		return playerId;
	}

	private String generateRandomId() {
		char[] chars = {'0',
				'1',
				'2',
				'3',
				'4',
				'5',
				'6',
				'7',
				'8',
				'9',
				'a',
				'b',
				'c',
				'd',
				'e',
				'f',
				'g',
				'h',
				'i',
				'j',
				'k',
				'l',
				'm',
				'n',
				'o',
				'p',
				'q',
				'r',
				's',
				't',
				'u',
				'v',
				'w',
				'x',
				'y',
				'z',
				'A',
				'B',
				'C',
				'D',
				'E',
				'F',
				'G',
				'H',
				'I',
				'J',
				'K',
				'L',
				'M',
				'N',
				'O',
				'P',
				'Q',
				'R',
				'S',
				'T',
				'U',
				'V',
				'W',
				'X',
				'Y',
				'Z'};
		Random r = new Random();
		StringBuilder sb = new StringBuilder(10);
		for (int i = 0; i < sb.capacity(); i++) {
			sb.append(chars[r.nextInt(chars.length)]);
		}
		return sb.toString();
	}

	protected abstract ByteBuffer onSend();

	protected abstract void onReceive(ByteBuffer data, int index);

	public abstract int getSize();
}

@@ -141,33 +141,8 @@
 Map;
-%0Aimport java.util.Random;
 %0A%0Apu
@@ -1376,811 +1376,8 @@
 %09%7D%0A%0A
-%09private String generateRandomId() %7B%0A%09%09char%5B%5D chars = %7B'0',%0A%09%09%09%09'1',%0A%09%09%09%09'2',%0A%09%09%09%09'3',%0A%09%09%09%09'4',%0A%09%09%09%09'5',%0A%09%09%09%09'6',%0A%09%09%09%09'7',%0A%09%09%09%09'8',%0A%09%09%09%09'9',%0A%09%09%09%09'a',%0A%09%09%09%09'b',%0A%09%09%09%09'c',%0A%09%09%09%09'd',%0A%09%09%09%09'e',%0A%09%09%09%09'f',%0A%09%09%09%09'g',%0A%09%09%09%09'h',%0A%09%09%09%09'i',%0A%09%09%09%09'j',%0A%09%09%09%09'k',%0A%09%09%09%09'l',%0A%09%09%09%09'm',%0A%09%09%09%09'n',%0A%09%09%09%09'o',%0A%09%09%09%09'p',%0A%09%09%09%09'q',%0A%09%09%09%09'r',%0A%09%09%09%09's',%0A%09%09%09%09't',%0A%09%09%09%09'u',%0A%09%09%09%09'v',%0A%09%09%09%09'w',%0A%09%09%09%09'x',%0A%09%09%09%09'y',%0A%09%09%09%09'z',%0A%09%09%09%09'A',%0A%09%09%09%09'B',%0A%09%09%09%09'C',%0A%09%09%09%09'D',%0A%09%09%09%09'E',%0A%09%09%09%09'F',%0A%09%09%09%09'G',%0A%09%09%09%09'H',%0A%09%09%09%09'I',%0A%09%09%09%09'J',%0A%09%09%09%09'K',%0A%09%09%09%09'L',%0A%09%09%09%09'M',%0A%09%09%09%09'N',%0A%09%09%09%09'O',%0A%09%09%09%09'P',%0A%09%09%09%09'Q',%0A%09%09%09%09'R',%0A%09%09%09%09'S',%0A%09%09%09%09'T',%0A%09%09%09%09'U',%0A%09%09%09%09'V',%0A%09%09%09%09'W',%0A%09%09%09%09'X',%0A%09%09%09%09'Y',%0A%09%09%09%09'Z'%7D;%0A%09%09Random r = new Random();%0A%09%09StringBuilder sb = new StringBuilder(10);%0A%09%09for (int i = 0; i %3C sb.capacity(); i++) %7B%0A%09%09%09sb.append(chars%5Br.nextInt(chars.length)%5D);%0A%09%09%7D%0A%09%09return sb.toString();%0A%09%7D%0A%0A
 %09pro

Remove unused method