Guest User

IceCreamMaker.java

a guest
May 18th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.99 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.client.Minecraft;
  14. import net.minecraft.entity.EntityLivingBase;
  15. import net.minecraft.entity.player.EntityPlayer;
  16. import net.minecraft.inventory.Container;
  17. import net.minecraft.inventory.InventoryHelper;
  18. import net.minecraft.item.Item;
  19. import net.minecraft.item.ItemStack;
  20. import net.minecraft.tileentity.TileEntity;
  21. import net.minecraft.util.BlockPos;
  22. import net.minecraft.util.EnumFacing;
  23. import net.minecraft.util.IStringSerializable;
  24. import net.minecraft.world.IBlockAccess;
  25. import net.minecraft.world.World;
  26. import net.minecraftforge.fml.relauncher.Side;
  27. import net.minecraftforge.fml.relauncher.SideOnly;
  28.  
  29. import com.chef.mod.Chef;
  30. import com.chef.mod.Debugger;
  31. import com.chef.mod.init.MyBlocks;
  32. import com.chef.mod.tileentity.TileEntityIceCreamMaker;
  33.  
  34. public class IceCreamMaker extends BlockContainer {
  35.     public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
  36.     public static final PropertyEnum ICE = PropertyEnum.create("ice", EnumIce.class);
  37.  
  38.     private final boolean isActive;
  39.     private static boolean keepInventory;
  40.  
  41.     public IceCreamMaker(boolean isActive) {
  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.         return Item.getItemFromBlock(MyBlocks.ice_cream_maker);
  58.     }
  59.  
  60.     public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) {
  61.         this.setDefaultFacing(worldIn, pos, state);
  62.     }
  63.  
  64.     private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state) {
  65.         if (!worldIn.isRemote) {
  66.             Block block = worldIn.getBlockState(pos.north()).getBlock();
  67.             Block block1 = worldIn.getBlockState(pos.south()).getBlock();
  68.             Block block2 = worldIn.getBlockState(pos.west()).getBlock();
  69.             Block block3 = worldIn.getBlockState(pos.east()).getBlock();
  70.             EnumFacing enumfacing = (EnumFacing) state.getValue(FACING);
  71.  
  72.             if (enumfacing == EnumFacing.NORTH && block.isFullBlock() && !block1.isFullBlock()) {
  73.                 enumfacing = EnumFacing.SOUTH;
  74.             } else if (enumfacing == EnumFacing.SOUTH && block1.isFullBlock() && !block.isFullBlock()) {
  75.                 enumfacing = EnumFacing.NORTH;
  76.             } else if (enumfacing == EnumFacing.WEST && block2.isFullBlock() && !block3.isFullBlock()) {
  77.                 enumfacing = EnumFacing.EAST;
  78.             } else if (enumfacing == EnumFacing.EAST && block3.isFullBlock() && !block2.isFullBlock()) {
  79.                 enumfacing = EnumFacing.WEST;
  80.             }
  81.  
  82.             worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
  83.         }
  84.     }
  85.  
  86.     public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) {
  87.         if (worldIn.isRemote) {
  88.             return true;
  89.         } else {
  90.  
  91.             TileEntity tileentity = worldIn.getTileEntity(pos);
  92.  
  93.             if (tileentity instanceof TileEntityIceCreamMaker) {
  94.                 playerIn.openGui(Chef.instance, MyBlocks.guiID_ice_cream_maker, worldIn, pos.getX(), pos.getY(), pos.getZ());
  95.             }
  96.  
  97.             return true;
  98.         }
  99.     }
  100.  
  101.     public static void setState(boolean isActive, World worldIn, BlockPos pos) {
  102.         IBlockState iblockstate = worldIn.getBlockState(pos);
  103.         TileEntity tileentity = worldIn.getTileEntity(pos);
  104.         keepInventory = true;
  105.         Debugger.log("Get Ice Amount: " + getIceAmount());
  106.  
  107.         if (getIceAmount() == 0) {
  108.  
  109.             setIceState(EnumIce.ICE0);
  110.  
  111.         } else if (getIceAmount() > 0 && getIceAmount() <= 1000) {
  112.  
  113.             setIceState(EnumIce.ICE1);
  114.  
  115.         } else if (getIceAmount() > 1000 && getIceAmount() <= 2000) {
  116.  
  117.             setIceState(EnumIce.ICE2);
  118.  
  119.         } else if (getIceAmount() > 2000 && getIceAmount() <= 3000) {
  120.  
  121.             setIceState(EnumIce.ICE3);
  122.  
  123.         } else if (getIceAmount() > 3000 && getIceAmount() <= 4000) {
  124.  
  125.             setIceState(EnumIce.ICE4);
  126.  
  127.         } else if (getIceAmount() > 4000 && getIceAmount() <= 5000) {
  128.  
  129.             setIceState(EnumIce.ICE5);
  130.  
  131.         } else if (getIceAmount() > 5000 && getIceAmount() <= 6000) {
  132.  
  133.             setIceState(EnumIce.ICE6);
  134.  
  135.         } else if (getIceAmount() > 6000 && getIceAmount() <= 7000) {
  136.  
  137.             setIceState(EnumIce.ICE7);
  138.  
  139.         } else if (getIceAmount() > 7000 && getIceAmount() <= 8000) {
  140.  
  141.             setIceState(EnumIce.ICE8);
  142.  
  143.         } else if (getIceAmount() > 8000 && getIceAmount() < 9950) {
  144.  
  145.             setIceState(EnumIce.ICE9);
  146.  
  147.         } else if (getIceAmount() >= 9950) {
  148.  
  149.             setIceState(EnumIce.ICE10);
  150.  
  151.         }
  152.        
  153.         if (isActive) {
  154.  
  155.             worldIn.setBlockState(pos, MyBlocks.ice_cream_maker_on.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, getIceState()), 3);
  156.  
  157.         } else {
  158.  
  159.             worldIn.setBlockState(pos, MyBlocks.ice_cream_maker.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, getIceState()), 3);
  160.  
  161.         }
  162.  
  163.         keepInventory = false;
  164.  
  165.         if (tileentity != null) {
  166.             tileentity.validate();
  167.             worldIn.setTileEntity(pos, tileentity);
  168.         }
  169.     }
  170.  
  171.     public TileEntity createNewTileEntity(World worldIn, int meta) {
  172.        
  173.         return new TileEntityIceCreamMaker();
  174.     }
  175.  
  176.     public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) {
  177.  
  178.         EnumFacing enumfacing = (placer == null) ? EnumFacing.NORTH : EnumFacing.fromAngle(placer.rotationYaw);
  179.  
  180.         return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite()).withProperty(ICE, EnumIce.ICE0);
  181.     }
  182.  
  183.     public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
  184.         worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing().getOpposite()).withProperty(ICE, EnumIce.ICE0), 2);
  185.  
  186.         if (stack.hasDisplayName()) {
  187.             TileEntity tileentity = worldIn.getTileEntity(pos);
  188.  
  189.             if (tileentity instanceof TileEntityIceCreamMaker) {
  190.                 ((TileEntityIceCreamMaker) tileentity).setCustomInventoryName(stack.getDisplayName());
  191.             }
  192.         }
  193.     }
  194.  
  195.     public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
  196.         if (!keepInventory) {
  197.             TileEntity tileentity = worldIn.getTileEntity(pos);
  198.  
  199.             if (tileentity instanceof TileEntityIceCreamMaker) {
  200.                 InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityIceCreamMaker) tileentity);
  201.                 worldIn.updateComparatorOutputLevel(pos, this);
  202.             }
  203.         }
  204.  
  205.         super.breakBlock(worldIn, pos, state);
  206.     }
  207.  
  208.     public boolean hasComparatorInputOverride() {
  209.         return true;
  210.     }
  211.  
  212.     public int getComparatorInputOverride(World worldIn, BlockPos pos) {
  213.         return Container.calcRedstone(worldIn.getTileEntity(pos));
  214.     }
  215.  
  216.     @SideOnly(Side.CLIENT)
  217.     public Item getItem(World worldIn, BlockPos pos) {
  218.         return Item.getItemFromBlock(MyBlocks.ice_cream_maker);
  219.     }
  220.  
  221.     public int getRenderType() {
  222.         return 3;
  223.     }
  224.  
  225.     @SideOnly(Side.CLIENT)
  226.     public IBlockState getStateForEntityRender(IBlockState state) {
  227.  
  228.         return this.getDefaultState().withProperty(FACING, EnumFacing.SOUTH).withProperty(ICE, getIceState());
  229.     }
  230.  
  231.     /*
  232.      * public IBlockState getStateFromMeta(int meta) { EnumFacing facing =
  233.      * EnumFacing.getHorizontal(meta); int icebits = (meta & 0x0c) >> 2; //int
  234.      * icebits = 0; EnumIce ice = EnumIce.byMetadata(icebits);
  235.      *
  236.      * return this.getDefaultState().withProperty(FACING,
  237.      * facing).withProperty(ICE, ice); }
  238.      */
  239.  
  240.     public int getMetaFromState(IBlockState state) {
  241.         return 0;
  242.     }
  243.  
  244.     /**
  245.      * Get the actual Block state of this Block at the given position. This
  246.      * applies properties not visible in the metadata, such as fence
  247.      * connections.
  248.      */
  249.     public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
  250.        
  251.         Debugger.log("Ice-State in IceCreamMaker.java: " + getIceState());
  252.        
  253.         EnumFacing facing = (EnumFacing) state.getValue(FACING);
  254.         return state.withProperty(FACING, facing).withProperty(ICE, getIceState());
  255.        
  256.     }
  257.  
  258.     protected BlockState createBlockState() {
  259.        
  260.         return new BlockState(this, new IProperty[] { FACING, ICE });
  261.     }
  262.  
  263.     public static enum EnumIce implements IStringSerializable {
  264.         ICE0(0, "ice0"), ICE1(1, "ice1"), ICE2(2, "ice2"), ICE3(3, "ice3"), ICE4(4, "ice4"), ICE5(5, "ice5"), ICE6(6, "ice6"), ICE7(7, "ice7"), ICE8(8, "ice8"), ICE9(9, "ice9"), ICE10(10, "ice10");
  265.  
  266.         @Override
  267.         public String toString() {
  268.             return this.name;
  269.         }
  270.  
  271.         public String getName() {
  272.             return this.name;
  273.         }
  274.  
  275.         private final String name;
  276.  
  277.         private EnumIce(int i_meta, String i_name) {
  278.             this.name = i_name;
  279.         }
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment