Guest User

IceCreamMaker.java

a guest
May 17th, 2015
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.40 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.     public static int iceAmount = 0;
  41.    
  42.     public static int getIceAmount() {
  43.        
  44.         return iceAmount;
  45.     }
  46.  
  47.     public static void setIceAmount(int iceAmount) {
  48.        
  49.         IceCreamMaker.iceAmount = iceAmount;
  50.     }
  51.  
  52.     public static EnumIce iceState = EnumIce.ICE0;
  53.  
  54.     public IceCreamMaker(boolean isActive) {
  55.         super(Material.iron);
  56.         this.isActive = isActive;
  57.         this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(ICE, EnumIce.ICE0));
  58.         this.setHardness(5.0F);
  59.         this.setResistance(10.0F);
  60.         this.setStepSound(soundTypeMetal);
  61.     }
  62.  
  63.     public static BlockPos getFurnacePosition(BlockPos pos) {
  64.  
  65.         return pos;
  66.  
  67.     }
  68.  
  69.     public Item getItemDropped(IBlockState state, Random rand, int fortune) {
  70.         return Item.getItemFromBlock(MyBlocks.ice_cream_maker);
  71.     }
  72.  
  73.     public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) {
  74.         this.setDefaultFacing(worldIn, pos, state);
  75.     }
  76.  
  77.     private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state) {
  78.         if (!worldIn.isRemote) {
  79.             Block block = worldIn.getBlockState(pos.north()).getBlock();
  80.             Block block1 = worldIn.getBlockState(pos.south()).getBlock();
  81.             Block block2 = worldIn.getBlockState(pos.west()).getBlock();
  82.             Block block3 = worldIn.getBlockState(pos.east()).getBlock();
  83.             EnumFacing enumfacing = (EnumFacing) state.getValue(FACING);
  84.  
  85.             if (enumfacing == EnumFacing.NORTH && block.isFullBlock() && !block1.isFullBlock()) {
  86.                 enumfacing = EnumFacing.SOUTH;
  87.             } else if (enumfacing == EnumFacing.SOUTH && block1.isFullBlock() && !block.isFullBlock()) {
  88.                 enumfacing = EnumFacing.NORTH;
  89.             } else if (enumfacing == EnumFacing.WEST && block2.isFullBlock() && !block3.isFullBlock()) {
  90.                 enumfacing = EnumFacing.EAST;
  91.             } else if (enumfacing == EnumFacing.EAST && block3.isFullBlock() && !block2.isFullBlock()) {
  92.                 enumfacing = EnumFacing.WEST;
  93.             }
  94.  
  95.             worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
  96.         }
  97.     }
  98.  
  99.     public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) {
  100.         if (worldIn.isRemote) {
  101.             return true;
  102.         } else {
  103.  
  104.             TileEntity tileentity = worldIn.getTileEntity(pos);
  105.  
  106.             if (tileentity instanceof TileEntityIceCreamMaker) {
  107.                 playerIn.openGui(Chef.instance, MyBlocks.guiID_ice_cream_maker, worldIn, pos.getX(), pos.getY(), pos.getZ());
  108.             }
  109.  
  110.             return true;
  111.         }
  112.     }
  113.  
  114.     public static EnumIce getIceState() {
  115.  
  116.         return iceState;
  117.     }
  118.  
  119.     public static void setIceState(EnumIce iceState) {
  120.  
  121.         IceCreamMaker.iceState = iceState;
  122.     }
  123.  
  124.     public static void setState(boolean isActive, World worldIn, BlockPos pos) {
  125.         IBlockState iblockstate = worldIn.getBlockState(pos);
  126.         TileEntity tileentity = worldIn.getTileEntity(pos);
  127.         keepInventory = true;
  128.         Debugger.log("Get Ice Amount: " + getIceAmount());
  129.  
  130.         if (getIceAmount() == 0) {
  131.  
  132.             setIceState(EnumIce.ICE0);
  133.  
  134.         } else if (getIceAmount() > 0 && getIceAmount() <= 1000) {
  135.  
  136.             setIceState(EnumIce.ICE1);
  137.  
  138.         } else if (getIceAmount() > 1000 && getIceAmount() <= 2000) {
  139.  
  140.             setIceState(EnumIce.ICE2);
  141.  
  142.         } else if (getIceAmount() > 2000 && getIceAmount() <= 3000) {
  143.  
  144.             setIceState(EnumIce.ICE3);
  145.  
  146.         } else if (getIceAmount() > 3000 && getIceAmount() <= 4000) {
  147.  
  148.             setIceState(EnumIce.ICE4);
  149.  
  150.         } else if (getIceAmount() > 4000 && getIceAmount() <= 5000) {
  151.  
  152.             setIceState(EnumIce.ICE5);
  153.  
  154.         } else if (getIceAmount() > 5000 && getIceAmount() <= 6000) {
  155.  
  156.             setIceState(EnumIce.ICE6);
  157.  
  158.         } else if (getIceAmount() > 6000 && getIceAmount() <= 7000) {
  159.  
  160.             setIceState(EnumIce.ICE7);
  161.  
  162.         } else if (getIceAmount() > 7000 && getIceAmount() <= 8000) {
  163.  
  164.             setIceState(EnumIce.ICE8);
  165.  
  166.         } else if (getIceAmount() > 8000 && getIceAmount() < 9950) {
  167.  
  168.             setIceState(EnumIce.ICE9);
  169.  
  170.         } else if (getIceAmount() >= 9950) {
  171.  
  172.             setIceState(EnumIce.ICE10);
  173.  
  174.         }
  175.        
  176.         if (isActive) {
  177.  
  178.             worldIn.setBlockState(pos, MyBlocks.ice_cream_maker_on.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, getIceState()), 3);
  179.  
  180.         } else {
  181.  
  182.             worldIn.setBlockState(pos, MyBlocks.ice_cream_maker.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)).withProperty(ICE, getIceState()), 3);
  183.  
  184.         }
  185.  
  186.         keepInventory = false;
  187.  
  188.         if (tileentity != null) {
  189.             tileentity.validate();
  190.             worldIn.setTileEntity(pos, tileentity);
  191.         }
  192.     }
  193.  
  194.     public TileEntity createNewTileEntity(World worldIn, int meta) {
  195.        
  196.         return new TileEntityIceCreamMaker();
  197.     }
  198.  
  199.     public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) {
  200.  
  201.         EnumFacing enumfacing = (placer == null) ? EnumFacing.NORTH : EnumFacing.fromAngle(placer.rotationYaw);
  202.  
  203.         return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite()).withProperty(ICE, EnumIce.ICE0);
  204.     }
  205.  
  206.     public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
  207.         worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing().getOpposite()).withProperty(ICE, EnumIce.ICE0), 2);
  208.  
  209.         if (stack.hasDisplayName()) {
  210.             TileEntity tileentity = worldIn.getTileEntity(pos);
  211.  
  212.             if (tileentity instanceof TileEntityIceCreamMaker) {
  213.                 ((TileEntityIceCreamMaker) tileentity).setCustomInventoryName(stack.getDisplayName());
  214.             }
  215.         }
  216.     }
  217.  
  218.     public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
  219.         if (!keepInventory) {
  220.             TileEntity tileentity = worldIn.getTileEntity(pos);
  221.  
  222.             if (tileentity instanceof TileEntityIceCreamMaker) {
  223.                 InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityIceCreamMaker) tileentity);
  224.                 worldIn.updateComparatorOutputLevel(pos, this);
  225.             }
  226.         }
  227.  
  228.         super.breakBlock(worldIn, pos, state);
  229.     }
  230.  
  231.     public boolean hasComparatorInputOverride() {
  232.         return true;
  233.     }
  234.  
  235.     public int getComparatorInputOverride(World worldIn, BlockPos pos) {
  236.         return Container.calcRedstone(worldIn.getTileEntity(pos));
  237.     }
  238.  
  239.     @SideOnly(Side.CLIENT)
  240.     public Item getItem(World worldIn, BlockPos pos) {
  241.         return Item.getItemFromBlock(MyBlocks.ice_cream_maker);
  242.     }
  243.  
  244.     public int getRenderType() {
  245.         return 3;
  246.     }
  247.  
  248.     @SideOnly(Side.CLIENT)
  249.     public IBlockState getStateForEntityRender(IBlockState state) {
  250.  
  251.         return this.getDefaultState().withProperty(FACING, EnumFacing.SOUTH).withProperty(ICE, getIceState());
  252.     }
  253.  
  254.     /*
  255.      * public IBlockState getStateFromMeta(int meta) { EnumFacing facing =
  256.      * EnumFacing.getHorizontal(meta); int icebits = (meta & 0x0c) >> 2; //int
  257.      * icebits = 0; EnumIce ice = EnumIce.byMetadata(icebits);
  258.      *
  259.      * return this.getDefaultState().withProperty(FACING,
  260.      * facing).withProperty(ICE, ice); }
  261.      */
  262.  
  263.     public int getMetaFromState(IBlockState state) {
  264.         return 0;
  265.     }
  266.  
  267.     /**
  268.      * Get the actual Block state of this Block at the given position. This
  269.      * applies properties not visible in the metadata, such as fence
  270.      * connections.
  271.      */
  272.     public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
  273.        
  274.         Debugger.log("Ice-State in IceCreamMaker.java: " + getIceState());
  275.        
  276.         EnumFacing facing = (EnumFacing) state.getValue(FACING);
  277.         return state.withProperty(FACING, facing).withProperty(ICE, getIceState());
  278.        
  279.     }
  280.  
  281.     protected BlockState createBlockState() {
  282.        
  283.         return new BlockState(this, new IProperty[] { FACING, ICE });
  284.     }
  285.  
  286.     public static enum EnumIce implements IStringSerializable {
  287.         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");
  288.  
  289.         @Override
  290.         public String toString() {
  291.             return this.name;
  292.         }
  293.  
  294.         public String getName() {
  295.             return this.name;
  296.         }
  297.  
  298.         private final String name;
  299.  
  300.         private EnumIce(int i_meta, String i_name) {
  301.             this.name = i_name;
  302.         }
  303.     }
  304. }
Advertisement
Add Comment
Please, Sign In to add comment