Skip to content

Commit

Permalink
Add ExemptInteractivity flag, and make chest upgrade preserve Automat…
Browse files Browse the repository at this point in the history
…e modData
  • Loading branch information
SinZ163 committed Apr 8, 2024
1 parent 919c0fa commit a55bb68
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
22 changes: 15 additions & 7 deletions AutomateChests/ModEntry.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using HarmonyLib;
using HarmonyLib;
using Microsoft.Xna.Framework;
using Pathoschild.Stardew.Automate;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
Expand All @@ -18,7 +17,9 @@ public class ModEntry : Mod
{

/// <summary>The <see cref="Item.modData"/> flag which indicates a chest is automatable.</summary>
private readonly string ModDataFlag = "SinZ.AutomateChests";
public const string ModDataFlag = "SinZ.AutomateChests";
/// <summary>The <see cref="Item.modData"/> flag which indicates a chest should not have interactions applied from this mod (taking/removing hopper or equivalent).</summary>
public const string ModDataExemptFlag = "SinZ.AutomateChests/ExemptInteractivity";

private ModConfig Config;

Expand All @@ -40,6 +41,10 @@ public override void Entry(IModHelper helper)
original: AccessTools.Method(Type.GetType("Pathoschild.Stardew.Automate.ModEntry,Automate"), "OnModMessageReceived", parameters: new Type[] { typeof(object), typeof(ModMessageReceivedEventArgs)}),
postfix: new HarmonyMethod(typeof(ObjectPatches), nameof(ObjectPatches.Automate_ModEntry_OnModMessageReceived__Postfix))
);
harmony.Patch(
original: AccessTools.Method(typeof(Chest), nameof(Chest.performObjectDropInAction)),
transpiler: new HarmonyMethod(typeof(ObjectPatches), nameof(ObjectPatches.Chest__performObjectDropInAction__Transpiler))
);
this.Monitor.Log("This mod patches Automate. If you notice issues with Automate, make sure it happens without this mod before reporting it to the Automate page.", LogLevel.Trace);
}

Expand Down Expand Up @@ -80,12 +85,15 @@ private void OnButtonPressed(object sender, ButtonPressedEventArgs e)
Game1.currentLocation.objects.TryGetValue(e.Cursor.GrabTile, out SObject obj);
if (obj != null && obj is Chest { SpecialChestType: Chest.SpecialChestTypes.None or Chest.SpecialChestTypes.BigChest } chest)
{
if (chest.modData.ContainsKey(ModDataExemptFlag))
return;

var heldItem = Game1.player.ActiveObject;
// if the player is holding a hopper and the chest isn't tagged by us, it should be now
if (heldItem != null && heldItem.QualifiedItemId == Config.ActivationItemId && !chest.modData.ContainsKey(this.ModDataFlag))
if (heldItem != null && heldItem.QualifiedItemId == Config.ActivationItemId && !chest.modData.ContainsKey(ModDataFlag))
{
chest.Tint = Color.DarkViolet;
chest.modData[this.ModDataFlag] = "1";
chest.modData[ModDataFlag] = "1";

if (heldItem.Stack > 1)
Game1.player.ActiveObject.Stack--;
Expand All @@ -96,11 +104,11 @@ private void OnButtonPressed(object sender, ButtonPressedEventArgs e)
NotifyAutomateOfChestUpdate(Game1.currentLocation.Name, chest.TileLocation);
}
// otherwise if it is already marked by us, we should just return it to them if they have an empty hand
else if (Game1.player.CurrentItem == null && chest.modData.ContainsKey(this.ModDataFlag))
else if (Game1.player.CurrentItem == null && chest.modData.ContainsKey(ModDataFlag))
{
chest.Tint = Color.White;
chest.heldObject.Value = null;
chest.modData.Remove(this.ModDataFlag);
chest.modData.Remove(ModDataFlag);

Item item = ItemRegistry.Create(Config.ActivationItemId);
Game1.player.addItemByMenuIfNecessary(item);
Expand Down
27 changes: 26 additions & 1 deletion AutomateChests/ObjectPatches.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Pathoschild.Stardew.Automate;
using HarmonyLib;
using Pathoschild.Stardew.Automate;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Objects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -21,6 +23,29 @@ public static void Initialize(IMonitor monitor)
Monitor = monitor;
}

public static IEnumerable<CodeInstruction> Chest__performObjectDropInAction__Transpiler(ILGenerator generator, IEnumerable<CodeInstruction> instructions)
{
var output = new List<CodeInstruction>();
foreach (var instruction in instructions)
{
output.Add(instruction);
if (instruction.opcode == OpCodes.Stloc_1)
{
output.Add(new CodeInstruction(OpCodes.Ldarg_0));
output.Add(new CodeInstruction(OpCodes.Ldloc_1));
output.Add(new CodeInstruction(OpCodes.Call, typeof(ObjectPatches).GetMethod(nameof(Chest__performObjectDropInAction__PreserveAutomateModData))));
}
}
return output;
}
public static void Chest__performObjectDropInAction__PreserveAutomateModData(Chest instance, Chest otherChest)
{
if (instance.modData.ContainsKey(ModEntry.ModDataExemptFlag))
otherChest.modData[ModEntry.ModDataExemptFlag] = instance.modData[ModEntry.ModDataExemptFlag];
if (instance.modData.ContainsKey(ModEntry.ModDataFlag))
otherChest.modData[ModEntry.ModDataFlag] = instance.modData[ModEntry.ModDataFlag];
}

public static void Automate_AutomationFactory_GetFor_SObject__Postfix(ref IAutomatable __result)
{
try
Expand Down

0 comments on commit a55bb68

Please sign in to comment.