using System;
using System.IO;
using Omegasis.MuseumRearranger.Framework;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Locations;
namespace Omegasis.MuseumRearranger
{
/// The mod entry point.
public class MuseumRearranger : Mod
{
/*********
** Properties
*********/
/// The key which shows the museum rearranging menu.
private string ShowMenuKey = "R";
/// The key which toggles the inventory box when the menu is open.
private string ToggleInventoryKey = "T";
/// Whether the player loaded a save.
private bool IsGameLoaded;
/// The open museum menu (if any).
private NewMuseumMenu OpenMenu;
/*********
** Public methods
*********/
/// The mod entry point, called after the mod is first loaded.
/// Provides simplified APIs for writing mods.
public override void Entry(IModHelper helper)
{
SaveEvents.AfterLoad += this.SaveEvents_AfterLoad;
ControlEvents.KeyPressed += this.ControlEvents_KeyPressed;
}
/*********
** Private methods
*********/
/// The method invoked when the presses a keyboard button.
/// The event sender.
/// The event data.
private void ControlEvents_KeyPressed(object sender, EventArgsKeyPressed e)
{
if (Game1.player == null || Game1.player.currentLocation == null || !this.IsGameLoaded)
return;
// open menu
if (e.KeyPressed.ToString() == this.ShowMenuKey)
{
if (Game1.activeClickableMenu != null)
return;
if (Game1.player.currentLocation is LibraryMuseum)
Game1.activeClickableMenu = this.OpenMenu = new NewMuseumMenu(this.Helper.Reflection);
else
this.Monitor.Log("You can't rearrange the museum here.");
}
// toggle inventory box
if (e.KeyPressed.ToString() == this.ToggleInventoryKey)
this.OpenMenu?.ToggleInventory();
}
/// The method invoked after the player loads a save.
/// The event sender.
/// The event data.
private void SaveEvents_AfterLoad(object sender, EventArgs e)
{
this.IsGameLoaded = true;
this.LoadConfig();
this.WriteConfig();
}
/// Load the configuration settings.
private void LoadConfig()
{
string path = Path.Combine(Helper.DirectoryPath, "Museum_Rearrange_Config.txt");
if (!File.Exists(path))
{
this.ShowMenuKey = "R";
this.ToggleInventoryKey = "T";
}
else
{
string[] text = File.ReadAllLines(path);
this.ShowMenuKey = Convert.ToString(text[3]);
this.ToggleInventoryKey = Convert.ToString(text[5]);
}
}
/// Save the configuration settings.
private void WriteConfig()
{
string path = Path.Combine(Helper.DirectoryPath, "Museum_Rearrange_Config.txt");
string[] text = new string[20];
text[0] = "Config: Museum_Rearranger. Feel free to mess with these settings.";
text[1] = "====================================================================================";
text[2] = "Key binding for rearranging the museum.";
text[3] = this.ShowMenuKey;
text[4] = "Key binding for showing the menu when rearranging the museum.";
text[5] = this.ToggleInventoryKey;
File.WriteAllLines(path, text);
}
}
}