NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/BlockGoldPowder.java

199 lines
8 KiB
Java
Raw Normal View History

2018-10-16 01:36:30 +02:00
package de.ellpeck.naturesaura.blocks;
import de.ellpeck.naturesaura.reg.IColorProvidingBlock;
import net.minecraft.block.Block;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
2019-11-04 19:08:49 +01:00
import net.minecraft.block.state.BlockFaceShape;
import net.minecraft.client.renderer.color.IBlockColor;
import net.minecraft.state.EnumProperty;
import net.minecraft.state.StateContainer;
2018-10-16 01:36:30 +02:00
import net.minecraft.util.BlockRenderLayer;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.Direction;
2018-10-16 01:36:30 +02:00
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
2019-11-04 19:08:49 +01:00
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.shapes.VoxelShapes;
2018-10-16 01:36:30 +02:00
import net.minecraft.world.IBlockAccess;
2019-11-04 19:08:49 +01:00
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorldReader;
2019-10-20 22:30:49 +02:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
2018-10-16 01:36:30 +02:00
import javax.annotation.Nullable;
2019-11-04 19:08:49 +01:00
// TODO
2018-10-16 01:36:30 +02:00
public class BlockGoldPowder extends BlockImpl implements IColorProvidingBlock {
2019-11-04 19:08:49 +01:00
public static final EnumProperty<AttachPos> NORTH = EnumProperty.create("north", AttachPos.class);
public static final EnumProperty<AttachPos> EAST = EnumProperty.create("east", AttachPos.class);
public static final EnumProperty<AttachPos> SOUTH = EnumProperty.create("south", AttachPos.class);
public static final EnumProperty<AttachPos> WEST = EnumProperty.create("west", AttachPos.class);
protected static final VoxelShape[] SHAPES = new VoxelShape[]{
VoxelShapes.create(0.1875D, 0.0D, 0.1875D, 0.8125D, 0.0625D, 0.8125D),
VoxelShapes.create(0.1875D, 0.0D, 0.1875D, 0.8125D, 0.0625D, 1.0D),
VoxelShapes.create(0.0D, 0.0D, 0.1875D, 0.8125D, 0.0625D, 0.8125D),
VoxelShapes.create(0.0D, 0.0D, 0.1875D, 0.8125D, 0.0625D, 1.0D),
VoxelShapes.create(0.1875D, 0.0D, 0.0D, 0.8125D, 0.0625D, 0.8125D),
VoxelShapes.create(0.1875D, 0.0D, 0.0D, 0.8125D, 0.0625D, 1.0D),
VoxelShapes.create(0.0D, 0.0D, 0.0D, 0.8125D, 0.0625D, 0.8125D),
VoxelShapes.create(0.0D, 0.0D, 0.0D, 0.8125D, 0.0625D, 1.0D),
VoxelShapes.create(0.1875D, 0.0D, 0.1875D, 1.0D, 0.0625D, 0.8125D),
VoxelShapes.create(0.1875D, 0.0D, 0.1875D, 1.0D, 0.0625D, 1.0D),
VoxelShapes.create(0.0D, 0.0D, 0.1875D, 1.0D, 0.0625D, 0.8125D),
VoxelShapes.create(0.0D, 0.0D, 0.1875D, 1.0D, 0.0625D, 1.0D),
VoxelShapes.create(0.1875D, 0.0D, 0.0D, 1.0D, 0.0625D, 0.8125D),
VoxelShapes.create(0.1875D, 0.0D, 0.0D, 1.0D, 0.0625D, 1.0D),
VoxelShapes.create(0.0D, 0.0D, 0.0D, 1.0D, 0.0625D, 0.8125D),
VoxelShapes.create(0.0D, 0.0D, 0.0D, 1.0D, 0.0625D, 1.0D)
2018-10-16 01:36:30 +02:00
};
public BlockGoldPowder() {
2019-11-04 19:08:49 +01:00
super("gold_powder", ModBlocks.prop(Blocks.REDSTONE_WIRE));
this.setDefaultState(this.getDefaultState()
2019-11-04 19:08:49 +01:00
.with(NORTH, AttachPos.NONE)
.with(EAST, AttachPos.NONE)
.with(SOUTH, AttachPos.NONE)
.with(WEST, AttachPos.NONE));
2018-10-16 01:36:30 +02:00
}
@Override
2019-11-04 19:08:49 +01:00
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
builder.add(NORTH, EAST, SOUTH, WEST);
2018-10-16 01:36:30 +02:00
}
@Override
2019-11-04 19:08:49 +01:00
public IBlockColor getBlockColor() {
return (state, worldIn, pos, tintIndex) -> 0xf4cb42;
2018-10-16 01:36:30 +02:00
}
@Override
2019-11-04 19:08:49 +01:00
public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
return SHAPES[getShapeIndex(state)];
2018-10-16 01:36:30 +02:00
}
@Override
2019-10-20 22:30:49 +02:00
public AxisAlignedBB getBoundingBox(BlockState state, IBlockAccess source, BlockPos pos) {
2019-11-04 19:08:49 +01:00
return SHAPES[getShapeIndex(state.getActualState(source, pos))];
2018-10-16 01:36:30 +02:00
}
2019-11-04 19:08:49 +01:00
private static int getShapeIndex(BlockState state) {
2018-10-16 01:36:30 +02:00
int i = 0;
2019-11-04 19:08:49 +01:00
boolean n = state.get(NORTH) != AttachPos.NONE;
boolean e = state.get(EAST) != AttachPos.NONE;
boolean s = state.get(SOUTH) != AttachPos.NONE;
boolean w = state.get(WEST) != AttachPos.NONE;
2018-10-16 01:36:30 +02:00
if (n || s && !n && !e && !w) {
2019-10-20 22:30:49 +02:00
i |= 1 << Direction.NORTH.getHorizontalIndex();
2018-10-16 01:36:30 +02:00
}
if (e || w && !n && !e && !s) {
2019-10-20 22:30:49 +02:00
i |= 1 << Direction.EAST.getHorizontalIndex();
2018-10-16 01:36:30 +02:00
}
if (s || n && !e && !s && !w) {
2019-10-20 22:30:49 +02:00
i |= 1 << Direction.SOUTH.getHorizontalIndex();
2018-10-16 01:36:30 +02:00
}
if (w || e && !n && !s && !w) {
2019-10-20 22:30:49 +02:00
i |= 1 << Direction.WEST.getHorizontalIndex();
2018-10-16 01:36:30 +02:00
}
return i;
}
@Override
2019-10-20 22:30:49 +02:00
public BlockState getActualState(BlockState state, IBlockAccess worldIn, BlockPos pos) {
state = state.withProperty(WEST, this.getAttachPosition(worldIn, pos, Direction.WEST));
state = state.withProperty(EAST, this.getAttachPosition(worldIn, pos, Direction.EAST));
state = state.withProperty(NORTH, this.getAttachPosition(worldIn, pos, Direction.NORTH));
state = state.withProperty(SOUTH, this.getAttachPosition(worldIn, pos, Direction.SOUTH));
2018-10-16 01:36:30 +02:00
return state;
}
2019-10-20 22:30:49 +02:00
private AttachPos getAttachPosition(IBlockAccess worldIn, BlockPos pos, Direction direction) {
2018-10-16 01:36:30 +02:00
BlockPos dirPos = pos.offset(direction);
2019-10-20 22:30:49 +02:00
BlockState state = worldIn.getBlockState(pos.offset(direction));
2018-10-16 01:36:30 +02:00
if (!this.canConnectTo(worldIn.getBlockState(dirPos), direction, worldIn, dirPos)
&& (state.isNormalCube() || !this.canConnectUpwardsTo(worldIn, dirPos.down()))) {
2019-10-20 22:30:49 +02:00
BlockState iblockstate1 = worldIn.getBlockState(pos.up());
2018-10-16 01:36:30 +02:00
if (!iblockstate1.isNormalCube()) {
2019-10-20 22:30:49 +02:00
boolean flag = worldIn.getBlockState(dirPos).isSideSolid(worldIn, dirPos, Direction.UP)
2018-10-16 01:36:30 +02:00
|| worldIn.getBlockState(dirPos).getBlock() == Blocks.GLOWSTONE;
if (flag && this.canConnectUpwardsTo(worldIn, dirPos.up())) {
if (state.isBlockNormalCube()) {
return AttachPos.UP;
}
return AttachPos.SIDE;
}
}
return AttachPos.NONE;
} else {
return AttachPos.SIDE;
}
}
@Override
2019-11-04 19:08:49 +01:00
public VoxelShape getCollisionShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
return VoxelShapes.empty();
2018-10-16 01:36:30 +02:00
}
@Override
2019-11-04 19:08:49 +01:00
public boolean isFireSource(BlockState state, IBlockReader world, BlockPos pos, Direction side) {
2018-10-16 01:36:30 +02:00
return false;
}
2019-11-04 19:08:49 +01:00
public boolean canPlaceBlockAt(IWorldReader worldIn, BlockPos pos) {
2019-10-20 22:30:49 +02:00
BlockState downState = worldIn.getBlockState(pos.down());
2019-11-04 19:08:49 +01:00
return downState.isSolid()
2019-10-20 22:30:49 +02:00
|| downState.getBlockFaceShape(worldIn, pos.down(), Direction.UP) == BlockFaceShape.SOLID
2018-10-16 01:36:30 +02:00
|| worldIn.getBlockState(pos.down()).getBlock() == Blocks.GLOWSTONE;
}
@Override
2019-11-04 19:08:49 +01:00
public void onNeighborChange(BlockState state, IWorldReader world, BlockPos pos, BlockPos neighbor) {
if (!world.isRemote()) {
if (!this.canPlaceBlockAt(world, pos)) {
this.dropBlockAsItem(world, pos, state, 0);
world.setBlockToAir(pos);
2018-10-16 01:36:30 +02:00
}
}
}
private boolean canConnectUpwardsTo(IBlockAccess worldIn, BlockPos pos) {
return this.canConnectTo(worldIn.getBlockState(pos), null, worldIn, pos);
}
2019-10-20 22:30:49 +02:00
private boolean canConnectTo(BlockState blockState, @Nullable Direction side, IBlockAccess world, BlockPos pos) {
2018-10-16 01:36:30 +02:00
Block block = blockState.getBlock();
return block == this;
}
@Override
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
2018-10-16 01:36:30 +02:00
public BlockRenderLayer getRenderLayer() {
return BlockRenderLayer.CUTOUT;
}
private enum AttachPos implements IStringSerializable {
UP("up"),
SIDE("side"),
NONE("none");
private final String name;
AttachPos(String name) {
this.name = name;
}
public String toString() {
return this.getName();
}
@Override
public String getName() {
return this.name;
}
}
}