Skip to content

Commit

Permalink
fix: tall & short grass bugs (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhaiven authored Dec 2, 2024
1 parent 5a186c9 commit 7c602c6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ public BlockShortGrassBaseComponentImpl(BlockType<? extends BlockBehavior> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends BlockBehavior> blockType) {

protected final ItemId shearDrop;

public BlockTallGrassBaseComponentImpl(BlockType<? extends BlockBehavior> 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<ItemStack> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 7c602c6

Please sign in to comment.