2016-10-11 15:28:18 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2017-08-06 03:51:44 +08:00
|
|
|
|
using System.Linq;
|
2017-07-31 11:07:07 +08:00
|
|
|
|
using Omegasis.BuildEndurance.Framework;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
using StardewModdingAPI;
|
2017-07-30 02:05:24 +08:00
|
|
|
|
using StardewModdingAPI.Events;
|
|
|
|
|
using StardewValley;
|
2017-05-04 05:00:17 +08:00
|
|
|
|
|
2017-07-28 08:28:39 +08:00
|
|
|
|
namespace Omegasis.BuildEndurance
|
2016-10-11 15:28:18 +08:00
|
|
|
|
{
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <summary>The mod entry point.</summary>
|
2016-10-11 15:28:18 +08:00
|
|
|
|
public class BuildEndurance : Mod
|
|
|
|
|
{
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/*********
|
|
|
|
|
** Properties
|
|
|
|
|
*********/
|
2017-08-06 03:51:44 +08:00
|
|
|
|
/// <summary>The relative path for the current player's data file.</summary>
|
|
|
|
|
private string DataFilePath => Path.Combine("data", $"{Constants.SaveFolderName}.json");
|
|
|
|
|
|
|
|
|
|
/// <summary>The absolute path for the current player's legacy data file.</summary>
|
2018-05-01 09:21:31 +08:00
|
|
|
|
private string LegacyDataFilePath => Path.Combine(this.Helper.DirectoryPath, "PlayerData", $"BuildEndurance_data_{Game1.player.Name}.txt");
|
2017-08-06 03:51:44 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>The mod settings.</summary>
|
2017-07-31 11:07:07 +08:00
|
|
|
|
private ModConfig Config;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
/// <summary>The data for the current player.</summary>
|
|
|
|
|
private PlayerData PlayerData;
|
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <summary>Whether the player has been exhausted today.</summary>
|
|
|
|
|
private bool WasExhausted;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <summary>Whether the player has collapsed today.</summary>
|
|
|
|
|
private bool WasCollapsed;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <summary>Whether the player recently gained XP for tool use.</summary>
|
|
|
|
|
private bool HasRecentToolExp;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <summary>Whether the player was eating last time we checked.</summary>
|
|
|
|
|
private bool WasEating;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
|
|
|
|
|
|
2018-05-01 09:21:31 +08:00
|
|
|
|
public IModHelper ModHelper;
|
|
|
|
|
public IMonitor ModMonitor;
|
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/*********
|
|
|
|
|
** Public methods
|
|
|
|
|
*********/
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
this.Config = helper.ReadConfig<ModConfig>();
|
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
GameEvents.UpdateTick += this.GameEvents_UpdateTick;
|
|
|
|
|
GameEvents.OneSecondTick += this.GameEvents_OneSecondTick;
|
|
|
|
|
SaveEvents.AfterLoad += this.SaveEvents_AfterLoad;
|
2017-08-06 03:51:44 +08:00
|
|
|
|
SaveEvents.BeforeSave += this.SaveEvents_BeforeSave;
|
2018-05-01 09:21:31 +08:00
|
|
|
|
|
|
|
|
|
this.ModHelper = this.Helper;
|
|
|
|
|
this.ModMonitor = this.Monitor;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
}
|
2017-05-04 05:00:17 +08:00
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
|
|
|
|
|
/*********
|
|
|
|
|
** Private methods
|
|
|
|
|
*********/
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <summary>The method invoked once per second during a game update.</summary>
|
|
|
|
|
/// <param name="sender">The event sender.</param>
|
|
|
|
|
/// <param name="e">The event data.</param>
|
2017-08-06 03:51:44 +08:00
|
|
|
|
private void GameEvents_OneSecondTick(object sender, EventArgs e)
|
2016-10-11 15:28:18 +08:00
|
|
|
|
{
|
2017-07-30 02:05:24 +08:00
|
|
|
|
// nerf how quickly tool xp is gained (I hope)
|
|
|
|
|
if (this.HasRecentToolExp)
|
|
|
|
|
this.HasRecentToolExp = false;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <summary>The method invoked when the game updates (roughly 60 times per second).</summary>
|
|
|
|
|
/// <param name="sender">The event sender.</param>
|
|
|
|
|
/// <param name="e">The event data.</param>
|
2017-08-06 03:51:44 +08:00
|
|
|
|
private void GameEvents_UpdateTick(object sender, EventArgs e)
|
2016-10-11 15:28:18 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
if (!Context.IsWorldReady)
|
|
|
|
|
return;
|
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
// give XP when player finishes eating
|
2018-05-01 09:21:31 +08:00
|
|
|
|
if (Game1.player.isEating)
|
2017-07-30 02:05:24 +08:00
|
|
|
|
this.WasEating = true;
|
|
|
|
|
else if (this.WasEating)
|
2016-10-11 15:28:18 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
this.PlayerData.CurrentExp += this.Config.ExpForEating;
|
2017-07-30 02:05:24 +08:00
|
|
|
|
this.WasEating = false;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
}
|
2017-07-30 02:05:24 +08:00
|
|
|
|
|
|
|
|
|
// give XP when player uses tool
|
2018-05-01 09:21:31 +08:00
|
|
|
|
if (!this.HasRecentToolExp && Game1.player.UsingTool)
|
2016-10-11 15:28:18 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
this.PlayerData.CurrentExp += this.Config.ExpForToolUse;
|
2017-07-30 02:05:24 +08:00
|
|
|
|
this.HasRecentToolExp = true;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
// give XP when exhausted
|
|
|
|
|
if (!this.WasExhausted && Game1.player.exhausted)
|
2016-10-11 15:28:18 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
this.PlayerData.CurrentExp += this.Config.ExpForExhaustion;
|
2017-07-30 02:05:24 +08:00
|
|
|
|
this.WasExhausted = true;
|
2018-05-01 09:21:31 +08:00
|
|
|
|
//this.Monitor.Log("The player is exhausted");
|
2016-10-11 15:28:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
// give XP when player stays up too late or collapses
|
|
|
|
|
if (!this.WasCollapsed && Game1.farmerShouldPassOut)
|
2016-10-11 15:28:18 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
this.PlayerData.CurrentExp += this.Config.ExpForCollapsing;
|
2017-07-30 02:05:24 +08:00
|
|
|
|
this.WasCollapsed = true;
|
2018-05-01 09:21:31 +08:00
|
|
|
|
//this.Monitor.Log("The player has collapsed!");
|
2016-10-11 15:28:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <summary>The method invoked after the player loads a save.</summary>
|
|
|
|
|
/// <param name="sender">The event sender.</param>
|
|
|
|
|
/// <param name="e">The event data.</param>
|
2017-08-06 03:51:44 +08:00
|
|
|
|
private void SaveEvents_AfterLoad(object sender, EventArgs e)
|
2016-10-11 15:28:18 +08:00
|
|
|
|
{
|
2017-08-06 11:00:37 +08:00
|
|
|
|
// reset state
|
|
|
|
|
this.WasExhausted = false;
|
|
|
|
|
this.WasCollapsed = false;
|
|
|
|
|
this.HasRecentToolExp = false;
|
|
|
|
|
this.WasEating = false;
|
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
// load player data
|
|
|
|
|
this.MigrateLegacyData();
|
|
|
|
|
this.PlayerData = this.Helper.ReadJsonFile<PlayerData>(this.DataFilePath) ?? new PlayerData();
|
|
|
|
|
if (this.PlayerData.OriginalMaxStamina == 0)
|
|
|
|
|
this.PlayerData.OriginalMaxStamina = Game1.player.MaxStamina;
|
2017-07-30 02:05:24 +08:00
|
|
|
|
|
|
|
|
|
// reset if needed
|
2017-08-06 03:51:44 +08:00
|
|
|
|
if (this.PlayerData.ClearModEffects)
|
|
|
|
|
{
|
|
|
|
|
Game1.player.MaxStamina = this.PlayerData.OriginalMaxStamina;
|
|
|
|
|
this.PlayerData.ExpToNextLevel = this.Config.ExpToNextLevel;
|
|
|
|
|
this.PlayerData.CurrentExp = this.Config.CurrentExp;
|
|
|
|
|
this.PlayerData.CurrentLevelStaminaBonus = 0;
|
|
|
|
|
this.PlayerData.OriginalMaxStamina = Game1.player.MaxStamina;
|
|
|
|
|
this.PlayerData.BaseStaminaBonus = 0;
|
|
|
|
|
this.PlayerData.CurrentLevel = 0;
|
|
|
|
|
this.PlayerData.ClearModEffects = false;
|
|
|
|
|
}
|
2017-07-30 02:05:24 +08:00
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
// else apply stamina
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Game1.player.MaxStamina = this.PlayerData.NightlyStamina <= 0
|
|
|
|
|
? this.PlayerData.BaseStaminaBonus + this.PlayerData.CurrentLevelStaminaBonus + this.PlayerData.OriginalMaxStamina
|
|
|
|
|
: this.PlayerData.NightlyStamina;
|
|
|
|
|
}
|
2016-10-11 15:28:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
/// <summary>The method invoked just before the game is saved.</summary>
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <param name="sender">The event sender.</param>
|
|
|
|
|
/// <param name="e">The event data.</param>
|
2017-08-06 03:51:44 +08:00
|
|
|
|
private void SaveEvents_BeforeSave(object sender, EventArgs e)
|
2016-10-11 15:28:18 +08:00
|
|
|
|
{
|
2017-07-30 02:05:24 +08:00
|
|
|
|
// reset data
|
|
|
|
|
this.WasExhausted = false;
|
|
|
|
|
this.WasCollapsed = false;
|
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
// update player data
|
|
|
|
|
this.PlayerData.CurrentExp += this.Config.ExpForSleeping;
|
|
|
|
|
if (this.PlayerData.OriginalMaxStamina == 0)
|
|
|
|
|
this.PlayerData.OriginalMaxStamina = Game1.player.MaxStamina; //grab the initial stamina value
|
2016-10-11 15:28:18 +08:00
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
if (this.PlayerData.CurrentLevel < this.Config.MaxLevel)
|
2017-07-30 02:05:24 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
while (this.PlayerData.CurrentExp >= this.PlayerData.ExpToNextLevel)
|
2016-10-11 15:28:18 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
this.PlayerData.CurrentLevel += 1;
|
|
|
|
|
this.PlayerData.CurrentExp = this.PlayerData.CurrentExp - this.PlayerData.ExpToNextLevel;
|
|
|
|
|
this.PlayerData.ExpToNextLevel = (this.Config.ExpCurve * this.PlayerData.ExpToNextLevel);
|
|
|
|
|
Game1.player.MaxStamina += this.Config.StaminaIncreasePerLevel;
|
|
|
|
|
this.PlayerData.CurrentLevelStaminaBonus += this.Config.StaminaIncreasePerLevel;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-06 03:51:44 +08:00
|
|
|
|
this.PlayerData.ClearModEffects = false;
|
|
|
|
|
this.PlayerData.NightlyStamina = Game1.player.maxStamina;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
// save data
|
|
|
|
|
this.Helper.WriteJsonFile(this.DataFilePath, this.PlayerData);
|
2016-10-11 15:28:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
/// <summary>Migrate the legacy settings for the current player.</summary>
|
|
|
|
|
private void MigrateLegacyData()
|
2016-10-11 15:28:18 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
// skip if no legacy data or new data already exists
|
|
|
|
|
if (!File.Exists(this.LegacyDataFilePath) || File.Exists(this.DataFilePath))
|
|
|
|
|
return;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
// migrate to new file
|
|
|
|
|
try
|
2017-07-30 02:05:24 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
string[] text = File.ReadAllLines(this.LegacyDataFilePath);
|
|
|
|
|
this.Helper.WriteJsonFile(this.DataFilePath, new PlayerData
|
|
|
|
|
{
|
|
|
|
|
CurrentLevel = Convert.ToInt32(text[3]),
|
|
|
|
|
CurrentExp = Convert.ToDouble(text[5]),
|
|
|
|
|
ExpToNextLevel = Convert.ToDouble(text[7]),
|
|
|
|
|
BaseStaminaBonus = Convert.ToInt32(text[9]),
|
|
|
|
|
CurrentLevelStaminaBonus = Convert.ToInt32(text[11]),
|
|
|
|
|
ClearModEffects = Convert.ToBoolean(text[14]),
|
|
|
|
|
OriginalMaxStamina = Convert.ToInt32(text[16]),
|
|
|
|
|
NightlyStamina = Convert.ToInt32(text[18])
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
FileInfo file = new FileInfo(this.LegacyDataFilePath);
|
|
|
|
|
file.Delete();
|
|
|
|
|
if (!file.Directory.EnumerateFiles().Any())
|
|
|
|
|
file.Directory.Delete();
|
2016-10-11 15:28:18 +08:00
|
|
|
|
}
|
2017-08-06 03:51:44 +08:00
|
|
|
|
catch (Exception ex)
|
2016-10-11 15:28:18 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
this.Monitor.Log($"Error migrating data from the legacy 'PlayerData' folder for the current player. Technical details:\n {ex}", LogLevel.Error);
|
2016-10-11 15:28:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-30 02:05:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|