Skip to content

Commit ab7866b

Browse files
LeFauxMattCaranudLapinMyNameRoman
authored
Carry Chests 1.2.0 (#10)
* Update fr.json (Carry Chests, 1.1 update) (#7) Update on the lines added * Update to version 1.2.0-beta.1 with new features - Renamed and added constants in `Constants.cs` for Better Chests integration. - Introduced `GrabEmptyAsItem` and `SwapChests` properties in `ModConfig`. - Added `carry_chests` console command in `ModEntry` for managing backups and help. - Implemented slowness effect management in `OnOneSecondUpdateTicked`. - Updated `ConfigMenu` to include new configuration options. - Added patches in `ModPatches.cs` for `InventoryMenu` interactions. - Renamed `InventoryHelper` to `ModExtensions` and updated methods. - Updated multiple language files for new features and commands. - Updated `FauxCore` subproject to a new commit hash. * ru.json (#8) Co-authored-by: Matthew <3005344+LeFauxMatt@users.noreply.github.com> * Bump version and refactor command handling - Updated version number to `1.2.0-beta.2`. - Refactored constant keys in `Constants.cs` for maintainability. - Cleaned up `using` directives in `ModEntry.cs`. - Implemented `CommandHelper` for better command organization. - Added `OnCommandReceived` method for improved command handling. - Updated localization strings in `default.json` for clarity. - Updated subproject commit reference in `FauxCore`. * Update help command description in localization files The description for the command help has been changed from "Lists all available commands." to "Commands:" across multiple language files. This update standardizes the phrasing used in the help command, making it more concise and uniform across different translations. * Fix GitHub repository path in CarryChests.csproj Updated the `<UpdateKeys>` element to reflect the correct GitHub repository path from `LeFauxMatt/CarryChest` to `LeFauxMatt/CarryChests`. * Bump version to 1.2.0 for stable release Updated the version number in CarryChests.csproj from 1.2.0-beta.2 to 1.2.0, marking the transition to a stable release version. --------- Co-authored-by: Caranud <caranud@gmail.com> Co-authored-by: MyNameRoman <157910852+MyNameRoman@users.noreply.github.com>
1 parent c73a92f commit ab7866b

File tree

21 files changed

+363
-129
lines changed

21 files changed

+363
-129
lines changed

CarryChests/CarryChests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
<PropertyGroup>
77
<Name>Carry Chests</Name>
88
<Description>Allows you to pick up placed chests with items.</Description>
9-
<Version>1.1.0</Version>
9+
<Version>1.2.0</Version>
1010
<EnableHarmony>true</EnableHarmony>
1111
<RootNamespace>LeFauxMods.CarryChest</RootNamespace>
1212
<UniqueId>furyx639.CarryChest</UniqueId>
13-
<UpdateKeys>Nexus:30321, CurseForge:1169557, GitHub:LeFauxMatt/CarryChest</UpdateKeys>
13+
<UpdateKeys>Nexus:30321, CurseForge:1169557, GitHub:LeFauxMatt/CarryChests</UpdateKeys>
1414
<RepositoryUrl>https://github.com/LeFauxMatt/CarryChest</RepositoryUrl>
1515
<RepositoryType>git</RepositoryType>
1616
</PropertyGroup>

CarryChests/Constants.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ namespace LeFauxMods.CarryChest;
22

33
internal static class Constants
44
{
5-
public const string ColorKey = Prefix + "PlayerChoiceColor";
5+
public const string BetterChestsColorKey = BetterChestsPrefix + "PlayerChoiceColor";
66

7-
public const string FridgeKey = Prefix + "Fridge";
7+
public const string BetterChestsFridgeKey = BetterChestsPrefix + "Fridge";
88

9-
public const string GlobalInventoryKey = Prefix + "GlobalInventoryId";
9+
public const string BetterChestsPrefix = "furyx639.BetterChests-ProxyChestFactory-";
10+
11+
public const string GlobalInventoryId = ModId + "-Backup";
12+
13+
public const string BetterChestsGlobalInventoryKey = BetterChestsPrefix + "GlobalInventoryId";
1014

1115
public const string ModId = "furyx639.CarryChests";
1216

13-
public const string Prefix = "furyx639.BetterChests-ProxyChestFactory-";
17+
public const string Prefix = ModId + "-Chest-";
1418

1519
public const string SlowEffectKey = ModId + "/Slow";
1620
}

CarryChests/ModConfig.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ internal class ModConfig : IModConfig<ModConfig>, IConfigWithLogAmount
1414
/// <summary>Gets or sets a value indicating whether to override using a tool against a chest.</summary>
1515
public bool OverrideTool { get; set; }
1616

17+
/// <summary>Gets or sets a value indicating whether empty chests will be picked up as items.</summary>
18+
public bool GrabEmptyAsItem { get; set; }
19+
1720
/// <summary>Gets or sets the amount the player will be slowed when carrying chests above the limit.</summary>
1821
public float SlownessAmount { get; set; } = -1f;
1922

2023
/// <summary>Gets or sets the maximum number of chests the player can hold before being slowed.</summary>
2124
public int SlownessLimit { get; set; } = 1;
2225

26+
/// <summary>Gets or sets a value indicating whether to allow swapping.</summary>
27+
public bool SwapChests { get; set; } = true;
28+
2329
/// <summary>Gets or sets the number of chests the player can carry.</summary>
2430
public int TotalLimit { get; set; } = 3;
2531

@@ -30,19 +36,23 @@ internal class ModConfig : IModConfig<ModConfig>, IConfigWithLogAmount
3036
public void CopyTo(ModConfig other)
3137
{
3238
other.LogAmount = this.LogAmount;
39+
other.GrabEmptyAsItem = this.GrabEmptyAsItem;
3340
other.OpenHeldChest = this.OpenHeldChest;
3441
other.OverrideTool = this.OverrideTool;
3542
other.SlownessAmount = this.SlownessAmount;
3643
other.SlownessLimit = this.SlownessLimit;
44+
other.SwapChests = this.SwapChests;
3745
other.TotalLimit = this.TotalLimit;
3846
}
3947

4048
public string GetSummary() =>
4149
new StringBuilder()
50+
.AppendLine(CultureInfo.InvariantCulture, $"{nameof(this.GrabEmptyAsItem),25}: {this.GrabEmptyAsItem}")
4251
.AppendLine(CultureInfo.InvariantCulture, $"{nameof(this.OpenHeldChest),25}: {this.OpenHeldChest}")
4352
.AppendLine(CultureInfo.InvariantCulture, $"{nameof(this.OverrideTool),25}: {this.OverrideTool}")
4453
.AppendLine(CultureInfo.InvariantCulture, $"{nameof(this.SlownessAmount),25}: {this.SlownessAmount}")
4554
.AppendLine(CultureInfo.InvariantCulture, $"{nameof(this.SlownessLimit),25}: {this.SlownessLimit}")
55+
.AppendLine(CultureInfo.InvariantCulture, $"{nameof(this.SwapChests),25}: {this.SwapChests}")
4656
.AppendLine(CultureInfo.InvariantCulture, $"{nameof(this.TotalLimit),25}: {this.TotalLimit}")
4757
.ToString();
4858
}

CarryChests/ModEntry.cs

Lines changed: 121 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
using LeFauxMods.CarryChest.Services;
22
using LeFauxMods.CarryChest.Utilities;
33
using LeFauxMods.Common.Models;
4+
using LeFauxMods.Common.Services;
45
using LeFauxMods.Common.Utilities;
6+
using Microsoft.Xna.Framework;
57
using StardewModdingAPI.Events;
68
using StardewValley.Buffs;
9+
using StardewValley.Menus;
710
using StardewValley.Objects;
811

912
namespace LeFauxMods.CarryChest;
1013

1114
/// <inheritdoc />
1215
internal sealed class ModEntry : Mod
1316
{
17+
private CommandHelper commandHelper = null!;
18+
1419
/// <inheritdoc />
1520
public override void Entry(IModHelper helper)
1621
{
@@ -21,12 +26,64 @@ public override void Entry(IModHelper helper)
2126
Log.Init(this.Monitor, ModState.Config);
2227
ModPatches.Apply();
2328

24-
// TBD: Command to access global inventory chests
29+
// Commands
30+
this.commandHelper = new CommandHelper(
31+
helper,
32+
"carry_chests",
33+
I18n.Command_CarryChests_Description,
34+
I18n.Command_Unknown_Description)
35+
.AddCommand("help", I18n.Command_Help_Description)
36+
.AddCommand("backup", I18n.Command_Backups_Description);
2537

2638
// Events
2739
helper.Events.GameLoop.GameLaunched += this.OnGameLaunched;
2840
helper.Events.GameLoop.SaveLoaded += this.OnSaveLoaded;
2941
helper.Events.GameLoop.ReturnedToTitle += this.OnReturnedToTitle;
42+
ModEvents.Subscribe<CommandReceivedEventArgs>(this.OnCommandReceived);
43+
}
44+
45+
private static void OnOneSecondUpdateTicked(object? sender, OneSecondUpdateTickedEventArgs e)
46+
{
47+
// Add status effect to the player
48+
if (Game1.player.Items.OfType<Chest>().Count() >= ModState.Config.SlownessLimit)
49+
{
50+
Game1.player.applyBuff(
51+
new Buff(
52+
Constants.SlowEffectKey,
53+
duration: 60_000,
54+
iconTexture: Game1.buffsIcons,
55+
iconSheetIndex: 13,
56+
effects: new BuffEffects { Speed = { ModState.Config.SlownessAmount } },
57+
displayName: I18n.Effect_Overburdened()));
58+
59+
Log.Trace("Adding the slowness effect");
60+
return;
61+
}
62+
63+
if (!Game1.player.hasBuff(Constants.SlowEffectKey))
64+
{
65+
return;
66+
}
67+
68+
// Remove status effect from the player
69+
Game1.player.buffs.Remove(Constants.SlowEffectKey);
70+
Log.Trace("Removing the slowness effect");
71+
}
72+
73+
private void OnCommandReceived(CommandReceivedEventArgs e)
74+
{
75+
switch (e.Command)
76+
{
77+
case "help":
78+
Log.Info(this.commandHelper.HelpText);
79+
return;
80+
case "backup" when !Context.IsWorldReady:
81+
Log.Warn(I18n.Alert_CommandBackup_InvalidContext());
82+
return;
83+
case "backup":
84+
Game1.activeClickableMenu = new ItemGrabMenu(ModState.Backups);
85+
return;
86+
}
3087
}
3188

3289
private void OnGameLaunched(object? sender, GameLaunchedEventArgs e) =>
@@ -55,13 +112,13 @@ private void OnButtonPressed(object? sender, ButtonPressedEventArgs e)
55112
return;
56113
}
57114

58-
if (InventoryHelper.SwapChest(chest))
115+
if (ModState.Config.SwapChests && chest.TrySwap())
59116
{
60117
this.Helper.Input.Suppress(e.Button);
61118
return;
62119
}
63120

64-
if (InventoryHelper.PickUpChest(chest, ModState.Config.TotalLimit))
121+
if (chest.TryCarry())
65122
{
66123
this.Helper.Input.Suppress(e.Button);
67124
_ = chest.Location.Objects.Remove(chest.TileLocation);
@@ -93,34 +150,6 @@ private void OnConfigChanged(ConfigChangedEventArgs<ModConfig> e)
93150
}
94151
}
95152

96-
private static void OnOneSecondUpdateTicked(object? sender, OneSecondUpdateTickedEventArgs e)
97-
{
98-
// Add status effect to the player
99-
if (Game1.player.Items.OfType<Chest>().Count() >= ModState.Config.SlownessLimit)
100-
{
101-
Game1.player.applyBuff(
102-
new Buff(
103-
Constants.SlowEffectKey,
104-
duration: 60_000,
105-
iconTexture: Game1.buffsIcons,
106-
iconSheetIndex: 13,
107-
effects: new BuffEffects { Speed = { ModState.Config.SlownessAmount } },
108-
displayName: I18n.Effect_Overburdened()));
109-
110-
Log.Trace("Adding the slowness effect");
111-
return;
112-
}
113-
114-
if (!Game1.player.hasBuff(Constants.SlowEffectKey))
115-
{
116-
return;
117-
}
118-
119-
// Remove status effect from the player
120-
Game1.player.buffs.Remove(Constants.SlowEffectKey);
121-
Log.Trace("Removing the slowness effect");
122-
}
123-
124153
private void OnReturnedToTitle(object? sender, ReturnedToTitleEventArgs e)
125154
{
126155
this.Helper.Events.Input.ButtonPressed -= this.OnButtonPressed;
@@ -134,5 +163,66 @@ private void OnSaveLoaded(object? sender, SaveLoadedEventArgs e)
134163
{
135164
this.Helper.Events.GameLoop.OneSecondUpdateTicked += OnOneSecondUpdateTicked;
136165
}
166+
167+
// Create missing backups
168+
for (var i = 0; i < Game1.player.Items.Count; i++)
169+
{
170+
var item = Game1.player.Items[i];
171+
if (item is not Chest chest)
172+
{
173+
if (item is null ||
174+
!item.modData.TryGetValue(Constants.BetterChestsGlobalInventoryKey, out var id) ||
175+
!Game1.player.team.globalInventories.ContainsKey(id))
176+
{
177+
continue;
178+
}
179+
180+
// Attempt to restore a Better Chest proxy
181+
var color = Color.Black;
182+
if (item.modData.TryGetValue(Constants.BetterChestsColorKey, out var colorString) &&
183+
int.TryParse(colorString, out var colorValue))
184+
{
185+
var r = (byte)(colorValue & 0xFF);
186+
var g = (byte)((colorValue >> 8) & 0xFF);
187+
var b = (byte)((colorValue >> 16) & 0xFF);
188+
color = new Color(r, g, b);
189+
}
190+
191+
chest = new Chest(true, item.ItemId)
192+
{
193+
GlobalInventoryId = id,
194+
fridge = { Value = item.modData.ContainsKey(Constants.BetterChestsFridgeKey) },
195+
playerChoiceColor = { Value = color }
196+
};
197+
198+
chest.CopyFieldsFrom(item);
199+
_ = chest.modData.Remove(Constants.BetterChestsFridgeKey);
200+
_ = chest.modData.Remove(Constants.BetterChestsColorKey);
201+
_ = chest.modData.Remove(Constants.BetterChestsGlobalInventoryKey);
202+
Game1.player.Items[i] = chest;
203+
}
204+
205+
_ = ModState.Backups.TryAddBackup(chest, Constants.Prefix);
206+
}
207+
208+
// Create generic backups for any missing
209+
foreach (var (id, _) in Game1.player.team.globalInventories.Pairs)
210+
{
211+
// Only create backups for known ids
212+
if (!id.StartsWith(Constants.Prefix, StringComparison.OrdinalIgnoreCase) &&
213+
!id.StartsWith(Constants.BetterChestsPrefix, StringComparison.OrdinalIgnoreCase))
214+
{
215+
continue;
216+
}
217+
218+
// Do not create if backup already exists
219+
if (ModState.Backups.OfType<Chest>()
220+
.Any(chest => chest.GlobalInventoryId.Equals(id, StringComparison.OrdinalIgnoreCase)))
221+
{
222+
continue;
223+
}
224+
225+
ModState.Backups.Add(new Chest(true) { GlobalInventoryId = id });
226+
}
137227
}
138228
}

CarryChests/Services/ConfigMenu.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,12 @@ private void SetupMenu()
6565
static value => Config.OverrideTool = value,
6666
I18n.ConfigOption_OverrideToool_Name,
6767
I18n.ConfigOption_OverrideTool_Description);
68+
69+
this.api.AddBoolOption(
70+
this.manifest,
71+
static () => Config.SwapChests,
72+
static value => Config.SwapChests = value,
73+
I18n.ConfigOption_SwapChests_Name,
74+
I18n.ConfigOption_SwapChests_Description);
6875
}
6976
}

0 commit comments

Comments
 (0)