001package net.minecraft.item;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import java.util.Iterator;
006import java.util.List;
007import net.minecraft.block.Block;
008import net.minecraft.client.renderer.texture.IconRegister;
009import net.minecraft.creativetab.CreativeTabs;
010import net.minecraft.entity.Entity;
011import net.minecraft.entity.EntityEggInfo;
012import net.minecraft.entity.EntityList;
013import net.minecraft.entity.EntityLiving;
014import net.minecraft.entity.player.EntityPlayer;
015import net.minecraft.util.Facing;
016import net.minecraft.util.Icon;
017import net.minecraft.util.MathHelper;
018import net.minecraft.util.StatCollector;
019import net.minecraft.world.World;
020
021public class ItemMonsterPlacer extends Item
022{
023    @SideOnly(Side.CLIENT)
024    private Icon theIcon;
025
026    public ItemMonsterPlacer(int par1)
027    {
028        super(par1);
029        this.setHasSubtypes(true);
030        this.setCreativeTab(CreativeTabs.tabMisc);
031    }
032
033    public String getItemDisplayName(ItemStack par1ItemStack)
034    {
035        String s = ("" + StatCollector.translateToLocal(this.getUnlocalizedName() + ".name")).trim();
036        String s1 = EntityList.getStringFromID(par1ItemStack.getItemDamage());
037
038        if (s1 != null)
039        {
040            s = s + " " + StatCollector.translateToLocal("entity." + s1 + ".name");
041        }
042
043        return s;
044    }
045
046    @SideOnly(Side.CLIENT)
047    public int getColorFromItemStack(ItemStack par1ItemStack, int par2)
048    {
049        EntityEggInfo entityegginfo = (EntityEggInfo)EntityList.entityEggs.get(Integer.valueOf(par1ItemStack.getItemDamage()));
050        return entityegginfo != null ? (par2 == 0 ? entityegginfo.primaryColor : entityegginfo.secondaryColor) : 16777215;
051    }
052
053    /**
054     * Callback for item usage. If the item does something special on right clicking, he will have one of those. Return
055     * True if something happen and false if it don't. This is for ITEMS, not BLOCKS
056     */
057    public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
058    {
059        if (par3World.isRemote)
060        {
061            return true;
062        }
063        else
064        {
065            int i1 = par3World.getBlockId(par4, par5, par6);
066            par4 += Facing.offsetsXForSide[par7];
067            par5 += Facing.offsetsYForSide[par7];
068            par6 += Facing.offsetsZForSide[par7];
069            double d0 = 0.0D;
070
071            if (par7 == 1 && Block.blocksList[i1] != null && Block.blocksList[i1].getRenderType() == 11)
072            {
073                d0 = 0.5D;
074            }
075
076            Entity entity = spawnCreature(par3World, par1ItemStack.getItemDamage(), (double)par4 + 0.5D, (double)par5 + d0, (double)par6 + 0.5D);
077
078            if (entity != null)
079            {
080                if (entity instanceof EntityLiving && par1ItemStack.hasDisplayName())
081                {
082                    ((EntityLiving)entity).func_94058_c(par1ItemStack.getDisplayName());
083                }
084
085                if (!par2EntityPlayer.capabilities.isCreativeMode)
086                {
087                    --par1ItemStack.stackSize;
088                }
089            }
090
091            return true;
092        }
093    }
094
095    /**
096     * Spawns the creature specified by the egg's type in the location specified by the last three parameters.
097     * Parameters: world, entityID, x, y, z.
098     */
099    public static Entity spawnCreature(World par0World, int par1, double par2, double par4, double par6)
100    {
101        if (!EntityList.entityEggs.containsKey(Integer.valueOf(par1)))
102        {
103            return null;
104        }
105        else
106        {
107            Entity entity = null;
108
109            for (int j = 0; j < 1; ++j)
110            {
111                entity = EntityList.createEntityByID(par1, par0World);
112
113                if (entity != null && entity instanceof EntityLiving)
114                {
115                    EntityLiving entityliving = (EntityLiving)entity;
116                    entity.setLocationAndAngles(par2, par4, par6, MathHelper.wrapAngleTo180_float(par0World.rand.nextFloat() * 360.0F), 0.0F);
117                    entityliving.rotationYawHead = entityliving.rotationYaw;
118                    entityliving.renderYawOffset = entityliving.rotationYaw;
119                    entityliving.initCreature();
120                    par0World.spawnEntityInWorld(entity);
121                    entityliving.playLivingSound();
122                }
123            }
124
125            return entity;
126        }
127    }
128
129    @SideOnly(Side.CLIENT)
130    public boolean requiresMultipleRenderPasses()
131    {
132        return true;
133    }
134
135    @SideOnly(Side.CLIENT)
136
137    /**
138     * Gets an icon index based on an item's damage value and the given render pass
139     */
140    public Icon getIconFromDamageForRenderPass(int par1, int par2)
141    {
142        return par2 > 0 ? this.theIcon : super.getIconFromDamageForRenderPass(par1, par2);
143    }
144
145    @SideOnly(Side.CLIENT)
146
147    /**
148     * returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
149     */
150    public void getSubItems(int par1, CreativeTabs par2CreativeTabs, List par3List)
151    {
152        Iterator iterator = EntityList.entityEggs.values().iterator();
153
154        while (iterator.hasNext())
155        {
156            EntityEggInfo entityegginfo = (EntityEggInfo)iterator.next();
157            par3List.add(new ItemStack(par1, 1, entityegginfo.spawnedID));
158        }
159    }
160
161    @SideOnly(Side.CLIENT)
162    public void updateIcons(IconRegister par1IconRegister)
163    {
164        super.updateIcons(par1IconRegister);
165        this.theIcon = par1IconRegister.registerIcon("monsterPlacer_overlay");
166    }
167}