From ca22ea6a05afb6fa134aab2468a6a807b5373fe8 Mon Sep 17 00:00:00 2001 From: omergunr100 <24607757+omergunr100@users.noreply.github.com> Date: Fri, 20 Dec 2024 08:48:24 +0200 Subject: [PATCH] Fix crop harvest yielding extra seeds (#2582) --- .../tool/behavior/HarvestCropsBehavior.java | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/gregtechceu/gtceu/common/item/tool/behavior/HarvestCropsBehavior.java b/src/main/java/com/gregtechceu/gtceu/common/item/tool/behavior/HarvestCropsBehavior.java index 3eabef59a3..7b5912f4a1 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/item/tool/behavior/HarvestCropsBehavior.java +++ b/src/main/java/com/gregtechceu/gtceu/common/item/tool/behavior/HarvestCropsBehavior.java @@ -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; @@ -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; @@ -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 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); }