Advertisement
Guest User

IceCreamMaker.java

a guest
Jun 26th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.21 KB | None | 0 0
  1. package com.chef.mod.blocks;
  2.  
  3. import java.util.Random;
  4.  
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.BlockContainer;
  7. import net.minecraft.block.material.Material;
  8. import net.minecraft.block.properties.IProperty;
  9. import net.minecraft.block.properties.PropertyDirection;
  10. import net.minecraft.block.properties.PropertyInteger;
  11. import net.minecraft.block.state.BlockState;
  12. import net.minecraft.block.state.IBlockState;
  13. import net.minecraft.entity.EntityLivingBase;
  14. import net.minecraft.entity.player.EntityPlayer;
  15. import net.minecraft.inventory.Container;
  16. import net.minecraft.inventory.InventoryHelper;
  17. import net.minecraft.item.Item;
  18. import net.minecraft.item.ItemStack;
  19. import net.minecraft.tileentity.TileEntity;
  20. import net.minecraft.util.BlockPos;
  21. import net.minecraft.util.EnumFacing;
  22. import net.minecraft.world.IBlockAccess;
  23. import net.minecraft.world.World;
  24. import net.minecraftforge.fml.relauncher.Side;
  25. import net.minecraftforge.fml.relauncher.SideOnly;
  26.  
  27. import com.chef.mod.Chef;
  28. import com.chef.mod.Debugger;
  29. import com.chef.mod.init.MyBlocks;
  30. import com.chef.mod.tileentity.TileEntityIceCreamMaker;
  31.  
  32. public class IceCreamMaker extends BlockContainer {
  33.     public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
  34.     public static final PropertyInteger ICE = PropertyInteger.create("ice", 0, 10);
  35.  
  36.     private final boolean isActive;
  37.     private static boolean keepInventory;
  38.  
  39.     public IceCreamMaker(boolean isActive) {
  40.         super(Material.iron);
  41.         this.isActive = isActive;
  42.         this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(ICE, Integer.valueOf(0)));
  43.         this.setHardness(5.0F);
  44.         this.setResistance(10.0F);
  45.         this.setStepSound(soundTypeMetal);
  46.     }
  47.  
  48.     public static BlockPos getFurnacePosition(BlockPos pos) {
  49.  
  50.         return pos;
  51.  
  52.     }
  53.  
  54.     public Item getItemDropped(IBlockState state, Random rand, int fortune) {
  55.         return Item.getItemFromBlock(MyBlocks.ice_cream_maker);
  56.     }
  57.  
  58.     public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) {
  59.        
  60.         this.setDefaultFacing(worldIn, pos, state);
  61.     }
  62.    
  63.  
  64.     /**
  65.      * Get the actual Block state of this Block at the given position. This
  66.      * applies properties not visible in the metadata, such as fence
  67.      * connections.
  68.      */
  69.     @Override
  70.     public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
  71.        
  72.         EnumFacing facing = (EnumFacing) state.getValue(FACING);
  73.         int ice = ((Integer) state.getValue(ICE)).intValue();
  74.        
  75.         return state.withProperty(FACING, facing).withProperty(ICE, Integer.valueOf(ice));
  76.        
  77.     }
  78.    
  79.     @Override
  80.     public int getMetaFromState(IBlockState state)
  81.     {
  82.         return 0;
  83.     }
  84.  
  85.     private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state) {
  86.         if (!worldIn.isRemote) {
  87.             Block block = worldIn.getBlockState(pos.north()).getBlock();
  88.             Block block1 = worldIn.getBlockState(pos.south()).getBlock();
  89.             Block block2 = worldIn.getBlockState(pos.west()).getBlock();
  90.             Block block3 = worldIn.getBlockState(pos.east()).getBlock();
  91.             EnumFacing enumfacing = (EnumFacing) state.getValue(FACING);
  92.  
  93.             if (enumfacing == EnumFacing.NORTH && block.isFullBlock() && !block1.isFullBlock()) {
  94.                 enumfacing = EnumFacing.SOUTH;
  95.             } else if (enumfacing == EnumFacing.SOUTH && block1.isFullBlock() && !block.isFullBlock()) {
  96.                 enumfacing = EnumFacing.NORTH;
  97.             } else if (enumfacing == EnumFacing.WEST && block2.isFullBlock() && !block3.isFullBlock()) {
  98.                 enumfacing = EnumFacing.EAST;
  99.             } else if (enumfacing == EnumFacing.EAST && block3.isFullBlock() && !block2.isFullBlock()) {
  100.                 enumfacing = EnumFacing.WEST;
  101.             }
  102.  
  103.             worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
  104.         }
  105.     }
  106.  
  107.     public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) {
  108.         if (worldIn.isRemote) {
  109.             return true;
  110.         } else {
  111.  
  112.             TileEntity tileentity = worldIn.getTileEntity(pos);
  113.  
  114.             if (tileentity instanceof TileEntityIceCreamMaker) {
  115.                 playerIn.openGui(Chef.instance, MyBlocks.guiID_ice_cream_maker, worldIn, pos.getX(), pos.getY(), pos.getZ());
  116.             }
  117.  
  118.             return true;
  119.         }
  120.     }
  121.  
  122.     public static void setState(boolean isActive, int iceAmount, World worldIn, BlockPos pos) {
  123.         IBlockState iblockstate = worldIn.getBlockState(pos);
  124.         TileEntity tileentity = worldIn.getTileEntity(pos);
  125.        
  126.         keepInventory = true;
  127.        
  128.         if (isActive) {
  129.                
  130.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker_on.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, Integer.valueOf(iceAmount)), 3);
  131.  
  132.         } else {
  133.  
  134.             worldIn.setBlockState(pos, MyBlocks.ice_cream_maker.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, Integer.valueOf(iceAmount)), 3);
  135.  
  136.         }
  137.  
  138.         keepInventory = false;
  139.  
  140.         if (tileentity != null) {
  141.             tileentity.validate();
  142.             worldIn.setTileEntity(pos, tileentity);
  143.         }
  144.     }
  145.    
  146.  
  147.     public TileEntity createNewTileEntity(World worldIn, int meta) {
  148.        
  149.         return new TileEntityIceCreamMaker();
  150.     }
  151.  
  152.     public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) {
  153.  
  154.         EnumFacing enumfacing = (placer == null) ? EnumFacing.NORTH : EnumFacing.fromAngle(placer.rotationYaw);
  155.  
  156.         return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite()).withProperty(ICE, Integer.valueOf(0));
  157.     }
  158.  
  159.     public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
  160.        
  161.         worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing().getOpposite()).withProperty(ICE, Integer.valueOf(0)), 2);
  162.  
  163.         if (stack.hasDisplayName()) {
  164.             TileEntity tileentity = worldIn.getTileEntity(pos);
  165.  
  166.             if (tileentity instanceof TileEntityIceCreamMaker) {
  167.                 ((TileEntityIceCreamMaker) tileentity).setCustomInventoryName(stack.getDisplayName());
  168.             }
  169.         }
  170.     }
  171.  
  172.     public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
  173.        
  174.         if (!keepInventory) {
  175.            
  176.             TileEntity tileentity = worldIn.getTileEntity(pos);
  177.  
  178.             if (tileentity instanceof TileEntityIceCreamMaker) {
  179.                 InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityIceCreamMaker) tileentity);
  180.                 worldIn.updateComparatorOutputLevel(pos, this);
  181.             }
  182.         }
  183.  
  184.         super.breakBlock(worldIn, pos, state);
  185.     }
  186.  
  187.     public boolean hasComparatorInputOverride() {
  188.         return true;
  189.     }
  190.  
  191.     public int getComparatorInputOverride(World worldIn, BlockPos pos) {
  192.         return Container.calcRedstone(worldIn.getTileEntity(pos));
  193.     }
  194.  
  195.     @SideOnly(Side.CLIENT)
  196.     public Item getItem(World worldIn, BlockPos pos) {
  197.         return Item.getItemFromBlock(MyBlocks.ice_cream_maker);
  198.     }
  199.  
  200.     public int getRenderType() {
  201.         return 3;
  202.     }
  203.  
  204.     @SideOnly(Side.CLIENT)
  205.     public IBlockState getStateForEntityRender(IBlockState state) {
  206.  
  207.         int ice = ((Integer) state.getValue(ICE)).intValue();
  208.        
  209.         return this.getDefaultState().withProperty(FACING, EnumFacing.SOUTH).withProperty(ICE, Integer.valueOf(ice));
  210.        
  211.     }
  212.  
  213.     protected BlockState createBlockState() {
  214.        
  215.         return new BlockState(this, new IProperty[] { FACING, ICE });
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement