package me.lilmac.cNoHacks.ForceField;

import me.lilmac.cNoHacks.Main;
import me.lilmac.cNoHacks.Config.ConfigManager;
import me.lilmac.cNoHacks.Utils.Utils;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

public class ForceField implements CommandExecutor, Listener {

	public Main plugin;

	public ForceField(Main instance) {
		plugin = instance;
	}

	@EventHandler
	public void onEntityDamage(EntityDamageEvent event) {
		if(event.getEntity() instanceof Villager) {
 			event.setCancelled(true);
		}
	}

	@EventHandler
	public void onVillagerDamage(EntityDamageByEntityEvent event) {
		if(event.getDamager() instanceof Player && event.getEntity() instanceof Villager) {
			Villager villager = (Villager) event.getEntity();
			Player damager = (Player) event.getDamager();
			if(villager.getCustomName().equals(damager.getName())) {
				for(Player online : Bukkit.getOnlinePlayers()) {
					if(online.hasPermission("cnohacks.view") || online.isOp()) {
						if (ConfigManager.contains("alerts.yml", online.getName())) {
							if (ConfigManager.get("alerts.yml").getBoolean(online.getName() + ".Alerts")) {
								online.sendMessage(Utils.getPrefix() + damager.getDisplayName() + " 7might be using cForcefield");
							}
						}
					}
				}
			}
		}
	}

	@EventHandler
	public void onVillagerInteract(PlayerInteractEntityEvent event) { 
		if(event.getRightClicked() instanceof Villager) {
			Villager villager = (Villager) event.getRightClicked();
			if(villager.getCustomName() != null) {
				event.setCancelled(true);
			}
		}
	}	

	@EventHandler
	public void onJoin(PlayerJoinEvent e) {
		if (!(ConfigManager.contains("alerts.yml", e.getPlayer().getName()))) {
			ConfigManager.set("alerts.yml", e.getPlayer().getName() + ".Alerts", false);
			ConfigManager.save(plugin, "alerts.yml");
		}
	}
	
	public boolean onCommand(CommandSender sender, Command cmd, String tag, String[] args) {
		if (tag.equalsIgnoreCase("test")) {
				for (Player online : Bukkit.getOnlinePlayers()) {
					final Villager villager = (Villager) online.getLocation().getWorld().spawnEntity(online.getLocation().subtract(3,0,3), EntityType.VILLAGER);
					villager.setCustomName(online.getName());
					villager.setCustomNameVisible(true);
					villager.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1));
					Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
						public void run() {
							villager.remove();
						}
					}, 10);
				}
			}
		return false;
	}
}
@@ -1027,24 +1027,67 @@
 eof Villager
+ && event.getEntity().hasMetaData(%22cancel%22)
 ) %7B%0A %09%09%09even
@@ -1794,16 +1794,17 @@
 e() + %22 
+%C2%A7
 7might b
@@ -1811,16 +1811,17 @@
 e using 
+%C2%A7
 cForcefi
@@ -2929,24 +2929,130 @@
 VALUE, 1));%0A
+%09%09%09%09%09villager.setMetadata(%22cancel%22, (MetadataValue)new FixedMetadataValue((Plugin)plugin, (Object)true));%0A
 %09%09%09%09%09Bukkit.
@@ -3126,24 +3126,65 @@
 oid run() %7B%0A
+%09%09%09%09%09%09%09vilager.removeMetaData(%22cancel%22);%0A
 %09%09%09%09%09%09%09villa
@@ -3249,8 +3249,9 @@
 se;%0A%09%7D%0A%7D
+%0A

Update ForceField.java