Advertisement
Guest User

IceCreamMaker.java

a guest
Jun 28th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.29 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.Reference;
  7. import com.chef.mod.init.MyBlocks;
  8. import com.chef.mod.tileentity.TileEntityIceCreamMaker;
  9.  
  10. import cpw.mods.fml.common.network.internal.FMLNetworkHandler;
  11. import cpw.mods.fml.relauncher.Side;
  12. import cpw.mods.fml.relauncher.SideOnly;
  13. import net.minecraft.block.Block;
  14. import net.minecraft.block.BlockContainer;
  15. import net.minecraft.block.ITileEntityProvider;
  16. import net.minecraft.block.material.Material;
  17. import net.minecraft.client.renderer.texture.IIconRegister;
  18. import net.minecraft.entity.EntityLivingBase;
  19. import net.minecraft.entity.item.EntityItem;
  20. import net.minecraft.entity.player.EntityPlayer;
  21. import net.minecraft.item.Item;
  22. import net.minecraft.item.ItemStack;
  23. import net.minecraft.nbt.NBTTagCompound;
  24. import net.minecraft.tileentity.TileEntity;
  25. import net.minecraft.util.IIcon;
  26. import net.minecraft.util.MathHelper;
  27. import net.minecraft.world.IBlockAccess;
  28. import net.minecraft.world.World;
  29.  
  30. public class IceCreamMaker extends BlockContainer {
  31.  
  32.     private final boolean isActive;
  33.  
  34.     @SideOnly(Side.CLIENT)
  35.     private IIcon[] iconFrontOff;
  36.  
  37.     @SideOnly(Side.CLIENT)
  38.     private IIcon[] iconFrontActive;
  39.  
  40.     @SideOnly(Side.CLIENT)
  41.     private IIcon iconTop;
  42.  
  43.     private static boolean keepInventory;
  44.     private Random rand = new Random();
  45.  
  46.     public IceCreamMaker(boolean isActive) {
  47.  
  48.         super(Material.iron);
  49.         this.setTickRandomly(false);
  50.         this.isActive = isActive;
  51.         this.setHarvestLevel("pickaxe", 1);
  52.         this.setStepSound(soundTypeMetal);
  53.         this.setHardness(5.0F);
  54.         this.setResistance(10.0F);
  55.  
  56.     }
  57.  
  58.     @SideOnly(Side.CLIENT)
  59.     public void registerBlockIcons(IIconRegister iconRegister) {
  60.  
  61.         this.blockIcon = iconRegister.registerIcon("minecraft:" + "iron_block");
  62.         this.iconTop = iconRegister.registerIcon("minecraft:" + "iron_block");
  63.         this.iconFrontOff = new IIcon[11];
  64.         this.iconFrontActive = new IIcon[11];
  65.  
  66.         for (int i = 0; i < this.iconFrontActive.length; i++) {
  67.  
  68.             this.iconFrontActive[i] = iconRegister.registerIcon(Reference.MOD_ID + ":" + "IceCreamMaker/ice_cream_maker_front_on_ice" + i);
  69.         }
  70.  
  71.         for (int i = 0; i < this.iconFrontOff.length; i++) {
  72.  
  73.             this.iconFrontOff[i] = iconRegister.registerIcon(Reference.MOD_ID + ":" + "IceCreamMaker/ice_cream_maker_front_ice" + i);
  74.  
  75.         }
  76.     }
  77.  
  78.     @SideOnly(Side.CLIENT)
  79.     public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side) {
  80.  
  81.         int metadata = world.getBlockMetadata(x, y, z);
  82.  
  83.         if (side == 1) {
  84.  
  85.             return this.iconTop;
  86.  
  87.         } else if (side == 0) {
  88.  
  89.             return this.iconTop;
  90.  
  91.         }
  92.  
  93.         TileEntity tile = world.getTileEntity(x, y, z);
  94.         int level = 0;
  95.  
  96.         if (tile instanceof TileEntityIceCreamMaker) {
  97.  
  98.             level = Math.round(((TileEntityIceCreamMaker) tile).scaledIceLevel);
  99.  
  100.         }
  101.  
  102.         if ((metadata == 0 && side == 3) || side == metadata) {
  103.  
  104.             if (this.isActive) {
  105.  
  106.                 return this.iconFrontActive[level];
  107.  
  108.             } else {
  109.  
  110.                 return this.iconFrontOff[level];
  111.  
  112.             }
  113.         }
  114.         return this.blockIcon;
  115.     }
  116.  
  117.     public Item getItemDropped(int i, Random random, int j) {
  118.         return Item.getItemFromBlock(MyBlocks.ice_cream_maker);
  119.     }
  120.  
  121.     public static String getFurnaceName(int x, int y, int z) {
  122.  
  123.         return (x + "/" + y + "/" + z);
  124.  
  125.     }
  126.  
  127.     public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
  128.         if (!world.isRemote) {
  129.             FMLNetworkHandler.openGui(player, Chef.instance, MyBlocks.guiID_ice_cream_maker, world, x, y, z);
  130.         }
  131.  
  132.         return true;
  133.     }
  134.  
  135.     public TileEntity createNewTileEntity(World worldIn, int meta) {
  136.         return new TileEntityIceCreamMaker();
  137.     }
  138.  
  139.     @Override
  140.     public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityLiving, ItemStack itemStack) {
  141.         int metadata = world.getBlockMetadata(x, y, z);
  142.  
  143.         int angle = MathHelper.floor_double(entityLiving.rotationYaw * 4.0F / 360.0F + 0.5D) & 3;
  144.         int change = 0;
  145.  
  146.         switch (angle) {
  147.         case 0:
  148.             change = 3;
  149.             break;
  150.         case 1:
  151.             change = 1;
  152.             break;
  153.         case 2:
  154.             change = 2;
  155.             break;
  156.         case 3:
  157.             change = 0;
  158.             break;
  159.         }
  160.  
  161.         world.setBlockMetadataWithNotify(x, y, z, (metadata & 12) + change, 3);
  162.     }
  163.  
  164.     public static void updateBlockState(boolean isActive, World worldObj, int xCoord, int yCoord, int zCoord) {
  165.         int metadata = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);
  166.  
  167.         TileEntity tileentity = worldObj.getTileEntity(xCoord, yCoord, zCoord);
  168.         keepInventory = true;
  169.  
  170.         if (isActive) {
  171.             worldObj.setBlock(xCoord, yCoord, zCoord, MyBlocks.ice_cream_maker_on);
  172.         } else {
  173.             worldObj.setBlock(xCoord, yCoord, zCoord, MyBlocks.ice_cream_maker);
  174.         }
  175.  
  176.         keepInventory = false;
  177.  
  178.         worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, metadata, 2);
  179.  
  180.         if (tileentity != null) {
  181.             tileentity.validate();
  182.             worldObj.setTileEntity(xCoord, yCoord, zCoord, tileentity);
  183.         }
  184.     }
  185.  
  186.     public void breakBlock(World world, int x, int y, int z, Block oldblock, int oldMetadata) {
  187.         if (!keepInventory) {
  188.             TileEntityIceCreamMaker tileentity = (TileEntityIceCreamMaker) world.getTileEntity(x, y, z);
  189.  
  190.             if (tileentity != null) {
  191.                 for (int i = 0; i < tileentity.getSizeInventory(); i++) {
  192.                     ItemStack itemstack = tileentity.getStackInSlot(i);
  193.  
  194.                     if (itemstack != null) {
  195.                         float f = this.rand.nextFloat() * 0.8F + 0.1F;
  196.                         float f1 = this.rand.nextFloat() * 0.8F + 0.1F;
  197.                         float f2 = this.rand.nextFloat() * 0.8F + 0.1F;
  198.  
  199.                         while (itemstack.stackSize > 0) {
  200.                             int j = this.rand.nextInt(21) + 10;
  201.  
  202.                             if (j > itemstack.stackSize) {
  203.                                 j = itemstack.stackSize;
  204.                             }
  205.  
  206.                             itemstack.stackSize -= j;
  207.  
  208.                             EntityItem item = new EntityItem(world, (double) ((float) x + f), (double) ((float) y + f1), (double) ((float) z + f2),
  209.                                     new ItemStack(itemstack.getItem(), j, itemstack.getItemDamage()));
  210.  
  211.                             if (itemstack.hasTagCompound()) {
  212.                                 item.getEntityItem().setTagCompound((NBTTagCompound) itemstack.getTagCompound().copy());
  213.                             }
  214.  
  215.                             world.spawnEntityInWorld(item);
  216.                         }
  217.                     }
  218.                 }
  219.  
  220.                 world.func_147453_f(x, y, z, oldblock);
  221.             }
  222.         }
  223.  
  224.         super.breakBlock(world, x, y, z, oldblock, oldMetadata);
  225.     }
  226.  
  227.     public Item getItem(World world, int x, int y, int z) {
  228.         return Item.getItemFromBlock(MyBlocks.ice_cream_maker);
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement