Advertisement
Guest User

IceCreamMaker

a guest
May 16th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.67 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.PropertyEnum;
  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.util.IStringSerializable;
  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.init.MyBlocks;
  29. import com.chef.mod.tileentity.TileEntityIceCreamMaker;
  30.  
  31. public class IceCreamMaker extends BlockContainer
  32. {
  33.     public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
  34.     public static final PropertyEnum ICE = PropertyEnum.create("ice", EnumIce.class);
  35.    
  36.     private final boolean isActive;
  37.     private static boolean keepInventory;
  38.  
  39.     public IceCreamMaker(boolean isActive)
  40.     {
  41.         super(Material.iron);
  42.         this.isActive = isActive;
  43.         this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(ICE, EnumIce.ICE0));
  44.         this.setHardness(5.0F);
  45.         this.setResistance(10.0F);
  46.         this.setStepSound(soundTypeMetal);
  47.     }
  48.    
  49.     public static BlockPos getFurnacePosition(BlockPos pos) {
  50.        
  51.         return pos;
  52.        
  53.     }
  54.  
  55.     public Item getItemDropped(IBlockState state, Random rand, int fortune)
  56.     {
  57.         return Item.getItemFromBlock(MyBlocks.ice_cream_maker);
  58.     }
  59.  
  60.     public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
  61.     {
  62.         this.setDefaultFacing(worldIn, pos, state);
  63.     }
  64.  
  65.     private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state)
  66.     {
  67.         if (!worldIn.isRemote)
  68.         {
  69.             Block block = worldIn.getBlockState(pos.north()).getBlock();
  70.             Block block1 = worldIn.getBlockState(pos.south()).getBlock();
  71.             Block block2 = worldIn.getBlockState(pos.west()).getBlock();
  72.             Block block3 = worldIn.getBlockState(pos.east()).getBlock();
  73.             EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
  74.             EnumIce ice = (EnumIce) state.getValue(ICE);
  75.  
  76.             if (enumfacing == EnumFacing.NORTH && block.isFullBlock() && !block1.isFullBlock())
  77.             {
  78.                 enumfacing = EnumFacing.SOUTH;
  79.             }
  80.             else if (enumfacing == EnumFacing.SOUTH && block1.isFullBlock() && !block.isFullBlock())
  81.             {
  82.                 enumfacing = EnumFacing.NORTH;
  83.             }
  84.             else if (enumfacing == EnumFacing.WEST && block2.isFullBlock() && !block3.isFullBlock())
  85.             {
  86.                 enumfacing = EnumFacing.EAST;
  87.             }
  88.             else if (enumfacing == EnumFacing.EAST && block3.isFullBlock() && !block2.isFullBlock())
  89.             {
  90.                 enumfacing = EnumFacing.WEST;
  91.             }
  92.  
  93.             worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing).withProperty(ICE, ice), 2);
  94.         }
  95.     }
  96.  
  97.     public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
  98.     {
  99.         if (worldIn.isRemote)
  100.         {
  101.             return true;
  102.         }
  103.         else
  104.         {
  105.            
  106.             TileEntity tileentity = worldIn.getTileEntity(pos);
  107.  
  108.             if (tileentity instanceof TileEntityIceCreamMaker)
  109.             {
  110.                 playerIn.openGui(Chef.instance, MyBlocks.guiID_ice_cream_maker, worldIn, pos.getX(), pos.getY(), pos.getZ());
  111.             }
  112.  
  113.             return true;
  114.         }
  115.     }
  116.  
  117.     public static void setState(int iceAmount, boolean isActive, World worldIn, BlockPos pos)
  118.     {
  119.         IBlockState iblockstate = worldIn.getBlockState(pos);
  120.         TileEntity tileentity = worldIn.getTileEntity(pos);
  121.         keepInventory = true;
  122.  
  123.         if (isActive) {
  124.            
  125.             switch (iceAmount) {
  126.            
  127.             case 1:
  128.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker_on.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE1), 3);
  129.                 break;
  130.             case 2:
  131.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker_on.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE2), 3);
  132.                 break;
  133.             case 3:
  134.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker_on.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE3), 3);
  135.                 break;
  136.             case 4:
  137.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker_on.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE4), 3);
  138.                 break;
  139.             case 5:
  140.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker_on.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE5), 3);
  141.                 break;
  142.             case 6:
  143.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker_on.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE6), 3);
  144.                 break;
  145.             case 7:
  146.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker_on.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE7), 3);
  147.                 break;
  148.             case 8:
  149.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker_on.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE8), 3);
  150.                 break;
  151.             case 9:
  152.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker_on.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE9), 3);
  153.                 break;
  154.             case 10:
  155.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker_on.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE10), 3);
  156.                 break;
  157.             }
  158.            
  159.         } else {
  160.            
  161.             switch (iceAmount) {
  162.            
  163.             case 1:
  164.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE1), 3);
  165.                 break;
  166.             case 2:
  167.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE2), 3);
  168.                 break;
  169.             case 3:
  170.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE3), 3);
  171.                 break;
  172.             case 4:
  173.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE4), 3);
  174.                 break;
  175.             case 5:
  176.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE5), 3);
  177.                 break;
  178.             case 6:
  179.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE6), 3);
  180.                 break;
  181.             case 7:
  182.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE7), 3);
  183.                 break;
  184.             case 8:
  185.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE8), 3);
  186.                 break;
  187.             case 9:
  188.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE9), 3);
  189.                 break;
  190.             case 10:
  191.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE1), 3);
  192.                 break;
  193.             default:
  194.                 worldIn.setBlockState(pos, MyBlocks.ice_cream_maker.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, EnumIce.ICE0), 3);
  195.                 break;
  196.             }
  197.            
  198.         }
  199.  
  200.         keepInventory = false;
  201.  
  202.         if (tileentity != null)
  203.         {
  204.             tileentity.validate();
  205.             worldIn.setTileEntity(pos, tileentity);
  206.         }
  207.     }
  208.  
  209.     public TileEntity createNewTileEntity(World worldIn, int meta)
  210.     {
  211.         return new TileEntityIceCreamMaker();
  212.     }
  213.  
  214.     public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
  215.     {
  216.         EnumIce ice = EnumIce.byMetadata(meta);
  217.        
  218.         EnumFacing enumfacing = (placer == null) ? EnumFacing.NORTH : EnumFacing.fromAngle(placer.rotationYaw);
  219.        
  220.         return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite()).withProperty(ICE, ice);
  221.     }
  222.  
  223.     public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
  224.     {
  225.         worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing().getOpposite()).withProperty(ICE, EnumIce.ICE0), 2);
  226.  
  227.         if (stack.hasDisplayName())
  228.         {
  229.             TileEntity tileentity = worldIn.getTileEntity(pos);
  230.  
  231.             if (tileentity instanceof TileEntityIceCreamMaker)
  232.             {
  233.                 ((TileEntityIceCreamMaker)tileentity).setCustomInventoryName(stack.getDisplayName());
  234.             }
  235.         }
  236.     }
  237.  
  238.     /*
  239.     public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
  240.     {
  241.         if (!keepInventory)
  242.         {
  243.             TileEntity tileentity = worldIn.getTileEntity(pos);
  244.  
  245.             if (tileentity instanceof TileEntityIceCreamMaker)
  246.             {
  247.                 InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityIceCreamMaker)tileentity);
  248.                 worldIn.updateComparatorOutputLevel(pos, this);
  249.             }
  250.         }
  251.  
  252.         super.breakBlock(worldIn, pos, state);
  253.     }
  254.     */
  255.  
  256.     public boolean hasComparatorInputOverride()
  257.     {
  258.         return true;
  259.     }
  260.  
  261.     public int getComparatorInputOverride(World worldIn, BlockPos pos)
  262.     {
  263.         return Container.calcRedstone(worldIn.getTileEntity(pos));
  264.     }
  265.  
  266.     @SideOnly(Side.CLIENT)
  267.     public Item getItem(World worldIn, BlockPos pos)
  268.     {
  269.         return Item.getItemFromBlock(MyBlocks.ice_cream_maker);
  270.     }
  271.  
  272.     public int getRenderType()
  273.     {
  274.         return 3;
  275.     }
  276.  
  277.     @SideOnly(Side.CLIENT)
  278.     public IBlockState getStateForEntityRender(IBlockState state)
  279.     {
  280.         return this.getDefaultState().withProperty(FACING, EnumFacing.SOUTH).withProperty(ICE, 0);
  281.     }
  282.    
  283.     public IBlockState getStateFromMeta(int meta)
  284.     {
  285.         EnumFacing facing = EnumFacing.getHorizontal(meta);
  286.         int icebits = (meta & 0x0c) >> 2;
  287.         EnumIce ice = EnumIce.byMetadata(icebits);
  288.        
  289.         return this.getDefaultState().withProperty(FACING, facing).withProperty(ICE, ice);
  290.     }
  291.  
  292.     public int getMetaFromState(IBlockState state)
  293.     {
  294.        
  295.         EnumFacing facing = (EnumFacing) state.getValue(FACING);
  296.         EnumIce ice = (EnumIce) state.getValue(ICE);
  297.        
  298.         int facingbits = facing.getHorizontalIndex();
  299.         int icebits = ice.getMetadata() << 2;
  300.        
  301.         return facingbits | icebits;
  302.     }
  303.  
  304.     protected BlockState createBlockState()
  305.     {
  306.         return new BlockState(this, new IProperty[] {FACING, ICE});
  307.     }
  308.    
  309.     public static enum EnumIce implements IStringSerializable
  310.     {
  311.       ICE0(0, "ice0"),
  312.       ICE1(1, "ice1"),
  313.       ICE2(2, "ice2"),
  314.       ICE3(3, "ice3"),
  315.       ICE4(4, "ice4"),
  316.       ICE5(5, "ice5"),
  317.       ICE6(6, "ice6"),
  318.       ICE7(7, "ice7"),
  319.       ICE8(8, "ice8"),
  320.       ICE9(9, "ice9"),
  321.       ICE10(10, "ice10");
  322.  
  323.       public int getMetadata()
  324.       {
  325.         return this.meta;
  326.       }
  327.  
  328.       @Override
  329.       public String toString()
  330.       {
  331.         return this.name;
  332.       }
  333.  
  334.       public static EnumIce byMetadata(int meta)
  335.       {
  336.         if (meta < 0 || meta >= META_LOOKUP.length)
  337.         {
  338.           meta = 0;
  339.         }
  340.  
  341.         return META_LOOKUP[meta];
  342.       }
  343.  
  344.       public String getName()
  345.       {
  346.         return this.name;
  347.       }
  348.  
  349.       private final int meta;
  350.       private final String name;
  351.       private static final EnumIce[] META_LOOKUP = new EnumIce[values().length];
  352.  
  353.       private EnumIce(int i_meta, String i_name)
  354.       {
  355.         this.meta = i_meta;
  356.         this.name = i_name;
  357.       }
  358.  
  359.       static
  360.       {
  361.         for (EnumIce ice : values()) {
  362.           META_LOOKUP[ice.getMetadata()] = ice;
  363.       }
  364.     }
  365.   }
  366.    
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement