Guest User

ButterChurn.java

a guest
Aug 11th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.81 KB | None | 0 0
  1. package com.chef.mod.blocks;
  2.  
  3. import java.util.Random;
  4.  
  5. import com.chef.mod.Chef;
  6. import com.chef.mod.init.MyBlocks;
  7. import com.chef.mod.init.MyItems;
  8. import com.chef.mod.tileentity.TileEntityButterChurn;
  9.  
  10. import net.minecraft.block.BlockContainer;
  11. import net.minecraft.block.material.Material;
  12. import net.minecraft.block.properties.IProperty;
  13. import net.minecraft.block.properties.PropertyInteger;
  14. import net.minecraft.block.state.BlockState;
  15. import net.minecraft.block.state.IBlockState;
  16. import net.minecraft.entity.item.EntityItem;
  17. import net.minecraft.entity.player.EntityPlayer;
  18. import net.minecraft.entity.player.EntityPlayerMP;
  19. import net.minecraft.init.Items;
  20. import net.minecraft.item.Item;
  21. import net.minecraft.item.ItemStack;
  22. import net.minecraft.tileentity.TileEntity;
  23. import net.minecraft.util.BlockPos;
  24. import net.minecraft.util.EnumFacing;
  25. import net.minecraft.util.MathHelper;
  26. import net.minecraft.world.World;
  27. import net.minecraftforge.fml.relauncher.Side;
  28. import net.minecraftforge.fml.relauncher.SideOnly;
  29.  
  30. public class ButterChurn extends BlockContainer
  31. {
  32.     public static final PropertyInteger LEVEL = PropertyInteger.create("level", 0, 6);
  33.  
  34.     public ButterChurn()
  35.     {
  36.         super(Material.wood);
  37.         this.setDefaultState(this.blockState.getBaseState().withProperty(LEVEL, Integer.valueOf(0)));
  38.         this.setBlockBounds(0.19F, 0.0F, 0.19F, 0.81F, 1.0F, 0.81F);
  39.         this.setCreativeTab(Chef.tabChef);
  40.     }
  41.    
  42.     @Override
  43.     public TileEntity createNewTileEntity(World world, int meta) {
  44.  
  45.         return new TileEntityButterChurn();
  46.  
  47.     }
  48.    
  49.     @Override
  50.     public int getRenderType() {
  51.  
  52.         return 3;
  53.  
  54.     }
  55.  
  56.     @Override
  57.     public boolean isOpaqueCube()
  58.     {
  59.         return false;
  60.     }
  61.  
  62.     @Override
  63.     public boolean isFullCube()
  64.     {
  65.         return false;
  66.     }
  67.  
  68.     public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
  69.     {
  70.         if (worldIn.isRemote)
  71.         {
  72.             return true;
  73.         }
  74.         else
  75.         {
  76.             ItemStack itemstack = playerIn.getCurrentEquippedItem();
  77.  
  78.             if (itemstack == null)
  79.             {
  80.                 return false;
  81.             }
  82.             else
  83.             {
  84.                 int level = ((Integer)state.getValue(LEVEL)).intValue();
  85.                 Item item = itemstack.getItem();
  86.  
  87.                 if (item == Items.milk_bucket)
  88.                 {
  89.                     if (level == 0)
  90.                     {
  91.                         if (!playerIn.capabilities.isCreativeMode)
  92.                         {
  93.                             playerIn.inventory.setInventorySlotContents(playerIn.inventory.currentItem, new ItemStack(Items.bucket));
  94.                         }
  95.  
  96.                         this.setMilkLevel(worldIn, pos, state, 1);
  97.                        
  98.                     }
  99.  
  100.                     return true;
  101.                 }
  102.                 else
  103.                 {
  104.                     ItemStack itemstack1;
  105.                     Random rand = new Random();
  106.  
  107.                     if (item == MyItems.stamper)
  108.                     {
  109.                         if (level > 0 && level < 5)
  110.                         {
  111.                            
  112.                             this.setMilkLevel(worldIn, pos, state, level + 1);
  113.                            
  114.                             if (!playerIn.capabilities.isCreativeMode)
  115.                             {
  116.  
  117.                                 itemstack.damageItem(1, playerIn);
  118.  
  119.                                 if (itemstack.getItemDamage() >= itemstack.getMaxDamage()) {
  120.  
  121.                                     itemstack.stackSize--;
  122.  
  123.                                 }
  124.                             }
  125.                            
  126.                         } else if (level == 6) {
  127.                            
  128.                             if (!playerIn.capabilities.isCreativeMode)
  129.                             {
  130.                                 itemstack.damageItem(1, playerIn);
  131.  
  132.                                 if (itemstack.getItemDamage() >= itemstack.getMaxDamage()) {
  133.  
  134.                                     itemstack.stackSize--;
  135.  
  136.                                 }
  137.                                
  138.                                 itemstack1 = new ItemStack(MyItems.butter, 5 + rand.nextInt(6), 0);
  139.  
  140.                                 if (!playerIn.inventory.addItemStackToInventory(itemstack1))
  141.                                 {
  142.                                     worldIn.spawnEntityInWorld(new EntityItem(worldIn, (double)pos.getX() + 0.5D, (double)pos.getY() + 1.5D, (double)pos.getZ() + 0.5D, itemstack1));
  143.                                 }
  144.                                 else if (playerIn instanceof EntityPlayerMP)
  145.                                 {
  146.                                     ((EntityPlayerMP)playerIn).sendContainerToPlayer(playerIn.inventoryContainer);
  147.                                 }
  148.                                
  149.                             }
  150.                            
  151.                             this.setMilkLevel(worldIn, pos, state, 0);
  152.                            
  153.                         }
  154.  
  155.                         return true;
  156.                        
  157.                     } else {
  158.                        
  159.                         return false;
  160.                        
  161.                     }
  162.                 }
  163.             }
  164.         }
  165.     }
  166.  
  167.     public void setMilkLevel(World worldIn, BlockPos pos, IBlockState state, int level)
  168.     {
  169.         worldIn.setBlockState(pos, state.withProperty(LEVEL, Integer.valueOf(MathHelper.clamp_int(level, 0, 6))), 2);
  170.         worldIn.updateComparatorOutputLevel(pos, this);
  171.     }
  172.  
  173.     /**
  174.      * Get the Item that this Block should drop when harvested.
  175.      *  
  176.      * @param fortune the level of the Fortune enchantment on the player's tool
  177.      */
  178.     @Override
  179.     public Item getItemDropped(IBlockState state, Random rand, int fortune)
  180.     {
  181.         return Item.getItemFromBlock(MyBlocks.butter_churn);
  182.     }
  183.  
  184.     @SideOnly(Side.CLIENT)
  185.     public Item getItem(World worldIn, BlockPos pos)
  186.     {
  187.         return Item.getItemFromBlock(MyBlocks.butter_churn);
  188.     }
  189.  
  190.     @Override
  191.     public boolean hasComparatorInputOverride()
  192.     {
  193.         return true;
  194.     }
  195.  
  196.     @Override
  197.     public int getComparatorInputOverride(World worldIn, BlockPos pos)
  198.     {
  199.         return ((Integer)worldIn.getBlockState(pos).getValue(LEVEL)).intValue();
  200.     }
  201.  
  202.     /**
  203.      * Convert the given metadata into a BlockState for this Block
  204.      */
  205.     public IBlockState getStateFromMeta(int meta)
  206.     {
  207.         return this.getDefaultState().withProperty(LEVEL, Integer.valueOf(meta));
  208.     }
  209.  
  210.     /**
  211.      * Convert the BlockState into the correct metadata value
  212.      */
  213.     public int getMetaFromState(IBlockState state)
  214.     {
  215.         return ((Integer)state.getValue(LEVEL)).intValue();
  216.     }
  217.  
  218.     protected BlockState createBlockState()
  219.     {
  220.         return new BlockState(this, new IProperty[] { LEVEL });
  221.     }
  222. }
Add Comment
Please, Sign In to add comment