Guest User

IceCreamMaker.java

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