/*
 * Copyright 2015 RITcraft
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.justinwflory.ritcommandcenter;

import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Ocelot;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.BlockIterator;

public class RitCommandCenter extends JavaPlugin implements Listener {

    private static final int TABBY_SPAWN_RANGE = 3;

    public void onEnable() {
        getCommand("rit").setExecutor(new RitCommand());
        Bukkit.getServer().getPluginManager().registerEvents(this, this);
    }

    @EventHandler
    public void onRitchieCat(PlayerInteractEvent event) {
        if (event.getAction() != Action.RIGHT_CLICK_BLOCK
                || event.getAction() != Action.RIGHT_CLICK_AIR
                || event.getItem() == null) {
            return;
        }
        ItemMeta meta = event.getItem().getItemMeta();
        if (!meta.hasDisplayName()) {
            return;
        }
        String name = meta.getDisplayName();
        String stripped = ChatColor.stripColor(name);
        if (name.length() == stripped.length() || !stripped.equals("Ritchie")) {
            return; //no custom named eggs making ritchies
        }
        //at this point we've validated their egg
        event.setUseItemInHand(Event.Result.DENY);
        if (event.getItem().getAmount() > 1) {
            ItemStack set = event.getPlayer().getItemInHand();
            set.setAmount(set.getAmount() - 1);
            event.getPlayer().setItemInHand(set);
        } else {
            event.getPlayer().setItemInHand(null);
        }
        Player p = event.getPlayer();
        Location spawn;
        if (p.getLocation().getPitch() > 0) {
            spawn = p.getEyeLocation().add(p.getLocation().getDirection().normalize().multiply(TABBY_SPAWN_RANGE));
        } else {
            BlockIterator itr = new BlockIterator(p.getWorld(), p.getLocation().toVector(), p.getLocation().getDirection(), 0, TABBY_SPAWN_RANGE);
            if (itr.hasNext()) {
                Block b = itr.next();
                Block up = b.getRelative(BlockFace.UP);
                Material m = up.getType();
                if (m != Material.AIR && (m.isBlock() && (m.isSolid() || !m.isTransparent()))) {
                    spawn = up.getLocation();
                } else {
                    spawn = p.getLocation();
                }
            } else {
                spawn = p.getLocation();
            }
        }
        Ocelot o = spawn.getWorld().spawn(spawn, Ocelot.class);
        o.setCatType(Ocelot.Type.RED_CAT);
        o.setBaby();
        o.setAge(-Integer.MAX_VALUE);
        o.setAgeLock(true);
        o.setCustomName(ChatColor.GOLD.toString() + ChatColor.BOLD + "Ritchie");
        o.setCustomNameVisible(true);
        o.setOwner(p);
        o.setSitting(true);
        o.setTamed(true);
        o.setTarget(p);
    }

    @EventHandler
    public void hitRitchie(EntityDamageByEntityEvent event) {
        if (event.getEntityType() == EntityType.OCELOT
                && event.getEntity().getCustomName() != null
                && ChatColor.stripColor(event.getEntity().getCustomName()).equals("Ritchie")) {
            event.setCancelled(true);
        }
    }

}

// if (tired) justin.goToSleep();
@@ -816,16 +816,49 @@
 ckFace;%0A
+import org.bukkit.entity.Entity;%0A
 import o
@@ -1147,24 +1147,82 @@
 ntityEvent;%0A
+import org.bukkit.event.player.PlayerInteractEntityEvent;%0A
 import org.b
@@ -1343,16 +1343,56 @@
 emMeta;%0A
+import org.bukkit.material.MonsterEggs;%0A
 import o
@@ -4431,16 +4431,1157 @@
 %0A    %7D%0A%0A
+    @EventHandler%0A    public void eggRitchie(PlayerInteractEntityEvent event) %7B%0A        if (event.getPlayer().getItemInHand() == null%0A                %7C%7C event.getPlayer().getItemInHand().getType() != Material.EGG) %7B%0A            return;%0A        %7D%0A        Entity e = event.getRightClicked();%0A        if (e.getType() != EntityType.OCELOT) %7B%0A            return;%0A        %7D%0A        String name = e.getCustomName();%0A        String stripped = ChatColor.stripColor(name);%0A        if (name.length() == stripped.length() %7C%7C !stripped.equals(%22Ritchie%22)) %7B%0A            return;%0A        %7D%0A        event.setCancelled(true);%0A        ItemStack eggs = event.getPlayer().getItemInHand();%0A        if (eggs.getAmount() %3E 1) %7B%0A            eggs.setAmount(eggs.getAmount() - 1);%0A            event.getPlayer().setItemInHand(eggs);%0A        %7D else %7B%0A            event.getPlayer().setItemInHand(null);%0A        %7D%0A        ItemStack regg = new ItemStack(Material.MONSTER_EGG, 1, EntityType.OCELOT.getTypeId());%0A        regg.getItemMeta().setDisplayName(ChatColor.GOLD.toString() + ChatColor.BOLD + %22Ritchie%22);%0A        event.getPlayer().getInventory().addItem(regg);%0A    %7D%0A%0A
 %7D%0A%0A// if

allow ritchie to be egged up