Advertisement
Guest User

IceCreamMaker.java

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