| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import java.util.function.IntFunction;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundEvents;
|
| 9 | import net.minecraft.sounds.SoundSource;
|
| 10 | import net.minecraft.tags.BlockTags;
|
| 11 | import net.minecraft.tags.FluidTags;
|
| 12 | import net.minecraft.tags.ItemTags;
|
| 13 | import net.minecraft.util.ByIdMap;
|
| 14 | import net.minecraft.util.RandomSource;
|
| 15 | import net.minecraft.util.StringRepresentable;
|
| 16 | import net.minecraft.world.InteractionHand;
|
| 17 | import net.minecraft.world.InteractionResult;
|
| 18 | import net.minecraft.world.entity.player.Player;
|
| 19 | import net.minecraft.world.item.ItemStack;
|
| 20 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 21 | import net.minecraft.world.level.BlockGetter;
|
| 22 | import net.minecraft.world.level.Level;
|
| 23 | import net.minecraft.world.level.LevelReader;
|
| 24 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 25 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 26 | import net.minecraft.world.level.block.entity.CopperGolemStatueBlockEntity;
|
| 27 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 28 | import net.minecraft.world.level.block.state.BlockState;
|
| 29 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 30 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 31 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 32 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 33 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 34 | import net.minecraft.world.level.material.FluidState;
|
| 35 | import net.minecraft.world.level.material.Fluids;
|
| 36 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 37 | import net.minecraft.world.phys.BlockHitResult;
|
| 38 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 39 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 40 | import org.jspecify.annotations.Nullable;
|
| 41 |
|
| 42 | public class CopperGolemStatueBlock extends BaseEntityBlock implements SimpleWaterloggedBlock {
|
| 43 | public static final EnumProperty<Direction> FACING = BlockStateProperties.HORIZONTAL_FACING;
|
| 44 | public static final EnumProperty<CopperGolemStatueBlock.Pose> POSE = BlockStateProperties.COPPER_GOLEM_POSE;
|
| 45 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 46 | private static final VoxelShape SHAPE = Block.column(10.0, 0.0, 14.0);
|
| 47 | private final WeatheringCopper.WeatherState weatheringState;
|
| 48 |
|
| 49 | public CopperGolemStatueBlock(final WeatheringCopper.WeatherState weatherState, final BlockBehaviour.Properties properties) {
|
| 50 | super(properties);
|
| 51 | this.weatheringState = weatherState;
|
| 52 | this.registerDefaultState(
|
| 53 | this.defaultBlockState().setValue(FACING, Direction.NORTH).setValue(POSE, CopperGolemStatueBlock.Pose.STANDING).setValue(WATERLOGGED, false)
|
| 54 | );
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 59 | super.createBlockStateDefinition(builder);
|
| 60 | builder.add(FACING, POSE, WATERLOGGED);
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 65 | FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
|
| 66 | return this.defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite()).setValue(WATERLOGGED, replacedFluidState.is(Fluids.WATER));
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 71 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 76 | return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 81 | return SHAPE;
|
| 82 | }
|
| 83 |
|
| 84 | public WeatheringCopper.WeatherState getWeatheringState() {
|
| 85 | return this.weatheringState;
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | protected InteractionResult useItemOn(
|
| 90 | final ItemStack itemStack,
|
| 91 | final BlockState state,
|
| 92 | final Level level,
|
| 93 | final BlockPos pos,
|
| 94 | final Player player,
|
| 95 | final InteractionHand hand,
|
| 96 | final BlockHitResult hitResult
|
| 97 | ) {
|
| 98 | if (itemStack.is(ItemTags.AXES)) {
|
| 99 | return InteractionResult.PASS;
|
| 100 | }
|
| 101 |
|
| 102 | this.updatePose(level, state, pos, player);
|
| 103 | return InteractionResult.SUCCESS;
|
| 104 | }
|
| 105 |
|
| 106 | protected void updatePose(final Level level, final BlockState state, final BlockPos pos, final Player player) {
|
| 107 | level.playSound(null, pos, SoundEvents.COPPER_GOLEM_BECOME_STATUE, SoundSource.BLOCKS);
|
| 108 | level.setBlockAndUpdate(pos, state.setValue(POSE, state.getValue(POSE).getNextPose()));
|
| 109 | level.gameEvent(player, GameEvent.BLOCK_CHANGE, pos);
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 114 | return type == PathComputationType.WATER && state.getFluidState().is(FluidTags.WATER);
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public @Nullable BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 119 | return new CopperGolemStatueBlockEntity(worldPosition, blockState);
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | public boolean shouldChangedStateKeepBlockEntity(final BlockState oldState) {
|
| 124 | return oldState.is(BlockTags.COPPER_GOLEM_STATUES);
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | protected boolean hasAnalogOutputSignal(final BlockState state) {
|
| 129 | return true;
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
|
| 134 | return state.getValue(POSE).ordinal() + 1;
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) {
|
| 139 | return level.getBlockEntity(pos) instanceof CopperGolemStatueBlockEntity entity
|
| 140 | ? entity.getItem(this.asItem().getDefaultInstance(), state.getValue(POSE))
|
| 141 | : super.getCloneItemStack(level, pos, state, includeData);
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 146 | level.updateNeighbourForOutputSignal(pos, state.getBlock());
|
| 147 | }
|
| 148 |
|
| 149 | @Override
|
| 150 | protected FluidState getFluidState(final BlockState state) {
|
| 151 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | protected BlockState updateShape(
|
| 156 | final BlockState state,
|
| 157 | final LevelReader level,
|
| 158 | final ScheduledTickAccess ticks,
|
| 159 | final BlockPos pos,
|
| 160 | final Direction directionToNeighbour,
|
| 161 | final BlockPos neighbourPos,
|
| 162 | final BlockState neighbourState,
|
| 163 | final RandomSource random
|
| 164 | ) {
|
| 165 | if (state.getValue(WATERLOGGED)) {
|
| 166 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 167 | }
|
| 168 |
|
| 169 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 170 | }
|
| 171 |
|
| 172 | public enum Pose implements StringRepresentable {
|
| 173 | STANDING("standing"),
|
| 174 | SITTING("sitting"),
|
| 175 | RUNNING("running"),
|
| 176 | STAR("star");
|
| 177 |
|
| 178 | public static final IntFunction<CopperGolemStatueBlock.Pose> BY_ID = ByIdMap.continuous(Enum::ordinal, values(), ByIdMap.OutOfBoundsStrategy.ZERO);
|
| 179 | public static final Codec<CopperGolemStatueBlock.Pose> CODEC = StringRepresentable.fromEnum(CopperGolemStatueBlock.Pose::values);
|
| 180 | private final String name;
|
| 181 |
|
| 182 | Pose(final String name) {
|
| 183 | this.name = name;
|
| 184 | }
|
| 185 |
|
| 186 | @Override
|
| 187 | public String getSerializedName() {
|
| 188 | return this.name;
|
| 189 | }
|
| 190 |
|
| 191 | public CopperGolemStatueBlock.Pose getNextPose() {
|
| 192 | return BY_ID.apply(this.ordinal() + 1);
|
| 193 | }
|
| 194 | }
|
| 195 | }
|
| 196 |
|