package info.tregmine.listeners;

import info.tregmine.Tregmine;
import info.tregmine.api.TregminePlayer;
import info.tregmine.quadtree.Point;
import info.tregmine.zones.Lot;
import info.tregmine.zones.Zone;
import info.tregmine.zones.ZoneWorld;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerEggThrowEvent;

public class EggListener implements Listener
{
    private Tregmine plugin;

    public EggListener(Tregmine instance)
    {
        this.plugin = instance;
    }

    @EventHandler
    public void onEggThrown(PlayerEggThrowEvent event)
    {
        TregminePlayer player = plugin.getPlayer(event.getPlayer());
        if (player.getRank().canModifyZones()) {
            return;
        }

        ZoneWorld world = plugin.getWorld(player.getWorld());
        if (world == null) {
            return;
        }

        Location location = player.getLocation();
        Point pos = new Point(location.getBlockX(), location.getBlockZ());

        Zone zone = world.findZone(pos);
        if (zone == null) {
            return;
        }

        Zone.Permission perm = zone.getUser(player);
        Lot lot = world.findLot(pos);

        if(zone != null && lot == null){ // If Zone but not Lot
            if(perm == Zone.Permission.Allowed ||
               perm == Zone.Permission.Maker ||
               perm == Zone.Permission.Owner){ // Checks if player is Allowed/Maker/Owner
                return;
            }
            if(zone.isPvp()){ // If pvp is on then let it fly... It's an awesome weapon
                return;
            }
            if(zone.getDestroyDefault()){ // If people can destroy, Then we will let them throw eggs.
                return;
            }
            // If still at this point then they are not allowed to throw eggs so BURN THEM ALIVE and stop the egg from hatching, I could've burnt the chicken? but thats harsh its only a chick!
            player.setFireTicks(50);
            event.setHatching(false);
            player.sendMessage(ChatColor.RED + "["
                    + zone.getName() + "] "
                    + "You are not allowed to throw eggs in "
                    + zone.getName() + ".");
            return;
        }else if(zone != null && lot != null){ // If lot in a zone
            if (perm == Zone.Permission.Owner && zone.isCommunist()) { // Zone owners can modify lots in communist zones
                return;
            }
            if (lot.isOwner(player)) { // Lot owners can throw eggs if they so please
                return;
            }
            // If still at this point then they are not allowed to throw eggs so BURN THEM ALIVE and stop the egg from hatching, I could've burnt the chicken? but thats harsh its only a chick!
            player.setFireTicks(50);
            event.setHatching(false);
            player.sendMessage(ChatColor.RED + "["
                    + zone.getName() + "] "
                    + "You are not allowed to throw eggs in "
                    + zone.getName() + ".");
            return;
        }else{ // No Zone (obviously No Lot) so just end.
            return;
        }
    }
}

@@ -103,205 +103,8 @@
 er;%0A
-import info.tregmine.quadtree.Point;%0Aimport info.tregmine.zones.Lot;%0Aimport info.tregmine.zones.Zone;%0Aimport info.tregmine.zones.ZoneWorld;%0A%0Aimport org.bukkit.ChatColor;%0Aimport org.bukkit.Location;
 %0Aimp
@@ -549,2482 +549,115 @@
     
-if (player.getRank().canModifyZones()) %7B%0A            return;%0A        %7D%0A%0A        ZoneWorld world = plugin.getWorld(player.getWorld());%0A        if (world == null) %7B%0A            return;%0A        %7D%0A%0A        Location location = player.getLocation();%0A        Point pos = new Point(location.getBlockX(), location.getBlockZ());%0A%0A        Zone zone = world.findZone(pos);%0A        if (zone == null) %7B%0A            return;%0A        %7D%0A%0A        Zone.Permission perm = zone.getUser(player);%0A        Lot lot = world.findLot(pos);%0A%0A        if(zone != null && lot == null)%7B // If Zone but not Lot%0A            if(perm == Zone.Permission.Allowed %7C%7C%0A               perm == Zone.Permission.Maker %7C%7C%0A               perm == Zone.Permission.Owner)%7B // Checks if player is Allowed/Maker/Owner%0A                return;%0A            %7D%0A            if(zone.isPvp())%7B // If pvp is on then let it fly... It's an awesome weapon%0A                return;%0A            %7D%0A            if(zone.getDestroyDefault())%7B // If people can destroy, Then we will let them throw eggs.%0A                return;%0A            %7D%0A            // If still at this point then they are not allowed to throw eggs so BURN THEM ALIVE and stop the egg from hatching, I could've burnt the chicken? but thats harsh its only a chick!%0A            player.setFireTicks(50);%0A            event.setHatching(false);%0A            player.sendMessage(ChatColor.RED + %22%5B%22%0A                    + zone.getName() + %22%5D %22%0A                    + %22You are not allowed to throw eggs in %22%0A                    + zone.getName() + %22.%22);%0A            return;%0A        %7Delse if(zone != null && lot != null)%7B // If lot in a zone%0A            if (perm == Zone.Permission.Owner && zone.isCommunist()) %7B // Zone owners can modify lots in communist zones%0A                return;%0A            %7D%0A            if (lot.isOwner(player)) %7B // Lot owners can throw eggs if they so please%0A                return;%0A            %7D%0A            // If still at this point then they are not allowed to throw eggs so BURN THEM ALIVE and stop the egg from hatching, I could've burnt the chicken? but thats harsh its only a chick!%0A            player.setFireTicks(50);%0A            event.setHatching(false);%0A            player.sendMessage(ChatColor.RED + %22%5B%22%0A                    + zone.getName() + %22%5D %22%0A                    + %22You are not allowed to throw eggs in %22%0A                    + zone.getName() + %22.%22);%0A            return;%0A        %7Delse%7B // No Zone (obviously No Lot) so just end.%0A            return
+%0A        if (!player.hasBlockPermission(player.getLocation(), true)) %7B%0A            event.setHatching(false)
 ;%0A  

Update EggListener.java