package protocolsupport.listeners;

import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerToggleSneakEvent;
import org.bukkit.event.vehicle.VehicleEnterEvent;

import protocolsupport.ProtocolSupport;
import protocolsupport.api.Connection;
import protocolsupport.api.ProtocolSupportAPI;
import protocolsupport.api.ProtocolVersion;
import protocolsupport.api.chat.components.BaseComponent;
import protocolsupport.api.tab.TabAPI;
import protocolsupport.protocol.utils.types.Position;
import protocolsupport.zplatform.ServerPlatform;

public class PlayerListener implements Listener {

	private final ProtocolSupport plugin;
	public PlayerListener(ProtocolSupport plugin) {
		this.plugin = plugin;
	}

	@EventHandler
	public void onShift(PlayerToggleSneakEvent event) {
		Player player = event.getPlayer();
		if (player.isInsideVehicle() && ProtocolSupportAPI.getProtocolVersion(player).isBeforeOrEq(ProtocolVersion.MINECRAFT_1_5_2)) {
			player.leaveVehicle();
		}
	}

	@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
	public void onVehicleInteract(PlayerInteractEntityEvent event) {
		Player player = event.getPlayer();
		if (player.isInsideVehicle() && ProtocolSupportAPI.getProtocolVersion(player).isBeforeOrEq(ProtocolVersion.MINECRAFT_1_5_2)) {
			if (player.getVehicle().equals(event.getRightClicked())) {
				player.leaveVehicle();
			}
		}
	}

	@EventHandler(ignoreCancelled = true)
	public void onVehicleEnter(VehicleEnterEvent event) {
		if (!event.getVehicle().getPassengers().isEmpty()) {
			event.setCancelled(true);
		}
	}

	@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
	public void onBlockPlace(BlockPlaceEvent event) {
		Connection connection = ProtocolSupportAPI.getConnection(event.getPlayer());
		if (connection.getVersion().isBefore(ProtocolVersion.MINECRAFT_1_9)) {
			Block block = event.getBlock();
			connection.sendPacket(ServerPlatform.get().getPacketFactory().createBlockBreakSoundPacket(new Position(block.getX(), block.getY(), block.getZ()), block.getType()));
		}
	}

	@EventHandler
	public void onJoin(final PlayerJoinEvent event) {
		Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
			@Override
			public void run() {
				BaseComponent header = TabAPI.getDefaultHeader();
				BaseComponent footer = TabAPI.getDefaultFooter();
				if ((header != null) || (footer != null)) {
					TabAPI.sendHeaderFooter(event.getPlayer(), header, footer);
				}
			}
		}, 1);
	}

}

@@ -1139,78 +1139,124 @@
 ;%0A%09%09
-if (player.isInsideVehicle() && ProtocolSupportAPI.getProtocol
+Connection connection = ProtocolSupportAPI.getConnection(player);%0A%09%09if (connection != null && connection.get
 Version(
 play
@@ -1243,38 +1243,32 @@
 tion.getVersion(
-player
 ).isBeforeOrEq(P
@@ -1518,78 +1518,124 @@
 ;%0A%09%09
-if (player.isInsideVehicle() && ProtocolSupportAPI.getProtocol
+Connection connection = ProtocolSupportAPI.getConnection(player);%0A%09%09if (connection != null && connection.get
 Version(
 play
@@ -1630,22 +1630,16 @@
 Version(
-player
 ).isBefo
@@ -1787,194 +1787,8 @@
 %09%7D%0A%0A
-%09@EventHandler(ignoreCancelled = true)%0A%09public void onVehicleEnter(VehicleEnterEvent event) %7B%0A%09%09if (!event.getVehicle().getPassengers().isEmpty()) %7B%0A%09%09%09event.setCancelled(true);%0A%09%09%7D%0A%09%7D%0A%0A
 %09@Ev
@@ -1994,32 +1994,54 @@
 %09%09if (connection
+ != null && connection
 .getVersion().is
@@ -2284,32 +2284,218 @@
 pe()));%0A%09%09%7D%0A%09%7D%0A%0A
+%09@EventHandler(ignoreCancelled = true)%0A%09public void onVehicleEnter(VehicleEnterEvent event) %7B%0A%09%09if (!event.getVehicle().getPassengers().isEmpty()) %7B%0A%09%09%09event.setCancelled(true);%0A%09%09%7D%0A%09%7D%0A%0A
 %09@EventHandler%0A%09

Check if connection is not null in case of fake players (players with severed connection link are not allowed to join server anymore so we don't care about them).