package ru.betterend.blocks.basis; import java.util.List; import com.google.common.collect.Lists; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.block.Material; import net.minecraft.block.ShapeContext; import net.minecraft.block.entity.BlockEntity; import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.enchantment.Enchantments; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.loot.context.LootContext; import net.minecraft.loot.context.LootContextParameters; import net.minecraft.sound.BlockSoundGroup; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.util.shape.VoxelShape; import net.minecraft.world.BlockView; import net.minecraft.world.World; import net.minecraft.world.WorldAccess; import net.minecraft.world.WorldView; import ru.betterend.client.render.ERenderLayer; import ru.betterend.interfaces.IRenderTypeable; import ru.betterend.registry.EndTags; public class BlockUpDownPlant extends BlockBaseNotFull implements IRenderTypeable { private static final VoxelShape SHAPE = Block.createCuboidShape(4, 0, 4, 12, 16, 12); public BlockUpDownPlant() { super(FabricBlockSettings.of(Material.PLANT) .breakByTool(FabricToolTags.SHEARS) .sounds(BlockSoundGroup.GRASS) .breakByHand(true) .noCollision()); } @Override public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext ePos) { return SHAPE; } @Override public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) { BlockState down = world.getBlockState(pos.down()); BlockState up = world.getBlockState(pos.up()); return (isTerrain(down) || down.getBlock() == this) && (isSupport(up, world, pos) || up.getBlock() == this); } protected boolean isTerrain(BlockState state) { return state.isIn(EndTags.END_GROUND); } protected boolean isSupport(BlockState state, WorldView world, BlockPos pos) { return sideCoversSmallSquare(world, pos.up(), Direction.UP); } @Override public BlockState getStateForNeighborUpdate(BlockState state, Direction facing, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) { if (!canPlaceAt(state, world, pos)) { return Blocks.AIR.getDefaultState(); } else { return state; } } @Override public List getDroppedStacks(BlockState state, LootContext.Builder builder) { ItemStack tool = builder.get(LootContextParameters.TOOL); if (tool != null && tool.getItem().isIn(FabricToolTags.SHEARS) || EnchantmentHelper.getLevel(Enchantments.SILK_TOUCH, tool) > 0) { return Lists.newArrayList(new ItemStack(this)); } else { return Lists.newArrayList(); } } @Override public ERenderLayer getRenderLayer() { return ERenderLayer.CUTOUT; } @Override public void afterBreak(World world, PlayerEntity player, BlockPos pos, BlockState state, BlockEntity blockEntity, ItemStack stack) { super.afterBreak(world, player, pos, state, blockEntity, stack); world.updateNeighbor(pos, Blocks.AIR, pos.down()); } }