Skip to content

Commit

Permalink
Fix crop harvest yielding extra seeds (GregTechCEu#2582)
Browse files Browse the repository at this point in the history
  • Loading branch information
omergunr100 committed Dec 25, 2024
1 parent 7107add commit ca22ea6
Showing 1 changed file with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.gregtechceu.gtceu.api.item.tool.behavior.IToolBehavior;

import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
Expand All @@ -20,7 +19,6 @@
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.CropBlock;
import net.minecraft.world.level.block.LevelEvent;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;

Expand Down Expand Up @@ -93,15 +91,27 @@ private static boolean isBlockCrops(ItemStack stack, Level world, Player player,
}

private static boolean harvestBlockRoutine(ItemStack stack, BlockPos pos, Player player) {
BlockState blockState = player.level().getBlockState(pos);
Block block = blockState.getBlock();
CropBlock blockCrops = (CropBlock) block;
if (blockCrops.isMaxAge(blockState)) {
NonNullList<ItemStack> drops = NonNullList.create();
drops.addAll(Block.getDrops(blockState, (ServerLevel) player.level(), pos, null));
dropListOfItems(player.level(), pos, drops);
player.level().levelEvent(LevelEvent.PARTICLES_DESTROY_BLOCK, pos, Block.getId(blockState));
player.level().setBlock(pos, blockCrops.getStateForAge(0), Block.UPDATE_ALL);
var level = player.level();
var blockState = level.getBlockState(pos);
var block = blockState.getBlock();
var cropBlock = (CropBlock) block;
final var seed = cropBlock.getCloneItemStack(level, pos, blockState).getItem();
if (cropBlock.isMaxAge(blockState)) {
var drops = Block.getDrops(blockState, (ServerLevel) level, pos, null);
var iterator = drops.listIterator();
while (iterator.hasNext()) {
var drop = iterator.next();
if (drop.is(seed)) {
drop.shrink(1);
if (drop.isEmpty()) {
iterator.remove();
}
break;
}
}
dropListOfItems(level, pos, drops);
level.levelEvent(LevelEvent.PARTICLES_DESTROY_BLOCK, pos, Block.getId(blockState));
level.setBlock(pos, cropBlock.getStateForAge(0), Block.UPDATE_ALL);
if (!player.isCreative()) {
ToolHelper.damageItem(stack, player);
}
Expand Down

0 comments on commit ca22ea6

Please sign in to comment.