Guest User

CookingFurnace

a guest
Feb 21st, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.62 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.tileentity.TileEntityCookingFurnace;
  8.  
  9. import net.minecraft.block.Block;
  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.PropertyDirection;
  14. import net.minecraft.block.state.BlockState;
  15. import net.minecraft.block.state.IBlockState;
  16. import net.minecraft.entity.EntityLivingBase;
  17. import net.minecraft.entity.player.EntityPlayer;
  18. import net.minecraft.init.Blocks;
  19. import net.minecraft.inventory.Container;
  20. import net.minecraft.inventory.InventoryHelper;
  21. import net.minecraft.item.Item;
  22. import net.minecraft.item.ItemStack;
  23. import net.minecraft.tileentity.TileEntity;
  24. import net.minecraft.tileentity.TileEntityFurnace;
  25. import net.minecraft.util.BlockPos;
  26. import net.minecraft.util.EnumFacing;
  27. import net.minecraft.util.EnumParticleTypes;
  28. import net.minecraft.world.World;
  29. import net.minecraftforge.fml.relauncher.Side;
  30. import net.minecraftforge.fml.relauncher.SideOnly;
  31.  
  32. public class CookingFurnace extends BlockContainer
  33. {
  34.     public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
  35.     private final boolean isBurning;
  36.     private static boolean keepInventory;
  37.  
  38.     public CookingFurnace(boolean isBurning)
  39.     {
  40.         super(Material.rock);
  41.         this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
  42.         this.isBurning = isBurning;
  43.         this.setCreativeTab(Chef.tabChef);
  44.         this.setHardness(3.5F);
  45.         this.setStepSound(soundTypePiston);
  46.     }
  47.  
  48.     public Item getItemDropped(IBlockState state, Random rand, int fortune)
  49.     {
  50.         return Item.getItemFromBlock(MyBlocks.cooking_furnace);
  51.     }
  52.  
  53.     public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
  54.     {
  55.         this.setDefaultFacing(worldIn, pos, state);
  56.     }
  57.  
  58.     private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state)
  59.     {
  60.         if (!worldIn.isRemote)
  61.         {
  62.             Block block = worldIn.getBlockState(pos.north()).getBlock();
  63.             Block block1 = worldIn.getBlockState(pos.south()).getBlock();
  64.             Block block2 = worldIn.getBlockState(pos.west()).getBlock();
  65.             Block block3 = worldIn.getBlockState(pos.east()).getBlock();
  66.             EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
  67.  
  68.             if (enumfacing == EnumFacing.NORTH && block.isFullBlock() && !block1.isFullBlock())
  69.             {
  70.                 enumfacing = EnumFacing.SOUTH;
  71.             }
  72.             else if (enumfacing == EnumFacing.SOUTH && block1.isFullBlock() && !block.isFullBlock())
  73.             {
  74.                 enumfacing = EnumFacing.NORTH;
  75.             }
  76.             else if (enumfacing == EnumFacing.WEST && block2.isFullBlock() && !block3.isFullBlock())
  77.             {
  78.                 enumfacing = EnumFacing.EAST;
  79.             }
  80.             else if (enumfacing == EnumFacing.EAST && block3.isFullBlock() && !block2.isFullBlock())
  81.             {
  82.                 enumfacing = EnumFacing.WEST;
  83.             }
  84.  
  85.             worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
  86.         }
  87.     }
  88.  
  89.     @SideOnly(Side.CLIENT)
  90.     public void randomDisplayTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
  91.     {
  92.         if (this.isBurning)
  93.         {
  94.             EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
  95.             double d0 = (double)pos.getX() + 0.5D;
  96.             double d1 = (double)pos.getY() + rand.nextDouble() * 6.0D / 16.0D;
  97.             double d2 = (double)pos.getZ() + 0.5D;
  98.             double d3 = 0.52D;
  99.             double d4 = rand.nextDouble() * 0.6D - 0.3D;
  100.  
  101.             switch (CookingFurnace.SwitchEnumFacing.FACING_LOOKUP[enumfacing.ordinal()])
  102.             {
  103.                 case 1:
  104.                     worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 - d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D, new int[0]);
  105.                     worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 - d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D, new int[0]);
  106.                     break;
  107.                 case 2:
  108.                     worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D, new int[0]);
  109.                     worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D, new int[0]);
  110.                     break;
  111.                 case 3:
  112.                     worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 - d3, 0.0D, 0.0D, 0.0D, new int[0]);
  113.                     worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + d4, d1, d2 - d3, 0.0D, 0.0D, 0.0D, new int[0]);
  114.                     break;
  115.                 case 4:
  116.                     worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 + d3, 0.0D, 0.0D, 0.0D, new int[0]);
  117.                     worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + d4, d1, d2 + d3, 0.0D, 0.0D, 0.0D, new int[0]);
  118.             }
  119.         }
  120.     }
  121.  
  122.     public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
  123.     {
  124.         if (worldIn.isRemote)
  125.         {
  126.             return true;
  127.         }
  128.         else
  129.         {
  130.             TileEntity tileentity = worldIn.getTileEntity(pos);
  131.  
  132.             if (tileentity instanceof TileEntityCookingFurnace)
  133.             {
  134.                 playerIn.displayGUIChest((TileEntityCookingFurnace)tileentity);
  135.             }
  136.  
  137.             return true;
  138.         }
  139.     }
  140.  
  141.     public static void setState(boolean active, World worldIn, BlockPos pos)
  142.     {
  143.         IBlockState iblockstate = worldIn.getBlockState(pos);
  144.         TileEntity tileentity = worldIn.getTileEntity(pos);
  145.         keepInventory = true;
  146.  
  147.         if (active)
  148.         {
  149.             worldIn.setBlockState(pos, MyBlocks.cooking_furnace_on.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
  150.             worldIn.setBlockState(pos, MyBlocks.cooking_furnace_on.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
  151.         }
  152.         else
  153.         {
  154.             worldIn.setBlockState(pos, MyBlocks.cooking_furnace.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
  155.             worldIn.setBlockState(pos, MyBlocks.cooking_furnace.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
  156.         }
  157.  
  158.         keepInventory = false;
  159.  
  160.         if (tileentity != null)
  161.         {
  162.             tileentity.validate();
  163.             worldIn.setTileEntity(pos, tileentity);
  164.         }
  165.     }
  166.  
  167.     public TileEntity createNewTileEntity(World worldIn, int meta)
  168.     {
  169.         return new TileEntityCookingFurnace();
  170.     }
  171.  
  172.     public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
  173.     {
  174.         return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
  175.     }
  176.  
  177.     public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
  178.     {
  179.         worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing().getOpposite()), 2);
  180.  
  181.         if (stack.hasDisplayName())
  182.         {
  183.             TileEntity tileentity = worldIn.getTileEntity(pos);
  184.  
  185.             if (tileentity instanceof TileEntityCookingFurnace)
  186.             {
  187.                 ((TileEntityCookingFurnace)tileentity).setCustomInventoryName(stack.getDisplayName());
  188.             }
  189.         }
  190.     }
  191.  
  192.     public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
  193.     {
  194.         if (!keepInventory)
  195.         {
  196.             TileEntity tileentity = worldIn.getTileEntity(pos);
  197.  
  198.             if (tileentity instanceof TileEntityCookingFurnace)
  199.             {
  200.                 InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityCookingFurnace)tileentity);
  201.                 worldIn.updateComparatorOutputLevel(pos, this);
  202.             }
  203.         }
  204.  
  205.         super.breakBlock(worldIn, pos, state);
  206.     }
  207.  
  208.     public boolean hasComparatorInputOverride()
  209.     {
  210.         return true;
  211.     }
  212.  
  213.     public int getComparatorInputOverride(World worldIn, BlockPos pos)
  214.     {
  215.         return Container.calcRedstone(worldIn.getTileEntity(pos));
  216.     }
  217.  
  218.     @SideOnly(Side.CLIENT)
  219.     public Item getItem(World worldIn, BlockPos pos)
  220.     {
  221.         return Item.getItemFromBlock(MyBlocks.cooking_furnace);
  222.     }
  223.  
  224.     public int getRenderType()
  225.     {
  226.         return 3;
  227.     }
  228.  
  229.     @SideOnly(Side.CLIENT)
  230.     public IBlockState getStateForEntityRender(IBlockState state)
  231.     {
  232.         return this.getDefaultState().withProperty(FACING, EnumFacing.SOUTH);
  233.     }
  234.  
  235.     public IBlockState getStateFromMeta(int meta)
  236.     {
  237.         EnumFacing enumfacing = EnumFacing.getFront(meta);
  238.  
  239.         if (enumfacing.getAxis() == EnumFacing.Axis.Y)
  240.         {
  241.             enumfacing = EnumFacing.NORTH;
  242.         }
  243.  
  244.         return this.getDefaultState().withProperty(FACING, enumfacing);
  245.     }
  246.  
  247.     public int getMetaFromState(IBlockState state)
  248.     {
  249.         return ((EnumFacing)state.getValue(FACING)).getIndex();
  250.     }
  251.  
  252.     protected BlockState createBlockState()
  253.     {
  254.         return new BlockState(this, new IProperty[] {FACING});
  255.     }
  256.  
  257.     @SideOnly(Side.CLIENT)
  258.  
  259.     static final class SwitchEnumFacing
  260.         {
  261.             static final int[] FACING_LOOKUP = new int[EnumFacing.values().length];
  262.             private static final String __OBFID = "CL_00002111";
  263.  
  264.             static
  265.             {
  266.                 try
  267.                 {
  268.                     FACING_LOOKUP[EnumFacing.WEST.ordinal()] = 1;
  269.                 }
  270.                 catch (NoSuchFieldError var4)
  271.                 {
  272.                     ;
  273.                 }
  274.  
  275.                 try
  276.                 {
  277.                     FACING_LOOKUP[EnumFacing.EAST.ordinal()] = 2;
  278.                 }
  279.                 catch (NoSuchFieldError var3)
  280.                 {
  281.                     ;
  282.                 }
  283.  
  284.                 try
  285.                 {
  286.                     FACING_LOOKUP[EnumFacing.NORTH.ordinal()] = 3;
  287.                 }
  288.                 catch (NoSuchFieldError var2)
  289.                 {
  290.                     ;
  291.                 }
  292.  
  293.                 try
  294.                 {
  295.                     FACING_LOOKUP[EnumFacing.SOUTH.ordinal()] = 4;
  296.                 }
  297.                 catch (NoSuchFieldError var1)
  298.                 {
  299.                     ;
  300.                 }
  301.             }
  302.         }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment