Skip to content

Instantly share code, notes, and snippets.

@Random832
Created July 21, 2021 04:07
Show Gist options
  • Save Random832/9ffc0c25b4c41e116429cb33863c7891 to your computer and use it in GitHub Desktop.
Save Random832/9ffc0c25b4c41e116429cb33863c7891 to your computer and use it in GitHub Desktop.
six-way connecting log block
package random832.content.block.wood;
import mcp.MethodsReturnNonnullByDefault;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.IWaterLoggable;
import net.minecraft.block.SixWayBlock;
import net.minecraft.block.material.Material;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.state.BooleanProperty;
import net.minecraft.state.EnumProperty;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.gen.IWorldGenerationBaseReader;
import javax.annotation.ParametersAreNonnullByDefault;
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public class MiniLogBlock extends SixWayBlock implements IWaterLoggable {
public static final EnumProperty<Direction.Axis> AXIS = BlockStateProperties.AXIS;
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
public MiniLogBlock(Properties properties) {
super(5f / 16, properties);
setDefaultState(getStateContainer().getBaseState()
.with(AXIS, Direction.Axis.Y)
.with(UP, true)
.with(DOWN, true)
.with(NORTH, false)
.with(SOUTH, false)
.with(EAST, false)
.with(WEST, false)
.with(WATERLOGGED, false));
}
@Override
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
super.fillStateContainer(builder);
builder.add(NORTH, EAST, WEST, SOUTH, UP, DOWN, AXIS, WATERLOGGED);
}
public BlockState getStateForPlacement(BlockItemUseContext context) {
IBlockReader level = context.getWorld();
BlockPos pos = context.getPos();
FluidState fluidstate = context.getWorld().getFluidState(context.getPos());
Direction.Axis axis = context.getFace().getAxis();
return this.getDefaultState().with(AXIS, axis)
.with(NORTH, initConnect(level, Direction.NORTH, pos, axis))
.with(SOUTH, initConnect(level, Direction.SOUTH, pos, axis))
.with(EAST, initConnect(level, Direction.EAST, pos, axis))
.with(WEST, initConnect(level, Direction.WEST, pos, axis))
.with(UP, initConnect(level, Direction.UP, pos, axis))
.with(DOWN, initConnect(level, Direction.DOWN, pos, axis))
.with(WATERLOGGED, fluidstate.getFluid() == Fluids.WATER);
}
public BlockState fixupWorldgenState(IWorldGenerationBaseReader level, BlockPos pos, BlockState fixState) {
Direction.Axis axis = fixState.get(AXIS);
return fixState
.with(NORTH, initConnect(level, Direction.NORTH, pos, axis))
.with(SOUTH, initConnect(level, Direction.SOUTH, pos, axis))
.with(EAST, initConnect(level, Direction.EAST, pos, axis))
.with(WEST, initConnect(level, Direction.WEST, pos, axis))
.with(UP, initConnect(level, Direction.UP, pos, axis))
.with(DOWN, initConnect(level, Direction.DOWN, pos, axis));
}
private boolean initConnect(IBlockReader level, Direction dir, BlockPos pos, Direction.Axis axis) {
if (dir.getAxis() == axis) return true;
BlockPos facingPos = pos.offset(dir);
BlockState facingState = level.getBlockState(facingPos);
return canConnect(facingState);
}
private boolean initConnect(IWorldGenerationBaseReader level, Direction dir, BlockPos pos, Direction.Axis axis) {
if (dir.getAxis() == axis) return true;
BlockPos facingPos = pos.offset(dir);
return level.hasBlockState(facingPos, this::canConnect);
}
public FluidState getFluidState(BlockState state) {
return state.get(WATERLOGGED) ? Fluids.WATER.getStillFluidState(false) : super.getFluidState(state);
}
public BlockState updatePostPlacement(BlockState state, Direction facing, BlockState facingState, IWorld level, BlockPos currentPos, BlockPos facingPos) {
if (facing.getAxis() == state.get(AXIS)) return state;
return state.with(FACING_TO_PROPERTY_MAP.get(facing), canConnect(facingState));
}
private boolean canConnect(BlockState facingState) {
return facingState.matchesBlock(this) || facingState.getMaterial() == Material.LEAVES;
}
@Override
public int getFlammability(BlockState state, IBlockReader level, BlockPos pos, Direction face) {
return 5;
}
@Override
public int getFireSpreadSpeed(BlockState state, IBlockReader level, BlockPos pos, Direction face) {
return 5;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment