package ru.betterend.blocks.basis; import java.util.List; import java.util.Random; 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.world.level.block.AbstractBlock; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.Fertilizable; import net.minecraft.world.level.block.FluidFillable; import net.minecraft.world.level.material.Material; import net.minecraft.world.level.block.ShapeContext; import net.minecraft.world.item.enchantment.EnchantmentHelper; import net.minecraft.world.item.enchantment.Enchantments; import net.minecraft.world.entity.ItemEntity; import net.minecraft.fluid.Fluid; import net.minecraft.fluid.FluidState; import net.minecraft.world.level.material.Fluids; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.level.block.SoundType; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.util.math.Vec3d; import net.minecraft.util.shape.VoxelShape; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.WorldView; import ru.betterend.client.render.ERenderLayer; import ru.betterend.interfaces.IRenderTypeable; import ru.betterend.registry.EndBlocks; import ru.betterend.registry.EndTags; public class UnderwaterPlantBlock extends BlockBaseNotFull implements IRenderTypeable, Fertilizable, FluidFillable { private static final VoxelShape SHAPE = Block.createCuboidShape(4, 0, 4, 12, 14, 12); public UnderwaterPlantBlock() { super(FabricBlockSettings.of(Material.UNDERWATER_PLANT).breakByTool(FabricToolTags.SHEARS) .sounds(SoundType.WET_GRASS).breakByHand(true).noCollision()); } public UnderwaterPlantBlock(int light) { super(FabricBlockSettings.of(Material.UNDERWATER_PLANT).breakByTool(FabricToolTags.SHEARS) .sounds(SoundType.WET_GRASS).luminance(light).breakByHand(true).noCollision()); } public UnderwaterPlantBlock(Properties settings) { super(settings); } @Override public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext ePos) { Vec3d vec3d = state.getModelOffset(view, pos); return SHAPE.offset(vec3d.x, vec3d.y, vec3d.z); } @Override public AbstractBlock.OffsetType getOffsetType() { return AbstractBlock.OffsetType.XZ; } @Override public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) { BlockState down = world.getBlockState(pos.below()); state = world.getBlockState(pos); return isTerrain(down) && state.getFluidState().getFluid().equals(Fluids.WATER.getStill()); } protected boolean isTerrain(BlockState state) { return state.isIn(EndTags.END_GROUND) || state.getBlock() == EndBlocks.ENDSTONE_DUST; } @Override public BlockState updateShape(BlockState state, Direction facing, BlockState neighborState, LevelAccessor world, BlockPos pos, BlockPos neighborPos) { if (!canPlaceAt(state, world, pos)) { world.breakBlock(pos, true); return Blocks.WATER.defaultBlockState(); } else { return state; } } @Override public List getDrops(BlockState state, LootContext.Builder builder) { ItemStack tool = builder.getParameter(LootContextParams.TOOL); if (tool != null && tool.getItem().isIn(FabricToolTags.SHEARS) || EnchantmentHelper.getItemEnchantmentLevel(Enchantments.SILK_TOUCH, tool) > 0) { return Lists.newArrayList(new ItemStack(this)); } else { return Lists.newArrayList(); } } @Override public ERenderLayer getRenderLayer() { return ERenderLayer.CUTOUT; } @Override public boolean isFertilizable(BlockView world, BlockPos pos, BlockState state, boolean isClient) { return true; } @Override public boolean canGrow(Level world, Random random, BlockPos pos, BlockState state) { return true; } @Override public void grow(ServerLevel world, Random random, BlockPos pos, BlockState state) { ItemEntity item = new ItemEntity(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, new ItemStack(this)); world.spawnEntity(item); } @Override public boolean canFillWithFluid(BlockView world, BlockPos pos, BlockState state, Fluid fluid) { return false; } @Override public boolean tryFillWithFluid(LevelAccessor world, BlockPos pos, BlockState state, FluidState fluidState) { return false; } @Override public FluidState getFluidState(BlockState state) { return Fluids.WATER.getStill(false); } }