Guest User

BlockSapphireCrop

a guest
Jul 2nd, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.17 KB | None | 0 0
  1. package sometimesHardcoreSometimes;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5.  
  6. import cpw.mods.fml.relauncher.Side;
  7. import cpw.mods.fml.relauncher.SideOnly;
  8. import net.minecraft.block.Block;
  9. import net.minecraft.block.BlockContainer;
  10. import net.minecraft.block.material.Material;
  11. import net.minecraft.client.renderer.texture.IconRegister;
  12. import net.minecraft.entity.EntityLivingBase;
  13. import net.minecraft.entity.item.EntityItem;
  14. import net.minecraft.entity.player.EntityPlayer;
  15. import net.minecraft.item.Item;
  16. import net.minecraft.item.ItemStack;
  17. import net.minecraft.nbt.NBTTagCompound;
  18. import net.minecraft.tileentity.TileEntity;
  19. import net.minecraft.tileentity.TileEntitySkull;
  20. import net.minecraft.util.Icon;
  21. import net.minecraft.util.MathHelper;
  22. import net.minecraft.world.World;
  23. import net.minecraftforge.common.ForgeDirection;
  24.  
  25. public class BlockSapphireCrop extends BlockContainer
  26. {
  27.     @SideOnly(Side.CLIENT)
  28.     private Icon[] iconArray;
  29.     int bAmt;
  30.    
  31.     public BlockSapphireCrop(int id)
  32.     {
  33.         super(id, Material.cactus);
  34.        
  35.         this.setHardness(3.0F);
  36.         this.setTickRandomly(true);
  37.         this.setUnlocalizedName("sapphireCrop");
  38.         this.setCreativeTab(Base.tabSometimes);
  39.        
  40.         //Block bounds -- What you collide with
  41.         this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.75F, 1.0F);
  42.     }
  43.    
  44.     //slows down growth speeds
  45.     public void updateTick(World world, int x, int y, int z, Random rand)
  46.     {
  47.         TileEntity te = world.getBlockTileEntity(x, y, z);
  48.        
  49.         if(te != null)
  50.         {
  51.             if(te instanceof TileEntitySapphireCrop)
  52.                 te = (TileEntitySapphireCrop)te;
  53.            
  54.             bAmt = ((TileEntitySapphireCrop) te).getBerryAmount();
  55.         }
  56.        
  57.             if(world.getBlockId(x, y - 1, z) != Block.slowSand.blockID)
  58.                 return;
  59.             if(world.getBlockLightValue(x, y + 1, z) < 9)
  60.                 return;
  61.             if(rand.nextInt() % 5 != 0)
  62.                 return;
  63.             if(bAmt < 7)
  64.             {
  65.                 bAmt += 2;
  66.                 ((TileEntitySapphireCrop) te).setBerryAmount(bAmt);
  67.             }
  68.        
  69.         //checks for staff and slows growth speed
  70. //      for(int i = 0; i < 3; i++)
  71. //      {
  72. //          if(world.getBlockId(x + (i + 3), y, z))
  73. //      }
  74.     }
  75.    
  76.     //Called when block is sheared
  77.     public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9)
  78.     {
  79.         ItemStack itemStack = player.inventory.getCurrentItem();
  80.         TileEntity te = world.getBlockTileEntity(x, y, z);
  81.        
  82.         if(te != null)
  83.         {
  84.             if(te instanceof TileEntitySapphireCrop)
  85.                 te = (TileEntitySapphireCrop)te;
  86.            
  87.             bAmt = ((TileEntitySapphireCrop) te).getBerryAmount();
  88.         }
  89.        
  90.         if(itemStack.itemID == Base.itemSapphireBerry.itemID)
  91.         {
  92.             if(bAmt < 14)
  93.             {
  94.                 for(int i = 0; i <= 0; i++)
  95.                     bAmt++;
  96.                
  97.                 ((TileEntitySapphireCrop) te).setBerryAmount(bAmt);
  98.             }
  99.            
  100.             if(!player.capabilities.isCreativeMode)
  101.                 itemStack.stackSize--;
  102.            
  103.             return true;
  104.         }
  105.         else if(itemStack.itemID == Item.shears.itemID && isShearable())
  106.         {  
  107.             ItemStack stack = new ItemStack(Base.itemSapphireBerry);
  108.            
  109.             Random rand = new Random();
  110.             EntityItem item = player.entityDropItem(stack, 1.0F);
  111.             item.motionY += rand.nextFloat() * 0.05F;
  112.             item.motionX += (rand.nextFloat() - rand.nextFloat()) * 0.2F;
  113.             item.motionZ += (rand.nextFloat() - rand.nextFloat()) * 0.2F;
  114.            
  115.             if(!player.capabilities.isCreativeMode)
  116.                 itemStack.damageItem(5, player);
  117.            
  118.             bAmt--;
  119.             ((TileEntitySapphireCrop) te).setBerryAmount(bAmt);
  120.            
  121.             return true;
  122.         }
  123.         else
  124.             return true;
  125.     }
  126.    
  127.     public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5)
  128.     {
  129.         if (!par1World.doesBlockHaveSolidTopSurface(par2, par3 - 1, par4))
  130.         {
  131.             this.dropBlockAsItem(par1World, par2, par3, par4, par1World.getBlockMetadata(par2, par3, par4), 0);
  132.             par1World.setBlockToAir(par2, par3, par4);
  133.         }
  134.     }
  135.    
  136.     //Given berry amount, checks to see if shear-able
  137.     public boolean isShearable()
  138.     {
  139.         if(bAmt > 0)
  140.             return true;
  141.         else
  142.             return false;
  143.     }
  144.    
  145.     public void onBlockAdded(World par1World, int par2, int par3, int par4)
  146.     {
  147.         super.onBlockAdded(par1World, par2, par3, par4);
  148.        
  149.         createNewTileEntity(par1World);
  150.        
  151.         this.setDefaultDirection(par1World, par2, par3, par4);
  152.     }
  153.  
  154.     private void setDefaultDirection(World par1World, int par2, int par3, int par4)
  155.     {
  156.         if (!par1World.isRemote)
  157.         {
  158.             int l = par1World.getBlockId(par2, par3, par4 - 1);
  159.             int i1 = par1World.getBlockId(par2, par3, par4 + 1);
  160.             int j1 = par1World.getBlockId(par2 - 1, par3, par4);
  161.             int k1 = par1World.getBlockId(par2 + 1, par3, par4);
  162.             byte b0 = 3;
  163.  
  164.             if (Block.opaqueCubeLookup[l] && !Block.opaqueCubeLookup[i1])
  165.             {
  166.                 b0 = 3;
  167.             }
  168.  
  169.             if (Block.opaqueCubeLookup[i1] && !Block.opaqueCubeLookup[l])
  170.             {
  171.                 b0 = 2;
  172.             }
  173.  
  174.             if (Block.opaqueCubeLookup[j1] && !Block.opaqueCubeLookup[k1])
  175.             {
  176.                 b0 = 5;
  177.             }
  178.  
  179.             if (Block.opaqueCubeLookup[k1] && !Block.opaqueCubeLookup[j1])
  180.             {
  181.                 b0 = 4;
  182.             }
  183.  
  184.             par1World.setBlockMetadataWithNotify(par2, par3, par4, b0, 2);
  185.         }
  186.     }
  187.    
  188.     public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLivingBase par5EntityLivingBase, ItemStack par6ItemStack)
  189.     {
  190.         int l = MathHelper.floor_double((double)(par5EntityLivingBase.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
  191.  
  192.         if (l == 0)
  193.         {
  194.             par1World.setBlockMetadataWithNotify(par2, par3, par4, 2, 2);
  195.         }
  196.  
  197.         if (l == 1)
  198.         {
  199.             par1World.setBlockMetadataWithNotify(par2, par3, par4, 5, 2);
  200.         }
  201.  
  202.         if (l == 2)
  203.         {
  204.             par1World.setBlockMetadataWithNotify(par2, par3, par4, 3, 2);
  205.         }
  206.  
  207.         if (l == 3)
  208.         {
  209.             par1World.setBlockMetadataWithNotify(par2, par3, par4, 4, 2);
  210.         }
  211.     }
  212.    
  213.     public TileEntity createNewTileEntity(World world)
  214.     {
  215.         return new TileEntitySapphireCrop();
  216.     }
  217.    
  218.     @Override
  219.     public int getRenderType()
  220.     {
  221.         return -1;
  222.     }
  223.    
  224.     @Override
  225.     public boolean isOpaqueCube()
  226.     {
  227.         return false;
  228.     }
  229.    
  230.     public boolean renderAsNormalBlock()
  231.     {
  232.         return false;
  233.     }
  234.    
  235.     @SideOnly(Side.CLIENT)
  236.     public Icon getIcon(int side, int meta)
  237.     {
  238.         if (meta < 0 || meta > 7)
  239.         {
  240.             meta = 7;
  241.         }
  242.  
  243.         return this.iconArray[meta];
  244.     }
  245.    
  246.     public void registerIcons(IconRegister icon)
  247.     {
  248.         this.iconArray = new Icon[8];
  249.  
  250.         for (int i = 0; i < this.iconArray.length; ++i)
  251.         {
  252.             this.iconArray[i] = icon.registerIcon(Base.modid + ":" + (this.getUnlocalizedName().substring(5)) + "_" + i);
  253.         }
  254.     }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment