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;
2021-01-14 23:15:02 +01:00
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.RedstoneWireBlock;
2020-01-29 01:34:58 +01:00
import net.minecraft.client.renderer.RenderType;
2019-11-04 19:08:49 +01:00
import net.minecraft.client.renderer.color.IBlockColor;
2020-01-24 17:05:41 +01:00
import net.minecraft.item.BlockItemUseContext;
2019-11-04 19:08:49 +01:00
import net.minecraft.state.EnumProperty;
import net.minecraft.state.StateContainer;
2020-01-24 17:05:41 +01:00
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.state.properties.RedstoneSide;
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.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;
import net.minecraft.world.IBlockReader;
2020-01-21 21:04:44 +01:00
import net.minecraft.world.IWorld;
2019-11-04 19:08:49 +01:00
import net.minecraft.world.IWorldReader;
2020-01-24 17:05:41 +01:00
import net.minecraft.world.World;
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
2020-01-24 17:05:41 +01:00
public static final EnumProperty<RedstoneSide> NORTH = BlockStateProperties.REDSTONE_NORTH;
public static final EnumProperty<RedstoneSide> EAST = BlockStateProperties.REDSTONE_EAST;
public static final EnumProperty<RedstoneSide> SOUTH = BlockStateProperties.REDSTONE_SOUTH;
public static final EnumProperty<RedstoneSide> WEST = BlockStateProperties.REDSTONE_WEST;
2020-01-21 21:04:44 +01:00
protected static final VoxelShape[] SHAPES = new VoxelShape[]{Block.makeCuboidShape(3.0D, 0.0D, 3.0D, 13.0D, 1.0D, 13.0D), Block.makeCuboidShape(3.0D, 0.0D, 3.0D, 13.0D, 1.0D, 16.0D), Block.makeCuboidShape(0.0D, 0.0D, 3.0D, 13.0D, 1.0D, 13.0D), Block.makeCuboidShape(0.0D, 0.0D, 3.0D, 13.0D, 1.0D, 16.0D), Block.makeCuboidShape(3.0D, 0.0D, 0.0D, 13.0D, 1.0D, 13.0D), Block.makeCuboidShape(3.0D, 0.0D, 0.0D, 13.0D, 1.0D, 16.0D), Block.makeCuboidShape(0.0D, 0.0D, 0.0D, 13.0D, 1.0D, 13.0D), Block.makeCuboidShape(0.0D, 0.0D, 0.0D, 13.0D, 1.0D, 16.0D), Block.makeCuboidShape(3.0D, 0.0D, 3.0D, 16.0D, 1.0D, 13.0D), Block.makeCuboidShape(3.0D, 0.0D, 3.0D, 16.0D, 1.0D, 16.0D), Block.makeCuboidShape(0.0D, 0.0D, 3.0D, 16.0D, 1.0D, 13.0D), Block.makeCuboidShape(0.0D, 0.0D, 3.0D, 16.0D, 1.0D, 16.0D), Block.makeCuboidShape(3.0D, 0.0D, 0.0D, 16.0D, 1.0D, 13.0D), Block.makeCuboidShape(3.0D, 0.0D, 0.0D, 16.0D, 1.0D, 16.0D), Block.makeCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 1.0D, 13.0D), Block.makeCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 1.0D, 16.0D)};
2018-10-16 01:36:30 +02:00
public BlockGoldPowder() {
2020-10-17 21:13:45 +02:00
super("gold_powder", Properties.from(Blocks.REDSTONE_WIRE));
2020-01-24 17:05:41 +01:00
this.setDefaultState(this.stateContainer.getBaseState().with(NORTH, RedstoneSide.NONE).with(EAST, RedstoneSide.NONE).with(SOUTH, RedstoneSide.NONE).with(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) {
2018-10-16 01:36:30 +02:00
int i = 0;
2020-01-24 17:05:41 +01:00
boolean n = state.get(NORTH) != RedstoneSide.NONE;
boolean e = state.get(EAST) != RedstoneSide.NONE;
boolean s = state.get(SOUTH) != RedstoneSide.NONE;
boolean w = state.get(WEST) != RedstoneSide.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;
}
2020-02-07 15:22:30 +01:00
@Override
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
builder.add(NORTH, EAST, SOUTH, WEST);
}
@Override
public IBlockColor getBlockColor() {
return (state, worldIn, pos, tintIndex) -> 0xf4cb42;
}
@Override
public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
return SHAPES[getShapeIndex(state)];
}
2018-10-16 01:36:30 +02:00
@Override
2020-01-24 17:05:41 +01:00
public BlockState getStateForPlacement(BlockItemUseContext context) {
IBlockReader iblockreader = context.getWorld();
BlockPos blockpos = context.getPos();
return this.getDefaultState().with(WEST, this.getSide(iblockreader, blockpos, Direction.WEST)).with(EAST, this.getSide(iblockreader, blockpos, Direction.EAST)).with(NORTH, this.getSide(iblockreader, blockpos, Direction.NORTH)).with(SOUTH, this.getSide(iblockreader, blockpos, Direction.SOUTH));
}
@Override
public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn, BlockPos currentPos, BlockPos facingPos) {
if (facing == Direction.DOWN) {
return stateIn;
2018-10-16 01:36:30 +02:00
} else {
2020-01-24 17:05:41 +01:00
return facing == Direction.UP ? stateIn.with(WEST, this.getSide(worldIn, currentPos, Direction.WEST)).with(EAST, this.getSide(worldIn, currentPos, Direction.EAST)).with(NORTH, this.getSide(worldIn, currentPos, Direction.NORTH)).with(SOUTH, this.getSide(worldIn, currentPos, Direction.SOUTH)) : stateIn.with(RedstoneWireBlock.FACING_PROPERTY_MAP.get(facing), this.getSide(worldIn, currentPos, facing));
}
}
private RedstoneSide getSide(IBlockReader worldIn, BlockPos pos, Direction face) {
BlockPos blockpos = pos.offset(face);
BlockState blockstate = worldIn.getBlockState(blockpos);
BlockPos blockpos1 = pos.up();
BlockState blockstate1 = worldIn.getBlockState(blockpos1);
if (!blockstate1.isNormalCube(worldIn, blockpos1)) {
2020-01-28 18:08:56 +01:00
boolean flag = blockstate.isSolidSide(worldIn, blockpos, Direction.UP) || blockstate.getBlock() == Blocks.HOPPER;
2020-01-24 17:05:41 +01:00
if (flag && this.canConnectTo(worldIn.getBlockState(blockpos.up()))) {
2020-09-22 03:17:02 +02:00
if (blockstate.hasOpaqueCollisionShape(worldIn, blockpos)) {
2020-01-24 17:05:41 +01:00
return RedstoneSide.UP;
}
return RedstoneSide.SIDE;
}
2018-10-16 01:36:30 +02:00
}
2020-01-24 17:05:41 +01:00
return !this.canConnectTo(blockstate) && (blockstate.isNormalCube(worldIn, blockpos) || !this.canConnectTo(worldIn.getBlockState(blockpos.down()))) ? 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) {
Block block = blockState.getBlock();
return block == this;
2018-10-16 01:36:30 +02:00
}
@Override
2020-01-21 21:04:44 +01:00
public boolean isValidPosition(BlockState state, IWorldReader worldIn, BlockPos pos) {
BlockPos blockpos = pos.down();
BlockState blockstate = worldIn.getBlockState(blockpos);
2020-01-28 18:08:56 +01:00
return blockstate.isSolidSide(worldIn, 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, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
return VoxelShapes.empty();
2018-10-16 01:36:30 +02:00
}
2020-01-24 17:05:41 +01:00
@Override
public void onBlockAdded(BlockState state, World worldIn, BlockPos pos, BlockState oldState, boolean isMoving) {
if (oldState.getBlock() != state.getBlock() && !worldIn.isRemote) {
for (Direction direction : Direction.Plane.VERTICAL) {
worldIn.notifyNeighborsOfStateChange(pos.offset(direction), this);
}
for (Direction direction1 : Direction.Plane.HORIZONTAL) {
this.notifyWireNeighborsOfStateChange(worldIn, pos.offset(direction1));
}
for (Direction direction2 : Direction.Plane.HORIZONTAL) {
BlockPos blockpos = pos.offset(direction2);
if (worldIn.getBlockState(blockpos).isNormalCube(worldIn, blockpos)) {
this.notifyWireNeighborsOfStateChange(worldIn, blockpos.up());
} else {
this.notifyWireNeighborsOfStateChange(worldIn, blockpos.down());
}
}
}
2018-10-16 01:36:30 +02:00
}
@Override
2020-01-24 17:05:41 +01:00
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
if (!isMoving && state.getBlock() != newState.getBlock()) {
super.onReplaced(state, worldIn, pos, newState, isMoving);
if (!worldIn.isRemote) {
for (Direction direction : Direction.values()) {
worldIn.notifyNeighborsOfStateChange(pos.offset(direction), this);
}
for (Direction direction1 : Direction.Plane.HORIZONTAL) {
this.notifyWireNeighborsOfStateChange(worldIn, pos.offset(direction1));
}
for (Direction direction2 : Direction.Plane.HORIZONTAL) {
BlockPos blockpos = pos.offset(direction2);
if (worldIn.getBlockState(blockpos).isNormalCube(worldIn, blockpos)) {
this.notifyWireNeighborsOfStateChange(worldIn, blockpos.up());
} else {
this.notifyWireNeighborsOfStateChange(worldIn, blockpos.down());
}
}
}
}
2018-10-16 01:36:30 +02:00
}
2020-01-24 17:05:41 +01:00
@Override
public void neighborChanged(BlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos, boolean isMoving) {
if (!worldIn.isRemote) {
if (!state.isValidPosition(worldIn, pos)) {
spawnDrops(state, worldIn, pos);
worldIn.removeBlock(pos, false);
}
}
}
2018-10-16 01:36:30 +02:00
2020-01-24 17:05:41 +01:00
@Override
2020-09-22 03:17:02 +02:00
public void updateDiagonalNeighbors(BlockState state, IWorld worldIn, BlockPos pos, int flags, int recursionLeft) {
BlockPos.Mutable pool = new BlockPos.Mutable();
for (Direction direction : Direction.Plane.HORIZONTAL) {
RedstoneSide redstoneside = state.get(RedstoneWireBlock.FACING_PROPERTY_MAP.get(direction));
if (redstoneside != RedstoneSide.NONE && worldIn.getBlockState(pool.setPos(pos).move(direction)).getBlock() != this) {
pool.move(Direction.DOWN);
BlockState blockstate = worldIn.getBlockState(pool);
if (blockstate.getBlock() != Blocks.OBSERVER) {
BlockPos blockpos = pool.offset(direction.getOpposite());
BlockState blockstate1 = blockstate.updatePostPlacement(direction.getOpposite(), worldIn.getBlockState(blockpos), worldIn, pool, blockpos);
replaceBlock(blockstate, blockstate1, worldIn, pool, flags);
}
2018-10-16 01:36:30 +02:00
2020-09-22 03:17:02 +02:00
pool.setPos(pos).move(direction).move(Direction.UP);
BlockState blockstate3 = worldIn.getBlockState(pool);
if (blockstate3.getBlock() != Blocks.OBSERVER) {
BlockPos blockpos1 = pool.offset(direction.getOpposite());
BlockState blockstate2 = blockstate3.updatePostPlacement(direction.getOpposite(), worldIn.getBlockState(blockpos1), worldIn, pool, blockpos1);
replaceBlock(blockstate3, blockstate2, worldIn, 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
}
private void notifyWireNeighborsOfStateChange(World worldIn, BlockPos pos) {
if (worldIn.getBlockState(pos).getBlock() == this) {
worldIn.notifyNeighborsOfStateChange(pos, this);
2018-10-16 01:36:30 +02:00
2020-01-24 17:05:41 +01:00
for (Direction direction : Direction.values()) {
worldIn.notifyNeighborsOfStateChange(pos.offset(direction), this);
}
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() {
2020-09-22 03:17:02 +02:00
return RenderType::getCutoutMipped;
2020-01-29 01:34:58 +01:00
}
2018-10-16 01:36:30 +02:00
}