Advertisement
Guest User

wirelesschestblock

a guest
May 4th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.74 KB | None | 0 0
  1. package com.cclloyd.ccmodpack;
  2.  
  3. import net.minecraft.block.BlockContainer;
  4. import net.minecraft.block.material.Material;
  5. import net.minecraft.block.state.IBlockState;
  6. import net.minecraft.creativetab.CreativeTabs;
  7. import net.minecraft.entity.item.EntityItem;
  8. import net.minecraft.entity.player.EntityPlayer;
  9. import net.minecraft.inventory.IInventory;
  10. import net.minecraft.tileentity.TileEntity;
  11. import net.minecraft.util.BlockPos;
  12. import net.minecraft.util.EnumFacing;
  13. import net.minecraft.util.EnumWorldBlockLayer;
  14. import net.minecraft.world.World;
  15. import net.minecraftforge.fml.relauncher.Side;
  16. import net.minecraftforge.fml.relauncher.SideOnly;
  17.  
  18.  
  19. public class WirelessChest extends BlockContainer {
  20.  
  21.     /**
  22.      * BlockInventoryBasic is a simple inventory capable of storing 9 item stacks. The block itself doesn't do much more
  23.      * then any regular block except create a tile entity when placed, open a gui when right clicked and drop tne
  24.      * inventory's contents when harvested. The actual storage is handled by the tile entity.
  25.      */
  26.    
  27.     public final static String name = "wirelessChest";
  28.    
  29.     public WirelessChest()
  30.     {
  31.         super(Material.rock);
  32.         setUnlocalizedName(CCModpack.MODID + "_" +  name);
  33.         this.setCreativeTab(CreativeTabs.tabRedstone);     // the block will appear on the Blocks tab.
  34.         this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
  35.     }
  36.  
  37.     // Called when the block is placed or loaded client side to get the tile entity for the block
  38.     // Should return a new instance of the tile entity for the block
  39.     @Override
  40.     public TileEntity createNewTileEntity(World worldIn, int meta) {
  41.         return new WirelessChestEntity();
  42.     }
  43.  
  44.     // Called when the block is right clicked
  45.     // In this block it is used to open the blocks gui when right clicked by a player
  46.     @Override
  47.     public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) {
  48.         // Uses the gui handler registered to your mod to open the gui for the given gui id
  49.         // open on the server side only  (not sure why you shouldn't open client side too... vanilla doesn't, so we better not either)
  50.         if (worldIn.isRemote) return true;
  51.  
  52.         playerIn.openGui(CCModpack.instance, WirelessChestGuiHandler.getGuiID(), worldIn, pos.getX(), pos.getY(), pos.getZ());
  53.         return true;
  54.     }
  55.  
  56.     // This is where you can do something when the block is broken. In this case drop the inventory's contents
  57.     @Override
  58.     public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
  59.  
  60.         IInventory inventory = worldIn.getTileEntity(pos) instanceof IInventory ? (IInventory)worldIn.getTileEntity(pos) : null;
  61.  
  62.         if (inventory != null){
  63.             // For each slot in the inventory
  64.             for (int i = 0; i < inventory.getSizeInventory(); i++){
  65.                 // If the slot is not empty
  66.                 if (inventory.getStackInSlot(i) != null)
  67.                 {
  68.                     // Create a new entity item with the item stack in the slot
  69.                     EntityItem item = new EntityItem(worldIn, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, inventory.getStackInSlot(i));
  70.  
  71.                     // Apply some random motion to the item
  72.                     float multiplier = 0.1f;
  73.                     float motionX = worldIn.rand.nextFloat() - 0.5f;
  74.                     float motionY = worldIn.rand.nextFloat() - 0.5f;
  75.                     float motionZ = worldIn.rand.nextFloat() - 0.5f;
  76.  
  77.                     item.motionX = motionX * multiplier;
  78.                     item.motionY = motionY * multiplier;
  79.                     item.motionZ = motionZ * multiplier;
  80.  
  81.                     // Spawn the item in the world
  82.                     worldIn.spawnEntityInWorld(item);
  83.                 }
  84.             }
  85.  
  86.             // Clear the inventory so nothing else (such as another mod) can do anything with the items
  87.             inventory.clear();
  88.         }
  89.  
  90.         // Super MUST be called last because it removes the tile entity
  91.         super.breakBlock(worldIn, pos, state);
  92.     }
  93.  
  94.     //---------------------------------------------------------
  95.  
  96.     // the block will render in the SOLID layer.  See http://greyminecraftcoder.blogspot.co.at/2014/12/block-rendering-18.html for more information.
  97.     @SideOnly(Side.CLIENT)
  98.     public EnumWorldBlockLayer getBlockLayer()
  99.     {
  100.         return EnumWorldBlockLayer.SOLID;
  101.     }
  102.  
  103.     // used by the renderer to control lighting and visibility of other blocks.
  104.     // set to false because this block doesn't fill the entire 1x1x1 space
  105.     @Override
  106.     public boolean isOpaqueCube() {
  107.         return false;
  108.     }
  109.  
  110.     // used by the renderer to control lighting and visibility of other blocks, also by
  111.     // (eg) wall or fence to control whether the fence joins itself to this block
  112.     // set to false because this block doesn't fill the entire 1x1x1 space
  113.     @Override
  114.     public boolean isFullCube() {
  115.         return false;
  116.     }
  117.  
  118.     // render using a BakedModel
  119.     // not strictly required because the default (super method) is 3.
  120.     @Override
  121.     public int getRenderType() {
  122.         return 3;
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement