Skip to content

Commit

Permalink
feat: complete grass breaking logic
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jun 22, 2024
1 parent ab84e61 commit f243674
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,22 @@ default <DATATYPE> void updateBlockProperty(BlockPropertyType<DATATYPE> property
chunk.sendChunkPacket(Dimension.createBlockUpdatePacket(newBlockState, x, y, z, layer));
}

/**
* Check if the block can keep existing when a neighbor block updates.
* @param current The current block
* @param face The face of the current block that is being updated
* @return true if the block can keep existing, false if the block should be broken
*/
default boolean canKeepExisting(BlockStateWithPos current, BlockStateWithPos neighbor, BlockFace face) {
return true;
}

/**
* Call when a blockState causes another blockState to update.
*
* @param current The current block
* @param neighbor The neighbor block that triggered the update
* @param face The face of the current block
* @param face The face of the current block that is being updated
*/
void onNeighborUpdate(BlockStateWithPos current, BlockStateWithPos neighbor, BlockFace face);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public BlockBaseComponentImpl(BlockType<? extends BlockBehavior> blockType) {
@Override
public void onNeighborUpdate(BlockStateWithPos current, BlockStateWithPos neighbor, BlockFace face) {
manager.callEvent(new BlockOnNeighborUpdateEvent(current, neighbor, face));
if (!canKeepExisting(current, neighbor, face)) {
current.pos().dimension().breakBlock(current.pos(), null, null);
}
}

@Override
Expand Down Expand Up @@ -95,7 +98,7 @@ public void onBreak(BlockStateWithPos blockState, ItemStack usedItem, EntityPlay
@Override
public boolean isDroppable(BlockStateWithPos blockState, ItemStack usedItem, EntityPlayer player) {
if (player != null && player.getGameType() == GameType.CREATIVE) return false;
return blockState.blockState().getBlockAttributes().canHarvestWithHand() || usedItem.isCorrectToolFor(blockState.blockState());
return blockState.blockState().getBlockAttributes().canHarvestWithHand() || (usedItem != null && usedItem.isCorrectToolFor(blockState.blockState()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ public BlockTallWheatSeedDropableBaseComponentImpl(BlockType<? extends BlockBeha
}

@Override
public void onNeighborUpdate(BlockStateWithPos current, BlockStateWithPos neighbor, BlockFace face) {
if (face != BlockFace.UP && face != BlockFace.DOWN) return;
public boolean canKeepExisting(BlockStateWithPos current, BlockStateWithPos neighbor, BlockFace face) {
if (!super.canKeepExisting(current, neighbor, face)) {
return false;
}
if (face != BlockFace.UP && face != BlockFace.DOWN) return false;
var dimension = current.pos().dimension();
var isUpperBlock = current.blockState().getPropertyValue(VanillaBlockPropertyTypes.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 = isPlaceableBlock(dimension.getBlockState(BlockFace.DOWN.offsetPos(current.pos())).getBlockType());
}
if (willBreak) dimension.breakBlock(current.pos(), null, null);
return willBreak;
}

protected boolean notSamePlant(BlockState downBlock) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.allaymc.server.block.component.common;

import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.data.BlockFace;
import org.allaymc.api.block.data.BlockStateWithPos;
import org.allaymc.api.block.type.BlockType;
import org.allaymc.api.item.ItemStack;
Expand All @@ -9,6 +10,8 @@

import java.util.concurrent.ThreadLocalRandom;

import static org.allaymc.api.block.type.BlockTypes.*;

/**
* Allay Project 2024/6/18
*
Expand All @@ -28,4 +31,23 @@ public ItemStack[] getDrops(BlockStateWithPos blockState, ItemStack usedItem) {
}
return Utils.EMPTY_ITEM_STACK_ARRAY;
}

@Override
public boolean canKeepExisting(BlockStateWithPos current, BlockStateWithPos neighbor, BlockFace face) {
if (face != BlockFace.UP && face != BlockFace.DOWN) return false;
var blockUnder = current.pos().dimension().getBlockState(BlockFace.DOWN.offsetPos(current.pos()));
return isPlaceableBlock(blockUnder.getBlockType());
}

protected boolean isPlaceableBlock(BlockType<?> block) {
return block == GRASS_BLOCK_TYPE ||
block == MYCELIUM_TYPE ||
block == PODZOL_TYPE ||
block == DIRT_TYPE ||
block == DIRT_WITH_ROOTS_TYPE ||
block == FARMLAND_TYPE ||
block == MUD_TYPE ||
block == MUDDY_MANGROVE_ROOTS_TYPE ||
block == MOSS_BLOCK_TYPE;
}
}

0 comments on commit f243674

Please sign in to comment.