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

240 lines
12 KiB
Java
Raw Normal View History

2018-10-16 01:36:30 +02:00
package de.ellpeck.naturesaura.blocks;
2020-01-29 00:40:28 +01:00
import de.ellpeck.naturesaura.data.BlockStateGenerator;
import de.ellpeck.naturesaura.data.ItemModelGenerator;
2018-10-16 01:36:30 +02:00
import de.ellpeck.naturesaura.reg.IColorProvidingBlock;
2020-01-29 00:40:28 +01:00
import de.ellpeck.naturesaura.reg.ICustomBlockState;
import de.ellpeck.naturesaura.reg.ICustomItemModel;
2020-01-29 01:34:58 +01:00
import de.ellpeck.naturesaura.reg.ICustomRenderType;
import net.minecraft.client.color.block.BlockColor;
2020-01-29 01:34:58 +01:00
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.RedStoneWireBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.RedstoneSide;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
2018-10-16 01:36:30 +02:00
2020-01-29 01:34:58 +01:00
import java.util.function.Supplier;
public class BlockGoldPowder extends BlockImpl implements IColorProvidingBlock, ICustomBlockState, ICustomItemModel, ICustomRenderType {
2018-10-16 01:36:30 +02:00
public static final EnumProperty<RedstoneSide> NORTH = BlockStateProperties.NORTH_REDSTONE;
public static final EnumProperty<RedstoneSide> EAST = BlockStateProperties.EAST_REDSTONE;
public static final EnumProperty<RedstoneSide> SOUTH = BlockStateProperties.SOUTH_REDSTONE;
public static final EnumProperty<RedstoneSide> WEST = BlockStateProperties.WEST_REDSTONE;
protected static final VoxelShape[] SHAPES = new VoxelShape[]{box(3.0D, 0.0D, 3.0D, 13.0D, 1.0D, 13.0D), box(3.0D, 0.0D, 3.0D, 13.0D, 1.0D, 16.0D), box(0.0D, 0.0D, 3.0D, 13.0D, 1.0D, 13.0D), box(0.0D, 0.0D, 3.0D, 13.0D, 1.0D, 16.0D), box(3.0D, 0.0D, 0.0D, 13.0D, 1.0D, 13.0D), box(3.0D, 0.0D, 0.0D, 13.0D, 1.0D, 16.0D), box(0.0D, 0.0D, 0.0D, 13.0D, 1.0D, 13.0D), box(0.0D, 0.0D, 0.0D, 13.0D, 1.0D, 16.0D), box(3.0D, 0.0D, 3.0D, 16.0D, 1.0D, 13.0D), box(3.0D, 0.0D, 3.0D, 16.0D, 1.0D, 16.0D), box(0.0D, 0.0D, 3.0D, 16.0D, 1.0D, 13.0D), box(0.0D, 0.0D, 3.0D, 16.0D, 1.0D, 16.0D), box(3.0D, 0.0D, 0.0D, 16.0D, 1.0D, 13.0D), box(3.0D, 0.0D, 0.0D, 16.0D, 1.0D, 16.0D), box(0.0D, 0.0D, 0.0D, 16.0D, 1.0D, 13.0D), box(0.0D, 0.0D, 0.0D, 16.0D, 1.0D, 16.0D)};
2018-10-16 01:36:30 +02:00
public BlockGoldPowder() {
super("gold_powder", Properties.copy(Blocks.REDSTONE_WIRE));
this.registerDefaultState(this.defaultBlockState().setValue(NORTH, RedstoneSide.NONE).setValue(EAST, RedstoneSide.NONE).setValue(SOUTH, RedstoneSide.NONE).setValue(WEST, RedstoneSide.NONE));
2018-10-16 01:36:30 +02:00
}
2019-11-04 19:08:49 +01:00
private static int getShapeIndex(BlockState state) {
2021-12-15 16:30:22 +01:00
var i = 0;
var n = state.getValue(NORTH) != RedstoneSide.NONE;
var e = state.getValue(EAST) != RedstoneSide.NONE;
var s = state.getValue(SOUTH) != RedstoneSide.NONE;
var w = state.getValue(WEST) != RedstoneSide.NONE;
2018-10-16 01:36:30 +02:00
if (n || s && !n && !e && !w) {
2021-12-16 21:56:27 +01:00
i |= 1 << Direction.NORTH.get2DDataValue();
2018-10-16 01:36:30 +02:00
}
if (e || w && !n && !e && !s) {
2021-12-16 21:56:27 +01:00
i |= 1 << Direction.EAST.get2DDataValue();
2018-10-16 01:36:30 +02:00
}
if (s || n && !e && !s && !w) {
2021-12-16 21:56:27 +01:00
i |= 1 << Direction.SOUTH.get2DDataValue();
2018-10-16 01:36:30 +02:00
}
if (w || e && !n && !s && !w) {
2021-12-16 21:56:27 +01:00
i |= 1 << Direction.WEST.get2DDataValue();
2018-10-16 01:36:30 +02:00
}
return i;
}
2020-02-07 15:22:30 +01:00
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
2020-02-07 15:22:30 +01:00
builder.add(NORTH, EAST, SOUTH, WEST);
}
@Override
public BlockColor getBlockColor() {
2021-12-04 15:40:09 +01:00
return (state, levelIn, pos, tintIndex) -> 0xf4cb42;
2020-02-07 15:22:30 +01:00
}
@Override
public VoxelShape getShape(BlockState state, BlockGetter levelIn, BlockPos pos, CollisionContext context) {
2020-02-07 15:22:30 +01:00
return SHAPES[getShapeIndex(state)];
}
2018-10-16 01:36:30 +02:00
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
BlockGetter iblockreader = context.getLevel();
2021-12-15 16:30:22 +01:00
var blockpos = context.getClickedPos();
return this.defaultBlockState().setValue(WEST, this.getSide(iblockreader, blockpos, Direction.WEST)).setValue(EAST, this.getSide(iblockreader, blockpos, Direction.EAST)).setValue(NORTH, this.getSide(iblockreader, blockpos, Direction.NORTH)).setValue(SOUTH, this.getSide(iblockreader, blockpos, Direction.SOUTH));
2020-01-24 17:05:41 +01:00
}
@Override
public BlockState updateShape(BlockState stateIn, Direction facing, BlockState facingState, LevelAccessor levelIn, BlockPos currentPos, BlockPos facingPos) {
2020-01-24 17:05:41 +01:00
if (facing == Direction.DOWN) {
return stateIn;
2018-10-16 01:36:30 +02:00
} else {
return facing == Direction.UP ? stateIn.setValue(WEST, this.getSide(levelIn, currentPos, Direction.WEST)).setValue(EAST, this.getSide(levelIn, currentPos, Direction.EAST)).setValue(NORTH, this.getSide(levelIn, currentPos, Direction.NORTH)).setValue(SOUTH, this.getSide(levelIn, currentPos, Direction.SOUTH)) : stateIn.setValue(RedStoneWireBlock.PROPERTY_BY_DIRECTION.get(facing), this.getSide(levelIn, currentPos, facing));
2020-01-24 17:05:41 +01:00
}
}
private RedstoneSide getSide(BlockGetter levelIn, BlockPos pos, Direction face) {
2021-12-15 16:30:22 +01:00
var blockpos = pos.relative(face);
var blockstate = levelIn.getBlockState(blockpos);
var blockpos1 = pos.above();
var blockstate1 = levelIn.getBlockState(blockpos1);
if (!blockstate1.isCollisionShapeFullBlock(levelIn, blockpos1)) {
2021-12-15 16:30:22 +01:00
var flag = blockstate.isFaceSturdy(levelIn, blockpos, Direction.UP) || blockstate.getBlock() == Blocks.HOPPER;
if (flag && this.canConnectTo(levelIn.getBlockState(blockpos.above()))) {
if (blockstate.isCollisionShapeFullBlock(levelIn, blockpos)) {
2020-01-24 17:05:41 +01:00
return RedstoneSide.UP;
}
return RedstoneSide.SIDE;
}
2018-10-16 01:36:30 +02:00
}
return !this.canConnectTo(blockstate) && (blockstate.isCollisionShapeFullBlock(levelIn, blockpos) || !this.canConnectTo(levelIn.getBlockState(blockpos.below()))) ? RedstoneSide.NONE : RedstoneSide.SIDE;
2018-10-16 01:36:30 +02:00
}
2020-01-24 17:05:41 +01:00
protected boolean canConnectTo(BlockState blockState) {
2021-12-15 16:30:22 +01:00
var block = blockState.getBlock();
2020-01-24 17:05:41 +01:00
return block == this;
2018-10-16 01:36:30 +02:00
}
@Override
public boolean canSurvive(BlockState state, LevelReader levelIn, BlockPos pos) {
2021-12-15 16:30:22 +01:00
var blockpos = pos.below();
var blockstate = levelIn.getBlockState(blockpos);
return blockstate.isFaceSturdy(levelIn, blockpos, Direction.UP) || blockstate.getBlock() == Blocks.HOPPER;
2018-10-16 01:36:30 +02:00
}
2020-01-24 17:05:41 +01:00
@Override
public VoxelShape getCollisionShape(BlockState state, BlockGetter levelIn, BlockPos pos, CollisionContext context) {
2021-12-04 15:40:09 +01:00
return Shapes.empty();
2018-10-16 01:36:30 +02:00
}
2020-01-24 17:05:41 +01:00
@Override
public void onPlace(BlockState state, Level levelIn, BlockPos pos, BlockState oldState, boolean isMoving) {
2021-12-04 15:40:09 +01:00
if (oldState.getBlock() != state.getBlock() && !levelIn.isClientSide) {
2021-12-15 16:30:22 +01:00
for (var direction : Direction.Plane.VERTICAL) {
levelIn.updateNeighborsAt(pos.relative(direction), this);
2020-01-24 17:05:41 +01:00
}
2021-12-15 16:30:22 +01:00
for (var direction1 : Direction.Plane.HORIZONTAL) {
this.notifyWireNeighborsOfStateChange(levelIn, pos.relative(direction1));
2020-01-24 17:05:41 +01:00
}
2021-12-15 16:30:22 +01:00
for (var direction2 : Direction.Plane.HORIZONTAL) {
var blockpos = pos.relative(direction2);
if (levelIn.getBlockState(blockpos).isCollisionShapeFullBlock(levelIn, blockpos)) {
this.notifyWireNeighborsOfStateChange(levelIn, blockpos.above());
2020-01-24 17:05:41 +01:00
} else {
this.notifyWireNeighborsOfStateChange(levelIn, blockpos.below());
2020-01-24 17:05:41 +01:00
}
}
}
2018-10-16 01:36:30 +02:00
}
@Override
public void onRemove(BlockState state, Level levelIn, BlockPos pos, BlockState newState, boolean isMoving) {
2020-01-24 17:05:41 +01:00
if (!isMoving && state.getBlock() != newState.getBlock()) {
super.onRemove(state, levelIn, pos, newState, isMoving);
2021-12-04 15:40:09 +01:00
if (!levelIn.isClientSide) {
2021-12-15 16:30:22 +01:00
for (var direction : Direction.values()) {
levelIn.updateNeighborsAt(pos.relative(direction), this);
2020-01-24 17:05:41 +01:00
}
2021-12-15 16:30:22 +01:00
for (var direction1 : Direction.Plane.HORIZONTAL) {
this.notifyWireNeighborsOfStateChange(levelIn, pos.relative(direction1));
2020-01-24 17:05:41 +01:00
}
2021-12-15 16:30:22 +01:00
for (var direction2 : Direction.Plane.HORIZONTAL) {
var blockpos = pos.relative(direction2);
if (levelIn.getBlockState(blockpos).isCollisionShapeFullBlock(levelIn, blockpos)) {
this.notifyWireNeighborsOfStateChange(levelIn, blockpos.above());
2020-01-24 17:05:41 +01:00
} else {
this.notifyWireNeighborsOfStateChange(levelIn, blockpos.below());
2020-01-24 17:05:41 +01:00
}
}
}
}
2018-10-16 01:36:30 +02:00
}
2020-01-24 17:05:41 +01:00
@Override
2021-12-04 15:40:09 +01:00
public void neighborChanged(BlockState state, Level levelIn, BlockPos pos, Block blockIn, BlockPos fromPos, boolean isMoving) {
if (!levelIn.isClientSide) {
if (!state.canSurvive(levelIn, pos)) {
dropResources(state, levelIn, pos);
2021-12-04 15:40:09 +01:00
levelIn.removeBlock(pos, false);
2020-01-24 17:05:41 +01:00
}
}
}
2018-10-16 01:36:30 +02:00
2020-01-24 17:05:41 +01:00
@Override
public void updateIndirectNeighbourShapes(BlockState state, LevelAccessor levelIn, BlockPos pos, int flags, int recursionLeft) {
2021-12-15 16:30:22 +01:00
var pool = new BlockPos.MutableBlockPos();
for (var direction : Direction.Plane.HORIZONTAL) {
var redstoneside = state.getValue(RedStoneWireBlock.PROPERTY_BY_DIRECTION.get(direction));
if (redstoneside != RedstoneSide.NONE && levelIn.getBlockState(pool.set(pos).move(direction)).getBlock() != this) {
2020-09-22 03:17:02 +02:00
pool.move(Direction.DOWN);
2021-12-15 16:30:22 +01:00
var blockstate = levelIn.getBlockState(pool);
2020-09-22 03:17:02 +02:00
if (blockstate.getBlock() != Blocks.OBSERVER) {
2021-12-15 16:30:22 +01:00
var blockpos = pool.relative(direction.getOpposite());
var blockstate1 = blockstate.updateShape(direction.getOpposite(), levelIn.getBlockState(blockpos), levelIn, pool, blockpos);
updateOrDestroy(blockstate, blockstate1, levelIn, pool, flags);
2020-09-22 03:17:02 +02:00
}
2018-10-16 01:36:30 +02:00
pool.set(pos).move(direction).move(Direction.UP);
2021-12-15 16:30:22 +01:00
var blockstate3 = levelIn.getBlockState(pool);
2020-09-22 03:17:02 +02:00
if (blockstate3.getBlock() != Blocks.OBSERVER) {
2021-12-15 16:30:22 +01:00
var blockpos1 = pool.relative(direction.getOpposite());
var blockstate2 = blockstate3.updateShape(direction.getOpposite(), levelIn.getBlockState(blockpos1), levelIn, pool, blockpos1);
updateOrDestroy(blockstate3, blockstate2, levelIn, pool, flags);
2020-01-24 17:05:41 +01:00
}
}
2018-10-16 01:36:30 +02:00
}
2020-01-24 17:05:41 +01:00
}
2021-12-04 15:40:09 +01:00
private void notifyWireNeighborsOfStateChange(Level levelIn, BlockPos pos) {
if (levelIn.getBlockState(pos).getBlock() == this) {
levelIn.updateNeighborsAt(pos, this);
2018-10-16 01:36:30 +02:00
2021-12-15 16:30:22 +01:00
for (var direction : Direction.values()) {
levelIn.updateNeighborsAt(pos.relative(direction), this);
2020-01-24 17:05:41 +01:00
}
2018-10-16 01:36:30 +02:00
}
}
2020-01-29 00:40:28 +01:00
@Override
public void generateCustomBlockState(BlockStateGenerator generator) {
// noop
}
@Override
public void generateCustomItemModel(ItemModelGenerator generator) {
generator.withExistingParent(this.getBaseName(), "item/generated").texture("layer0", "item/" + this.getBaseName());
}
2020-01-29 01:34:58 +01:00
@Override
public Supplier<RenderType> getRenderType() {
return RenderType::cutoutMipped;
2020-01-29 01:34:58 +01:00
}
2018-10-16 01:36:30 +02:00
}