package net.simpvp.Jail;

import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerRespawnEvent;

public class JailedEventListener implements Listener {

	/**
	 * Ensures dead jailed players keep their inventory.
	 */
	@EventHandler(priority=EventPriority.NORMAL, ignoreCancelled=true)
	public void onPlayerDeath(PlayerDeathEvent event) {
		if (!(Jail.jailed_players.contains(event.getEntity().getUniqueId())))
			return;

		event.setKeepInventory(true);
		event.setKeepLevel(true);
	}

	/**
	 * Ensure that jailed players respawn in the jail
	 */
	@EventHandler(priority=EventPriority.NORMAL, ignoreCancelled=true)
	public void onPlayerRespawn(PlayerRespawnEvent event) {
		if (!(Jail.jailed_players.contains(event.getPlayer().getUniqueId())))
			return;

		event.setRespawnLocation(Config.get_spawn());
	}

	/**
	 * Teleports all jailed players outside spawn back to the inside,
	 * preventing them from leaving
	 */
	@EventHandler(priority=EventPriority.NORMAL, ignoreCancelled=true)
	public void onPlayerMove(PlayerMoveEvent event) {
		if (!(Jail.jailed_players.contains(event.getPlayer().getUniqueId())))
			return;

		if (Config.is_inside_jail(event.getTo()))
			return;

		event.getPlayer().teleport(Config.get_spawn());
		event.getPlayer().sendMessage(ChatColor.RED + "You are jailed.");
	}

	/**
	 * Prevent jailed players from dropping any items
	 */
	@EventHandler(priority=EventPriority.NORMAL, ignoreCancelled=true)
	public void onPlayerDropItem(PlayerDropItemEvent event) {
		if (!(Jail.jailed_players.contains(event.getPlayer().getUniqueId())))
			return;

		event.setCancelled(true);
		event.getPlayer().sendMessage(ChatColor.RED + "Jailed players are not allowed to drop items.");
	}

	@EventHandler(priority=EventPriority.NORMAL,ignoreCancelled=true)
	public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
		if (!(Jail.jailed_players.contains(event.getPlayer().getUniqueId())))
			return;

		String command = event.getMessage().split(" ")[0].trim().substring(1).toLowerCase();
		if (!Config.is_command_blocked(command))
			return;

		event.setCancelled(true);
		event.getPlayer().sendMessage(ChatColor.RED + "Jailed players are not allowed to use that command.");
	}

}


@@ -419,16 +419,231 @@
 wnEvent;
+%0Aimport org.bukkit.event.entity.EntityDamageByEntityEvent;%0Aimport org.bukkit.event.inventory.InventoryOpenEvent;%0Aimport org.bukkit.event.inventory.InventoryType;%0Aimport org.bukkit.event.player.PlayerPickupItemEvent;
 %0A%0Apublic
@@ -2746,11 +2746,815 @@
 %22);%0A%09%7D%0A%0A
+%09@EventHandler(priority=EventPriority.NORMAL,ignoreCancelled=true)%0A%09public void onEntityDamageByEntity(EntityDamageByEntityEvent event) %7B%0A%09%09if (!(Jail.jailed_players.contains(event.getDamager().getUniqueId())))%0A%09%09%09return;%0A%0A%09%09event.setCancelled(true);%0A%09%7D%0A%0A%09@EventHandler(priority=EventPriority.NORMAL,ignoreCancelled=true)%0A%09public void onInventoryOpen(InventoryOpenEvent event) %7B%0A%09%09if (!(Jail.jailed_players.contains(event.getPlayer().getUniqueId()))%0A%09%09%09%09%7C%7C event.getInventory().getType() == InventoryType.PLAYER)%0A%09%09%09return;%0A%0A%09%09event.setCancelled(true);%0A%09%7D%0A%0A%09@EventHandler(priority=EventPriority.NORMAL,ignoreCancelled=true)%0A%09public void onPlayerPickupItem(PlayerPickupItemEvent event) %7B%0A%09%09if (!(Jail.jailed_players.contains(event.getPlayer().getUniqueId())))%0A%09%09%09return;%0A%0A%09%09event.setCancelled(true);%0A%09%7D%0A%0A%0A
 %7D%0A%0A

Add additional restrictions on jailed players