package minefantasy.block;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import minefantasy.block.BlockListMF;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLeavesBase;
import net.minecraft.block.material.Material;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.StatList;
import net.minecraft.util.Icon;
import net.minecraft.world.ColorizerFoliage;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.IShearable;
public class BlockLeavesMF extends BlockLeavesBase implements IShearable
{
public static final String[] LEAF_TYPES = new String[] {"ironbark", "ebony"};
public static final String[] names = new String[] {"IronBarkLeaves", "IronBarkLeaves_opaque", "leaves", "leaves_opaque"};
private Icon[] iconArray = new Icon[4];
int[] adjacentTreeBlocks;
protected BlockLeavesMF(int id)
{
super(id, Material.leaves, true);
this.setTickRandomly(true);
this.setCreativeTab(CreativeTabs.tabDecorations);
}
@SideOnly(Side.CLIENT)
public int getBlockColor()
{
double d0 = 0.5D;
double d1 = 1.0D;
return ColorizerFoliage.getFoliageColor(d0, d1);
}
@SideOnly(Side.CLIENT)
/**
* Returns the color this block should be rendered. Used by leaves.
*/
public int getRenderColor(int meta)
{
return (meta & 3) == 1 ? ColorizerFoliage.getFoliageColorBasic() : ((meta & 3) == 2 ? ColorizerFoliage.getFoliageColorBirch() : ColorizerFoliage.getFoliageColorBasic());
}
@SideOnly(Side.CLIENT)
/**
* Returns a integer with hex for 0xrrggbb with this color multiplied against the blocks color. Note only called
* when first determining what to render.
*/
public int colorMultiplier(IBlockAccess world, int x, int y, int z)
{
int l = world.getBlockMetadata(x, y, z);
if ((l & 3) == 1)
{
return ColorizerFoliage.getFoliageColorPine();
}
else if ((l & 3) == 2)
{
return ColorizerFoliage.getFoliageColorBirch();
}
else
{
int i1 = 0;
int j1 = 0;
int k1 = 0;
for (int l1 = -1; l1 <= 1; ++l1)
{
for (int i2 = -1; i2 <= 1; ++i2)
{
int j2 = world.getBiomeGenForCoords(x + i2, z + l1).getBiomeFoliageColor();
i1 += (j2 & 16711680) >> 16;
j1 += (j2 & 65280) >> 8;
k1 += j2 & 255;
}
}
return (i1 / 9 & 255) << 16 | (j1 / 9 & 255) << 8 | k1 / 9 & 255;
}
}
/**
* ejects contained items into the world, and notifies neighbours of an update, as appropriate
*/
public void breakBlock(World world, int x, int y, int z, int meta, int update)
{
byte b0 = 1;
int j1 = b0 + 1;
if (world.checkChunksExist(x - j1, y - j1, z - j1, x + j1, y + j1, z + j1))
{
for (int k1 = -b0; k1 <= b0; ++k1)
{
for (int l1 = -b0; l1 <= b0; ++l1)
{
for (int i2 = -b0; i2 <= b0; ++i2)
{
int j2 = world.getBlockId(x + k1, y + l1, z + i2);
if (Block.blocksList[j2] != null)
{
Block.blocksList[j2].beginLeavesDecay(world, x + k1, y + l1, z + i2);
}
}
}
}
}
}
/**
* Ticks the block if it's been scheduled
*/
public void updateTick(World world, int x, int y, int z, Random rand)
{
if (!world.isRemote)
{
int l = world.getBlockMetadata(x, y, z);
if ((l & 8) != 0 && (l & 4) == 0)
{
byte b0 = 4;
int i1 = b0 + 1;
byte b1 = 32;
int j1 = b1 * b1;
int k1 = b1 / 2;
if (this.adjacentTreeBlocks == null)
{
this.adjacentTreeBlocks = new int[b1 * b1 * b1];
}
int l1;
if (world.checkChunksExist(x - i1, y - i1, z - i1, x + i1, y + i1, z + i1))
{
int i2;
int j2;
int k2;
for (l1 = -b0; l1 <= b0; ++l1)
{
for (i2 = -b0; i2 <= b0; ++i2)
{
for (j2 = -b0; j2 <= b0; ++j2)
{
k2 = world.getBlockId(x + l1, y + i2, z + j2);
Block block = Block.blocksList[k2];
if (block != null && block.canSustainLeaves(world, x + l1, y + i2, z + j2))
{
this.adjacentTreeBlocks[(l1 + k1) * j1 + (i2 + k1) * b1 + j2 + k1] = 0;
}
else if (block != null && block.isLeaves(world, x + l1, y + i2, z + j2))
{
this.adjacentTreeBlocks[(l1 + k1) * j1 + (i2 + k1) * b1 + j2 + k1] = -2;
}
else
{
this.adjacentTreeBlocks[(l1 + k1) * j1 + (i2 + k1) * b1 + j2 + k1] = -1;
}
}
}
}
for (l1 = 1; l1 <= 4; ++l1)
{
for (i2 = -b0; i2 <= b0; ++i2)
{
for (j2 = -b0; j2 <= b0; ++j2)
{
for (k2 = -b0; k2 <= b0; ++k2)
{
if (this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1) * b1 + k2 + k1] == l1 - 1)
{
if (this.adjacentTreeBlocks[(i2 + k1 - 1) * j1 + (j2 + k1) * b1 + k2 + k1] == -2)
{
this.adjacentTreeBlocks[(i2 + k1 - 1) * j1 + (j2 + k1) * b1 + k2 + k1] = l1;
}
if (this.adjacentTreeBlocks[(i2 + k1 + 1) * j1 + (j2 + k1) * b1 + k2 + k1] == -2)
{
this.adjacentTreeBlocks[(i2 + k1 + 1) * j1 + (j2 + k1) * b1 + k2 + k1] = l1;
}
if (this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1 - 1) * b1 + k2 + k1] == -2)
{
this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1 - 1) * b1 + k2 + k1] = l1;
}
if (this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1 + 1) * b1 + k2 + k1] == -2)
{
this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1 + 1) * b1 + k2 + k1] = l1;
}
if (this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1) * b1 + (k2 + k1 - 1)] == -2)
{
this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1) * b1 + (k2 + k1 - 1)] = l1;
}
if (this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1) * b1 + k2 + k1 + 1] == -2)
{
this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1) * b1 + k2 + k1 + 1] = l1;
}
}
}
}
}
}
}
l1 = this.adjacentTreeBlocks[k1 * j1 + k1 * b1 + k1];
if (l1 >= 0)
{
world.setBlockMetadataWithNotify(x, y, z, l & -9, 4);
}
else
{
this.removeLeaves(world, x, y, z);
}
}
}
}
@SideOnly(Side.CLIENT)
/**
* A randomly called display update to be able to add particles or other items for display
*/
public void randomDisplayTick(World world, int x, int y, int z, Random rand)
{
if (world.canLightningStrikeAt(x, y + 1, z) && !world.doesBlockHaveSolidTopSurface(x, y - 1, z) && rand.nextInt(15) == 1)
{
double d0 = (double)((float)x + rand.nextFloat());
double d1 = (double)y - 0.05D;
double d2 = (double)((float)z + rand.nextFloat());
world.spawnParticle("dripWater", d0, d1, d2, 0.0D, 0.0D, 0.0D);
}
}
private void removeLeaves(World world, int x, int y, int z)
{
this.dropBlockAsItem(world, x, y, z, world.getBlockMetadata(x, y, z), 0);
world.setBlockToAir(x, y, z);
}
/**
* Returns the ID of the items to drop on destruction.
*/
public int idDropped(int id, Random rand, int meta)
{
return BlockListMF.sapling.blockID;
}
/**
* Drops the block items with a specified chance of dropping the specified items
*/
public void dropBlockAsItemWithChance(World world, int x, int y, int z, int meta, float chance, int fortune)
{
if (!world.isRemote)
{
int j1 = 20;
if ((meta & 3) == 1)
{
j1 = 600;
}
if (fortune > 0)
{
j1 -= 2 << fortune;
if (j1 < 10)
{
j1 = 10;
}
}
if (world.rand.nextInt(j1) == 0)
{
int k1 = this.idDropped(meta, world.rand, fortune);
this.dropBlockAsItem_do(world, x, y, z, new ItemStack(k1, 1, this.damageDropped(meta)));
}
j1 = 200;
if (fortune > 0)
{
j1 -= 10 << fortune;
if (j1 < 40)
{
j1 = 40;
}
}
}
}
/**
* Called when the player destroys a block with an item that can harvest it. (i, j, k) are the coordinates of the
* block and l is the block's subtype/damage.
*/
public void harvestBlock(World world, EntityPlayer player, int x, int y, int z, int meta)
{
super.harvestBlock(world, player, x, y, z, meta);
}
/**
* Determines the damage on the item the block drops. Used in cloth and wood.
*/
public int damageDropped(int meta)
{
return meta & 3;
}
/**
* Is this block (a) opaque and (b) a full 1m cube? This determines whether or not to render the shared face of two
* adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
*/
public boolean isOpaqueCube()
{
return !Block.leaves.graphicsLevel;
}
@SideOnly(Side.CLIENT)
/**
* From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
*/
@Override
public Icon getIcon(int side, int m)
{
int meta = m & 3;
int graphics = Block.leaves.graphicsLevel ? 0 : 1;
if(meta == 1)
{
return Block.leaves.getIcon(side, 0);
}
return iconArray[meta * 2 + graphics];
}
@SideOnly(Side.CLIENT)
/**
* returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
*/
public void getSubBlocks(int id, CreativeTabs tab, List list)
{
for(int a = 0; a < 2; a ++)
{
list.add(new ItemStack(id, 1, a));
}
}
/**
* Returns an item stack containing a single instance of the current block type. 'i' is the block's subtype/damage
* and is ignored for blocks which do not support subtypes. Blocks which cannot be harvested should return null.
*/
protected ItemStack createStackedBlock(int meta)
{
return new ItemStack(this.blockID, 1, meta & 3);
}
@SideOnly(Side.CLIENT)
/**
* When this method is called, your block should register all the icons it needs with the given IconRegister. This
* is the only chance you get to register icons.
*/
public void registerIcons(IconRegister reg)
{
this.iconArray[0] = reg.registerIcon("minefantasy:Tree/" + names[0]);
this.iconArray[1] = reg.registerIcon("minefantasy:Tree/" + names[1]);
this.iconArray[2] = reg.registerIcon("minecraft:" + names[2]);
this.iconArray[3] = reg.registerIcon("minecraft:" + names[3]);
}
@Override
public boolean isShearable(ItemStack item, World world, int x, int y, int z)
{
return true;
}
@Override
public int quantityDropped(int meta, int fort, Random random)
{
fort = Math.max(1, fort);
int c = meta == 0 ? 20 : 120;
return random.nextInt(getChance(meta)) == 0 ? (1 + random.nextInt(fort)) : 0;
}
private int getChance(int meta) {
int type = meta & 3;
return meta == 0 ? 20 : 100;
}
@Override
public ArrayList<ItemStack> onSheared(ItemStack item, World world, int x, int y, int z, int fortune)
{
ArrayList<ItemStack> ret = new ArrayList<ItemStack>();
ret.add(new ItemStack(this, 1, world.getBlockMetadata(x, y, z) & 3));
return ret;
}
@Override
public void beginLeavesDecay(World world, int x, int y, int z)
{
world.setBlockMetadataWithNotify(x, y, z, world.getBlockMetadata(x, y, z) | 8, 4);
}
@Override
public boolean isLeaves(World world, int x, int y, int z)
{
return true;
}
}