Guest User

Untitled

a guest
Sep 9th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. public class BlockMaterializer extends Block {
  2.    
  3.     public static final PropertyInteger LEVEL = PropertyInteger.create("level", 0, 3);
  4.    
  5.     public BlockMaterializer(Material material) {
  6.         super(material);   
  7.         this.setDefaultState(this.blockState.getBaseState().withProperty(LEVEL, 0));
  8.         setCreativeTab(Soulcraft.CreativeTab);
  9.         setRegistryName("materializer");   
  10.     }  
  11.    
  12.     @Override
  13.     public boolean hasTileEntity(IBlockState state) {
  14.         return true;
  15.     }
  16.  
  17.     @Override
  18.     public TileEntity createTileEntity(World world, IBlockState state) {
  19.         return new TileEntityMaterializer(EnumSoulType.EMPTY, 0);
  20.     }
  21.    
  22.     public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {     
  23.         ItemStack itemstack = player.getHeldItem(hand);
  24.         TileEntityMaterializer tileentity = getTileEntity(world, pos);
  25.        
  26.         if (tileentity == null) {
  27.             return false;
  28.         }
  29.             int level = tileentity.getLevel();     
  30.            
  31.             if(level < 3) {
  32.                 if(!(itemstack.getItem() instanceof ItemSoulBase)) {
  33.                     return false;
  34.                 }      
  35.                
  36.                 ItemSoulBase item = (ItemSoulBase)itemstack.getItem();
  37.                
  38.                 if(level == 0) {
  39.                     tileentity.setSoulType(item.getSoulType());
  40.                     tileentity.setLevel(1);
  41.                 }
  42.                 else {
  43.                     if(tileentity.getSoulType() == item.getSoulType()) {
  44.                         tileentity.setLevel(level + 1);
  45.                     }
  46.                     else {
  47.                         return false;
  48.                     }
  49.                 }
  50.                
  51.                 if (!player.capabilities.isCreativeMode)
  52.                 {
  53.                     player.setHeldItem(hand, new ItemStack(ModItems.SOUL_BOTTLE));
  54.                 }
  55.             }
  56.             else {
  57.                 ItemStack result = new ItemStack(tileentity.getSoulType().getResult());
  58.                
  59.                 if (itemstack.isEmpty())
  60.                 {
  61.                     player.setHeldItem(hand, result);
  62.                 }
  63.                 else if (!player.addItemStackToInventory(result))
  64.                 {
  65.                     player.dropItem(result, false);
  66.                 }
  67.                
  68.                 tileentity.setLevel(0);
  69.                 tileentity.setSoulType(EnumSoulType.EMPTY);
  70.             }
  71.              
  72.             tileentity.markDirty();
  73.             world.notifyBlockUpdate(pos, state, state, 2);
  74.             return true;
  75.     }
  76.    
  77.     @Nullable
  78.     private TileEntityMaterializer getTileEntity(World worldIn, BlockPos pos) {
  79.         TileEntity tileentity = worldIn.getTileEntity(pos);
  80.         return tileentity instanceof TileEntityMaterializer ? (TileEntityMaterializer)tileentity : null;
  81.     }
  82.    
  83.     public BlockStateContainer createBlockState() {
  84.         return new BlockStateContainer(this, LEVEL);
  85.     }
  86.    
  87.     public IBlockState getStateFromMeta(int meta) {
  88.         return this.getDefaultState().withProperty(LEVEL, meta);
  89.     }
  90.  
  91.     public int getMetaFromState(IBlockState state) {
  92.         return state.getValue(LEVEL);
  93.     }
  94.  
  95.     public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {     
  96.         int level = 0;
  97.         TileEntity tileentity = worldIn.getTileEntity(pos);
  98.        
  99.         if(tileentity instanceof TileEntityMaterializer) {
  100.             TileEntityMaterializer tileentitym = (TileEntityMaterializer)worldIn.getTileEntity(pos);
  101.            
  102.             level = tileentitym.getLevel();
  103.         }
  104.        
  105.         return state.withProperty(LEVEL, level);
  106.     }
  107.    
  108.     @SideOnly(Side.CLIENT)
  109.     public BlockRenderLayer getBlockLayer()
  110.     {
  111.         return BlockRenderLayer.CUTOUT;
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment