Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
"Fake fork" from the original repository: https://github.com/spacechase0/StardewValleyMods
  • Loading branch information
SampaioTS committed Mar 24, 2024
1 parent a02fc86 commit 582486f
Show file tree
Hide file tree
Showing 41 changed files with 3,854 additions and 0 deletions.
12 changes: 12 additions & 0 deletions CarryChest/CarryChest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\common.targets" />
<Import Project="..\SpaceShared\SpaceShared.projitems" Label="Shared" />
<Import Project="..\SpaceSharedPatching\SpaceSharedPatching.projitems" Label="Shared" />

<PropertyGroup>
<Version>1.3.6</Version>
<TargetFramework>net6.0</TargetFramework>

<EnableHarmony>true</EnableHarmony>
</PropertyGroup>
</Project>
12 changes: 12 additions & 0 deletions CarryChest/CarryChest.csproj.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\common.targets" />
<Import Project="..\SpaceShared\SpaceShared.projitems" Label="Shared" />
<Import Project="..\SpaceSharedPatching\SpaceSharedPatching.projitems" Label="Shared" />

<PropertyGroup>
<Version>1.3.6</Version>
<TargetFramework>net5.0</TargetFramework>

<EnableHarmony>true</EnableHarmony>
</PropertyGroup>
</Project>
26 changes: 26 additions & 0 deletions CarryChest/Framework/ChestHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using StardewValley;
using StardewValley.Objects;
using SObject = StardewValley.Object;

namespace CarryChest.Framework
{
/// <summary>Provides utility methods for chest carrying.</summary>
internal static class ChestHelper
{
/*********
** Public methods
*********/
/// <summary>Get whether an item is a chest that can be carried while full.</summary>
/// <param name="item">The item to check.</param>
public static bool IsSupported(ISalable item)
{
// We're checking the `.ParentSheetIndex` instead of `is Chest` because when you break a chest
// and pick it up it isn't a chest instance, it's just an object with the chest index.
return
item is SObject obj
&& obj.bigCraftable.Value
&& (obj.ParentSheetIndex is 130 or 232) // chest or stone chest
&& (obj is not Chest chest || chest.playerChest.Value);
}
}
}
100 changes: 100 additions & 0 deletions CarryChest/Mod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System.Linq;
using CarryChest.Framework;
using CarryChest.Patches;
using Microsoft.Xna.Framework;
using Spacechase.Shared.Patching;
using SpaceShared;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Objects;
using SObject = StardewValley.Object;

namespace CarryChest
{
internal class Mod : StardewModdingAPI.Mod
{
public static Mod Instance;

/// <summary>The previously selected chest on the toolbar.</summary>
private Chest PreviousHeldChest;

/// <summary>The mod entry point, called after the mod is first loaded.</summary>
/// <param name="helper">Provides simplified APIs for writing mods.</param>
public override void Entry(IModHelper helper)
{
Mod.Instance = this;
Log.Monitor = this.Monitor;

helper.Events.GameLoop.UpdateTicking += this.OnUpdateTicking;
helper.Events.Input.ButtonPressed += this.OnButtonPressed;
helper.Events.World.ObjectListChanged += this.OnObjectListChanged;

HarmonyPatcher.Apply(this,
new ItemPatcher(),
new ObjectPatcher()
);
}

/// <summary>Raised before the game state is updated (≈60 times per second).</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnUpdateTicking(object sender, UpdateTickingEventArgs e)
{
if (!Context.IsPlayerFree)
return;

// track toolbar info before the game handles any user input
this.PreviousHeldChest = Game1.player.CurrentItem as Chest;
}

/// <summary>Raised after the player presses a button on the keyboard, controller, or mouse.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnButtonPressed(object sender, ButtonPressedEventArgs e)
{
if (!Context.IsPlayerFree)
return;

// pick up clicked chest
if (e.Button == SButton.MouseLeft && Game1.player.CurrentItem == null)
{
GameLocation location = Game1.currentLocation;
Vector2 tile = e.Cursor.Tile;
if (location.objects.TryGetValue(tile, out SObject obj) && ChestHelper.IsSupported(obj) && Game1.player.addItemToInventoryBool(obj, true))
{
location.objects.Remove(tile);
this.Helper.Input.Suppress(e.Button);
}
}
}

/// <summary>Raised after objects are added or removed in a location.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnObjectListChanged(object sender, ObjectListChangedEventArgs e)
{
if (!Context.IsPlayerFree)
return;

// transfer fields for placed chest
if (this.PreviousHeldChest != null && e.Location == Game1.currentLocation)
{
var placed = e.Added.Select(p => p.Value).OfType<Chest>().LastOrDefault();
if (placed != null)
{
Chest original = this.PreviousHeldChest;

placed.Name = original.Name;
placed.playerChoiceColor.Value = original.playerChoiceColor.Value;
placed.heldObject.Value = original.heldObject.Value;
placed.MinutesUntilReady = original.MinutesUntilReady;
if (original.Items.Any())
placed.Items.OverwriteWith(original.Items);
foreach (var modData in original.modData)
placed.modData.CopyFrom(modData);
}
}
}
}
}
50 changes: 50 additions & 0 deletions CarryChest/Patches/ItemPatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Diagnostics.CodeAnalysis;
using CarryChest.Framework;
using HarmonyLib;
using Spacechase.Shared.Patching;
using SpaceShared;
using StardewModdingAPI;
using StardewValley;
using StardewValley.Objects;

namespace CarryChest.Patches
{
/// <summary>Applies Harmony patches to <see cref="Item"/>.</summary>
[SuppressMessage("ReSharper", "InconsistentNaming", Justification = DiagnosticMessages.NamedForHarmony)]
internal class ItemPatcher : BasePatcher
{
/*********
** Public methods
*********/
/// <inheritdoc />
public override void Apply(Harmony harmony, IMonitor monitor)
{
harmony.Patch(
original: this.RequireMethod<Item>(nameof(Item.canStackWith)),
postfix: this.GetHarmonyMethod(nameof(After_CanStackWith))
);
}


/*********
** Private methods
*********/
/// <summary>The method to call after <see cref="Item.canStackWith"/>.</summary>
private static void After_CanStackWith(Item __instance, ISalable other, ref bool __result)
{
// prevent stacking chests that contain items
if (__result)
__result = !ItemPatcher.ShouldPreventStacking(__instance) && !ItemPatcher.ShouldPreventStacking(other);
}

/// <summary>Get whether to prevent stacking the given item.</summary>
/// <param name="item">The item to check.</param>
private static bool ShouldPreventStacking(ISalable item)
{
return
ChestHelper.IsSupported(item)
&& item is Chest chest
&& chest.Items.Count != 0;
}
}
}
42 changes: 42 additions & 0 deletions CarryChest/Patches/ObjectPatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Diagnostics.CodeAnalysis;
using CarryChest.Framework;
using HarmonyLib;
using Spacechase.Shared.Patching;
using SpaceShared;
using StardewModdingAPI;
using StardewValley.Objects;
using SObject = StardewValley.Object;

namespace CarryChest.Patches
{
/// <summary>Applies Harmony patches to <see cref="SObject"/>.</summary>
[SuppressMessage("ReSharper", "InconsistentNaming", Justification = DiagnosticMessages.NamedForHarmony)]
internal class ObjectPatcher : BasePatcher
{
/*********
** Public methods
*********/
/// <inheritdoc />
public override void Apply(Harmony harmony, IMonitor monitor)
{
harmony.Patch(
original: this.RequireMethod<SObject>(nameof(SObject.getDescription)),
postfix: this.GetHarmonyMethod(nameof(After_GetDescription))
);
}


/*********
** Private methods
*********/
/// <summary>The method to call before <see cref="SObject.getDescription"/>.</summary>
private static void After_GetDescription(SObject __instance, ref string __result)
{
if (ChestHelper.IsSupported(__instance))
{
var chest = __instance as Chest;
__result += "\n" + $"Contains {chest?.Items?.Count ?? 0} items.";
}
}
}
}
Binary file added CarryChest/_releases/CarryChest 1.3.6.zip
Binary file not shown.

0 comments on commit 582486f

Please sign in to comment.