Skip to content

Commit

Permalink
Better fix for (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
Crystal-Spider committed Nov 12, 2024
1 parent d28c612 commit 9b2178b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 95 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ and this project adheres to [Crystal Nest Semantic Versioning](https://crystalne

- Nothing new.

## [v9.3.0] - 2024/11/12

- Fix compatibility with Pam's HarvestCraft mods.
- Fix compatibility with Croptopia.
- Reverted generalization of logic to handle crops that do not yield their seeds (e.g. the Pitcher crop), which looked good on paper, but was akin to beg for mod incompatibility.
- Removed `use seeds from inventory` configuration option. Now it always happens, but for the Pitcher crop only.

## [v9.2.1] - 2024/09/30

- Fix support for 1.21.1.
Expand Down Expand Up @@ -529,6 +536,7 @@ and this project adheres to [Crystal Nest Semantic Versioning](https://crystalne
[Unreleased]: https://github.com/crystal-nest/harvest-with-ease
[README]: https://github.com/crystal-nest/harvest-with-ease#readme

[v9.3.0]: https://github.com/crystal-nest/harvest-with-ease/releases?q=9.3.0
[v9.2.1]: https://github.com/crystal-nest/harvest-with-ease/releases?q=9.2.1
[v9.2.0]: https://github.com/crystal-nest/harvest-with-ease/releases?q=9.2.0
[v9.1.1]: https://github.com/crystal-nest/harvest-with-ease/releases?q=9.1.1
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ It's also compatible with **almost any** modded crops, to ease your mind of one
- `area increment step`:
Increment step for the harvest area size with higher tool tiers.
Setting this to `none` and **`starting harvest area size`** to `single` will effectively disable multi-harvest.
- `use seeds from inventory`: whether to use seeds from the player's inventory to replant crops if seeds are not dropped, for example with the pitcher crop.
- `crops`: list of additional in-game IDs for crops that are not supported out of the box, defaults to an empty list.
This last config option is just a safety measure, so far no crop needs it.
- `blacklist`: list in-game IDs for crops that under no condition can be right-click harvested.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.crystalnest.harvest_with_ease.api.event;

import it.crystalnest.cobweb.api.block.BlockUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
Expand Down Expand Up @@ -136,20 +135,6 @@ interface HarvestDropsEvent extends HarvestEvent<ServerPlayer, ServerLevel> {
@ApiStatus.Internal
void setDrops(List<ItemStack> drops);

/**
* Marks that the seeds are included in the drops.
*/
@ApiStatus.Internal
void seedsIncluded();

/**
* Returns whether the seeds are included in the drops.
*
* @return whether the seeds are included in the drops.
*/
@ApiStatus.Internal
boolean areSeedsIncluded();

/**
* Cancel this event to prevent further computations.
*/
Expand Down Expand Up @@ -186,15 +171,8 @@ default boolean didDropsChange() {
@ApiStatus.Internal
default List<ItemStack> initDefaultDrops(ServerLevel level, BlockState crop, BlockPos pos, InteractionHand hand) {
List<ItemStack> drops = Block.getDrops(crop, level, pos, crop.hasBlockEntity() ? level.getBlockEntity(pos) : null, getEntity(), getEntity().getItemInHand(hand));
// Pam's HarvestCraft mods are kinda weird with seeds, so it needs special treatment.
if (BlockUtils.getStringKey(crop.getBlock()).contains("pamhc2")) {
seedsIncluded();
return drops;
}
// Iterate over the drops and remove the seed if present.
for (ItemStack stack : drops) {
if (stack.is(crop.getBlock().getCloneItemStack(level, pos, crop).getItem())) {
seedsIncluded();
stack.shrink(1);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ public final class ModConfig extends CommonConfig {
*/
private IntValue grantedExp;

/**
* Whether to use seeds from the player's inventory to replant crops if seeds are not dropped.
*/
private BooleanValue useSeedsFromInventory;

/**
* Whether to gather drops near the player when harvesting.
*/
Expand Down Expand Up @@ -148,15 +143,6 @@ public static Integer getGrantedExp() {
return CONFIG.grantedExp.get();
}

/**
* Returns the value of {@link #useSeedsFromInventory} as read from the configuration file.
*
* @return the value of {@link #useSeedsFromInventory} as read from the configuration file.
*/
public static Boolean getUseSeedsFromInventory() {
return CONFIG.useSeedsFromInventory.get();
}

/**
* Returns the value of {@link #gatherDrops} as read from the configuration file.
*
Expand Down Expand Up @@ -254,7 +240,6 @@ protected void define(ModConfigSpec.Builder builder) {
requireHoe = builder.comment(" Require holding a hoe (either hands) to right-click harvest.").define("require hoe", false);
damageOnHarvest = builder.comment(" If [require hoe] is set to true, damage the hoe of the given amount (0 to disable, must be an integer).").defineInRange("damage on harvest", 0, 0, Integer.MAX_VALUE);
grantedExp = builder.comment(" Amount of experience to grant on harvest (0 to disable, must be an integer).").defineInRange("exp on harvest", 0, 0, Integer.MAX_VALUE);
useSeedsFromInventory = builder.comment(" Whether to use seeds from the player's inventory to replant crops if seeds are not dropped.").define("use seeds from inventory", true);
gatherDrops = builder.comment(" Whether to gather drops near the player when harvesting.").define("gather drops", false);
tiers = builder.comment(
" Ordered list of tiers.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.Shapes;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.Nullable;

import java.util.List;
Expand Down Expand Up @@ -134,30 +134,27 @@ private static void harvest(ServerLevel level, IntegerProperty age, BlockState c
* @param crop crop.
* @param basePos position of the crop base.
* @param player player.
* @param dropsFlags a Pair, with left equal to whether the crop seed was in the drops, and right equal to whether custom drops were added.
* @param customDrops whether custom drops were added.
*/
private static void updateCrop(ServerLevel level, IntegerProperty age, Block crop, BlockPos basePos, ServerPlayer player, Pair<Boolean, Boolean> dropsFlags) {
BlockState cropState = level.getBlockState(basePos);
int i = player.getInventory().findSlotMatchingItem(crop.getCloneItemStack(level, basePos, cropState));
if (dropsFlags.getLeft()) {
// If seeds are dropped, simply revert the crop's age.
level.setBlockAndUpdate(basePos, level.getBlockState(basePos).setValue(age, 0));
} else if (ModConfig.getUseSeedsFromInventory() && i >= 0) {
// If the seeds were not dropped, but are in the inventory and the mod is allowed to use them, then revert the crop's age and consume the seeds from the inventory.
level.setBlockAndUpdate(basePos, level.getBlockState(basePos).setValue(age, 0));
if (!player.isCreative()) {
player.getInventory().getItem(i).shrink(1);
private static void updateCrop(ServerLevel level, IntegerProperty age, Block crop, BlockPos basePos, ServerPlayer player, boolean customDrops) {
if (crop == Blocks.PITCHER_CROP) {
// Pitcher crop does not drop its seed (bulb). Revert its age and consume the seed from the inventory if possible, otherwise break it.
int i = player.getInventory().findSlotMatchingItem(crop.getCloneItemStack(level, basePos, level.getBlockState(basePos)));
if (i >= 0 || player.isCreative()) {
level.setBlockAndUpdate(basePos, level.getBlockState(basePos).setValue(age, 0));
if (!player.isCreative()) {
player.getInventory().getItem(i).shrink(1);
}
} else {
level.destroyBlock(basePos, !customDrops, player);
}
} else if (isTallButSeparate(crop)) {
// If the crop is tall but separate, revert its age without destroying it.
level.setBlockAndUpdate(basePos, level.getBlockState(basePos).setValue(age, 0));
} else {
// If the crop is not tall but separate (it's single or tall and not separate), destroy it and prevent drops since they will be accounted for in the condition below.
level.destroyBlock(basePos, false, player);
// Revert the crop's age. It's assumed that either seeds were dropped or that the crop is not supposed to drop them.
level.setBlockAndUpdate(basePos, level.getBlockState(basePos).setValue(age, 0));
}
if (level.getBlockState(basePos).is(BlockTags.CROPS) && level.getBlockState(basePos.above()).is(crop) && !isTallButSeparate(crop)) {
// If the crop is tall and not separate, destroy the block above to break all the crop-blocks, and drop only if custom drops were not set.
level.destroyBlock(basePos.above(), !dropsFlags.getRight(), player);
level.destroyBlock(basePos.above(), !customDrops, player);
}
}

Expand Down Expand Up @@ -212,15 +209,15 @@ private static void damageHoe(ServerPlayer player, InteractionHand hand) {
* @param hitResult {@link BlockHitResult}.
* @param player player.
* @param hand player's hand.
* @return a Pair, with left equal to whether the crop seed was in the drops, and right equal to whether custom drops were added.
* @return whether custom drops were added.
*/
private static Pair<Boolean, Boolean> dropResources(ServerLevel level, BlockState crop, BlockPos pos, BlockPos originalPos, Direction face, @Nullable BlockHitResult hitResult, ServerPlayer player, InteractionHand hand) {
private static boolean dropResources(ServerLevel level, BlockState crop, BlockPos pos, BlockPos originalPos, Direction face, @Nullable BlockHitResult hitResult, ServerPlayer player, InteractionHand hand) {
if (level.getGameRules().getBoolean(GameRules.RULE_DOBLOCKDROPS)) {
HarvestEvent.HarvestDropsEvent event = Services.EVENT.fireHarvestDropsEvent(level, crop, pos, face, hitResult, player, hand);
dropStacks(level, ModConfig.getGatherDrops() ? originalPos : pos, face, event.getDrops());
return Pair.of(event.areSeedsIncluded() || player.isCreative(), event.didDropsChange());
return event.didDropsChange();
}
return Pair.of(false, false);
return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,6 @@ public static class HarvestDropsEvent extends FabricHarvestEvent<ServerPlayer, S
*/
private List<ItemStack> drops;

/**
* Whether the seeds are included in the drops.
*/
private boolean seedsIncluded = false;

/**
* Whether the event is canceled.
*/
Expand Down Expand Up @@ -360,18 +355,6 @@ public void setDrops(List<ItemStack> drops) {
this.drops = drops;
}

@Override
@ApiStatus.Internal
public void seedsIncluded() {
seedsIncluded = true;
}

@Override
@ApiStatus.Internal
public boolean areSeedsIncluded() {
return seedsIncluded;
}

@Override
public void cancel() {
canceled = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,6 @@ public static class HarvestDropsEvent extends NeoForgeHarvestEvent<ServerPlayer,
*/
private List<ItemStack> drops;

/**
* Whether the seeds are included in the drops.
*/
private boolean seedsIncluded = false;

/**
* @param level {@link #level}.
* @param crop {@link #crop}.
Expand Down Expand Up @@ -191,18 +186,6 @@ public void setDrops(List<ItemStack> drops) {
this.drops = drops;
}

@Override
@ApiStatus.Internal
public void seedsIncluded() {
seedsIncluded = true;
}

@Override
@ApiStatus.Internal
public boolean areSeedsIncluded() {
return seedsIncluded;
}

@Override
public void cancel() {
setCanceled(true);
Expand Down

0 comments on commit 9b2178b

Please sign in to comment.