Skip to content

Commit

Permalink
Fixed Crop Modules
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon3055 committed Dec 22, 2024
1 parent f328c40 commit 9d308a6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.NetherWartBlock;
import net.minecraft.world.level.block.state.BlockState;
Expand All @@ -29,7 +30,12 @@ public boolean isSeedValid(@Nonnull ItemStack seed)
@Override
public BlockState getCropFromSeed(@Nonnull ItemStack seed, Level world, BlockPos pos)
{
return Blocks.NETHER_WART.defaultBlockState();
Block cropBlock = Block.byItem(seed.getItem());
BlockState state = cropBlock.defaultBlockState();
if (state.is(Blocks.NETHER_WART)) {
return state;
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.CropBlock;
import net.minecraft.world.level.block.NetherWartBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootParams;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
Expand All @@ -32,6 +33,7 @@
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public abstract class ModuleFarmer extends ModuleTool implements ISuppliesModule
{
Expand Down Expand Up @@ -59,10 +61,8 @@ public void init()
{
super.init();
plantModules = new ArrayList<>();
for (final ModuleBase module : getCart().getModules())
{
if (module instanceof ICropModule)
{
for (final ModuleBase module : getCart().getModules()) {
if (module instanceof ICropModule) {
plantModules.add((ICropModule) module);
}
}
Expand Down Expand Up @@ -157,7 +157,7 @@ protected boolean plant(Level world, BlockPos pos)
{
if (!getStack(i).isEmpty() && isSeedValidHandler(getStack(i)))
{
BlockState cropblock = getCropFromSeedHandler(getStack(i));
BlockState cropblock = getCropFromSeedHandler(getStack(i), world, pos);
if (cropblock != null && cropblock.getBlock() instanceof IPlantable && world.getBlockState(pos.above()).isAir() && soilblock.canSustainPlant(soilState, world, pos, Direction.UP, (IPlantable) cropblock.getBlock()))
{
hasSeeds = i;
Expand All @@ -173,7 +173,7 @@ protected boolean plant(Level world, BlockPos pos)
return true;
}
stopWorking();
BlockState cropblock2 = getCropFromSeedHandler(getStack(hasSeeds));
BlockState cropblock2 = getCropFromSeedHandler(getStack(hasSeeds), world, pos);
world.setBlock(pos.above(), cropblock2, 3);
ItemStack stack = getStack(hasSeeds);
stack.shrink(1);
Expand Down Expand Up @@ -232,27 +232,31 @@ protected int getBaseFarmingTime()

public boolean isSeedValidHandler(@Nonnull ItemStack seed)
{
return seed.is(Tags.Items.SEEDS) || seed.is(Tags.Items.CROPS);
return seed.is(Tags.Items.SEEDS) || plantModules.stream().anyMatch(e -> e.isSeedValid(seed));
}

protected BlockState getCropFromSeedHandler(@Nonnull ItemStack seed)
protected BlockState getCropFromSeedHandler(@Nonnull ItemStack seed, Level level, BlockPos pos)
{
Block cropBlock = Block.byItem(seed.getItem());
if (cropBlock == null) return null;
if (cropBlock instanceof CropBlock cropsBlock)
{
return cropsBlock.defaultBlockState();
BlockState state = cropBlock.defaultBlockState();
if (cropBlock instanceof CropBlock) {
return state;
}
return null;
state = plantModules.stream()
.map(e -> e.getCropFromSeed(seed, level, pos))
.filter(Objects::nonNull)
.findFirst()
.orElse(null);
return state;
}

protected boolean isReadyToHarvestHandler(Level world, BlockPos pos)
{
if (world.getBlockState(pos).getBlock() instanceof CropBlock cropsBlock)
{
BlockState state = world.getBlockState(pos);
if (state.getBlock() instanceof CropBlock cropsBlock) {
return cropsBlock.isMaxAge(world.getBlockState(pos));
}
return false;
return plantModules.stream().anyMatch(e -> e.isReadyToHarvest(world, pos));
}

public float getFarmAngle()
Expand Down

0 comments on commit 9d308a6

Please sign in to comment.