package protocolsupport.api.title;

import org.apache.commons.lang3.Validate;
import org.bukkit.entity.Player;

import protocolsupport.api.Connection;
import protocolsupport.api.ProtocolSupportAPI;
import protocolsupport.api.chat.ChatAPI;
import protocolsupport.api.chat.components.BaseComponent;
import protocolsupport.zplatform.ServerPlatform;

public class TitleAPI {

	/**
	 * Sends title, subtitle, and it's params <br>
	 * Title and subtitle can't be both null
	 * @param player Player to which title is sent
	 * @param title title chat component or null
	 * @param subtitle subtitle chat component or null
	 * @param fadeIn ticks to spend fading in
	 * @param stay ticks to display
	 * @param fadeOut ticks to spend fading out
	 */
	public static void sendSimpleTitle(Player player, BaseComponent title, BaseComponent subtitle, int fadeIn, int stay, int fadeOut) {
		sendSimpleTitle(player, ChatAPI.toJSON(title), ChatAPI.toJSON(subtitle), fadeIn, stay, fadeOut);
	}

	/**
	 * Sends title, subtitle, and it's params <br>
	 * Title and subtitle can't be both null
	 * @param player Player to which title is sent
	 * @param titleJson title chat json or null
	 * @param subtitleJson subtitle chat json or null
	 * @param fadeIn ticks to spend fading in
	 * @param stay ticks to display
	 * @param fadeOut ticks to spend fading out
	 */
	public static void sendSimpleTitle(Player player, String titleJson, String subtitleJson, int fadeIn, int stay, int fadeOut) {
		Validate.notNull(player, "Player can't be null");
		if ((titleJson == null) && (subtitleJson == null)) {
			throw new IllegalArgumentException("Title and subtitle can't be both null");
		}
		Connection connection = ProtocolSupportAPI.getConnection(player);
		if (titleJson != null) {
			connection.sendPacket(ServerPlatform.get().getPacketFactory().createTitleMainPacket(titleJson));
		}
		if (subtitleJson != null) {
			connection.sendPacket(ServerPlatform.get().getPacketFactory().createTitleSubPacket(subtitleJson));
		}
		connection.sendPacket(ServerPlatform.get().getPacketFactory().createTitleParamsPacket(fadeIn, stay, fadeOut));
	}

	/**
	 * Removes and resets title, subtitle and it's params
	 * @param player Player to which reset should be sent
	 */
	public static void removeSimpleTitle(Player player) {
		Connection connection = ProtocolSupportAPI.getConnection(player);
		connection.sendPacket(ServerPlatform.get().getPacketFactory().createTitleClearPacket());
		connection.sendPacket(ServerPlatform.get().getPacketFactory().createTitleResetPacket());
	}

}

@@ -1723,36 +1723,8 @@
 r);%0A
-%09%09if (titleJson != null) %7B%0A%09
 %09%09co
@@ -1798,35 +1798,45 @@
 itle
-Main
+Params
 Packet(
-titleJson));%0A%09%09%7D
+fadeIn, stay, fadeOut));
 %0A%09%09i
@@ -1968,16 +1968,66 @@
 ));%0A%09%09%7D%0A
+%09%09if (titleJson == null) %7B%0A%09%09%09titleJson = %22%22;%0A%09%09%7D%0A
 %09%09connec
@@ -2097,42 +2097,28 @@
 itle
-Params
+Main
 Packet(
-fadeIn, stay, fadeOut
+titleJson
 ));%0A

Fix TitleAPI