package net.minecraft.world.level.block; import com.mojang.datafixers.DataFixUtils; import com.mojang.serialization.MapCodec; import com.mojang.serialization.codecs.RecordCodecBuilder; import java.util.Map; import java.util.Optional; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceKey; import net.minecraft.tags.TagKey; import net.minecraft.util.RandomSource; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.ScheduledTickAccess; 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.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; /** * Access widened by fabric-transitive-access-wideners-v1 to accessible */ public class AttachedStemBlock extends VegetationBlock { public static final MapCodec CODEC = RecordCodecBuilder.mapCodec( i -> i.group( ResourceKey.codec(Registries.BLOCK).fieldOf("fruit").forGetter(b -> b.fruit), ResourceKey.codec(Registries.BLOCK).fieldOf("stem").forGetter(b -> b.stem), ResourceKey.codec(Registries.ITEM).fieldOf("seed").forGetter(b -> b.seed), TagKey.codec(Registries.BLOCK).fieldOf("support_blocks").forGetter(b -> b.supportBlocks), propertiesCodec() ) .apply(i, AttachedStemBlock::new) ); public static final EnumProperty FACING = HorizontalDirectionalBlock.FACING; private static final Map SHAPES = Shapes.rotateHorizontal(Block.boxZ(4.0, 0.0, 10.0, 0.0, 10.0)); private final ResourceKey fruit; private final ResourceKey stem; private final ResourceKey seed; private final TagKey supportBlocks; @Override public MapCodec codec() { return CODEC; } /** * Access widened by fabric-transitive-access-wideners-v1 to accessible */ public AttachedStemBlock( final ResourceKey stem, final ResourceKey fruit, final ResourceKey seed, final TagKey supportBlocks, final BlockBehaviour.Properties properties ) { super(properties); this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH)); this.stem = stem; this.fruit = fruit; this.seed = seed; this.supportBlocks = supportBlocks; } @Override protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) { return (VoxelShape)SHAPES.get(state.getValue(FACING)); } @Override protected BlockState updateShape( final BlockState state, final LevelReader level, final ScheduledTickAccess ticks, final BlockPos pos, final Direction directionToNeighbour, final BlockPos neighbourPos, final BlockState neighbourState, final RandomSource random ) { if (!neighbourState.is(this.fruit) && directionToNeighbour == state.getValue(FACING)) { Optional stem = level.registryAccess().lookupOrThrow(Registries.BLOCK).getOptional(this.stem); if (stem.isPresent()) { return ((Block)stem.get()).defaultBlockState().trySetValue(StemBlock.AGE, 7); } } return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random); } @Override protected boolean mayPlaceOn(final BlockState state, final BlockGetter level, final BlockPos pos) { return state.is(this.supportBlocks); } @Override protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) { return new ItemStack(DataFixUtils.orElse(level.registryAccess().lookupOrThrow(Registries.ITEM).getOptional(this.seed), this)); } @Override protected BlockState rotate(final BlockState state, final Rotation rotation) { return state.setValue(FACING, rotation.rotate(state.getValue(FACING))); } @Override protected BlockState mirror(final BlockState state, final Mirror mirror) { return state.rotate(mirror.getRotation(state.getValue(FACING))); } @Override protected void createBlockStateDefinition(final StateDefinition.Builder builder) { builder.add(FACING); } }