Advertisement
Guest User

Untitled

a guest
Dec 27th, 2019
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.08 KB | None | 0 0
  1. /*
  2.  * Class for a generic fence gate in 1.12.2: unlike the vanilla one, the constructors don't require a wood plank type
  3.  */
  4.  
  5. import net.minecraftforge.fml.relauncher.SideOnly;
  6. import net.minecraftforge.fml.relauncher.Side;
  7.  
  8. import net.minecraft.world.World;
  9. import net.minecraft.world.IBlockAccess;
  10. import net.minecraft.util.math.BlockPos;
  11. import net.minecraft.util.math.AxisAlignedBB;
  12. import net.minecraft.util.Rotation;
  13. import net.minecraft.util.Mirror;
  14. import net.minecraft.util.EnumHand;
  15. import net.minecraft.util.EnumFacing;
  16. import net.minecraft.init.Blocks;
  17. import net.minecraft.entity.player.EntityPlayer;
  18. import net.minecraft.entity.EntityLivingBase;
  19. import net.minecraft.creativetab.CreativeTabs;
  20. import net.minecraft.block.state.IBlockState;
  21. import net.minecraft.block.state.BlockStateContainer;
  22. import net.minecraft.block.state.BlockFaceShape;
  23. import net.minecraft.block.properties.PropertyBool;
  24. import net.minecraft.block.properties.IProperty;
  25. import net.minecraft.block.material.Material;
  26. import net.minecraft.block.material.MapColor;
  27. import net.minecraft.block.SoundType;
  28. import net.minecraft.block.BlockWall;
  29. import net.minecraft.block.BlockHorizontal;
  30. import net.minecraft.block.BlockFenceGate;
  31. import net.minecraft.block.BlockFence;
  32. import net.minecraft.block.Block;
  33.  
  34. import javax.annotation.Nullable;
  35.  
  36. public class BlockCustomFenceGate extends BlockHorizontal {
  37.     public static final PropertyBool OPEN = PropertyBool.create("open");
  38.     public static final PropertyBool POWERED = PropertyBool.create("powered");
  39.     public static final PropertyBool IN_WALL = PropertyBool.create("in_wall");
  40.     protected static final AxisAlignedBB AABB_HITBOX_ZAXIS = new AxisAlignedBB(0.0D, 0.0D, 0.375D, 1.0D, 1.0D, 0.625D);
  41.     protected static final AxisAlignedBB AABB_HITBOX_XAXIS = new AxisAlignedBB(0.375D, 0.0D, 0.0D, 0.625D, 1.0D, 1.0D);
  42.     protected static final AxisAlignedBB AABB_HITBOX_ZAXIS_INWALL = new AxisAlignedBB(0.0D, 0.0D, 0.375D, 1.0D, 0.8125D, 0.625D);
  43.     protected static final AxisAlignedBB AABB_HITBOX_XAXIS_INWALL = new AxisAlignedBB(0.375D, 0.0D, 0.0D, 0.625D, 0.8125D, 1.0D);
  44.     protected static final AxisAlignedBB AABB_COLLISION_BOX_ZAXIS = new AxisAlignedBB(0.0D, 0.0D, 0.375D, 1.0D, 1.5D, 0.625D);
  45.     protected static final AxisAlignedBB AABB_COLLISION_BOX_XAXIS = new AxisAlignedBB(0.375D, 0.0D, 0.0D, 0.625D, 1.5D, 1.0D);
  46.  
  47.     // I added a quick constructor for wooden gates, for the sake of convenience
  48.     public BlockCustomFenceGate(MapColor mapColor) {
  49.         this(Material.WOOD, mapColor);
  50.         setSoundType(SoundType.WOOD);
  51.         setHardness(2F);
  52.         setResistance(15F);
  53.         Blocks.FIRE.setFireInfo(this, 5, 20);
  54.         this.setDefaultState(this.blockState.getBaseState().withProperty(OPEN, Boolean.valueOf(false)).withProperty(POWERED, Boolean.valueOf(false)).withProperty(IN_WALL, Boolean.valueOf(false)));
  55.     }
  56.  
  57.     // More generic constructor: hardness, resistance and sound type should be added manually
  58.     public BlockCustomFenceGate(Material material, MapColor mapColor) {
  59.         super(material, mapColor);
  60.         setLightLevel(0F);
  61.         setLightOpacity(0);
  62.         setCreativeTab(CreativeTabs.REDSTONE);
  63.         this.setDefaultState(this.blockState.getBaseState().withProperty(OPEN, Boolean.valueOf(false)).withProperty(POWERED, Boolean.valueOf(false)).withProperty(IN_WALL, Boolean.valueOf(false)));
  64.     }
  65.  
  66.     public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
  67.         state = this.getActualState(state, source, pos);
  68.         if (((Boolean) state.getValue(IN_WALL)).booleanValue()) {
  69.             return ((EnumFacing) state.getValue(FACING)).getAxis() == EnumFacing.Axis.X ? AABB_HITBOX_XAXIS_INWALL : AABB_HITBOX_ZAXIS_INWALL;
  70.         } else {
  71.             return ((EnumFacing) state.getValue(FACING)).getAxis() == EnumFacing.Axis.X ? AABB_HITBOX_XAXIS : AABB_HITBOX_ZAXIS;
  72.         }
  73.     }
  74.  
  75.     public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
  76.         EnumFacing.Axis enumfacing$axis = ((EnumFacing) state.getValue(FACING)).getAxis();
  77.         if (enumfacing$axis == EnumFacing.Axis.Z && (worldIn.getBlockState(pos.west()).getBlock() instanceof BlockWall || worldIn.getBlockState(pos.east()).getBlock() instanceof BlockWall) || enumfacing$axis == EnumFacing.Axis.X && (worldIn.getBlockState(pos.north()).getBlock() instanceof BlockWall || worldIn.getBlockState(pos.south()).getBlock() instanceof BlockWall)) {
  78.             state = state.withProperty(IN_WALL, Boolean.valueOf(true));
  79.         }
  80.         return state;
  81.     }
  82.  
  83.     public IBlockState withRotation(IBlockState state, Rotation rot) {
  84.         return state.withProperty(FACING, rot.rotate((EnumFacing) state.getValue(FACING)));
  85.     }
  86.  
  87.     public IBlockState withMirror(IBlockState state, Mirror mirrorIn) {
  88.         return state.withRotation(mirrorIn.toRotation((EnumFacing) state.getValue(FACING)));
  89.     }
  90.  
  91.     @Nullable
  92.     public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, IBlockAccess worldIn, BlockPos pos) {
  93.         if (((Boolean) blockState.getValue(OPEN)).booleanValue()) {
  94.             return NULL_AABB;
  95.         } else {
  96.             return ((EnumFacing) blockState.getValue(FACING)).getAxis() == EnumFacing.Axis.Z ? AABB_COLLISION_BOX_ZAXIS : AABB_COLLISION_BOX_XAXIS;
  97.         }
  98.     }
  99.  
  100.     /*
  101.      * Fence gates in 1.12 can't be placed if the block below is not solid. I
  102.      * think this behavior was removed in 1.14, but I'm not sure
  103.      */
  104.     public boolean canPlaceBlockAt(World worldIn, BlockPos pos) {
  105.         return worldIn.getBlockState(pos.down()).getMaterial().isSolid() ? super.canPlaceBlockAt(worldIn, pos) : false;
  106.     }
  107.  
  108.     public boolean isOpaqueCube(IBlockState state) {
  109.         return false;
  110.     }
  111.  
  112.     public boolean isFullCube(IBlockState state) {
  113.         return false;
  114.     }
  115.  
  116.     public boolean isPassable(IBlockAccess worldIn, BlockPos pos) {
  117.         return ((Boolean) worldIn.getBlockState(pos).getValue(OPEN)).booleanValue();
  118.     }
  119.  
  120.     public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) {
  121.         boolean flag = worldIn.isBlockPowered(pos);
  122.         return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing()).withProperty(OPEN, Boolean.valueOf(flag)).withProperty(POWERED, Boolean.valueOf(flag)).withProperty(IN_WALL, Boolean.valueOf(false));
  123.     }
  124.  
  125.     public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing,    float hitX, float hitY, float hitZ) {
  126.         if (((Boolean) state.getValue(OPEN)).booleanValue()) {
  127.             state = state.withProperty(OPEN, Boolean.valueOf(false));
  128.             worldIn.setBlockState(pos, state, 10);
  129.         } else {
  130.             EnumFacing enumfacing = EnumFacing.fromAngle((double) playerIn.rotationYaw);
  131.             if (state.getValue(FACING) == enumfacing.getOpposite()) {
  132.                 state = state.withProperty(FACING, enumfacing);
  133.             }
  134.             state = state.withProperty(OPEN, Boolean.valueOf(true));
  135.             worldIn.setBlockState(pos, state, 10);
  136.         }
  137.         worldIn.playEvent(playerIn, ((Boolean) state.getValue(OPEN)).booleanValue() ? 1008 : 1014, pos, 0);
  138.         return true;
  139.     }
  140.  
  141.     public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos) {
  142.         if (!worldIn.isRemote) {
  143.             boolean flag = worldIn.isBlockPowered(pos);
  144.             if (((Boolean) state.getValue(POWERED)).booleanValue() != flag) {
  145.                 worldIn.setBlockState(pos, state.withProperty(POWERED, Boolean.valueOf(flag)).withProperty(OPEN, Boolean.valueOf(flag)), 2);
  146.                 if (((Boolean) state.getValue(OPEN)).booleanValue() != flag) {
  147.                     worldIn.playEvent((EntityPlayer) null, flag ? 1008 : 1014, pos, 0);
  148.                 }
  149.             }
  150.         }
  151.     }
  152.  
  153.     @SideOnly(Side.CLIENT)
  154.     public boolean shouldSideBeRendered(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side) {
  155.         return true;
  156.     }
  157.  
  158.     public IBlockState getStateFromMeta(int meta) {
  159.         return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)).withProperty(OPEN, Boolean.valueOf((meta & 4) != 0)).withProperty(POWERED, Boolean.valueOf((meta & 8) != 0));
  160.     }
  161.  
  162.     public int getMetaFromState(IBlockState state) {
  163.         int i = 0;
  164.         i = i | ((EnumFacing) state.getValue(FACING)).getHorizontalIndex();
  165.         if (((Boolean) state.getValue(POWERED)).booleanValue()) {
  166.             i |= 8;
  167.         }
  168.         if (((Boolean) state.getValue(OPEN)).booleanValue()) {
  169.             i |= 4;
  170.         }
  171.         return i;
  172.     }
  173.  
  174.     protected BlockStateContainer createBlockState() {
  175.         return new BlockStateContainer(this, new IProperty[]{FACING, OPEN, POWERED, IN_WALL});
  176.     }
  177.  
  178.     @Override
  179.     public boolean canBeConnectedTo(IBlockAccess world, BlockPos pos, EnumFacing facing) {
  180.         IBlockState state = world.getBlockState(pos);
  181.         if (state.getBlock() instanceof BlockFenceGate && state.getBlockFaceShape(world, pos, facing) == BlockFaceShape.MIDDLE_POLE) {
  182.             Block connector = world.getBlockState(pos.offset(facing)).getBlock();
  183.             return connector instanceof BlockFence || connector instanceof BlockWall;
  184.         }
  185.         return false;
  186.     }
  187.  
  188.     public BlockFaceShape getBlockFaceShape(IBlockAccess worldIn, IBlockState state, BlockPos pos, EnumFacing face) {
  189.         if (face != EnumFacing.UP && face != EnumFacing.DOWN) {
  190.             return ((EnumFacing) state.getValue(FACING)).getAxis() == face.rotateY().getAxis() ? BlockFaceShape.MIDDLE_POLE : BlockFaceShape.UNDEFINED;
  191.         } else {
  192.             return BlockFaceShape.UNDEFINED;
  193.         }
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement