Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Class for a generic fence gate in 1.12.2: unlike the vanilla one, the constructors don't require a wood plank type
- */
- import net.minecraftforge.fml.relauncher.SideOnly;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraft.world.World;
- import net.minecraft.world.IBlockAccess;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.math.AxisAlignedBB;
- import net.minecraft.util.Rotation;
- import net.minecraft.util.Mirror;
- import net.minecraft.util.EnumHand;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.init.Blocks;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.EntityLivingBase;
- import net.minecraft.creativetab.CreativeTabs;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.block.state.BlockStateContainer;
- import net.minecraft.block.state.BlockFaceShape;
- import net.minecraft.block.properties.PropertyBool;
- import net.minecraft.block.properties.IProperty;
- import net.minecraft.block.material.Material;
- import net.minecraft.block.material.MapColor;
- import net.minecraft.block.SoundType;
- import net.minecraft.block.BlockWall;
- import net.minecraft.block.BlockHorizontal;
- import net.minecraft.block.BlockFenceGate;
- import net.minecraft.block.BlockFence;
- import net.minecraft.block.Block;
- import javax.annotation.Nullable;
- public class BlockCustomFenceGate extends BlockHorizontal {
- public static final PropertyBool OPEN = PropertyBool.create("open");
- public static final PropertyBool POWERED = PropertyBool.create("powered");
- public static final PropertyBool IN_WALL = PropertyBool.create("in_wall");
- protected static final AxisAlignedBB AABB_HITBOX_ZAXIS = new AxisAlignedBB(0.0D, 0.0D, 0.375D, 1.0D, 1.0D, 0.625D);
- protected static final AxisAlignedBB AABB_HITBOX_XAXIS = new AxisAlignedBB(0.375D, 0.0D, 0.0D, 0.625D, 1.0D, 1.0D);
- protected static final AxisAlignedBB AABB_HITBOX_ZAXIS_INWALL = new AxisAlignedBB(0.0D, 0.0D, 0.375D, 1.0D, 0.8125D, 0.625D);
- protected static final AxisAlignedBB AABB_HITBOX_XAXIS_INWALL = new AxisAlignedBB(0.375D, 0.0D, 0.0D, 0.625D, 0.8125D, 1.0D);
- protected static final AxisAlignedBB AABB_COLLISION_BOX_ZAXIS = new AxisAlignedBB(0.0D, 0.0D, 0.375D, 1.0D, 1.5D, 0.625D);
- protected static final AxisAlignedBB AABB_COLLISION_BOX_XAXIS = new AxisAlignedBB(0.375D, 0.0D, 0.0D, 0.625D, 1.5D, 1.0D);
- // I added a quick constructor for wooden gates, for the sake of convenience
- public BlockCustomFenceGate(MapColor mapColor) {
- this(Material.WOOD, mapColor);
- setSoundType(SoundType.WOOD);
- setHardness(2F);
- setResistance(15F);
- Blocks.FIRE.setFireInfo(this, 5, 20);
- this.setDefaultState(this.blockState.getBaseState().withProperty(OPEN, Boolean.valueOf(false)).withProperty(POWERED, Boolean.valueOf(false)).withProperty(IN_WALL, Boolean.valueOf(false)));
- }
- // More generic constructor: hardness, resistance and sound type should be added manually
- public BlockCustomFenceGate(Material material, MapColor mapColor) {
- super(material, mapColor);
- setLightLevel(0F);
- setLightOpacity(0);
- setCreativeTab(CreativeTabs.REDSTONE);
- this.setDefaultState(this.blockState.getBaseState().withProperty(OPEN, Boolean.valueOf(false)).withProperty(POWERED, Boolean.valueOf(false)).withProperty(IN_WALL, Boolean.valueOf(false)));
- }
- public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
- state = this.getActualState(state, source, pos);
- if (((Boolean) state.getValue(IN_WALL)).booleanValue()) {
- return ((EnumFacing) state.getValue(FACING)).getAxis() == EnumFacing.Axis.X ? AABB_HITBOX_XAXIS_INWALL : AABB_HITBOX_ZAXIS_INWALL;
- } else {
- return ((EnumFacing) state.getValue(FACING)).getAxis() == EnumFacing.Axis.X ? AABB_HITBOX_XAXIS : AABB_HITBOX_ZAXIS;
- }
- }
- public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
- EnumFacing.Axis enumfacing$axis = ((EnumFacing) state.getValue(FACING)).getAxis();
- 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)) {
- state = state.withProperty(IN_WALL, Boolean.valueOf(true));
- }
- return state;
- }
- public IBlockState withRotation(IBlockState state, Rotation rot) {
- return state.withProperty(FACING, rot.rotate((EnumFacing) state.getValue(FACING)));
- }
- public IBlockState withMirror(IBlockState state, Mirror mirrorIn) {
- return state.withRotation(mirrorIn.toRotation((EnumFacing) state.getValue(FACING)));
- }
- @Nullable
- public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, IBlockAccess worldIn, BlockPos pos) {
- if (((Boolean) blockState.getValue(OPEN)).booleanValue()) {
- return NULL_AABB;
- } else {
- return ((EnumFacing) blockState.getValue(FACING)).getAxis() == EnumFacing.Axis.Z ? AABB_COLLISION_BOX_ZAXIS : AABB_COLLISION_BOX_XAXIS;
- }
- }
- /*
- * Fence gates in 1.12 can't be placed if the block below is not solid. I
- * think this behavior was removed in 1.14, but I'm not sure
- */
- public boolean canPlaceBlockAt(World worldIn, BlockPos pos) {
- return worldIn.getBlockState(pos.down()).getMaterial().isSolid() ? super.canPlaceBlockAt(worldIn, pos) : false;
- }
- public boolean isOpaqueCube(IBlockState state) {
- return false;
- }
- public boolean isFullCube(IBlockState state) {
- return false;
- }
- public boolean isPassable(IBlockAccess worldIn, BlockPos pos) {
- return ((Boolean) worldIn.getBlockState(pos).getValue(OPEN)).booleanValue();
- }
- public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) {
- boolean flag = worldIn.isBlockPowered(pos);
- return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing()).withProperty(OPEN, Boolean.valueOf(flag)).withProperty(POWERED, Boolean.valueOf(flag)).withProperty(IN_WALL, Boolean.valueOf(false));
- }
- public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
- if (((Boolean) state.getValue(OPEN)).booleanValue()) {
- state = state.withProperty(OPEN, Boolean.valueOf(false));
- worldIn.setBlockState(pos, state, 10);
- } else {
- EnumFacing enumfacing = EnumFacing.fromAngle((double) playerIn.rotationYaw);
- if (state.getValue(FACING) == enumfacing.getOpposite()) {
- state = state.withProperty(FACING, enumfacing);
- }
- state = state.withProperty(OPEN, Boolean.valueOf(true));
- worldIn.setBlockState(pos, state, 10);
- }
- worldIn.playEvent(playerIn, ((Boolean) state.getValue(OPEN)).booleanValue() ? 1008 : 1014, pos, 0);
- return true;
- }
- public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos) {
- if (!worldIn.isRemote) {
- boolean flag = worldIn.isBlockPowered(pos);
- if (((Boolean) state.getValue(POWERED)).booleanValue() != flag) {
- worldIn.setBlockState(pos, state.withProperty(POWERED, Boolean.valueOf(flag)).withProperty(OPEN, Boolean.valueOf(flag)), 2);
- if (((Boolean) state.getValue(OPEN)).booleanValue() != flag) {
- worldIn.playEvent((EntityPlayer) null, flag ? 1008 : 1014, pos, 0);
- }
- }
- }
- }
- @SideOnly(Side.CLIENT)
- public boolean shouldSideBeRendered(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side) {
- return true;
- }
- public IBlockState getStateFromMeta(int meta) {
- return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)).withProperty(OPEN, Boolean.valueOf((meta & 4) != 0)).withProperty(POWERED, Boolean.valueOf((meta & 8) != 0));
- }
- public int getMetaFromState(IBlockState state) {
- int i = 0;
- i = i | ((EnumFacing) state.getValue(FACING)).getHorizontalIndex();
- if (((Boolean) state.getValue(POWERED)).booleanValue()) {
- i |= 8;
- }
- if (((Boolean) state.getValue(OPEN)).booleanValue()) {
- i |= 4;
- }
- return i;
- }
- protected BlockStateContainer createBlockState() {
- return new BlockStateContainer(this, new IProperty[]{FACING, OPEN, POWERED, IN_WALL});
- }
- @Override
- public boolean canBeConnectedTo(IBlockAccess world, BlockPos pos, EnumFacing facing) {
- IBlockState state = world.getBlockState(pos);
- if (state.getBlock() instanceof BlockFenceGate && state.getBlockFaceShape(world, pos, facing) == BlockFaceShape.MIDDLE_POLE) {
- Block connector = world.getBlockState(pos.offset(facing)).getBlock();
- return connector instanceof BlockFence || connector instanceof BlockWall;
- }
- return false;
- }
- public BlockFaceShape getBlockFaceShape(IBlockAccess worldIn, IBlockState state, BlockPos pos, EnumFacing face) {
- if (face != EnumFacing.UP && face != EnumFacing.DOWN) {
- return ((EnumFacing) state.getValue(FACING)).getAxis() == face.rotateY().getAxis() ? BlockFaceShape.MIDDLE_POLE : BlockFaceShape.UNDEFINED;
- } else {
- return BlockFaceShape.UNDEFINED;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement