Advertisement
Raevox

BlockVendingMachine

Feb 22nd, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. import com.skcraft.creditvendor.CreditVendor;
  2. import com.skcraft.creditvendor.tileentity.TileEntityVendingMachine;
  3. import cpw.mods.fml.common.network.FMLNetworkHandler;
  4. import net.minecraft.block.Block;
  5. import net.minecraft.block.BlockContainer;
  6. import net.minecraft.block.material.Material;
  7. import net.minecraft.entity.item.EntityItem;
  8. import net.minecraft.entity.player.EntityPlayer;
  9. import net.minecraft.inventory.IInventory;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.tileentity.TileEntity;
  12. import net.minecraft.world.World;
  13.  
  14. public class BlockVendingMachine extends BlockContainer {
  15.     public BlockVendingMachine( int id ) {
  16.         super( id, Material.iron );
  17.         setCreativeTab( CreditVendor.cVendorTab );
  18.         setHardness( 0.6F );
  19.         setResistance( 6000000.0F );
  20.         setBlockUnbreakable();
  21.         setStepSound( Block.soundMetalFootstep );
  22.     }
  23.  
  24.     @Override
  25.     public TileEntity createNewTileEntity( World world ) {
  26.         return new TileEntityVendingMachine();
  27.     }
  28.  
  29.     @Override
  30.     public boolean onBlockActivated( World world, int x, int y, int z, EntityPlayer entityPlayer, int side, float hitX, float hitY, float hitZ ) {
  31.         if( !world.isRemote ) {
  32.             FMLNetworkHandler.openGui( entityPlayer, CreditVendor.instance, 0, world, x, y, z );
  33.         }
  34.         return true;
  35.     }
  36.  
  37.     @Override
  38.     public void breakBlock( World world, int x, int y, int z, int id, int metadata ) {
  39.         TileEntity tileEntity = world.getBlockTileEntity( x, y, z );
  40.         if( tileEntity != null && tileEntity instanceof IInventory ) {
  41.             IInventory inventory = ( IInventory )tileEntity;
  42.             for( int slot = 0; slot < inventory.getSizeInventory(); slot++ ) {
  43.                 ItemStack stack = inventory.getStackInSlotOnClosing( slot );
  44.                 if( stack != null ) {
  45.                     float spawnX = x + world.rand.nextFloat();
  46.                     float spawnY = y + world.rand.nextFloat();
  47.                     float spawnZ = z + world.rand.nextFloat();
  48.  
  49.                     EntityItem droppedItem = new EntityItem( world, spawnX, spawnY, spawnZ, stack );
  50.  
  51.                     float multiple = 0.05F;
  52.                     droppedItem.motionX = ( -0.5F + world.rand.nextFloat() ) * multiple;
  53.                     droppedItem.motionY = ( 4F + world.rand.nextFloat() ) * multiple;
  54.                     droppedItem.motionZ = ( -0.5F + world.rand.nextFloat() ) * multiple;
  55.  
  56.                     world.spawnEntityInWorld( droppedItem );
  57.                 }
  58.             }
  59.         }
  60.         super.breakBlock( world, x, y, z, id, metadata );
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement