Advertisement
Guest User

By Melonslise

a guest
Mar 10th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.75 KB | None | 0 0
  1. package celestek.hexcraft.common.block;
  2.  
  3. import java.util.Optional;
  4.  
  5. import celestek.hexcraft.client.model.HexStateMapper;
  6. import net.minecraft.block.Block;
  7. import net.minecraft.block.BlockDoor;
  8. import net.minecraft.block.BlockDoor.EnumDoorHalf;
  9. import net.minecraft.block.BlockDoor.EnumHingePosition;
  10. import net.minecraft.block.BlockHorizontal;
  11. import net.minecraft.block.material.EnumPushReaction;
  12. import net.minecraft.block.material.Material;
  13. import net.minecraft.block.properties.IProperty;
  14. import net.minecraft.block.properties.PropertyBool;
  15. import net.minecraft.block.properties.PropertyDirection;
  16. import net.minecraft.block.properties.PropertyEnum;
  17. import net.minecraft.block.state.BlockFaceShape;
  18. import net.minecraft.block.state.BlockStateContainer;
  19. import net.minecraft.block.state.IBlockState;
  20. import net.minecraft.creativetab.CreativeTabs;
  21. import net.minecraft.entity.EntityLivingBase;
  22. import net.minecraft.entity.player.EntityPlayer;
  23. import net.minecraft.item.ItemStack;
  24. import net.minecraft.util.EnumFacing;
  25. import net.minecraft.util.EnumHand;
  26. import net.minecraft.util.math.AxisAlignedBB;
  27. import net.minecraft.util.math.BlockPos;
  28. import net.minecraft.world.IBlockAccess;
  29. import net.minecraft.world.World;
  30.  
  31. public class HexBlockDoor extends HexBlockReinforceable
  32. {
  33.     public static final PropertyEnum<EnumDoorHalf> HALF = BlockDoor.HALF; // Stored on both blocks
  34.     public static final PropertyDirection FACING = BlockHorizontal.FACING; // Stored on lower block
  35.     public static final PropertyBool OPEN = BlockDoor.OPEN; // Stored on lower block
  36.     public static final PropertyEnum<EnumHingePosition> HINGE = BlockDoor.HINGE; // Stored on upper block
  37.     public static final PropertyBool POWERED = BlockDoor.POWERED; // Stored on upper block
  38.     public static final PropertyBool REINFORCED = HexBlockReinforceable.REINFORCED; // Stored on upper block
  39.  
  40.     protected static final AxisAlignedBB BOUNDS_NORTH = new AxisAlignedBB(0.0D, 0.0D, 0.8125D, 1.0D, 1.0D, 1.0D);
  41.     protected static final AxisAlignedBB BOUNDS_SOUTH = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 1.0D, 0.1875D);
  42.     protected static final AxisAlignedBB BOUNDS_WEST = new AxisAlignedBB(0.8125D, 0.0D, 0.0D, 1.0D, 1.0D, 1.0D);
  43.     protected static final AxisAlignedBB BOUNDS_EAST = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 0.1875D, 1.0D, 1.0D);   
  44.  
  45.     public HexBlockDoor(String name, Optional<HexStateMapper> mapper, CreativeTabs tab, Material material, int color, String... textures)
  46.     {
  47.         super(name, mapper, tab, material, color, textures);
  48.         this.setDefaultState(this.getDefaultState().withProperty(HALF, EnumDoorHalf.LOWER).withProperty(FACING, EnumFacing.NORTH).withProperty(OPEN, false).withProperty(HINGE, EnumHingePosition.LEFT).withProperty(POWERED, false));
  49.     }
  50.  
  51.     @Override
  52.     public boolean canPlaceBlockOnSide(World world, BlockPos position, EnumFacing side)
  53.     {
  54.         return side == EnumFacing.UP && this.canPlaceBlockAt(world, position);
  55.     }
  56.  
  57.     @Override
  58.     public boolean canPlaceBlockAt(World world, BlockPos position) //FIXME Can player edit and isTopSolid || solid face shape like in BlockDoor?
  59.     {
  60.         BlockPos upPosition = position.up();
  61.         BlockPos downPosition = position.down();
  62.         return position.getY() < world.getHeight() - 1 && world.getBlockState(downPosition).isSideSolid(world, downPosition, EnumFacing.UP) && super.canPlaceBlockAt(world, position) && super.canPlaceBlockAt(world, upPosition);
  63.     }
  64.  
  65.     @Override
  66.     public IBlockState getStateForPlacement(World world, BlockPos position, EnumFacing side, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer, EnumHand hand)
  67.     {
  68.         EnumFacing facing = EnumFacing.fromAngle((double) placer.rotationYaw);
  69.         boolean rightHinge = false;
  70.         BlockPos rightPosition = position.offset(facing.rotateY());
  71.         BlockPos leftPosition = position.offset(facing.rotateYCCW());
  72.         if(world.getBlockState(rightPosition).getBlock() == this && world.getBlockState(rightPosition.up()).getBlock() == this) rightHinge = false;
  73.         else if(world.getBlockState(leftPosition).getBlock() == this && world.getBlockState(leftPosition.up()).getBlock() == this) rightHinge = true;
  74.         else
  75.         {
  76.             int x = facing.getFrontOffsetX();
  77.             int y = facing.getFrontOffsetZ();
  78.             rightHinge = x < 0 && hitZ < 0.5F || x > 0 && hitZ > 0.5F || y < 0 && hitX > 0.5F || y > 0 && hitX < 0.5F;
  79.         }
  80.         boolean powered = world.isBlockPowered(position) || world.isBlockPowered(position.up());
  81.         return this.getDefaultState().withProperty(HALF, EnumDoorHalf.LOWER).withProperty(FACING, facing).withProperty(HINGE, rightHinge ? EnumHingePosition.RIGHT : EnumHingePosition.LEFT).withProperty(OPEN, powered).withProperty(POWERED, powered);
  82.     }
  83.  
  84.     @Override
  85.     public void onBlockPlacedBy(World world, BlockPos position, IBlockState state, EntityLivingBase placer, ItemStack stack)
  86.     {
  87.         world.setBlockState(position.up(), state.withProperty(HALF, EnumDoorHalf.UPPER));
  88.     }
  89.  
  90.     @Override
  91.     public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos position)
  92.     {
  93.         //state = state.getActualState(source, position);
  94.         boolean closed = !state.getValue(OPEN);
  95.         boolean right = state.getValue(HINGE) == EnumHingePosition.RIGHT;
  96.         switch (state.getValue(FACING))
  97.         {
  98.         case NORTH: return closed ? BOUNDS_NORTH : (right ? BOUNDS_WEST : BOUNDS_EAST);
  99.         case SOUTH: return closed ? BOUNDS_SOUTH : (right ? BOUNDS_EAST : BOUNDS_WEST);
  100.         case WEST: return closed ? BOUNDS_WEST : (right ? BOUNDS_SOUTH : BOUNDS_NORTH);
  101.         case EAST: return closed ? BOUNDS_EAST : (right ? BOUNDS_NORTH : BOUNDS_SOUTH);
  102.         default: return FULL_BLOCK_AABB;
  103.         }
  104.     }
  105.  
  106.     @Override
  107.     public boolean onBlockActivated(World world, BlockPos position, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
  108.     {
  109.         /*
  110.         BlockPos offsetPosition = state.getValue(HALF) == BlockDoor.EnumDoorHalf.LOWER ? position : position.down();
  111.         IBlockState offsetState = position.equals(offsetPosition) ? state : world.getBlockState(offsetPosition);
  112.         if (offsetState.getBlock() != this) return false;
  113.         else
  114.         {
  115.             state = offsetState.cycleProperty(OPEN);
  116.             world.setBlockState(offsetPosition, state, 10);
  117.             world.markBlockRangeForRenderUpdate(offsetPosition, position);
  118.             return true;
  119.         }
  120.         */
  121.         world.setBlockState(position, state.cycleProperty(OPEN));
  122.         world.playEvent(player, state.getValue(OPEN) ? 1011 : 1005, position, 0);
  123.         return true;
  124.     }
  125.  
  126.     @Override
  127.     public void neighborChanged(IBlockState state, World world, BlockPos position, Block block, BlockPos fromPosition)
  128.     {
  129.         BlockPos upPosition = position.up();
  130.         BlockPos downPosition = position.down();
  131.         if(state.getValue(HALF) == EnumDoorHalf.UPPER)
  132.         {
  133.             IBlockState downState = world.getBlockState(downPosition);
  134.             if(downState.getBlock() != this) world.setBlockToAir(position);
  135.             else world.setBlockState(position, downState.withProperty(HALF, EnumDoorHalf.UPPER));
  136.         }
  137.         else
  138.         {
  139.             IBlockState upState = world.getBlockState(upPosition);
  140.             IBlockState downState = world.getBlockState(downPosition);
  141.             if(upState.getBlock() != this || !downState.isSideSolid(world, downPosition, EnumFacing.UP)) world.setBlockToAir(position);
  142.             else world.setBlockState(position, upState.withProperty(HALF, EnumDoorHalf.LOWER));
  143.         }
  144.         if ((world.isBlockPowered(position) || world.isBlockPowered(state.getValue(HALF) == EnumDoorHalf.LOWER ? upPosition : downPosition)) != state.getValue(POWERED))
  145.         {
  146.             world.setBlockState(position, state.cycleProperty(POWERED));
  147.             if(state.getValue(POWERED) == state.getValue(OPEN))
  148.             {
  149.                 world.setBlockState(position, state.cycleProperty(OPEN));
  150.                 world.playEvent(null, state.getValue(OPEN) ? 1011 : 1005, position, 0);
  151.             }
  152.         }
  153.         /*
  154.         if (state.getValue(HALF) == EnumDoorHalf.UPPER)
  155.         {
  156.             BlockPos offsetPosition = position.down();
  157.             IBlockState offsetState = world.getBlockState(offsetPosition);
  158.             if (offsetState.getBlock() != this) world.setBlockToAir(position);
  159.             else if (block != this) offsetState.neighborChanged(world, offsetPosition, block, fromPosition);
  160.         }
  161.         else
  162.         {
  163.             BlockPos upPosition = position.up();
  164.             IBlockState upState = world.getBlockState(upPosition);
  165.             if (upState.getBlock() != this)
  166.             {
  167.                 world.setBlockToAir(position);
  168.                 if (!world.isRemote) this.dropBlockAsItem(world, position, state, 0);
  169.                 return;
  170.             }
  171.             BlockPos downPosition = position.down();
  172.             if (!world.getBlockState(downPosition).isSideSolid(world, downPosition, EnumFacing.UP))
  173.             {
  174.                 world.setBlockToAir(position);
  175.                 if (!world.isRemote) this.dropBlockAsItem(world, position, state, 0);
  176.                 if (upState.getBlock() == this) world.setBlockToAir(upPosition);
  177.                 return;
  178.             }
  179.             boolean powered = world.isBlockPowered(position) || world.isBlockPowered(upPosition);
  180.             if (block != this && (powered || block.getDefaultState().canProvidePower()) && powered != upState.getValue(POWERED))
  181.             {
  182.                 world.setBlockState(upPosition, upState.withProperty(POWERED, powered), 2);
  183.                 if (powered != state.getValue(OPEN))
  184.                 {
  185.                     world.setBlockState(position, state.withProperty(OPEN, powered), 2);
  186.                     world.markBlockRangeForRenderUpdate(position, position);
  187.                     world.playEvent(null, powered ? 1005 : 1011, position, 0);
  188.                 }
  189.             }
  190.         }
  191.         */
  192.     }
  193.  
  194.     @Override
  195.     public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos position)
  196.     {
  197.         if(state.getValue(HALF) == EnumDoorHalf.LOWER)
  198.         {
  199.             IBlockState offsetState = world.getBlockState(position.up());
  200.             if (offsetState.getBlock() == this) state = state.withProperty(HINGE, offsetState.getValue(HINGE)).withProperty(POWERED, offsetState.getValue(POWERED)).withProperty(REINFORCED, offsetState.getValue(REINFORCED));
  201.         }
  202.         else
  203.         {
  204.             IBlockState offsetState = world.getBlockState(position.down());
  205.             if (offsetState.getBlock() == this) state = state.withProperty(FACING, offsetState.getValue(FACING)).withProperty(OPEN, offsetState.getValue(OPEN));
  206.         }
  207.         return state;
  208.     }
  209.  
  210.     @Override
  211.     protected BlockStateContainer createBlockState()
  212.     {
  213.         return new BlockStateContainer(this, new IProperty[] {HALF, FACING, OPEN, HINGE, POWERED, REINFORCED});
  214.     }
  215.  
  216.     @Override
  217.     public int getMetaFromState(IBlockState state)
  218.     {
  219.         int meta = 0;
  220.         if(state.getValue(HALF) == EnumDoorHalf.LOWER) // 1 bit is half, 2 is open and 3-4 are facing
  221.         {
  222.             if(state.getValue(OPEN)) meta |= 4;
  223.             meta |= state.getValue(FACING).getHorizontalIndex();
  224.         }
  225.         else // 1 bit is half, 2 is hinge, 3 is powered and 4 is reinforced
  226.         {
  227.             meta |= 8;
  228.             if(state.getValue(HINGE) == EnumHingePosition.RIGHT) meta |= 4;
  229.             if(state.getValue(POWERED)) meta |= 2;
  230.             if(state.getValue(REINFORCED)) meta |= 1;
  231.         }
  232.         return meta;
  233.     }
  234.  
  235.     @Override
  236.     public IBlockState getStateFromMeta(int meta)
  237.     {
  238.         IBlockState state = this.getDefaultState();
  239.         if((meta & 8) == 0)
  240.         {
  241.             state = state.withProperty(HALF, EnumDoorHalf.LOWER);
  242.             state = state.withProperty(OPEN, (meta & 4) != 0);
  243.             state = state.withProperty(FACING, EnumFacing.getHorizontal(meta & 3));
  244.         }
  245.         else
  246.         {
  247.             state = state.withProperty(HALF, EnumDoorHalf.UPPER);
  248.             state = state.withProperty(HINGE, (meta & 4) != 0 ? EnumHingePosition.RIGHT : EnumHingePosition.LEFT);
  249.             state = state.withProperty(POWERED, (meta & 2) != 0);
  250.             state = state.withProperty(REINFORCED, (meta & 1) != 0);
  251.         }
  252.         return state;
  253.     }
  254.  
  255.     @Override
  256.     public boolean isPassable(IBlockAccess world, BlockPos position)
  257.     {
  258.         return world.getBlockState(position).getValue(OPEN);
  259.     }
  260.  
  261.     @Override
  262.     public EnumPushReaction getMobilityFlag(IBlockState state)
  263.     {
  264.         return EnumPushReaction.DESTROY;
  265.     }
  266.  
  267.     @Override
  268.     public boolean isOpaqueCube(IBlockState state)
  269.     {
  270.         return false;
  271.     }
  272.  
  273.     @Override
  274.     public boolean isFullCube(IBlockState state)
  275.     {
  276.         return false;
  277.     }
  278.  
  279.     @Override
  280.     public BlockFaceShape getBlockFaceShape(IBlockAccess world, IBlockState state, BlockPos position, EnumFacing face)
  281.     {
  282.         return BlockFaceShape.UNDEFINED;
  283.     }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement