New-Soviet-Era/src/main/java/su/a71/new_soviet/blocks/WMachineBlock.java

94 lines
No EOL
4.1 KiB
Java

package su.a71.new_soviet.blocks;
import net.minecraft.block.*;
import net.minecraft.block.piston.PistonBehavior;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.AirBlockItem;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.minecraft.world.WorldView;
import su.a71.new_soviet.NewSoviet;
import su.a71.new_soviet.registration.NSE_Sounds;
import su.a71.new_soviet.util.Shapes;
public class WMachineBlock extends HorizontalFacingBlock {
private final Shapes.HorizontalShape blockShape;
public static final BooleanProperty PAPER = BooleanProperty.of("paper");
public WMachineBlock(Settings settings, Shapes.HorizontalShape blockShape) {
super(settings.notSolid().nonOpaque().noBlockBreakParticles().pistonBehavior(PistonBehavior.DESTROY).strength(0.1f, 2f));
setDefaultState(getDefaultState().with(PAPER, false).with(Properties.HORIZONTAL_FACING, Direction.NORTH));
this.blockShape = blockShape;
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(Properties.HORIZONTAL_FACING, PAPER);
}
@Override
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext ctx) {
return switch (state.get(FACING)) {
case NORTH -> blockShape.north();
case SOUTH -> blockShape.south();
case EAST -> blockShape.east();
case WEST -> blockShape.west();
default -> VoxelShapes.fullCube();
};
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (state.get(PAPER) && player.isSneaking() && player.getInventory().getMainHandStack().getItem() == Blocks.AIR.asItem()) {
player.equipStack(EquipmentSlot.MAINHAND, new ItemStack(Items.PAPER));
BlockState updatedBlockState = state
.with(PAPER, false);
world.setBlockState(pos, updatedBlockState);
}
else if ((!state.get(PAPER) && player.getInventory().getMainHandStack().getItem() == Items.PAPER)) {
if (!player.isCreative())
player.getInventory().getMainHandStack().decrement(1);
BlockState updatedBlockState = state
.with(PAPER, true);
world.setBlockState(pos, updatedBlockState);
}
else {
float pitch = (float) NewSoviet.RANDOM.nextBetween(8, 12) / 10;
float volume = (float) NewSoviet.RANDOM.nextBetween(5, 7) / 10;
world.playSound(null, pos, NSE_Sounds.W_MACHINE_BUTTON_PRESS, SoundCategory.BLOCKS, volume, pitch);
}
return ActionResult.SUCCESS;
}
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return super.getPlacementState(ctx).with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing().getOpposite()).with(PAPER, false);
}
protected boolean canPlantOnTop(BlockState floor, BlockView world, BlockPos pos) {
return !floor.getCollisionShape(world, pos).getFace(Direction.UP).isEmpty() || floor.isSideSolidFullSquare(world, pos, Direction.UP);
}
public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
BlockPos blockPos = pos.down();
return this.canPlantOnTop(world.getBlockState(blockPos), world, blockPos);
}
}