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;
import net.minecraft.client.color.block.BlockColor;
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
public class BlockGoldPowder extends BlockImpl implements IColorProvidingBlock, ICustomBlockState, ICustomItemModel {
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;
2023-02-15 23:45:50 +01:00
protected static final VoxelShape[] SHAPES = {Block.box(3.0D, 0.0D, 3.0D, 13.0D, 1.0D, 13.0D), Block.box(3.0D, 0.0D, 3.0D, 13.0D, 1.0D, 16.0D), Block.box(0.0D, 0.0D, 3.0D, 13.0D, 1.0D, 13.0D), Block.box(0.0D, 0.0D, 3.0D, 13.0D, 1.0D, 16.0D), Block.box(3.0D, 0.0D, 0.0D, 13.0D, 1.0D, 13.0D), Block.box(3.0D, 0.0D, 0.0D, 13.0D, 1.0D, 16.0D), Block.box(0.0D, 0.0D, 0.0D, 13.0D, 1.0D, 13.0D), Block.box(0.0D, 0.0D, 0.0D, 13.0D, 1.0D, 16.0D), Block.box(3.0D, 0.0D, 3.0D, 16.0D, 1.0D, 13.0D), Block.box(3.0D, 0.0D, 3.0D, 16.0D, 1.0D, 16.0D), Block.box(0.0D, 0.0D, 3.0D, 16.0D, 1.0D, 13.0D), Block.box(0.0D, 0.0D, 3.0D, 16.0D, 1.0D, 16.0D), Block.box(3.0D, 0.0D, 0.0D, 16.0D, 1.0D, 13.0D), Block.box(3.0D, 0.0D, 0.0D, 16.0D, 1.0D, 16.0D), Block.box(0.0D, 0.0D, 0.0D, 16.0D, 1.0D, 13.0D), Block.box(0.0D, 0.0D, 0.0D, 16.0D, 1.0D, 16.0D)};
2018-10-16 01:36:30 +02:00
public BlockGoldPowder() {
2024-03-10 15:54:58 +01:00
super("gold_powder", Properties.ofFullCopy(Blocks.REDSTONE_WIRE));
2022-06-27 15:24:04 +02:00
this.registerDefaultState(this.defaultBlockState().setValue(BlockGoldPowder.NORTH, RedstoneSide.NONE).setValue(BlockGoldPowder.EAST, RedstoneSide.NONE).setValue(BlockGoldPowder.SOUTH, RedstoneSide.NONE).setValue(BlockGoldPowder.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;
2022-06-27 15:24:04 +02:00
var n = state.getValue(BlockGoldPowder.NORTH) != RedstoneSide.NONE;
var e = state.getValue(BlockGoldPowder.EAST) != RedstoneSide.NONE;
var s = state.getValue(BlockGoldPowder.SOUTH) != RedstoneSide.NONE;
var w = state.getValue(BlockGoldPowder.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) {
2022-06-27 15:24:04 +02:00
builder.add(BlockGoldPowder.NORTH, BlockGoldPowder.EAST, BlockGoldPowder.SOUTH, BlockGoldPowder.WEST);
2020-02-07 15:22:30 +01:00
}
@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
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
public VoxelShape getShape(BlockState state, BlockGetter levelIn, BlockPos pos, CollisionContext context) {
2022-06-27 15:24:04 +02:00
return BlockGoldPowder.SHAPES[BlockGoldPowder.getShapeIndex(state)];
2020-02-07 15:22:30 +01:00
}
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();
2022-06-27 15:24:04 +02:00
return this.defaultBlockState().setValue(BlockGoldPowder.WEST, this.getSide(iblockreader, blockpos, Direction.WEST)).setValue(BlockGoldPowder.EAST, this.getSide(iblockreader, blockpos, Direction.EAST)).setValue(BlockGoldPowder.NORTH, this.getSide(iblockreader, blockpos, Direction.NORTH)).setValue(BlockGoldPowder.SOUTH, this.getSide(iblockreader, blockpos, Direction.SOUTH));
2020-01-24 17:05:41 +01:00
}
@Override
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
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 {
2022-06-27 15:24:04 +02:00
return facing == Direction.UP ? stateIn.setValue(BlockGoldPowder.WEST, this.getSide(levelIn, currentPos, Direction.WEST)).setValue(BlockGoldPowder.EAST, this.getSide(levelIn, currentPos, Direction.EAST)).setValue(BlockGoldPowder.NORTH, this.getSide(levelIn, currentPos, Direction.NORTH)).setValue(BlockGoldPowder.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
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
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
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
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
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
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
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
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
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
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)) {
2022-06-27 15:24:04 +02:00
Block.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
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
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);
2022-06-27 15:24:04 +02:00
Block.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);
2022-06-27 15:24:04 +02:00
Block.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
2018-10-16 01:36:30 +02:00
}