Advertisement
Guest User

BlockMyPane

a guest
Apr 8th, 2015
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.70 KB | None | 0 0
  1. package com.smith.mod.blocks;
  2.  
  3. import java.util.List;
  4. import java.util.Random;
  5.  
  6. import net.minecraft.block.Block;
  7. import net.minecraft.block.material.Material;
  8. import net.minecraft.block.properties.IProperty;
  9. import net.minecraft.block.properties.PropertyBool;
  10. import net.minecraft.block.state.BlockState;
  11. import net.minecraft.block.state.IBlockState;
  12. import net.minecraft.creativetab.CreativeTabs;
  13. import net.minecraft.entity.Entity;
  14. import net.minecraft.init.Blocks;
  15. import net.minecraft.item.Item;
  16. import net.minecraft.util.AxisAlignedBB;
  17. import net.minecraft.util.BlockPos;
  18. import net.minecraft.util.EnumFacing;
  19. import net.minecraft.util.EnumWorldBlockLayer;
  20. import net.minecraft.world.IBlockAccess;
  21. import net.minecraft.world.World;
  22. import net.minecraftforge.fml.relauncher.Side;
  23. import net.minecraftforge.fml.relauncher.SideOnly;
  24.  
  25. public class BlockMyPane extends Block
  26. {
  27.     public static final PropertyBool NORTH = PropertyBool.create("north");
  28.     public static final PropertyBool EAST = PropertyBool.create("east");
  29.     public static final PropertyBool SOUTH = PropertyBool.create("south");
  30.     public static final PropertyBool WEST = PropertyBool.create("west");
  31.     private final boolean canDrop;
  32.  
  33.     public BlockMyPane(Material materialIn, boolean canDrop)
  34.     {
  35.         super(materialIn);
  36.         this.setDefaultState(this.blockState.getBaseState().withProperty(NORTH, Boolean.valueOf(false)).withProperty(EAST, Boolean.valueOf(false)).withProperty(SOUTH, Boolean.valueOf(false)).withProperty(WEST, Boolean.valueOf(false)));
  37.         this.canDrop = canDrop;
  38.     }
  39.  
  40.     /**
  41.      * Get the actual Block state of this Block at the given position. This applies properties not visible in the
  42.      * metadata, such as fence connections.
  43.      */
  44.     public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
  45.     {
  46.         return state.withProperty(NORTH, canPaneConnectTo(worldIn, pos, EnumFacing.NORTH))
  47.                 .withProperty(SOUTH, canPaneConnectTo(worldIn, pos, EnumFacing.SOUTH))
  48.                 .withProperty(WEST, canPaneConnectTo(worldIn, pos, EnumFacing.WEST))
  49.                 .withProperty(EAST, canPaneConnectTo(worldIn, pos, EnumFacing.EAST));
  50.     }
  51.  
  52.     /**
  53.      * Get the Item that this Block should drop when harvested.
  54.      *  
  55.      * @param fortune the level of the Fortune enchantment on the player's tool
  56.      */
  57.     public Item getItemDropped(IBlockState state, Random rand, int fortune)
  58.     {
  59.         return !this.canDrop ? null : super.getItemDropped(state, rand, fortune);
  60.     }
  61.  
  62.     public boolean isOpaqueCube()
  63.     {
  64.         return false;
  65.     }
  66.  
  67.     public boolean isFullCube()
  68.     {
  69.         return false;
  70.     }
  71.  
  72.     @SideOnly(Side.CLIENT)
  73.     public boolean shouldSideBeRendered(IBlockAccess worldIn, BlockPos pos, EnumFacing side)
  74.     {
  75.         return worldIn.getBlockState(pos).getBlock() == this ? false : super.shouldSideBeRendered(worldIn, pos, side);
  76.     }
  77.  
  78.     /**
  79.      * Add all collision boxes of this Block to the list that intersect with the given mask.
  80.      *  
  81.      * @param collidingEntity the Entity colliding with this Block
  82.      */
  83.     public void addCollisionBoxesToList(World worldIn, BlockPos pos, IBlockState state, AxisAlignedBB mask, List list, Entity collidingEntity)
  84.     {
  85.         boolean flag = this.canPaneConnectTo(worldIn, pos, EnumFacing.NORTH);
  86.         boolean flag1 = this.canPaneConnectTo(worldIn, pos, EnumFacing.SOUTH);
  87.         boolean flag2 = this.canPaneConnectTo(worldIn, pos, EnumFacing.WEST);
  88.         boolean flag3 = this.canPaneConnectTo(worldIn, pos, EnumFacing.EAST);
  89.  
  90.         if ((!flag2 || !flag3) && (flag2 || flag3 || flag || flag1))
  91.         {
  92.             if (flag2)
  93.             {
  94.                 this.setBlockBounds(0.0F, 0.0F, 0.4375F, 0.5F, 1.0F, 0.5625F);
  95.                 super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity);
  96.             }
  97.             else if (flag3)
  98.             {
  99.                 this.setBlockBounds(0.5F, 0.0F, 0.4375F, 1.0F, 1.0F, 0.5625F);
  100.                 super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity);
  101.             }
  102.         }
  103.         else
  104.         {
  105.             this.setBlockBounds(0.0F, 0.0F, 0.4375F, 1.0F, 1.0F, 0.5625F);
  106.             super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity);
  107.         }
  108.  
  109.         if ((!flag || !flag1) && (flag2 || flag3 || flag || flag1))
  110.         {
  111.             if (flag)
  112.             {
  113.                 this.setBlockBounds(0.4375F, 0.0F, 0.0F, 0.5625F, 1.0F, 0.5F);
  114.                 super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity);
  115.             }
  116.             else if (flag1)
  117.             {
  118.                 this.setBlockBounds(0.4375F, 0.0F, 0.5F, 0.5625F, 1.0F, 1.0F);
  119.                 super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity);
  120.             }
  121.         }
  122.         else
  123.         {
  124.             this.setBlockBounds(0.4375F, 0.0F, 0.0F, 0.5625F, 1.0F, 1.0F);
  125.             super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity);
  126.         }
  127.     }
  128.  
  129.     /**
  130.      * Sets the block's bounds for rendering it as an item
  131.      */
  132.     public void setBlockBoundsForItemRender()
  133.     {
  134.         this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
  135.     }
  136.  
  137.     public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
  138.     {
  139.         float f = 0.4375F;
  140.         float f1 = 0.5625F;
  141.         float f2 = 0.4375F;
  142.         float f3 = 0.5625F;
  143.         boolean flag = this.canPaneConnectToBlock(worldIn.getBlockState(pos.north()).getBlock());
  144.         boolean flag1 = this.canPaneConnectToBlock(worldIn.getBlockState(pos.south()).getBlock());
  145.         boolean flag2 = this.canPaneConnectToBlock(worldIn.getBlockState(pos.west()).getBlock());
  146.         boolean flag3 = this.canPaneConnectToBlock(worldIn.getBlockState(pos.east()).getBlock());
  147.  
  148.         if ((!flag2 || !flag3) && (flag2 || flag3 || flag || flag1))
  149.         {
  150.             if (flag2)
  151.             {
  152.                 f = 0.0F;
  153.             }
  154.             else if (flag3)
  155.             {
  156.                 f1 = 1.0F;
  157.             }
  158.         }
  159.         else
  160.         {
  161.             f = 0.0F;
  162.             f1 = 1.0F;
  163.         }
  164.  
  165.         if ((!flag || !flag1) && (flag2 || flag3 || flag || flag1))
  166.         {
  167.             if (flag)
  168.             {
  169.                 f2 = 0.0F;
  170.             }
  171.             else if (flag1)
  172.             {
  173.                 f3 = 1.0F;
  174.             }
  175.         }
  176.         else
  177.         {
  178.             f2 = 0.0F;
  179.             f3 = 1.0F;
  180.         }
  181.  
  182.         this.setBlockBounds(f, 0.0F, f2, f1, 1.0F, f3);
  183.     }
  184.  
  185.     public final boolean canPaneConnectToBlock(Block blockIn)
  186.     {
  187.         return blockIn.isFullBlock() || blockIn == this || blockIn == Blocks.glass || blockIn == Blocks.stained_glass || blockIn == Blocks.stained_glass_pane || blockIn instanceof BlockMyPane;
  188.     }
  189.  
  190.     protected boolean canSilkHarvest()
  191.     {
  192.         return true;
  193.     }
  194.  
  195.     @SideOnly(Side.CLIENT)
  196.     public EnumWorldBlockLayer getBlockLayer()
  197.     {
  198.         return EnumWorldBlockLayer.CUTOUT_MIPPED;
  199.     }
  200.  
  201.     /**
  202.      * Convert the BlockState into the correct metadata value
  203.      */
  204.     public int getMetaFromState(IBlockState state)
  205.     {
  206.         return 0;
  207.     }
  208.  
  209.     protected BlockState createBlockState()
  210.     {
  211.         return new BlockState(this, new IProperty[] {NORTH, EAST, WEST, SOUTH});
  212.     }
  213.  
  214.     public boolean canPaneConnectTo(IBlockAccess world, BlockPos pos, EnumFacing dir)
  215.     {
  216.         BlockPos off = pos.offset(dir);
  217.         Block block = world.getBlockState(off).getBlock();
  218.         return canPaneConnectToBlock(block) || block.isSideSolid(world, off, dir.getOpposite());
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement