179 lines
8.7 KiB
Java
179 lines
8.7 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* com.mojang.datafixers.kinds.App
|
|
* com.mojang.datafixers.kinds.Applicative
|
|
* com.mojang.serialization.MapCodec
|
|
* com.mojang.serialization.codecs.RecordCodecBuilder
|
|
* org.jspecify.annotations.Nullable
|
|
*/
|
|
package net.minecraft.world.level.block;
|
|
|
|
import com.mojang.datafixers.kinds.App;
|
|
import com.mojang.datafixers.kinds.Applicative;
|
|
import com.mojang.serialization.MapCodec;
|
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
|
import java.util.Map;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.tags.BlockTags;
|
|
import net.minecraft.util.RandomSource;
|
|
import net.minecraft.world.InteractionHand;
|
|
import net.minecraft.world.InteractionResult;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.item.HangingSignItem;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.item.context.BlockPlaceContext;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.LevelReader;
|
|
import net.minecraft.world.level.ScheduledTickAccess;
|
|
import net.minecraft.world.level.block.Block;
|
|
import net.minecraft.world.level.block.Blocks;
|
|
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
|
|
import net.minecraft.world.level.block.Mirror;
|
|
import net.minecraft.world.level.block.Rotation;
|
|
import net.minecraft.world.level.block.SignBlock;
|
|
import net.minecraft.world.level.block.SupportType;
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
|
import net.minecraft.world.level.block.entity.BlockEntityType;
|
|
import net.minecraft.world.level.block.entity.HangingSignBlockEntity;
|
|
import net.minecraft.world.level.block.entity.SignBlockEntity;
|
|
import net.minecraft.world.level.block.state.BlockBehaviour;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.level.block.state.StateDefinition;
|
|
import net.minecraft.world.level.block.state.properties.EnumProperty;
|
|
import net.minecraft.world.level.block.state.properties.WoodType;
|
|
import net.minecraft.world.level.material.FluidState;
|
|
import net.minecraft.world.level.material.Fluids;
|
|
import net.minecraft.world.level.pathfinder.PathComputationType;
|
|
import net.minecraft.world.phys.BlockHitResult;
|
|
import net.minecraft.world.phys.shapes.CollisionContext;
|
|
import net.minecraft.world.phys.shapes.Shapes;
|
|
import net.minecraft.world.phys.shapes.VoxelShape;
|
|
import org.jspecify.annotations.Nullable;
|
|
|
|
public class WallHangingSignBlock
|
|
extends SignBlock {
|
|
public static final MapCodec<WallHangingSignBlock> CODEC = RecordCodecBuilder.mapCodec(i -> i.group((App)WoodType.CODEC.fieldOf("wood_type").forGetter(SignBlock::type), WallHangingSignBlock.propertiesCodec()).apply((Applicative)i, WallHangingSignBlock::new));
|
|
public static final EnumProperty<Direction> FACING = HorizontalDirectionalBlock.FACING;
|
|
private static final Map<Direction.Axis, VoxelShape> SHAPES_PLANK = Shapes.rotateHorizontalAxis(Block.column(16.0, 4.0, 14.0, 16.0));
|
|
private static final Map<Direction.Axis, VoxelShape> SHAPES = Shapes.rotateHorizontalAxis(Shapes.or(SHAPES_PLANK.get(Direction.Axis.Z), Block.column(14.0, 2.0, 0.0, 10.0)));
|
|
|
|
public MapCodec<WallHangingSignBlock> codec() {
|
|
return CODEC;
|
|
}
|
|
|
|
public WallHangingSignBlock(WoodType type, BlockBehaviour.Properties properties) {
|
|
super(type, properties.sound(type.hangingSignSoundType()));
|
|
this.registerDefaultState((BlockState)((BlockState)((BlockState)this.stateDefinition.any()).setValue(FACING, Direction.NORTH)).setValue(WATERLOGGED, false));
|
|
}
|
|
|
|
@Override
|
|
protected InteractionResult useItemOn(ItemStack itemStack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) {
|
|
SignBlockEntity signEntity;
|
|
BlockEntity blockEntity = level.getBlockEntity(pos);
|
|
if (blockEntity instanceof SignBlockEntity && this.shouldTryToChainAnotherHangingSign(state, player, hitResult, signEntity = (SignBlockEntity)blockEntity, itemStack)) {
|
|
return InteractionResult.PASS;
|
|
}
|
|
return super.useItemOn(itemStack, state, level, pos, player, hand, hitResult);
|
|
}
|
|
|
|
private boolean shouldTryToChainAnotherHangingSign(BlockState state, Player player, BlockHitResult hitResult, SignBlockEntity signEntity, ItemStack itemStack) {
|
|
return !signEntity.canExecuteClickCommands(signEntity.isFacingFrontText(player), player) && itemStack.getItem() instanceof HangingSignItem && !this.isHittingEditableSide(hitResult, state);
|
|
}
|
|
|
|
private boolean isHittingEditableSide(BlockHitResult hitResult, BlockState state) {
|
|
return hitResult.getDirection().getAxis() == state.getValue(FACING).getAxis();
|
|
}
|
|
|
|
@Override
|
|
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
|
|
return SHAPES.get(state.getValue(FACING).getAxis());
|
|
}
|
|
|
|
@Override
|
|
protected VoxelShape getBlockSupportShape(BlockState state, BlockGetter level, BlockPos pos) {
|
|
return this.getShape(state, level, pos, CollisionContext.empty());
|
|
}
|
|
|
|
@Override
|
|
protected VoxelShape getCollisionShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
|
|
return SHAPES_PLANK.get(state.getValue(FACING).getAxis());
|
|
}
|
|
|
|
public boolean canPlace(BlockState state, LevelReader level, BlockPos pos) {
|
|
Direction clockwise = state.getValue(FACING).getClockWise();
|
|
Direction counterClockwise = state.getValue(FACING).getCounterClockWise();
|
|
return this.canAttachTo(level, state, pos.relative(clockwise), counterClockwise) || this.canAttachTo(level, state, pos.relative(counterClockwise), clockwise);
|
|
}
|
|
|
|
public boolean canAttachTo(LevelReader level, BlockState state, BlockPos attachPos, Direction attachFace) {
|
|
BlockState attachState = level.getBlockState(attachPos);
|
|
if (attachState.is(BlockTags.WALL_HANGING_SIGNS)) {
|
|
return attachState.getValue(FACING).getAxis().test(state.getValue(FACING));
|
|
}
|
|
return attachState.isFaceSturdy(level, attachPos, attachFace, SupportType.FULL);
|
|
}
|
|
|
|
@Override
|
|
public @Nullable BlockState getStateForPlacement(BlockPlaceContext context) {
|
|
BlockState state = this.defaultBlockState();
|
|
FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
|
|
Level level = context.getLevel();
|
|
BlockPos pos = context.getClickedPos();
|
|
for (Direction direction : context.getNearestLookingDirections()) {
|
|
Direction facing;
|
|
if (!direction.getAxis().isHorizontal() || direction.getAxis().test(context.getClickedFace()) || !(state = (BlockState)state.setValue(FACING, facing = direction.getOpposite())).canSurvive(level, pos) || !this.canPlace(state, level, pos)) continue;
|
|
return (BlockState)state.setValue(WATERLOGGED, replacedFluidState.getType() == Fluids.WATER);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
protected BlockState updateShape(BlockState state, LevelReader level, ScheduledTickAccess ticks, BlockPos pos, Direction directionToNeighbour, BlockPos neighbourPos, BlockState neighbourState, RandomSource random) {
|
|
if (directionToNeighbour.getAxis() == state.getValue(FACING).getClockWise().getAxis() && !state.canSurvive(level, pos)) {
|
|
return Blocks.AIR.defaultBlockState();
|
|
}
|
|
return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
|
}
|
|
|
|
@Override
|
|
public float getYRotationDegrees(BlockState state) {
|
|
return state.getValue(FACING).toYRot();
|
|
}
|
|
|
|
@Override
|
|
protected BlockState rotate(BlockState state, Rotation rotation) {
|
|
return (BlockState)state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
|
}
|
|
|
|
@Override
|
|
protected BlockState mirror(BlockState state, Mirror mirror) {
|
|
return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
|
}
|
|
|
|
@Override
|
|
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
|
|
builder.add(FACING, WATERLOGGED);
|
|
}
|
|
|
|
@Override
|
|
public BlockEntity newBlockEntity(BlockPos worldPosition, BlockState blockState) {
|
|
return new HangingSignBlockEntity(worldPosition, blockState);
|
|
}
|
|
|
|
@Override
|
|
protected boolean isPathfindable(BlockState state, PathComputationType type) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(Level level, BlockState blockState, BlockEntityType<T> type) {
|
|
return WallHangingSignBlock.createTickerHelper(type, BlockEntityType.HANGING_SIGN, SignBlockEntity::tick);
|
|
}
|
|
}
|
|
|