From 7c602c67729b896e859a3514d99d0761f13b18ed Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Mon, 2 Dec 2024 01:30:17 +0100 Subject: [PATCH] fix: tall & short grass bugs (#483) --- .../BlockShortGrassBaseComponentImpl.java | 5 +- .../BlockTallGrassBaseComponentImpl.java | 78 +++++++++++++++---- .../block/type/BlockTypeInitializer.java | 5 +- 3 files changed, 66 insertions(+), 22 deletions(-) diff --git a/server/src/main/java/org/allaymc/server/block/component/grass/BlockShortGrassBaseComponentImpl.java b/server/src/main/java/org/allaymc/server/block/component/grass/BlockShortGrassBaseComponentImpl.java index fcf44e72bc..6ee8b8e753 100644 --- a/server/src/main/java/org/allaymc/server/block/component/grass/BlockShortGrassBaseComponentImpl.java +++ b/server/src/main/java/org/allaymc/server/block/component/grass/BlockShortGrassBaseComponentImpl.java @@ -25,9 +25,8 @@ public BlockShortGrassBaseComponentImpl(BlockType block @Override public boolean canKeepExisting(BlockStateWithPos current, BlockStateWithPos neighbor, BlockFace face) { - if (face != BlockFace.UP && face != BlockFace.DOWN) return true; - var blockUnder = current.pos().dimension().getBlockState(BlockFace.DOWN.offsetPos(current.pos())); - return this.canPlaceOn(blockUnder.getBlockType()); + if (face != BlockFace.DOWN) return true; + return canPlaceOn(neighbor.blockState().getBlockType()); } protected boolean canPlaceOn(BlockType blockType) { diff --git a/server/src/main/java/org/allaymc/server/block/component/grass/BlockTallGrassBaseComponentImpl.java b/server/src/main/java/org/allaymc/server/block/component/grass/BlockTallGrassBaseComponentImpl.java index 6886916bbe..d62fa275c6 100644 --- a/server/src/main/java/org/allaymc/server/block/component/grass/BlockTallGrassBaseComponentImpl.java +++ b/server/src/main/java/org/allaymc/server/block/component/grass/BlockTallGrassBaseComponentImpl.java @@ -3,38 +3,82 @@ import org.allaymc.api.block.BlockBehavior; import org.allaymc.api.block.data.BlockFace; import org.allaymc.api.block.dto.BlockStateWithPos; -import org.allaymc.api.block.property.type.BlockPropertyTypes; +import org.allaymc.api.block.dto.PlayerInteractInfo; import org.allaymc.api.block.type.BlockState; import org.allaymc.api.block.type.BlockType; +import org.allaymc.api.entity.Entity; +import org.allaymc.api.item.ItemStack; +import org.allaymc.api.item.data.ItemId; +import org.allaymc.api.item.type.ItemTypes; +import org.allaymc.api.world.Dimension; +import org.joml.Vector3ic; + +import java.util.Set; + +import static org.allaymc.api.block.property.type.BlockPropertyTypes.UPPER_BLOCK_BIT; /** * Suitable for two-block high plants that can drop wheat seeds. * - * @author daoge_cmd + * @author daoge_cmd | Dhaiven */ public class BlockTallGrassBaseComponentImpl extends BlockShortGrassBaseComponentImpl { - public BlockTallGrassBaseComponentImpl(BlockType blockType) { + + protected final ItemId shearDrop; + + public BlockTallGrassBaseComponentImpl(BlockType blockType, ItemId shearDrop) { super(blockType); + this.shearDrop = shearDrop; + } + + @Override + public boolean place(Dimension dimension, BlockState blockState, Vector3ic placeBlockPos, PlayerInteractInfo placementInfo) { + dimension.setBlockState( + placeBlockPos.x(), placeBlockPos.y(), placeBlockPos.z(), + blockState, + placementInfo + ); + dimension.setBlockState( + placeBlockPos.x(), placeBlockPos.y() + 1, placeBlockPos.z(), + blockState.setProperty(UPPER_BLOCK_BIT, true), + placementInfo + ); + return true; } @Override public boolean canKeepExisting(BlockStateWithPos current, BlockStateWithPos neighbor, BlockFace face) { - if (face != BlockFace.UP && face != BlockFace.DOWN) return true; - - var dimension = current.pos().dimension(); - var isUpperBlock = current.blockState().getPropertyValue(BlockPropertyTypes.UPPER_BLOCK_BIT); - var willBreak = false; - if (isUpperBlock) { - willBreak = notSamePlant(dimension.getBlockState(BlockFace.DOWN.offsetPos(current.pos()))); - } else { - willBreak = notSamePlant(dimension.getBlockState(BlockFace.UP.offsetPos(current.pos()))); - if (!willBreak) - willBreak = !this.canPlaceOn(dimension.getBlockState(BlockFace.DOWN.offsetPos(current.pos())).getBlockType()); + if (face == BlockFace.UP) { + if (!current.blockState().getPropertyValue(UPPER_BLOCK_BIT)) { + return isSamePlant(neighbor.blockState()); + } + } else if (face == BlockFace.DOWN) { + if (current.blockState().getPropertyValue(UPPER_BLOCK_BIT)) { + return isSamePlant(neighbor.blockState()); + } + return canPlaceOn(neighbor.blockState().getBlockType()); } - return !willBreak; + + return true; } - protected boolean notSamePlant(BlockState downBlock) { - return downBlock.getBlockType() != blockType; + protected boolean isSamePlant(BlockState otherBlock) { + return otherBlock.getBlockType() == blockType; + } + + @Override + public boolean isDroppable(BlockStateWithPos blockState, ItemStack usedItem, Entity entity) { + if (blockState.blockState().getPropertyValue(UPPER_BLOCK_BIT)) return false; + // Don't drop if entity is null + return entity != null && super.isDroppable(blockState, usedItem, entity); + } + + @Override + public Set getDrops(BlockStateWithPos blockState, ItemStack usedItem, Entity entity) { + if (usedItem.getItemType() == ItemTypes.SHEARS) { + return Set.of(shearDrop.getItemType().createItemStack(2)); + } + + return super.getDrops(blockState, usedItem, entity); } } diff --git a/server/src/main/java/org/allaymc/server/block/type/BlockTypeInitializer.java b/server/src/main/java/org/allaymc/server/block/type/BlockTypeInitializer.java index fb08fcf790..5d89c4615a 100644 --- a/server/src/main/java/org/allaymc/server/block/type/BlockTypeInitializer.java +++ b/server/src/main/java/org/allaymc/server/block/type/BlockTypeInitializer.java @@ -12,6 +12,7 @@ import org.allaymc.api.block.type.BlockType; import org.allaymc.api.block.type.BlockTypes; import org.allaymc.api.blockentity.type.BlockEntityTypes; +import org.allaymc.api.item.data.ItemId; import org.allaymc.api.item.type.ItemType; import org.allaymc.api.item.type.ItemTypes; import org.allaymc.api.math.voxelshape.VoxelShapes; @@ -285,13 +286,13 @@ public static void initTallGrass() { .builder(BlockTallGrassBehaviorImpl.class) .vanillaBlock(BlockId.TALL_GRASS) .setProperties(BlockPropertyTypes.UPPER_BLOCK_BIT) - .setBaseComponentSupplier(BlockTallGrassBaseComponentImpl::new) + .setBaseComponentSupplier(blockType -> new BlockTallGrassBaseComponentImpl(blockType, ItemId.SHORT_GRASS)) .build(); BlockTypes.LARGE_FERN = AllayBlockType .builder(BlockLargeFernBehaviorImpl.class) .vanillaBlock(BlockId.LARGE_FERN) .setProperties(BlockPropertyTypes.UPPER_BLOCK_BIT) - .setBaseComponentSupplier(BlockTallGrassBaseComponentImpl::new) + .setBaseComponentSupplier(blockType -> new BlockTallGrassBaseComponentImpl(blockType, ItemId.FERN)) .build(); }