Advertisement
Guest User

BlockFancyBed

a guest
Dec 11th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.82 KB | None | 0 0
  1. public class BlockFancyBed extends BlockBed{
  2.    
  3.     public static final PropertyEnum<EnumDyeColor> COLOR = PropertyEnum.<EnumDyeColor>create("color", EnumDyeColor.class);
  4.    
  5.     public BlockFancyBed(String name)
  6.     {
  7.         setUnlocalizedName(name);
  8.         this.setHardness(1);
  9.         setRegistryName(name);
  10.         this.hasTileEntity = true;
  11.         ModBlocks.BLOCKS.add(this);
  12.     }
  13.    
  14.     @Override
  15.      public boolean isBed(IBlockState state, IBlockAccess world, BlockPos pos, @Nullable Entity player)
  16.         {
  17.             return true;
  18.         }
  19.    
  20.         @Override
  21.        public MapColor getMapColor(IBlockState state, IBlockAccess worldIn, BlockPos pos)
  22.         {
  23.             if (state.getValue(PART) == BlockFancyBed.EnumPartType.FOOT)
  24.             {
  25.                 TileEntity tileentity = worldIn.getTileEntity(pos);
  26.  
  27.                 if (tileentity instanceof TileEntityFancyBed)
  28.                 {
  29.                     EnumDyeColor enumdyecolor = ((TileEntityFancyBed)tileentity).getColor();
  30.                     return MapColor.getBlockColor(enumdyecolor);
  31.                 }
  32.             }
  33.             return MapColor.CLOTH;
  34.         }
  35.        
  36.    
  37.    
  38.     @Override
  39.     public TileEntity createNewTileEntity(World worldIn, int meta)
  40.     {
  41.         return new TileEntityFancyBed();
  42.     }
  43.    
  44.     @Override
  45.     public EnumBlockRenderType getRenderType(IBlockState state)
  46.     {
  47.         return EnumBlockRenderType.MODEL ;
  48.     }
  49.      
  50.     @Override
  51.     public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
  52.     {
  53.         if (worldIn.isRemote)
  54.         {      
  55.             return true;          
  56.         }
  57.         else
  58.         {
  59.             if (state.getValue(PART) != BlockFancyBed.EnumPartType.HEAD)
  60.             {
  61.                 pos = pos.offset((EnumFacing)state.getValue(FACING));
  62.                 state = worldIn.getBlockState(pos);
  63.  
  64.                 if (state.getBlock() != this)
  65.                 {
  66.                     return true;
  67.                 }
  68.             }
  69.  
  70.             net.minecraft.world.WorldProvider.WorldSleepResult sleepResult = worldIn.provider.canSleepAt(playerIn, pos);
  71.             if (sleepResult != net.minecraft.world.WorldProvider.WorldSleepResult.BED_EXPLODES)
  72.             {
  73.                 if (sleepResult == net.minecraft.world.WorldProvider.WorldSleepResult.DENY) return true;
  74.                 if (((Boolean)state.getValue(OCCUPIED)).booleanValue())
  75.                 {
  76.                     EntityPlayer entityplayer = this.getPlayerInBed(worldIn, pos);
  77.  
  78.                     if (entityplayer != null)
  79.                     {
  80.                         playerIn.sendStatusMessage(new TextComponentTranslation("tile.bed.occupied", new Object[0]), true);
  81.                         return true;
  82.                     }
  83.  
  84.                     state = state.withProperty(OCCUPIED, Boolean.valueOf(false));
  85.                     worldIn.setBlockState(pos, state, 4);
  86.                 }
  87.  
  88.                 EntityPlayer.SleepResult entityplayer$sleepresult = playerIn.trySleep(pos);
  89.  
  90.                 if (entityplayer$sleepresult == EntityPlayer.SleepResult.OK)
  91.                 {
  92.                     state = state.withProperty(OCCUPIED, Boolean.valueOf(true));
  93.                     worldIn.setBlockState(pos, state, 4);
  94.                     return true;
  95.                 }
  96.                 else
  97.                 {
  98.                     if (entityplayer$sleepresult == EntityPlayer.SleepResult.NOT_POSSIBLE_NOW)
  99.                     {
  100.                         playerIn.sendStatusMessage(new TextComponentTranslation("tile.bed.noSleep", new Object[0]), true);
  101.                     }
  102.                     else if (entityplayer$sleepresult == EntityPlayer.SleepResult.NOT_SAFE)
  103.                     {
  104.                         playerIn.sendStatusMessage(new TextComponentTranslation("tile.bed.notSafe", new Object[0]), true);
  105.                     }
  106.                     else if (entityplayer$sleepresult == EntityPlayer.SleepResult.TOO_FAR_AWAY)
  107.                     {
  108.                         playerIn.sendStatusMessage(new TextComponentTranslation("tile.bed.tooFarAway", new Object[0]), true);
  109.                     }
  110.  
  111.                     return true;
  112.                 }
  113.             }
  114.             else
  115.             {
  116.                 worldIn.setBlockToAir(pos);
  117.                 BlockPos blockpos = pos.offset(((EnumFacing)state.getValue(FACING)).getOpposite());
  118.  
  119.                 if (worldIn.getBlockState(blockpos).getBlock() == this)
  120.                 {
  121.                     worldIn.setBlockToAir(blockpos);
  122.                 }
  123.  
  124.                 worldIn.newExplosion((Entity)null, (double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D, 5.0F, true, true);
  125.                 return true;
  126.             }
  127.         }
  128.     }
  129.    
  130.     @Nullable
  131.     private EntityPlayer getPlayerInBed(World worldIn, BlockPos pos)
  132.     {
  133.         for (EntityPlayer entityplayer : worldIn.playerEntities)
  134.         {
  135.             if (entityplayer.isPlayerSleeping() && entityplayer.bedLocation.equals(pos))
  136.             {
  137.                 return entityplayer;
  138.             }
  139.         }
  140.        
  141.        
  142.  
  143.         return null;
  144.     }
  145.    
  146.  
  147.        
  148.         @Override
  149.         protected BlockStateContainer createBlockState()
  150.         {
  151.            // return new BlockStateContainer(this, new IProperty[] {FACING, PART, OCCUPIED});
  152.              return new BlockStateContainer.Builder(this).add(FACING, PART, OCCUPIED).add(COLOR).build();
  153.  
  154.         }
  155.        
  156.  
  157.         public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
  158.         {
  159.             super.breakBlock(worldIn, pos, state);
  160.             worldIn.removeTileEntity(pos);
  161.         }
  162.        
  163.         public Item getItemDropped(IBlockState state, Random rand, int fortune)
  164.         {
  165.             return state.getValue(PART) == BlockFancyBed.EnumPartType.FOOT ? Items.AIR : ModItems.ITEM_FANCY_BED ;
  166.         }
  167.        
  168.         @Override
  169.         public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, TileEntity te, ItemStack stack)
  170.         {
  171.             if (state.getValue(PART) == BlockFancyBed.EnumPartType.HEAD && te instanceof TileEntityFancyBed)
  172.             {
  173.                 TileEntityFancyBed tileentitybed = (TileEntityFancyBed)te;
  174.                 ItemStack itemstack = tileentitybed.getItemStack();
  175.                 spawnAsEntity(worldIn, pos, itemstack);
  176.             }
  177.             else
  178.             {
  179.                 super.harvestBlock(worldIn, player, pos, state, (TileEntity)null, stack);
  180.             }
  181.         }
  182.        
  183.         public EnumDyeColor LoadColor(World worldIn, BlockPos pos)
  184.         {
  185.             TileEntity tileentity = worldIn.getTileEntity(pos);
  186.             if(tileentity instanceof TileEntityFancyBed) {
  187.                 TileEntityFancyBed bed = (TileEntityFancyBed) tileentity;
  188.                 return bed.getColor();
  189.             }          
  190.             return EnumDyeColor.RED;
  191.         }
  192.        
  193.            @Override
  194.             public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
  195.             {
  196.                     IBlockState iblockstate = worldIn.getBlockState(pos);
  197.                     state = state.withProperty(OCCUPIED, iblockstate.getValue(OCCUPIED)).withProperty(COLOR , LoadColor((World)worldIn, pos));
  198.                     return state;
  199.             }
  200.            
  201.            @Override
  202.            public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, IBlockState state, float chance, int fortune)
  203.             {
  204.                 if (state.getValue(PART) == BlockFancyBed.EnumPartType.HEAD)
  205.                 {
  206.                      TileEntity tileentity = worldIn.getTileEntity(pos);
  207.                      TileEntityFancyBed te = (TileEntityFancyBed)tileentity;
  208.                      EnumDyeColor enumdyecolor = te.getColor();
  209.                     spawnAsEntity(worldIn, pos, new ItemStack(ModItems.ITEM_FANCY_BED, 1, enumdyecolor.getMetadata()));
  210.                 }
  211.             }
  212.            @Override
  213.            public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state)
  214.             {
  215.                 BlockPos blockpos = pos;
  216.  
  217.                 if (state.getValue(PART) == BlockFancyBed.EnumPartType.FOOT)
  218.                 {
  219.                     blockpos = pos.offset((EnumFacing)state.getValue(FACING));
  220.                 }
  221.  
  222.                 TileEntity tileentity = worldIn.getTileEntity(blockpos);
  223.                 TileEntityFancyBed te = (TileEntityFancyBed)tileentity;
  224.                 EnumDyeColor enumdyecolor = te.getColor();
  225.                 return new ItemStack(ModItems.ITEM_FANCY_BED, 1, enumdyecolor.getMetadata());
  226.             }
  227.            
  228.            @SideOnly(Side.CLIENT)
  229.             public static boolean isHeadPiece(int metadata)
  230.             {
  231.                 return false;
  232.             }
  233.            
  234.            @Override
  235.             @SideOnly(Side.CLIENT)
  236.             public BlockRenderLayer getBlockLayer()
  237.             {
  238.                 return BlockRenderLayer.CUTOUT_MIPPED;
  239.             }
  240.            
  241.  
  242.            
  243.            
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement