Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 53 additions & 19 deletions TractorMod/Framework/Attachments/SeedAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class SeedAttachment : BaseAttachment
** Fields
*********/
/// <summary>The attachment settings.</summary>
private readonly GenericAttachmentConfig Config;
private readonly SeedConfig Config;

/// <summary>Simplifies access to private code.</summary>
private readonly IReflectionHelper Reflection;
Expand All @@ -30,7 +30,7 @@ internal class SeedAttachment : BaseAttachment
/// <param name="config">The attachment settings.</param>
/// <param name="modRegistry">Fetches metadata about loaded mods.</param>
/// <param name="reflection">Simplifies access to private code.</param>
public SeedAttachment(GenericAttachmentConfig config, IModRegistry modRegistry, IReflectionHelper reflection)
public SeedAttachment(SeedConfig config, IModRegistry modRegistry, IReflectionHelper reflection)
: base(modRegistry)
{
this.Config = config;
Expand All @@ -40,9 +40,7 @@ public SeedAttachment(GenericAttachmentConfig config, IModRegistry modRegistry,
/// <inheritdoc />
public override bool IsEnabled(Farmer player, Tool? tool, Item? item, GameLocation location)
{
return
this.Config.Enable
&& item is { Category: SObject.SeedsCategory, Stack: > 0 };
return (this.Config.EnableSeeds || this.Config.EnableTreeSeeds) && this.IsSeed(item);
}

/// <inheritdoc />
Expand All @@ -51,24 +49,44 @@ public override bool Apply(Vector2 tile, SObject? tileObj, TerrainFeature? tileF
if (item is not { Stack: > 0 })
return false;

// get dirt
if (!this.TryGetHoeDirt(tileFeature, tileObj, out HoeDirt? dirt, out bool dirtCoveredByObj, out IndoorPot? pot) || dirt.crop != null || pot?.bush.Value is not null)
return false;
if (this.Config.EnableTreeSeeds && this.IsTreeSeed(item) && item is SObject obj)
{
if (obj.canBePlacedHere(location, tile) && obj.placementAction(location, (int)(tile.X * Game1.tileSize), (int)(tile.Y * Game1.tileSize), player))
{
this.ConsumeItem(player, item);
if (this.Config.UseFertilizerWhenPlantingTrees && location.terrainFeatures.TryGetValue(tile, out TerrainFeature feature))
{
Item? fertilizer = player.Items.GetById("(O)805").FirstOrDefault();
if (fertilizer != null && feature is Tree tree && !tree.fertilized.Value && tree.growthStage.Value < Tree.treeStage && tree.fertilize())
{
this.ConsumeItem(player, fertilizer);
}
}
return true;
}
}
else if (this.Config.EnableSeeds && this.IsSeed(item))
{
// get dirt
if (!this.TryGetHoeDirt(tileFeature, tileObj, out HoeDirt? dirt, out bool dirtCoveredByObj, out IndoorPot? pot) || dirt.crop != null || pot?.bush.Value is not null)
return false;

// ignore if there's a giant crop, meteorite, etc covering the tile
if (dirtCoveredByObj || this.HasResourceClumpCoveringTile(location, tile, this.Reflection))
return false;
// ignore if there's a giant crop, meteorite, etc covering the tile
if (dirtCoveredByObj || this.HasResourceClumpCoveringTile(location, tile, this.Reflection))
return false;

// sow seeds
bool sowed = dirt.plant(item.ItemId, player, false);
if (sowed)
{
this.ConsumeItem(player, item);
// sow seeds
bool sowed = dirt.plant(item.ItemId, player, false);
if (sowed)
{
this.ConsumeItem(player, item);

if (this.TryGetEnricher(location, tile, out Chest? enricher, out Item? fertilizer) && dirt.plant(fertilizer.ItemId, player, true))
this.ConsumeItem(enricher, fertilizer);
if (this.TryGetEnricher(location, tile, out Chest? enricher, out Item? fertilizer) && dirt.plant(fertilizer.ItemId, player, true))
this.ConsumeItem(enricher, fertilizer);
}
return sowed;
}
return sowed;
return false;
}


Expand All @@ -90,6 +108,22 @@ private bool TryGetEnricher(GameLocation location, Vector2 tile, [NotNullWhen(tr
return entry != null;
}

/// <summary>Whether the given item is a seed</summary>
/// <param name="item">The item to be checked</param>
/// <returns>Returns whether the item is a seed</returns>
private bool IsSeed(Item? item)
{
return item is { Category: SObject.SeedsCategory, Stack: > 0 };
}

/// <summary>Whether the given item is a tree seed</summary>
/// <param name="item">The item to be checked</param>
/// <returns>Returns whether the item is a tree seed</returns>
private bool IsTreeSeed(Item? item)
{
return item != null && SObject.isWildTreeSeed(item.ItemId);
}

/// <summary>Get the enricher and fertilizer in range of the given tile, if any.</summary>
/// <param name="location">The location to check.</param>
/// <param name="tile">The tile to which to apply fertilizer.</param>
Expand Down
20 changes: 20 additions & 0 deletions TractorMod/Framework/Config/SeedConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pathoschild.Stardew.TractorMod.Framework.Config
{
internal class SeedConfig
{
/// <summary>Whether to enable planting seeds.</summary>
public bool EnableSeeds { get; set; } = true;

/// <summary>Whether to enable planting tree seeds.</summary>
public bool EnableTreeSeeds { get; set; } = false;

/// <summary>Whether to use tree fertilizer from inventory when planting trees.</summary>
public bool UseFertilizerWhenPlantingTrees { get; set; } = false;
}
}
4 changes: 2 additions & 2 deletions TractorMod/Framework/Config/StandardAttachmentConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal class StandardAttachmentsConfig
public ScytheConfig Scythe { get; set; } = new();

/// <summary>Configuration for the seeds attachment.</summary>
public GenericAttachmentConfig Seeds { get; set; } = new();
public SeedConfig Seeds { get; set; } = new();

/// <summary>Configuration for the shears attachment.</summary>
public GenericAttachmentConfig Shears { get; set; } = new();
Expand Down Expand Up @@ -73,7 +73,7 @@ public void OnDeserialized(StreamingContext context)
this.MeleeSword ??= new MeleeSwordConfig();
this.PickAxe ??= new PickAxeConfig();
this.Scythe ??= new ScytheConfig();
this.Seeds ??= new GenericAttachmentConfig();
this.Seeds ??= new SeedConfig();
this.Shears ??= new GenericAttachmentConfig();
this.Slingshot ??= new GenericAttachmentConfig { Enable = false };
this.WateringCan ??= new GenericAttachmentConfig();
Expand Down
27 changes: 21 additions & 6 deletions TractorMod/Framework/GenericModConfigMenuIntegrationForTractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,27 @@ public void Register(GenericModConfigMenuIntegration<ModConfig> menu, IMonitor m
set: (config, value) => config.StandardAttachments.Scythe.ClearWeeds = value
)

// seeds
.AddSectionTitle(I18n.Config_Seeds)
.AddCheckbox(
name: I18n.Config_Seeds_Name,
tooltip: I18n.Config_Seeds_Tooltip,
get: config => config.StandardAttachments.Seeds.EnableSeeds,
set: (config, value) => config.StandardAttachments.Seeds.EnableSeeds = value
)
.AddCheckbox(
name: I18n.Config_TreeSeeds_Name,
tooltip: I18n.Config_TreeSeeds_Tooltip,
get: config => config.StandardAttachments.Seeds.EnableTreeSeeds,
set: (config, value) => config.StandardAttachments.Seeds.EnableTreeSeeds = value
)
.AddCheckbox(
name: I18n.Config_UseFertilizerForPlantingTrees_Name,
tooltip: I18n.Config_UseFertilizerForPlantingTrees_Tooltip,
get: config => config.StandardAttachments.Seeds.UseFertilizerWhenPlantingTrees,
set: (config, value) => config.StandardAttachments.Seeds.UseFertilizerWhenPlantingTrees = value
)

// melee blunt weapons
.AddSectionTitle(I18n.Config_MeleeBlunt)
.AddCheckbox(
Expand Down Expand Up @@ -479,12 +500,6 @@ public void Register(GenericModConfigMenuIntegration<ModConfig> menu, IMonitor m
get: config => config.StandardAttachments.GrassStarter.Enable,
set: (config, value) => config.StandardAttachments.GrassStarter.Enable = value
)
.AddCheckbox(
name: I18n.Config_Seeds_Name,
tooltip: I18n.Config_Seeds_Tooltip,
get: config => config.StandardAttachments.Seeds.Enable,
set: (config, value) => config.StandardAttachments.Seeds.Enable = value
)
.AddCheckbox(
name: I18n.Config_SeedBags_Name,
tooltip: I18n.Config_SeedBags_Tooltip,
Expand Down
7 changes: 7 additions & 0 deletions TractorMod/i18n/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"config.hoe": "Hoe Features",
"config.pickaxe": "Pickaxe Features",
"config.scythe": "Scythe Features",
"config.seeds": "Seed Features",
"config.melee-blunt": "Melee Blunt Weapons Features",
"config.melee-dagger": "Melee Dagger Features",
"config.melee-sword": "Melee Sword Features",
Expand Down Expand Up @@ -246,6 +247,12 @@
"config.seeds.name": "Enable Seeds",
"config.seeds.tooltip": "Whether to plant seeds.",

"config.tree-seeds.name": "Enable Tree Seeds",
"config.tree-seeds.tooltip": "Whether to plant tree seeds.",

"config.use-fertilizer-for-planting-trees.name": "Apply tree fertilizer",
"config.use-fertilizer-for-planting-trees.tooltip": "Whether to apply tree fertilizer if available in inventory upon planting tree seeds.",

"config.seed-bags.name": "Enable Seed Bags",
"config.seed-bags.tooltip": "Whether to plant seeds from the Seed Bag mod.",

Expand Down