package net.cpprograms.minecraft.TravelPortals;

/*
 * 2019 Max Lee aka Phoenix616 (mail@moep.tv)
 */

import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityAirChangeEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.player.PlayerMoveEvent;

/**
 * Handle events for all Player related events
 * @author cppchriscpp
 */
public class TravelPortalsDrownListener implements Listener {
	private final TravelPortals plugin;

	/**
	 * Constructor.
	 * @param instance The plugin to attach to.
	 */
	public TravelPortalsDrownListener(TravelPortals instance) {
		plugin = instance;
	}

	/**
	 * Runs when a player is inside a portal and looses air
	 * @param event The event related to this.
	 */
	@EventHandler(ignoreCancelled = true)
	public void onPlayerAirChange(EntityAirChangeEvent event)
	{
		if (event.getEntityType() != EntityType.PLAYER)
			return;

		WarpLocation portal = plugin.getPortalStorage().getPortal(event.getEntity().getLocation());
		if (portal == null)
			return;

		event.setCancelled(true);
	}
	/**
	 * Runs when a player is inside a portal and gets drowning damage to drown
	 * @param event The event related to this.
	 */
	@EventHandler(ignoreCancelled = true)
	public void onPlayerAirChange(EntityDamageEvent event)
	{
		if (event.getEntityType() != EntityType.PLAYER || event.getCause() == EntityDamageEvent.DamageCause.DROWNING)
			return;

		WarpLocation portal = plugin.getPortalStorage().getPortal(event.getEntity().getLocation());
		if (portal == null)
			return;

		event.setCancelled(true);
	}
}


@@ -118,81 +118,24 @@
 kit.
-Location;%0Aimport org.bukkit.block.Block;%0Aimport org.bukkit.block.BlockFac
+entity.EntityTyp
 e;%0Ai
@@ -154,34 +154,30 @@
 kkit.entity.
-EntityType
+Player
 ;%0Aimport org
@@ -344,114 +344,8 @@
 ent;
-%0Aimport org.bukkit.event.inventory.InventoryMoveItemEvent;%0Aimport org.bukkit.event.player.PlayerMoveEvent;
 %0A%0A/*
@@ -887,32 +887,34 @@
  event)%0A%09%7B%0A%09%09if 
+(!
 (event.getEntity
@@ -917,35 +917,127 @@
 tity
-Type() != EntityType.PLAYER
+() instanceof Player) %7C%7C ((Player) event.getEntity()).getMaximumAir() != ((Player) event.getEntity()).getRemainingAir()
 )%0A%09%09
@@ -1091,32 +1091,38 @@
 talStorage().get
+Nearby
 Portal(event.get
@@ -1135,32 +1135,35 @@
 ().getLocation()
+, 2
 );%0A%09%09if (portal 
@@ -1400,33 +1400,32 @@
 oid onPlayer
-AirCha
+Drowni
 ng
-e
 (EntityDamag
@@ -1614,24 +1614,30 @@
 torage().get
+Nearby
 Portal(event
@@ -1662,16 +1662,19 @@
 cation()
+, 2
 );%0A%09%09if 

Improve drowning check