Advertisement
Guest User

MyDoorBlock.java

a guest
Apr 7th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.56 KB | None | 0 0
  1. package com.smith.mod.blocks;
  2.  
  3. import java.util.Random;
  4.  
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.material.Material;
  7. import net.minecraft.block.properties.IProperty;
  8. import net.minecraft.block.properties.PropertyBool;
  9. import net.minecraft.block.properties.PropertyDirection;
  10. import net.minecraft.block.properties.PropertyEnum;
  11. import net.minecraft.block.state.BlockState;
  12. import net.minecraft.block.state.IBlockState;
  13. import net.minecraft.entity.player.EntityPlayer;
  14. import net.minecraft.init.Blocks;
  15. import net.minecraft.init.Items;
  16. import net.minecraft.item.Item;
  17. import net.minecraft.util.AxisAlignedBB;
  18. import net.minecraft.util.BlockPos;
  19. import net.minecraft.util.EnumFacing;
  20. import net.minecraft.util.EnumWorldBlockLayer;
  21. import net.minecraft.util.IStringSerializable;
  22. import net.minecraft.util.MovingObjectPosition;
  23. import net.minecraft.util.Vec3;
  24. import net.minecraft.world.IBlockAccess;
  25. import net.minecraft.world.World;
  26. import net.minecraftforge.fml.relauncher.Side;
  27. import net.minecraftforge.fml.relauncher.SideOnly;
  28.  
  29. public class BlockMyDoor extends Block
  30. {
  31.     public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
  32.     public static final PropertyBool OPEN = PropertyBool.create("open");
  33.     public static final PropertyEnum HINGE = PropertyEnum.create("hinge", BlockMyDoor.EnumHingePosition.class);
  34.     public static final PropertyBool POWERED = PropertyBool.create("powered");
  35.     public static final PropertyEnum HALF = PropertyEnum.create("half", BlockMyDoor.EnumDoorHalf.class);
  36.  
  37.     public BlockMyDoor(Material materialIn)
  38.     {
  39.         super(materialIn);
  40.         this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(OPEN, Boolean.valueOf(false)).withProperty(HINGE, BlockMyDoor.EnumHingePosition.LEFT).withProperty(POWERED, Boolean.valueOf(false)).withProperty(HALF, BlockMyDoor.EnumDoorHalf.LOWER));
  41.     }
  42.  
  43.     public boolean isOpaqueCube()
  44.     {
  45.         return false;
  46.     }
  47.  
  48.     public boolean isPassable(IBlockAccess worldIn, BlockPos pos)
  49.     {
  50.         return isOpen(combineMetadata(worldIn, pos));
  51.     }
  52.  
  53.     public boolean isFullCube()
  54.     {
  55.         return false;
  56.     }
  57.  
  58.     @SideOnly(Side.CLIENT)
  59.     public AxisAlignedBB getSelectedBoundingBox(World worldIn, BlockPos pos)
  60.     {
  61.         this.setBlockBoundsBasedOnState(worldIn, pos);
  62.         return super.getSelectedBoundingBox(worldIn, pos);
  63.     }
  64.  
  65.     public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state)
  66.     {
  67.         this.setBlockBoundsBasedOnState(worldIn, pos);
  68.         return super.getCollisionBoundingBox(worldIn, pos, state);
  69.     }
  70.  
  71.     public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
  72.     {
  73.         this.setBoundBasedOnMeta(combineMetadata(worldIn, pos));
  74.     }
  75.  
  76.     private void setBoundBasedOnMeta(int combinedMeta)
  77.     {
  78.         float f = 0.1875F;
  79.         this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 2.0F, 1.0F);
  80.         EnumFacing enumfacing = getFacing(combinedMeta);
  81.         boolean flag = isOpen(combinedMeta);
  82.         boolean flag1 = isHingeLeft(combinedMeta);
  83.  
  84.         if (flag)
  85.         {
  86.             if (enumfacing == EnumFacing.EAST)
  87.             {
  88.                 if (!flag1)
  89.                 {
  90.                     this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, f);
  91.                 }
  92.                 else
  93.                 {
  94.                     this.setBlockBounds(0.0F, 0.0F, 1.0F - f, 1.0F, 1.0F, 1.0F);
  95.                 }
  96.             }
  97.             else if (enumfacing == EnumFacing.SOUTH)
  98.             {
  99.                 if (!flag1)
  100.                 {
  101.                     this.setBlockBounds(1.0F - f, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
  102.                 }
  103.                 else
  104.                 {
  105.                     this.setBlockBounds(0.0F, 0.0F, 0.0F, f, 1.0F, 1.0F);
  106.                 }
  107.             }
  108.             else if (enumfacing == EnumFacing.WEST)
  109.             {
  110.                 if (!flag1)
  111.                 {
  112.                     this.setBlockBounds(0.0F, 0.0F, 1.0F - f, 1.0F, 1.0F, 1.0F);
  113.                 }
  114.                 else
  115.                 {
  116.                     this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, f);
  117.                 }
  118.             }
  119.             else if (enumfacing == EnumFacing.NORTH)
  120.             {
  121.                 if (!flag1)
  122.                 {
  123.                     this.setBlockBounds(0.0F, 0.0F, 0.0F, f, 1.0F, 1.0F);
  124.                 }
  125.                 else
  126.                 {
  127.                     this.setBlockBounds(1.0F - f, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
  128.                 }
  129.             }
  130.         }
  131.         else if (enumfacing == EnumFacing.EAST)
  132.         {
  133.             this.setBlockBounds(0.0F, 0.0F, 0.0F, f, 1.0F, 1.0F);
  134.         }
  135.         else if (enumfacing == EnumFacing.SOUTH)
  136.         {
  137.             this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, f);
  138.         }
  139.         else if (enumfacing == EnumFacing.WEST)
  140.         {
  141.             this.setBlockBounds(1.0F - f, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
  142.         }
  143.         else if (enumfacing == EnumFacing.NORTH)
  144.         {
  145.             this.setBlockBounds(0.0F, 0.0F, 1.0F - f, 1.0F, 1.0F, 1.0F);
  146.         }
  147.     }
  148.  
  149.     public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
  150.     {
  151.         if (this.blockMaterial == Material.iron)
  152.         {
  153.             return false; //Allow items to interact with the door
  154.         }
  155.         else
  156.         {
  157.             BlockPos blockpos1 = state.getValue(HALF) == BlockMyDoor.EnumDoorHalf.LOWER ? pos : pos.down();
  158.             IBlockState iblockstate1 = pos.equals(blockpos1) ? state : worldIn.getBlockState(blockpos1);
  159.  
  160.             if (iblockstate1.getBlock() != this)
  161.             {
  162.                 return false;
  163.             }
  164.             else
  165.             {
  166.                 state = iblockstate1.cycleProperty(OPEN);
  167.                 worldIn.setBlockState(blockpos1, state, 2);
  168.                 worldIn.markBlockRangeForRenderUpdate(blockpos1, pos);
  169.                 worldIn.playAuxSFXAtEntity(playerIn, ((Boolean)state.getValue(OPEN)).booleanValue() ? 1003 : 1006, pos, 0);
  170.                 return true;
  171.             }
  172.         }
  173.     }
  174.  
  175.     public void toggleDoor(World worldIn, BlockPos pos, boolean open)
  176.     {
  177.         IBlockState iblockstate = worldIn.getBlockState(pos);
  178.  
  179.         if (iblockstate.getBlock() == this)
  180.         {
  181.             BlockPos blockpos1 = iblockstate.getValue(HALF) == BlockMyDoor.EnumDoorHalf.LOWER ? pos : pos.down();
  182.             IBlockState iblockstate1 = pos == blockpos1 ? iblockstate : worldIn.getBlockState(blockpos1);
  183.  
  184.             if (iblockstate1.getBlock() == this && ((Boolean)iblockstate1.getValue(OPEN)).booleanValue() != open)
  185.             {
  186.                 worldIn.setBlockState(blockpos1, iblockstate1.withProperty(OPEN, Boolean.valueOf(open)), 2);
  187.                 worldIn.markBlockRangeForRenderUpdate(blockpos1, pos);
  188.                 worldIn.playAuxSFXAtEntity((EntityPlayer)null, open ? 1003 : 1006, pos, 0);
  189.             }
  190.         }
  191.     }
  192.  
  193.     /**
  194.      * Called when a neighboring block changes.
  195.      */
  196.     public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
  197.     {
  198.         if (state.getValue(HALF) == BlockMyDoor.EnumDoorHalf.UPPER)
  199.         {
  200.             BlockPos blockpos1 = pos.down();
  201.             IBlockState iblockstate1 = worldIn.getBlockState(blockpos1);
  202.  
  203.             if (iblockstate1.getBlock() != this)
  204.             {
  205.                 worldIn.setBlockToAir(pos);
  206.             }
  207.             else if (neighborBlock != this)
  208.             {
  209.                 this.onNeighborBlockChange(worldIn, blockpos1, iblockstate1, neighborBlock);
  210.             }
  211.         }
  212.         else
  213.         {
  214.             boolean flag1 = false;
  215.             BlockPos blockpos2 = pos.up();
  216.             IBlockState iblockstate2 = worldIn.getBlockState(blockpos2);
  217.  
  218.             if (iblockstate2.getBlock() != this)
  219.             {
  220.                 worldIn.setBlockToAir(pos);
  221.                 flag1 = true;
  222.             }
  223.  
  224.             if (!World.doesBlockHaveSolidTopSurface(worldIn, pos.down()))
  225.             {
  226.                 worldIn.setBlockToAir(pos);
  227.                 flag1 = true;
  228.  
  229.                 if (iblockstate2.getBlock() == this)
  230.                 {
  231.                     worldIn.setBlockToAir(blockpos2);
  232.                 }
  233.             }
  234.  
  235.             if (flag1)
  236.             {
  237.                 if (!worldIn.isRemote)
  238.                 {
  239.                     this.dropBlockAsItem(worldIn, pos, state, 0);
  240.                 }
  241.             }
  242.             else
  243.             {
  244.                 boolean flag = worldIn.isBlockPowered(pos) || worldIn.isBlockPowered(blockpos2);
  245.  
  246.                 if ((flag || neighborBlock.canProvidePower()) && neighborBlock != this && flag != ((Boolean)iblockstate2.getValue(POWERED)).booleanValue())
  247.                 {
  248.                     worldIn.setBlockState(blockpos2, iblockstate2.withProperty(POWERED, Boolean.valueOf(flag)), 2);
  249.  
  250.                     if (flag != ((Boolean)state.getValue(OPEN)).booleanValue())
  251.                     {
  252.                         worldIn.setBlockState(pos, state.withProperty(OPEN, Boolean.valueOf(flag)), 2);
  253.                         worldIn.markBlockRangeForRenderUpdate(pos, pos);
  254.                         worldIn.playAuxSFXAtEntity((EntityPlayer)null, flag ? 1003 : 1006, pos, 0);
  255.                     }
  256.                 }
  257.             }
  258.         }
  259.     }
  260.  
  261.     /**
  262.      * Get the Item that this Block should drop when harvested.
  263.      *  
  264.      * @param fortune the level of the Fortune enchantment on the player's tool
  265.      */
  266.     public Item getItemDropped(IBlockState state, Random rand, int fortune)
  267.     {
  268.         return state.getValue(HALF) == BlockMyDoor.EnumDoorHalf.UPPER ? null : this.getItem();
  269.     }
  270.  
  271.     /**
  272.      * Ray traces through the blocks collision from start vector to end vector returning a ray trace hit.
  273.      *  
  274.      * @param start The start vector
  275.      * @param end The end vector
  276.      */
  277.     public MovingObjectPosition collisionRayTrace(World worldIn, BlockPos pos, Vec3 start, Vec3 end)
  278.     {
  279.         this.setBlockBoundsBasedOnState(worldIn, pos);
  280.         return super.collisionRayTrace(worldIn, pos, start, end);
  281.     }
  282.  
  283.     public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
  284.     {
  285.         return pos.getY() >= worldIn.getHeight() - 1 ? false : World.doesBlockHaveSolidTopSurface(worldIn, pos.down()) && super.canPlaceBlockAt(worldIn, pos) && super.canPlaceBlockAt(worldIn, pos.up());
  286.     }
  287.  
  288.     public int getMobilityFlag()
  289.     {
  290.         return 1;
  291.     }
  292.  
  293.     public static int combineMetadata(IBlockAccess worldIn, BlockPos pos)
  294.     {
  295.         IBlockState iblockstate = worldIn.getBlockState(pos);
  296.         int i = iblockstate.getBlock().getMetaFromState(iblockstate);
  297.         boolean flag = isTop(i);
  298.         IBlockState iblockstate1 = worldIn.getBlockState(pos.down());
  299.         int j = iblockstate1.getBlock().getMetaFromState(iblockstate1);
  300.         int k = flag ? j : i;
  301.         IBlockState iblockstate2 = worldIn.getBlockState(pos.up());
  302.         int l = iblockstate2.getBlock().getMetaFromState(iblockstate2);
  303.         int i1 = flag ? i : l;
  304.         boolean flag1 = (i1 & 1) != 0;
  305.         boolean flag2 = (i1 & 2) != 0;
  306.         return removeHalfBit(k) | (flag ? 8 : 0) | (flag1 ? 16 : 0) | (flag2 ? 32 : 0);
  307.     }
  308.  
  309.     @SideOnly(Side.CLIENT)
  310.     public Item getItem(World worldIn, BlockPos pos)
  311.     {
  312.         return this.getItem();
  313.     }
  314.  
  315.     private Item getItem()
  316.     {
  317.         return this == Blocks.iron_door ? Items.iron_door : (this == Blocks.spruce_door ? Items.spruce_door : (this == Blocks.birch_door ? Items.birch_door : (this == Blocks.jungle_door ? Items.jungle_door : (this == Blocks.acacia_door ? Items.acacia_door : (this == Blocks.dark_oak_door ? Items.dark_oak_door : Items.oak_door)))));
  318.     }
  319.  
  320.     public void onBlockHarvested(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player)
  321.     {
  322.         BlockPos blockpos1 = pos.down();
  323.  
  324.         if (player.capabilities.isCreativeMode && state.getValue(HALF) == BlockMyDoor.EnumDoorHalf.UPPER && worldIn.getBlockState(blockpos1).getBlock() == this)
  325.         {
  326.             worldIn.setBlockToAir(blockpos1);
  327.         }
  328.     }
  329.  
  330.     /**
  331.      * Get the actual Block state of this Block at the given position. This applies properties not visible in the
  332.      * metadata, such as fence connections.
  333.      */
  334.     public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
  335.     {
  336.         IBlockState iblockstate1;
  337.  
  338.         if (state.getValue(HALF) == BlockMyDoor.EnumDoorHalf.LOWER)
  339.         {
  340.             iblockstate1 = worldIn.getBlockState(pos.up());
  341.  
  342.             if (iblockstate1.getBlock() == this)
  343.             {
  344.                 state = state.withProperty(HINGE, iblockstate1.getValue(HINGE)).withProperty(POWERED, iblockstate1.getValue(POWERED));
  345.             }
  346.         }
  347.         else
  348.         {
  349.             iblockstate1 = worldIn.getBlockState(pos.down());
  350.  
  351.             if (iblockstate1.getBlock() == this)
  352.             {
  353.                 state = state.withProperty(FACING, iblockstate1.getValue(FACING)).withProperty(OPEN, iblockstate1.getValue(OPEN));
  354.             }
  355.         }
  356.  
  357.         return state;
  358.     }
  359.  
  360.     /**
  361.      * Convert the given metadata into a BlockState for this Block
  362.      */
  363.     public IBlockState getStateFromMeta(int meta)
  364.     {
  365.         return (meta & 8) > 0 ? this.getDefaultState().withProperty(HALF, BlockMyDoor.EnumDoorHalf.UPPER).withProperty(HINGE, (meta & 1) > 0 ? BlockMyDoor.EnumHingePosition.RIGHT : BlockMyDoor.EnumHingePosition.LEFT).withProperty(POWERED, Boolean.valueOf((meta & 2) > 0)) : this.getDefaultState().withProperty(HALF, BlockMyDoor.EnumDoorHalf.LOWER).withProperty(FACING, EnumFacing.getHorizontal(meta & 3).rotateYCCW()).withProperty(OPEN, Boolean.valueOf((meta & 4) > 0));
  366.     }
  367.  
  368.     @SideOnly(Side.CLIENT)
  369.     public EnumWorldBlockLayer getBlockLayer()
  370.     {
  371.         return EnumWorldBlockLayer.CUTOUT;
  372.     }
  373.  
  374.     /**
  375.      * Convert the BlockState into the correct metadata value
  376.      */
  377.     public int getMetaFromState(IBlockState state)
  378.     {
  379.         byte b0 = 0;
  380.         int i;
  381.  
  382.         if (state.getValue(HALF) == BlockMyDoor.EnumDoorHalf.UPPER)
  383.         {
  384.             i = b0 | 8;
  385.  
  386.             if (state.getValue(HINGE) == BlockMyDoor.EnumHingePosition.RIGHT)
  387.             {
  388.                 i |= 1;
  389.             }
  390.  
  391.             if (((Boolean)state.getValue(POWERED)).booleanValue())
  392.             {
  393.                 i |= 2;
  394.             }
  395.         }
  396.         else
  397.         {
  398.             i = b0 | ((EnumFacing)state.getValue(FACING)).rotateY().getHorizontalIndex();
  399.  
  400.             if (((Boolean)state.getValue(OPEN)).booleanValue())
  401.             {
  402.                 i |= 4;
  403.             }
  404.         }
  405.  
  406.         return i;
  407.     }
  408.  
  409.     protected static int removeHalfBit(int meta)
  410.     {
  411.         return meta & 7;
  412.     }
  413.  
  414.     public static boolean isOpen(IBlockAccess worldIn, BlockPos pos)
  415.     {
  416.         return isOpen(combineMetadata(worldIn, pos));
  417.     }
  418.  
  419.     public static EnumFacing getFacing(IBlockAccess worldIn, BlockPos pos)
  420.     {
  421.         return getFacing(combineMetadata(worldIn, pos));
  422.     }
  423.  
  424.     public static EnumFacing getFacing(int combinedMeta)
  425.     {
  426.         return EnumFacing.getHorizontal(combinedMeta & 3).rotateYCCW();
  427.     }
  428.  
  429.     protected static boolean isOpen(int combinedMeta)
  430.     {
  431.         return (combinedMeta & 4) != 0;
  432.     }
  433.  
  434.     protected static boolean isTop(int meta)
  435.     {
  436.         return (meta & 8) != 0;
  437.     }
  438.  
  439.     protected static boolean isHingeLeft(int combinedMeta)
  440.     {
  441.         return (combinedMeta & 16) != 0;
  442.     }
  443.  
  444.     protected BlockState createBlockState()
  445.     {
  446.         return new BlockState(this, new IProperty[] {HALF, FACING, OPEN, HINGE, POWERED});
  447.     }
  448.  
  449.     public static enum EnumDoorHalf implements IStringSerializable
  450.     {
  451.         UPPER,
  452.         LOWER;
  453.  
  454.         private static final String __OBFID = "CL_00002124";
  455.  
  456.         public String toString()
  457.         {
  458.             return this.getName();
  459.         }
  460.  
  461.         public String getName()
  462.         {
  463.             return this == UPPER ? "upper" : "lower";
  464.         }
  465.     }
  466.  
  467.     public static enum EnumHingePosition implements IStringSerializable
  468.     {
  469.         LEFT,
  470.         RIGHT;
  471.  
  472.         private static final String __OBFID = "CL_00002123";
  473.  
  474.         public String toString()
  475.         {
  476.             return this.getName();
  477.         }
  478.  
  479.         public String getName()
  480.         {
  481.             return this == LEFT ? "left" : "right";
  482.         }
  483.     }
  484. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement